From 51b9a8cc5985cec422620f23032e3a3846b12bf2 Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 17 Jan 2023 16:40:56 +0800 Subject: [PATCH] =?UTF-8?q?Comparator=20=E6=8E=A5=E5=8F=A3=E5=AE=9A?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/mem_table.rs | 6 ++-- src/db/skip_list.rs | 6 ++-- src/traits/comparator_trait.rs | 2 +- src/util/comparator.rs | 66 ++++++++++++++++++++-------------- src/util/comparator_test.rs | 2 +- src/util/options.rs | 4 +-- 6 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/db/mem_table.rs b/src/db/mem_table.rs index 9bc002c..56566ce 100644 --- a/src/db/mem_table.rs +++ b/src/db/mem_table.rs @@ -1,5 +1,5 @@ use std::rc::Rc; -use crate::traits::comparator_trait::ComparatorTrait; +use crate::traits::comparator_trait::Comparator; use crate::traits::DataIterator; use crate::util::comparator::InternalKeyComparator; use crate::util::slice::Slice; @@ -12,14 +12,14 @@ pub enum ValueType { } /// 内存表 -pub struct MemTable { +pub struct MemTable { cmp: Rc, } /// 临时, 查找键 struct LookupKey {} -impl MemTable { +impl MemTable { /// 创建内存表 /// diff --git a/src/db/skip_list.rs b/src/db/skip_list.rs index 144c0b6..01331f2 100644 --- a/src/db/skip_list.rs +++ b/src/db/skip_list.rs @@ -1,6 +1,6 @@ use std::rc::Rc; -use crate::traits::comparator_trait::ComparatorTrait; +use crate::traits::comparator_trait::Comparator; use crate::util::Arena; use crate::util::comparator::BytewiseComparatorImpl; use crate::util::Result; @@ -11,12 +11,12 @@ struct Node { value: T, } -pub struct SkipList { +pub struct SkipList { node: Option>, comp: Rc, } -impl SkipList { +impl SkipList { pub fn create(comparator: Rc, _arena: Rc) -> Self { Self { diff --git a/src/traits/comparator_trait.rs b/src/traits/comparator_trait.rs index 608e223..a7a74c1 100644 --- a/src/traits/comparator_trait.rs +++ b/src/traits/comparator_trait.rs @@ -2,7 +2,7 @@ use std::cmp::Ordering; use crate::util::slice::Slice; /// 比较器 -pub trait ComparatorTrait { +pub trait Comparator { /// Slice 的大小比较, 按字典逐字节序进行比较 /// diff --git a/src/util/comparator.rs b/src/util/comparator.rs index d28c48d..1f9bbf7 100644 --- a/src/util/comparator.rs +++ b/src/util/comparator.rs @@ -1,6 +1,6 @@ use std::cmp::{min, Ordering}; -use crate::traits::comparator_trait::{ComparatorTrait}; +use crate::traits::comparator_trait::{Comparator}; use crate::util::slice::Slice; pub struct BytewiseComparatorImpl {} @@ -15,7 +15,7 @@ impl Default for BytewiseComparatorImpl { } } -impl ComparatorTrait for BytewiseComparatorImpl { +impl Comparator for BytewiseComparatorImpl { fn compare(&self, a: &Slice, b: &Slice) -> Option { a.partial_cmp(b) @@ -91,29 +91,43 @@ impl ComparatorTrait for BytewiseComparatorImpl { /// InternalKeyComparator pub struct InternalKeyComparator { - // user_comparator_: ComparatorTrait + user_comparator_: dyn Comparator } -// /// InternalKeyComparator 比较器: 用来比较内部键(Internal Key)。 -// /// 内部键值是为了方便处理,将原普通键、序列号和值类型组成的新键。 -// impl ComparatorTrait for InternalKeyComparator { -// fn new(c: ComparatorTrait) -> InternalKeyComparator { -// todo!() -// } -// -// fn compare(&self, _a: &Slice, _b: &Slice) -> Option { -// todo!() -// } -// -// fn get_name(&self) -> String { -// String::from("leveldb.InternalKeyComparator") -// } -// -// fn find_shortest_separator(&self, _start: &String, _limit: &Slice) -> String { -// todo!() -// } -// -// fn find_short_successor(&self, _key: &String) -> String { -// todo!() -// } -// } +impl InternalKeyComparator { + fn new(c: Box) -> Box { + todo!() + } + + fn user_comparator() -> Box { + todo!() + } + + // fn compare(a: InternalKey, b: InternalKey) -> u32 { + // todo!() + // } +} + +/// InternalKeyComparator 比较器: 用来比较内部键(Internal Key)。 +/// 内部键值是为了方便处理,将原普通键、序列号和值类型组成的新键。 +impl Comparator for InternalKeyComparator { + // fn new(c: Box) -> InternalKeyComparator { + // todo!() + // } + + fn compare(&self, _a: &Slice, _b: &Slice) -> Option { + todo!() + } + + fn get_name(&self) -> String { + String::from("leveldb.InternalKeyComparator") + } + + fn find_shortest_separator(&self, _start: &String, _limit: &Slice) -> String { + todo!() + } + + fn find_short_successor(&self, _key: &String) -> String { + todo!() + } +} diff --git a/src/util/comparator_test.rs b/src/util/comparator_test.rs index 8d5ec27..12d570f 100644 --- a/src/util/comparator_test.rs +++ b/src/util/comparator_test.rs @@ -2,7 +2,7 @@ mod test { use std::cmp::Ordering; use std::io::Write; - use crate::traits::comparator_trait::ComparatorTrait; + use crate::traits::comparator_trait::Comparator; use crate::util::comparator::{BytewiseComparatorImpl, InternalKeyComparator}; use crate::util::slice::Slice; diff --git a/src/util/options.rs b/src/util/options.rs index 3c2aad7..3c3060b 100644 --- a/src/util/options.rs +++ b/src/util/options.rs @@ -1,5 +1,5 @@ use crate::db::db::Snapshot; -use crate::traits::comparator_trait::ComparatorTrait; +use crate::traits::comparator_trait::Comparator; use crate::util::comparator::BytewiseComparatorImpl; pub enum CompressionType { @@ -22,7 +22,7 @@ pub struct Options { /// REQUIRES: The client must ensure that the comparator supplied /// here has the same name and orders keys *exactly* the same as the /// comparator provided to previous open calls on the same DB. - pub cmp: Box, + pub cmp: Box, /// If true, the database will be created if it is missing. pub create_if_missing: bool, /// If true, an error is raised if the database already exists. -- Gitee