From babdefd9d5fc4cc85dbcecaa0170e3617a70b6a6 Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 31 Jan 2023 20:37:48 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=8F=8C=E5=90=91=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +++---- src/util/linked_list.rs | 75 +++++++++++++++++++++++++++++++++++------ 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c31a5a8..d490375 100644 --- a/README.md +++ b/README.md @@ -74,22 +74,22 @@ LSM tree 是许多 KV型或日志型数据库所依赖的核心实现,例如Bi | util.Logger | peach | | | table.Block, BlockBuilder, FilterBlockBuilder | colagy | | | FilterBlock, FilterBlockReader | colagy | | -| table.format(Footer, BlockHandle) | fengyang、半支烟 | | -| db.dbformat(InternalKeyComparator, InternalFilterPolicy, LookupKey, InternalKey) | fengyang、半支烟 | | +| table.format(Footer, BlockHandle) | fengyang、半支烟 | 20% | +| db.dbformat(InternalKeyComparator, InternalFilterPolicy, LookupKey, InternalKey) | fengyang、半支烟 | 20% | | db.SkipList | wangboo | | | table.Iterator(DBIter, MergingIterator, TwoLevelIterator...) | kazeseiriou | | | IteratorWrapper | kazeseiriou | | | db.MemTable(MemTable, MemTableIterator) | wangboo | | -| SSTable | fengyang | | +| SSTable | fengyang | 0% | | table.Table | peach | | | db.leveldb_util | wangboo | | | db.log_format | wangboo | | | db.LogReader | wangboo | 90% | | db.LogWriter | wangboo | 90% | | db.TableCache | colagy | | -| LinkedList | fengyang | | -| db.VersionEdit(Tag, VersionEdit, FileMetaData) | fengyang | | -| db.VersionSet(Version, LevelFileNumIterator, SaverState) | fengyang | | +| LinkedList | fengyang | 60% | +| db.VersionEdit(Tag, VersionEdit, FileMetaData) | fengyang | 10% | +| db.VersionSet(Version, LevelFileNumIterator, SaverState) | fengyang | 10% | | WriteBatch | peach | | #### 1.1.0 计划 diff --git a/src/util/linked_list.rs b/src/util/linked_list.rs index 5c1b209..4736f7f 100644 --- a/src/util/linked_list.rs +++ b/src/util/linked_list.rs @@ -118,6 +118,37 @@ pub trait LinkedListBuilder: Default { /// ``` fn add_by_position(&mut self, position: usize, data: T) -> Result; + /// 弹出此列表所代表的堆栈中的元素。(将元素从链表中删除,并且返回) + /// 等价于 pop_last + fn pop(&mut self) -> Option; + + /// Removes the first element from a list and returns it, or `None` if it is empty. + fn pop_first(&mut self) -> Option; + + /// Removes the last element from a list and returns it, or `None` if it is empty. + fn pop_last(&mut self) -> Option; + + /// 查看和返回第一个元素。不可变引用类型 + /// 等价于 peek_first + /// 仅仅返回元素的引用,而元素的所有权还是在链表中 + fn peek(&mut self) -> Option; + + // public E element() 返回第一个元素。 + + /// 查看和返回第一个元素。可变引用类型 + /// 返回元素的可变引用类型,使得能够对链表中的节点元素值进行修改,但是不真正获取元素的所有权! + fn peek_mut(&mut self) -> Option; + + /// 返回头部元素 + fn peek_first(&mut self) -> Option; + + /// 返回尾部元素 + fn peek_last(&mut self) -> Option; + + /// 返回尾部元素 + /// 返回元素的可变引用类型,使得能够对链表中的节点元素值进行修改,但是不真正获取元素的所有权! + fn peek_last_mut(&mut self) -> Option; + /// 删除并返回第一个元素。 fn remove_first(&mut self) -> Result>; @@ -126,7 +157,9 @@ pub trait LinkedListBuilder: Default { /// 删除指定位置的元素并返回。 fn remove(&mut self, position: usize) -> Result>; + // public boolean remove(Object o) 删除某一元素,返回是否成功,成功为 true,失败为 false。 + // public boolean remove(int index) 删除某一位置元素,返回是否成功,成功为 true,失败为 false。 /// 获取列表开头的元素 fn get_first(&self) -> Result>; @@ -170,16 +203,6 @@ pub trait LinkedListBuilder: Default { // public E pollFirst() 检索并删除此列表的第一个元素,如果此列表为空,则返回 null 。 // public E pollLast() 检索并删除此列表的最后一个元素,如果此列表为空,则返回 null 。 - // public E pop() 弹出此列表所代表的堆栈中的元素。(将元素从链表中删除,并且返回) - // public E popFirst() - // public E popLast() - - // public E element() 返回第一个元素。 - // public E peek() 返回第一个元素。不可变引用类型 - // public E peek_mut() 返回第一个元素。可变引用类型 - // public E peekFirst() 返回头部元素。 - // public E peekLast() 返回尾部元素。 - // public Iterator descendingIterator() 返回倒序迭代器。 // public ListIterator listIterator(int index) 返回从指定位置开始到末尾的迭代器。 // public Object[] toArray() 返回一个由链表元素组成的数组。 @@ -318,6 +341,38 @@ impl LinkedListBuilder for LinkedList { Ok(true) } + fn pop(&mut self) -> Option { + self.pop_last() + } + + fn pop_first(&mut self) -> Option { + todo!() + } + + fn pop_last(&mut self) -> Option { + todo!() + } + + fn peek(&mut self) -> Option { + self.peek_first() + } + + fn peek_mut(&mut self) -> Option { + todo!() + } + + fn peek_first(&mut self) -> Option { + todo!() + } + + fn peek_last(&mut self) -> Option { + todo!() + } + + fn peek_last_mut(&mut self) -> Option { + todo!() + } + fn remove_first(&mut self) -> Result> { todo!() } -- Gitee From f4a2c285dfaffa74429857c18bf778ddfac9fdc7 Mon Sep 17 00:00:00 2001 From: fengyang Date: Sat, 4 Feb 2023 19:52:48 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=8F=8C=E5=90=91=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 1 + src/util/linked_list.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 064907a..ae56398 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![feature(box_syntax)] +#![feature(allocator_api)] mod db; mod table; diff --git a/src/util/linked_list.rs b/src/util/linked_list.rs index 4736f7f..f451302 100644 --- a/src/util/linked_list.rs +++ b/src/util/linked_list.rs @@ -1,3 +1,4 @@ +use std::alloc::{Allocator, Global}; use std::fmt::{Display, Formatter}; use std::ptr::NonNull; use crate::util::Result; @@ -18,6 +19,7 @@ struct Node { /// 双向链表 #[derive(Debug)] +#[allow(missing_debug_implementations)] pub struct LinkedList { // 双向链表的当前长度 length: usize, @@ -25,12 +27,19 @@ pub struct LinkedList { head: Option>>, // 尾 tail: Option>>, + // 内存分配器 + allocator: Allocator } -pub trait LinkedListBuilder: Default { +pub trait LinkedListBuilder: Default { /// 构造函数, 构造空的双向链表 fn new() -> Self; + /// 指定内存分配器 + #[inline] + #[unstable(feature = "allocator_api", issue = "32838")] + fn new_in(alloc: A) -> Self; + fn length(&self) -> usize; /// 链表末尾添加元素 @@ -215,6 +224,9 @@ pub trait LinkedListBuilder: Default { // public int lastIndexOf(Object o) 查找指定元素最后一次出现的索引。 } +pub trait LinkedListBuilderIn: Default { +} + impl Node { fn new(val: T) -> Node { Node { @@ -244,6 +256,18 @@ impl LinkedListBuilder for LinkedList { length: 0, head: None, tail: None, + allocator: Global + } + } + + // #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global + #[inline] + fn new_in(alloc: A) -> Self { + Self { + length: 0, + head: None, + tail: None, + allocator: alloc, } } @@ -265,6 +289,7 @@ impl LinkedListBuilder for LinkedList { #[inline] fn add_first(&mut self, val: T) -> Result { // 使用入参中的 val 创建一个链表节点Node,为了方便后续直接从 Box 获取到 raw ptr 裸指针, 使用 Box 包装 + // Box.new_in(v, 自定义 ) let mut node = Box::new(Node::new(val)); node.next = self.head; -- Gitee From 748f3458f73ce5b05d2e9169748802631da68caf Mon Sep 17 00:00:00 2001 From: xiao Date: Sat, 4 Feb 2023 20:58:08 +0800 Subject: [PATCH 3/5] doc --- src/lib.rs | 1 - src/util/linked_list.rs | 37 ++++++++++++++----------------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ae56398..064907a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ #![feature(box_syntax)] -#![feature(allocator_api)] mod db; mod table; diff --git a/src/util/linked_list.rs b/src/util/linked_list.rs index f451302..87c2dd0 100644 --- a/src/util/linked_list.rs +++ b/src/util/linked_list.rs @@ -1,4 +1,3 @@ -use std::alloc::{Allocator, Global}; use std::fmt::{Display, Formatter}; use std::ptr::NonNull; use crate::util::Result; @@ -19,7 +18,6 @@ struct Node { /// 双向链表 #[derive(Debug)] -#[allow(missing_debug_implementations)] pub struct LinkedList { // 双向链表的当前长度 length: usize, @@ -27,18 +25,17 @@ pub struct LinkedList { head: Option>>, // 尾 tail: Option>>, - // 内存分配器 - allocator: Allocator + // // 内存分配器 + // allocator: Box } -pub trait LinkedListBuilder: Default { +pub trait LinkedListBuilder: Default { /// 构造函数, 构造空的双向链表 fn new() -> Self; - /// 指定内存分配器 - #[inline] - #[unstable(feature = "allocator_api", issue = "32838")] - fn new_in(alloc: A) -> Self; + // /// 指定内存分配器 + // #[inline] + // fn new_in(alloc: Box) -> Self; fn length(&self) -> usize; @@ -224,9 +221,6 @@ pub trait LinkedListBuilder Self { - Self { - length: 0, - head: None, - tail: None, - allocator: alloc, - } - } + // fn new_in(alloc: Box) -> Self { + // Self { + // length: 0, + // head: None, + // tail: None, + // allocator: alloc + // } + // } #[inline] fn length(&self) -> usize { -- Gitee From cbdfa08b1d789b77346488d260056e515a558aac Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 10 Feb 2023 18:40:11 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=8F=8C=E5=90=91=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/linked_list.rs | 46 ++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/util/linked_list.rs b/src/util/linked_list.rs index f451302..10199b0 100644 --- a/src/util/linked_list.rs +++ b/src/util/linked_list.rs @@ -5,6 +5,8 @@ use crate::util::Result; use crate::util::slice::Slice; use crate::util::status::{LevelError, Status}; +type Link = Option>>; + /// 节点 #[derive(Debug)] struct Node { @@ -12,33 +14,32 @@ struct Node { val: T, // 前驱 // 因为会出现一个节点同时存在多个可变引用的情况,因此需要使用裸指针(裸指针的包装 NonNull) - prev: Option>>, + prev: Link, // 后继. Option 表示该节点为空,即不存在 prev 前置节点(整个链表为空时)、或不存在next 后置节点(链表的尾节点) - next: Option>>, + next: Link, } /// 双向链表 #[derive(Debug)] -#[allow(missing_debug_implementations)] pub struct LinkedList { // 双向链表的当前长度 length: usize, // 头 - head: Option>>, + head: Link, // 尾 - tail: Option>>, - // 内存分配器 - allocator: Allocator + tail: Link, + // // 内存分配器 + // allocator: Allocator } -pub trait LinkedListBuilder: Default { +pub trait LinkedListBuilder: Default { /// 构造函数, 构造空的双向链表 fn new() -> Self; - /// 指定内存分配器 - #[inline] - #[unstable(feature = "allocator_api", issue = "32838")] - fn new_in(alloc: A) -> Self; + // /// 指定内存分配器 + // #[inline] + // #[unstable(feature = "allocator_api", issue = "32838")] + // fn new_in(alloc: A) -> Self; fn length(&self) -> usize; @@ -256,20 +257,19 @@ impl LinkedListBuilder for LinkedList { length: 0, head: None, tail: None, - allocator: Global + // allocator: Global } } - // #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global - #[inline] - fn new_in(alloc: A) -> Self { - Self { - length: 0, - head: None, - tail: None, - allocator: alloc, - } - } + // #[inline] + // fn new_in(alloc: A) -> Self { + // Self { + // length: 0, + // head: None, + // tail: None, + // allocator: alloc, + // } + // } #[inline] fn length(&self) -> usize { -- Gitee From 506151e0b59fbd0c3dcd690be7d278a70bc900d2 Mon Sep 17 00:00:00 2001 From: fengyang Date: Fri, 10 Feb 2023 18:41:53 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=8F=8C=E5=90=91=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/linked_list.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util/linked_list.rs b/src/util/linked_list.rs index 10199b0..3cab807 100644 --- a/src/util/linked_list.rs +++ b/src/util/linked_list.rs @@ -1,4 +1,3 @@ -use std::alloc::{Allocator, Global}; use std::fmt::{Display, Formatter}; use std::ptr::NonNull; use crate::util::Result; @@ -225,7 +224,7 @@ pub trait LinkedListBuilder: Default { // public int lastIndexOf(Object o) 查找指定元素最后一次出现的索引。 } -pub trait LinkedListBuilderIn: Default { +pub trait LinkedListBuilderIn: Default { } impl Node { -- Gitee