diff --git a/src/db/mem_table.rs b/src/db/mem_table.rs new file mode 100644 index 0000000000000000000000000000000000000000..995858d0bc7fcc812445068b1df862ce14c45213 --- /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 e696750c9f453f8fb7390e07732e4fed64189c27..4e9b202d7c316ba34b846a2689ed69ea7fbf4a03 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 0000000000000000000000000000000000000000..725dfe05ab8a4a41d9c5ded367137b4383ea69d1 --- /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 47388a276f6f79912f5cbba844eeaf54f882aa61..608e223e4ae02f8773f8ce2227b635d4bf37437e 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 a985e81ad68b49c971c249bca5fefd5ae1908854..3e327f3aea7d35f34f0dac3ec5c7aabf0bc2579f 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 ffe5726d898f6ef4ede5a1cbdd9f7f5bc0e14a96..45c7ee32368446a52d4e8ac8666817e0171e61df 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 173c68a6ab8a098de67c6c6c721b40e4eafaa2cb..71560febc43ae7dcc5fa88e950541c95b55666a6 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 a9b28072864bafda6df6746c3cf8e86916aea592..fcfb0347d7ebc468869edacd7f4f8abd3a42660a 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); }