From 14a3a853241e6c671c141fe8d385008ff3cfceb9 Mon Sep 17 00:00:00 2001 From: wangboo <5417808+wangboa@user.noreply.gitee.com> Date: Mon, 16 Jan 2023 18:56:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B7=B3=E8=A1=A8=E5=92=8C?= =?UTF-8?q?=E5=86=85=E5=AD=98=E8=A1=A8=E7=9A=84=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/mem_table.rs | 80 ++++++++++++++++++++++++++++++++++ src/db/mod.rs | 4 +- src/db/skip_list.rs | 33 ++++++++++++++ src/traits/comparator_trait.rs | 2 +- src/traits/iterator.rs | 6 +-- src/traits/mod.rs | 1 + src/util/comparator.rs | 10 ++++- src/util/comparator_test.rs | 4 +- 8 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 src/db/mem_table.rs create mode 100644 src/db/skip_list.rs diff --git a/src/db/mem_table.rs b/src/db/mem_table.rs new file mode 100644 index 0000000..995858d --- /dev/null +++ b/src/db/mem_table.rs @@ -0,0 +1,80 @@ +use std::rc::Rc; +use crate::traits::comparator_trait::ComparatorTrait; +use crate::traits::DataIterator; +use crate::util::slice::Slice; + +use crate::util::Result; + +enum ValueType { + Insert, + Deletion, +} + +/// 内存表 +struct MemTable { + +} + +/// 临时, 查找键 +struct LookupKey {} + +type MemTableRef = Rc; + +impl MemTable { + + /// 创建内存表 + /// + /// # Arguments + /// + /// * `_comparator`: 比较器 + /// + /// returns: MemTable + /// + /// # Examples + /// + /// ``` + /// let mt = MemTable::create(comp); + /// ``` + pub fn create(_comparator: Rc>) -> Self { + todo!() + } + + /// 返回该表使用的内存近似值 + pub fn approximate_memory_usage(&self) -> usize { + todo!() + } + + /// 创建内存表迭代器 + /// + /// # Arguments + /// + /// returns: MemTable + /// + /// # Examples + /// + /// ``` + /// let mem = MemTable::create(comp); + /// let it = mem::new_new_iterator()?; + /// ``` + pub fn new_iterator(&self) -> Result> { + todo!() + } + + /// 像内存表中写入或删除一个元素 + pub fn add(&mut self, _seq_no: usize, _v_type: ValueType, _key: &Slice, _value: Slice) -> Result<()> { + todo!() + } + + /// 通过 key 查找结果 + pub fn get(&self, key: &LookupKey) -> Result> { + todo!() + } + +} + +mod test { + #[test] + fn test() { + + } +} \ No newline at end of file diff --git a/src/db/mod.rs b/src/db/mod.rs index e696750..4e9b202 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,3 +1,5 @@ pub mod log_writer; pub mod log_reader; -mod log_wr_test; \ No newline at end of file +mod log_wr_test; +pub mod skip_list; +mod mem_table; \ No newline at end of file diff --git a/src/db/skip_list.rs b/src/db/skip_list.rs new file mode 100644 index 0000000..725dfe0 --- /dev/null +++ b/src/db/skip_list.rs @@ -0,0 +1,33 @@ +use std::rc::Rc; +use crate::traits::comparator_trait::ComparatorTrait; +use crate::util::Arena; +use crate::util::slice::Slice; +use crate::util::Result; + +pub struct SkipList { + node: Node +} + +struct Node { + value: T, +} + +impl SkipList { + + pub fn create(_comparator: Rc>, _arena: Rc) -> Self { + todo!() + } + + pub fn insert(&mut self, _seq_no: usize, _key: &Slice) -> Result<()> { + todo!() + } + + pub fn contains(&self, _key: &Slice) -> bool { + todo!() + } + + pub fn get_max_height(&self) -> usize { + todo!() + } + +} \ No newline at end of file diff --git a/src/traits/comparator_trait.rs b/src/traits/comparator_trait.rs index 47388a2..608e223 100644 --- a/src/traits/comparator_trait.rs +++ b/src/traits/comparator_trait.rs @@ -29,7 +29,7 @@ pub trait ComparatorTrait { fn compare(&self, a: &Slice, b: &Slice) -> Option; /// 返回comparator的名字 - fn get_name() -> String; + fn get_name(&self) -> String; /// 函数:找到start、limit之间最短的字符串,如“helloworld”和”hellozoomer”之间最短的key可以是”hellox” /// diff --git a/src/traits/iterator.rs b/src/traits/iterator.rs index a985e81..3e327f3 100644 --- a/src/traits/iterator.rs +++ b/src/traits/iterator.rs @@ -1,6 +1,7 @@ use crate::util::slice::Slice; -pub trait Iterator { +pub trait DataIterator { + fn valid(&self) -> bool; fn seek_to_first(&mut self); @@ -16,6 +17,5 @@ pub trait Iterator { fn key(&self) -> &Slice; fn value(&self) -> &Slice; -} -trait AAA {} +} diff --git a/src/traits/mod.rs b/src/traits/mod.rs index ffe5726..45c7ee3 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -4,3 +4,4 @@ pub mod comparator_trait; pub mod coding_trait; pub mod filter_policy_trait; +pub use iterator::DataIterator; \ No newline at end of file diff --git a/src/util/comparator.rs b/src/util/comparator.rs index 173c68a..71560fe 100644 --- a/src/util/comparator.rs +++ b/src/util/comparator.rs @@ -21,7 +21,7 @@ impl ComparatorTrait for BytewiseComparatorImpl { a.partial_cmp(b) } - fn get_name() -> String { + fn get_name(&self) -> String { String::from("leveldb.BytewiseComparator") } @@ -98,6 +98,12 @@ pub struct InternalKeyComparator { // fn Compare(InternalKey, InternalKey) } +impl Default for InternalKeyComparator { + fn default() -> Self { + Self {} + } +} + /// InternalKeyComparator 比较器: 用来比较内部键(Internal Key)。 /// 内部键值是为了方便处理,将原普通键、序列号和值类型组成的新键。 impl ComparatorTrait for InternalKeyComparator { @@ -107,7 +113,7 @@ impl ComparatorTrait for InternalKeyComparator { todo!() } - fn get_name() -> String { + fn get_name(&self) -> String { String::from("leveldb.InternalKeyComparator") } diff --git a/src/util/comparator_test.rs b/src/util/comparator_test.rs index a9b2807..fcfb034 100644 --- a/src/util/comparator_test.rs +++ b/src/util/comparator_test.rs @@ -8,7 +8,7 @@ mod test { #[test] fn test_bytewise_comparator_impl_get_name() { - let name = BytewiseComparatorImpl::get_name(); + let name = BytewiseComparatorImpl::default().get_name(); println!("get_name: {}", &name); assert_eq!("leveldb.BytewiseComparator", name); } @@ -124,7 +124,7 @@ mod test { #[test] fn test_internal_key_comparator_get_name() { - let name = InternalKeyComparator::get_name(); + let name = InternalKeyComparator::default().get_name(); assert_eq!("leveldb.InternalKeyComparator", name); } -- Gitee