From fc17673fbc90a5583fbd5d3844b2084aaa976c5b Mon Sep 17 00:00:00 2001 From: fengyang Date: Mon, 3 Apr 2023 20:55:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?BlockBuilder=20=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/builder.rs | 6 +++--- src/table/filter_block.rs | 29 ++++++++++++++--------------- src/table/filter_block_test.rs | 13 +++++++------ src/table/table_builder.rs | 15 +++++++-------- src/util/options.rs | 7 ++++++- 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/db/builder.rs b/src/db/builder.rs index 9140e5e..a98102e 100644 --- a/src/db/builder.rs +++ b/src/db/builder.rs @@ -60,9 +60,9 @@ impl BuildTable { return Err(fileRS.err().unwrap()); } - let writableFile = Arc::new(fileRS.unwrap()); + let writable_file = Arc::new(fileRS.unwrap()); // 生成一个 TableBuilder - let builder: TableBuilder = TableBuilder::new_with_writable_file(options, writableFile); + let builder: TableBuilder = TableBuilder::new_with_writable_file(options, writable_file.clone()); meta.get_smallest().decode_from(&iter.key()); @@ -86,7 +86,7 @@ impl BuildTable { // Finish and check for file errors // 将文件刷新到磁盘 if s.is_ok() { - let rs:io::Result<()> = writableFile.sync_data(); + let rs:io::Result<()> = writable_file.sync_data(); if rs.is_ok() { s = Status::default(); }else{ diff --git a/src/table/filter_block.rs b/src/table/filter_block.rs index 625f2c9..53597ae 100644 --- a/src/table/filter_block.rs +++ b/src/table/filter_block.rs @@ -11,12 +11,13 @@ use crate::util::Result; const FILTER_BASE_LG: usize = 11; const FILTER_BASE: usize = 1 << FILTER_BASE_LG; +pub type FilterPolicyRef = Arc>; + /// /// meta block 构建器 /// pub trait FilterBlock { - #[inline] - fn new_with_policy(policy: Box) -> Self; + fn new_with_policy(policy: FilterPolicyRef) -> Self; /// /// 构造一个 FilterBlockBuilder @@ -33,8 +34,7 @@ pub trait FilterBlock { /// ``` /// /// ``` - #[inline] - fn new_with_policy_capacity(policy: Box, capacity: usize) -> Self; + fn new_with_policy_capacity(policy: FilterPolicyRef, capacity: usize) -> Self; /// 设置block的起始位置 /// @@ -49,7 +49,6 @@ pub trait FilterBlock { /// ``` /// filter_block_builder.start_block(1024_u64); /// ``` - #[inline] fn start_block(&mut self, block_offset: u64); fn add_key_from_str(&mut self, key: &str); @@ -78,7 +77,7 @@ pub trait FilterBlock { /// ``` fn finish(&mut self) -> Result; - fn get_policy(&self) -> Box<&dyn FilterPolicy>; + fn get_policy(&self) -> FilterPolicyRef; fn get_keys(&self) -> Vec; @@ -93,7 +92,7 @@ pub trait FilterBlock { /// SSTable 文件里面的 meta block 构建器, 按内存里面指定的格式整理在内存中 pub struct FilterBlockBuilder { - policy: Box, + policy: FilterPolicyRef, // Flattened key contents keys: Vec, // Starting index in keys_ of each key @@ -106,7 +105,7 @@ pub struct FilterBlockBuilder { } pub struct FilterBlockReader { - policy: Box, + policy: FilterPolicyRef, // Pointer to filter data (at block-start) data: Vec, // Pointer to beginning of offset array (at block-end) @@ -118,11 +117,11 @@ pub struct FilterBlockReader { } impl FilterBlock for FilterBlockBuilder { - fn new_with_policy(policy: Box) -> Self { + fn new_with_policy(policy: FilterPolicyRef) -> Self { FilterBlock::new_with_policy_capacity(policy, 64) } - fn new_with_policy_capacity(policy: Box, capacity: usize) -> Self { + fn new_with_policy_capacity(policy: FilterPolicyRef, capacity: usize) -> Self { let keys:Vec = Vec::with_capacity(capacity); let start:Vec = Vec::with_capacity(capacity); let result:Vec = Vec::with_capacity(capacity); @@ -187,8 +186,8 @@ impl FilterBlock for FilterBlockBuilder { Ok(Slice::from_buf(&self.result)) } - fn get_policy(&self) -> Box<&dyn FilterPolicy> { - Box::new(self.policy.as_ref()) + fn get_policy(&self) -> FilterPolicyRef { + self.policy.clone() } fn get_keys(&self) -> Vec { @@ -257,7 +256,7 @@ impl FilterBlockBuilder { } impl FilterBlockReader { - pub fn new_with_policy(policy: Box, contents: Slice) -> Self { + pub fn new_with_policy(policy: FilterPolicyRef, contents: Slice) -> Self { let data = Vec::new(); let offset = Vec::new(); @@ -292,8 +291,8 @@ impl FilterBlockReader { todo!() } - pub fn get_policy(&self) -> Box<&dyn FilterPolicy> { - Box::new(self.policy.as_ref()) + pub fn get_policy(&self) -> FilterPolicyRef { + self.policy.clone() } pub fn get_data(&self) -> Vec { diff --git a/src/table/filter_block_test.rs b/src/table/filter_block_test.rs index 9582b2a..2350619 100644 --- a/src/table/filter_block_test.rs +++ b/src/table/filter_block_test.rs @@ -113,9 +113,10 @@ mod test { #[test] fn test_filter_block_new_with_policy() { - let policy = Box::new(TestHashFilter::new()); + let policy: Arc> = Arc::new(Box::new(TestHashFilter::new())); - let filter_block: FilterBlockBuilder = FilterBlockBuilder::new_with_policy(policy, 10); + let filter_block: FilterBlockBuilder = FilterBlockBuilder:: + new_with_policy_capacity(policy, 10); let fp = filter_block.get_policy(); let filter_policy_name = fp.name(); @@ -129,10 +130,10 @@ mod test { #[test] fn test_filter_block_reader_new_with_policy_empty_content() { - let policy = Box::new(TestHashFilter::new()); + let policy: Arc> = Arc::new(Box::new(TestHashFilter::new())); let contents = Slice::default(); - let filter_block_reader: FilterBlockReader = FilterBlockReader::new_with_policy(policy, contents); + let filter_block_reader: FilterBlockReader = FilterBlockReader::new_with_policy(policy, contents); let fp_reader = filter_block_reader.get_policy(); let _reader_filter_policy_name = fp_reader.name(); @@ -145,8 +146,8 @@ mod test { #[test] fn test_filter_block_new_with_policy_and_addkey() { - let policy = Box::new(TestHashFilter::new()); - let mut filter_block_builder: FilterBlockBuilder = FilterBlockBuilder::new_with_policy( + let policy: Arc> = Arc::new(Box::new(TestHashFilter::new())); + let mut filter_block_builder: FilterBlockBuilder = FilterBlockBuilder::new_with_policy_capacity( policy, 10); filter_block_builder.start_block(100); diff --git a/src/table/table_builder.rs b/src/table/table_builder.rs index 4615fdc..e940246 100644 --- a/src/table/table_builder.rs +++ b/src/table/table_builder.rs @@ -5,7 +5,7 @@ use crate::table::block_builder::BlockBuilder; use crate::table::filter_block::{FilterBlock, FilterBlockBuilder}; use crate::table::format::BlockHandle; use crate::traits::filter_policy_trait::FilterPolicy; -use crate::util::options::{CompressionType, Options}; +use crate::util::options::{CompressionType, OptionRef, Options}; use crate::util::slice::Slice; use crate::util::status::Status; use crate::util::unsafe_slice::UnsafeSlice; @@ -47,8 +47,8 @@ struct Rep<> { } impl TableBuilder { - pub fn new_with_writable_file(options: &Options, writableFile: Arc) -> Self { - let rep = Rep::new(options, writableFile); + pub fn new_with_writable_file(options: &Options, writable_file: Arc) -> Self { + let rep = Rep::new(options, writable_file); // Self { // rep @@ -95,16 +95,15 @@ impl TableBuilder { } impl Rep { - pub fn new(opt: &Options, writableFile: Arc) -> Self { - // todo 如何赋值? Box::new(opt) - let options = Box::new(Default::default()); - let index_block_options = Box::new(Default::default()); + pub fn new(opt: OptionRef, writableFile: Arc) -> Self { + let options = Box::new(opt.clone()); + let index_block_options = Box::new(opt.clone()); let mut filter_block: Option; if opt.filter_policy.is_none() { filter_block = None; }else { - filter_block = Some(FilterBlockBuilder::new_with_policy(opt.filter_policy.unwrap())); + filter_block = Some(FilterBlockBuilder::new_with_policy(opt.filter_policy.unwrap().clone())); } Self { diff --git a/src/util/options.rs b/src/util/options.rs index 4764b88..17dc0da 100644 --- a/src/util/options.rs +++ b/src/util/options.rs @@ -6,6 +6,8 @@ use crate::traits::filter_policy_trait::FilterPolicy; use crate::util::comparator::BytewiseComparatorImpl; use crate::util::env::Env; +pub type OptionRef = Arc>; + pub enum CompressionType { NoCompression, SnappyCompression @@ -17,6 +19,9 @@ pub struct Cache {} // use crate::traits::filter_policy_trait::FilterPolicy; // pub struct FilterPolicy {} +// pub cmp: Box, +// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `dyn Comparator` +// #[derive(Clone)] pub struct Options { /// Comparator used to define the order of keys in the table. @@ -98,7 +103,7 @@ pub struct Options { /// If non-null, use the specified filter policy to reduce disk reads. /// Many applications will benefit from passing the result of /// NewBloomFilterPolicy() here. - pub filter_policy: Option>, + pub filter_policy: Option>>, } /// Options that control read operations pub struct ReadOptions { -- Gitee From c6654bb7c15d67aa789e4ccc992d83a763156f6c Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 4 Apr 2023 10:40:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Ptr=E5=AE=9A=E4=B9=89=E4=B8=8E=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/builder.rs | 4 ++-- src/db/builder_test.rs | 5 ----- src/db/table_cache.rs | 9 +++++---- src/table/block_builder.rs | 16 ++++++++++------ src/table/filter_block.rs | 24 +++++++++++------------- src/table/table_builder.rs | 29 +++++++++++++---------------- src/traits/filter_policy_trait.rs | 5 +++++ src/util/options.rs | 7 ++++--- 8 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/db/builder.rs b/src/db/builder.rs index a98102e..5a35ddc 100644 --- a/src/db/builder.rs +++ b/src/db/builder.rs @@ -10,7 +10,7 @@ use crate::table::table::Table; use crate::table::table_builder::TableBuilder; use crate::traits::DataIterator; use crate::util::env::Env; -use crate::util::options::{Options, ReadOptions}; +use crate::util::options::{Options, OptionsPtr, ReadOptions}; use crate::util::Result; use crate::util::slice::Slice; use crate::util::status::{LevelError, Status}; @@ -43,7 +43,7 @@ impl BuildTable { /// ``` /// /// ``` - pub fn build_table(dbname: &Slice, env: &Env, options: &Options, + pub fn build_table(dbname: &Slice, env: &Env, options: OptionsPtr, table_cache: &TableCache, mut iter: Box, meta: &mut FileMetaData) -> Result { meta.set_file_size(0); diff --git a/src/db/builder_test.rs b/src/db/builder_test.rs index 7f1746a..46899e3 100644 --- a/src/db/builder_test.rs +++ b/src/db/builder_test.rs @@ -7,11 +7,6 @@ mod test { #[test] fn test_build_table() { - - // BuildTable::build_table(&Slice::from("a"), Options::default(), - // TableCache::new(), - // Box::new()); println!("get_name: {}", "a"); - } } \ No newline at end of file diff --git a/src/db/table_cache.rs b/src/db/table_cache.rs index 5824617..6fa4804 100644 --- a/src/db/table_cache.rs +++ b/src/db/table_cache.rs @@ -4,7 +4,7 @@ use crate::util::options::ReadOptions; use crate::util::slice::Slice; use crate::util::Result; -struct Saver {} +pub struct Saver {} pub struct TableCache {} @@ -31,9 +31,10 @@ impl TableCache { /// ``` /// /// ``` - pub fn get(&self, _options: &ReadOptions, _file_number: u64, _file_size: usize, _k: &Slice, _arg: &mut Saver, _handle_result: F) - where F: FnMut(&mut Saver, &Slice, &Slice) -> Result<()> { - () + pub fn get(&self, _options: &ReadOptions, _file_number: u64, _file_size: usize, + _k: &Slice, _arg: &mut Saver, _handle_result: F) + where F: FnMut(&mut Saver, &Slice, &Slice) { + todo!() } /// 根据文件号消除缓存 /// diff --git a/src/table/block_builder.rs b/src/table/block_builder.rs index 3ca4eec..63166ce 100644 --- a/src/table/block_builder.rs +++ b/src/table/block_builder.rs @@ -1,6 +1,6 @@ use std::fs::File; use std::sync::Arc; -use crate::util::options::Options; +use crate::util::options::{Options, OptionsPtr}; use crate::util::slice::Slice; use crate::util::Result; @@ -13,10 +13,14 @@ use crate::util::status::Status; // Arc会追踪这个指针的所有拷贝,当最后一份拷贝离开作用域时,它就会安全释放内存。 // 智能指针 Box。 box 允许你将一个值放在堆上而不是栈上。留在栈上的则是指向堆数据的指针。 + +/// BlockBuilder 的 `Arc` 别名 +pub type BlockBuilderPtr = Arc; + pub struct BlockBuilder { // 在 BlockBuilder 初始化时,指定的配置项 - options: Box, - index_block_options: Box, + options: OptionsPtr, + index_block_options: OptionsPtr, // SSTable 生成后的文件 file: Arc, @@ -25,13 +29,13 @@ pub struct BlockBuilder { status: Status, // 生成 SSTable 中的数据区域 - data_block: Arc, + data_block: BlockBuilderPtr, // 生成 SSTable 中的数据索引区域 - index_block: Arc, + index_block: BlockBuilderPtr, } impl BlockBuilder { - pub fn new(options: &Options) -> Self { + pub fn new(options: OptionsPtr) -> Self { todo!() } diff --git a/src/table/filter_block.rs b/src/table/filter_block.rs index 53597ae..c24694e 100644 --- a/src/table/filter_block.rs +++ b/src/table/filter_block.rs @@ -1,7 +1,7 @@ use std::io::Write; use std::sync::Arc; use crate::traits::coding_trait::CodingTrait; -use crate::traits::filter_policy_trait::FilterPolicy; +use crate::traits::filter_policy_trait::{FilterPolicy, FilterPolicyPtr}; use crate::util::coding::Coding; use crate::util::slice::Slice; @@ -11,13 +11,11 @@ use crate::util::Result; const FILTER_BASE_LG: usize = 11; const FILTER_BASE: usize = 1 << FILTER_BASE_LG; -pub type FilterPolicyRef = Arc>; - /// /// meta block 构建器 /// pub trait FilterBlock { - fn new_with_policy(policy: FilterPolicyRef) -> Self; + fn new_with_policy(policy: FilterPolicyPtr) -> Self; /// /// 构造一个 FilterBlockBuilder @@ -34,7 +32,7 @@ pub trait FilterBlock { /// ``` /// /// ``` - fn new_with_policy_capacity(policy: FilterPolicyRef, capacity: usize) -> Self; + fn new_with_policy_capacity(policy: FilterPolicyPtr, capacity: usize) -> Self; /// 设置block的起始位置 /// @@ -77,7 +75,7 @@ pub trait FilterBlock { /// ``` fn finish(&mut self) -> Result; - fn get_policy(&self) -> FilterPolicyRef; + fn get_policy(&self) -> FilterPolicyPtr; fn get_keys(&self) -> Vec; @@ -92,7 +90,7 @@ pub trait FilterBlock { /// SSTable 文件里面的 meta block 构建器, 按内存里面指定的格式整理在内存中 pub struct FilterBlockBuilder { - policy: FilterPolicyRef, + policy: FilterPolicyPtr, // Flattened key contents keys: Vec, // Starting index in keys_ of each key @@ -105,7 +103,7 @@ pub struct FilterBlockBuilder { } pub struct FilterBlockReader { - policy: FilterPolicyRef, + policy: FilterPolicyPtr, // Pointer to filter data (at block-start) data: Vec, // Pointer to beginning of offset array (at block-end) @@ -117,11 +115,11 @@ pub struct FilterBlockReader { } impl FilterBlock for FilterBlockBuilder { - fn new_with_policy(policy: FilterPolicyRef) -> Self { + fn new_with_policy(policy: FilterPolicyPtr) -> Self { FilterBlock::new_with_policy_capacity(policy, 64) } - fn new_with_policy_capacity(policy: FilterPolicyRef, capacity: usize) -> Self { + fn new_with_policy_capacity(policy: FilterPolicyPtr, capacity: usize) -> Self { let keys:Vec = Vec::with_capacity(capacity); let start:Vec = Vec::with_capacity(capacity); let result:Vec = Vec::with_capacity(capacity); @@ -186,7 +184,7 @@ impl FilterBlock for FilterBlockBuilder { Ok(Slice::from_buf(&self.result)) } - fn get_policy(&self) -> FilterPolicyRef { + fn get_policy(&self) -> FilterPolicyPtr { self.policy.clone() } @@ -256,7 +254,7 @@ impl FilterBlockBuilder { } impl FilterBlockReader { - pub fn new_with_policy(policy: FilterPolicyRef, contents: Slice) -> Self { + pub fn new_with_policy(policy: FilterPolicyPtr, contents: Slice) -> Self { let data = Vec::new(); let offset = Vec::new(); @@ -291,7 +289,7 @@ impl FilterBlockReader { todo!() } - pub fn get_policy(&self) -> FilterPolicyRef { + pub fn get_policy(&self) -> FilterPolicyPtr { self.policy.clone() } diff --git a/src/table/table_builder.rs b/src/table/table_builder.rs index e940246..7e0155e 100644 --- a/src/table/table_builder.rs +++ b/src/table/table_builder.rs @@ -5,7 +5,7 @@ use crate::table::block_builder::BlockBuilder; use crate::table::filter_block::{FilterBlock, FilterBlockBuilder}; use crate::table::format::BlockHandle; use crate::traits::filter_policy_trait::FilterPolicy; -use crate::util::options::{CompressionType, OptionRef, Options}; +use crate::util::options::{CompressionType, OptionsPtr, Options}; use crate::util::slice::Slice; use crate::util::status::Status; use crate::util::unsafe_slice::UnsafeSlice; @@ -16,8 +16,8 @@ pub struct TableBuilder { /// TableBuilder Rep 结构体, 内部使用 struct Rep<> { - options: Box, - index_block_options: Box, + options: OptionsPtr, + index_block_options: OptionsPtr, // SSTable 生成后的文件 file: Arc, @@ -47,7 +47,7 @@ struct Rep<> { } impl TableBuilder { - pub fn new_with_writable_file(options: &Options, writable_file: Arc) -> Self { + pub fn new_with_writable_file(options: OptionsPtr, writable_file: Arc) -> Self { let rep = Rep::new(options, writable_file); // Self { @@ -95,33 +95,30 @@ impl TableBuilder { } impl Rep { - pub fn new(opt: OptionRef, writableFile: Arc) -> Self { - let options = Box::new(opt.clone()); - let index_block_options = Box::new(opt.clone()); - + pub fn new(opt: OptionsPtr, writableFile: Arc) -> Self { let mut filter_block: Option; if opt.filter_policy.is_none() { filter_block = None; }else { - filter_block = Some(FilterBlockBuilder::new_with_policy(opt.filter_policy.unwrap().clone())); + filter_block = Some(FilterBlockBuilder::new_with_policy(opt.filter_policy.clone().unwrap())); } Self { - options, - index_block_options, + options: opt.clone(), + index_block_options: opt.clone(), file: writableFile, offset: 0, // default Status::OK status: Status::default(), - data_block: BlockBuilder::new(&options.as_ref()), - index_block: BlockBuilder::new(&index_block_options.as_ref()), - last_key: Default::default(), + data_block: BlockBuilder::new(opt.clone()), + index_block: BlockBuilder::new(opt.clone()), + last_key: Slice::default(), num_entries: 0, closed: false, filter_block, pending_index_entry: false, - pending_handle: Default::default(), - compressed_output: Default::default(), + pending_handle: BlockHandle::default(), + compressed_output: Slice::default(), } } } \ No newline at end of file diff --git a/src/traits/filter_policy_trait.rs b/src/traits/filter_policy_trait.rs index 3920604..aaafafd 100644 --- a/src/traits/filter_policy_trait.rs +++ b/src/traits/filter_policy_trait.rs @@ -1,5 +1,10 @@ +use std::sync::Arc; use crate::util::slice::Slice; + +/// FilterPolicy 的 `Arc>` 别名 +pub type FilterPolicyPtr = Arc>; + /// 用于key过滤,可以快速的排除不存在的key pub trait FilterPolicy { diff --git a/src/util/options.rs b/src/util/options.rs index 17dc0da..ba51a8c 100644 --- a/src/util/options.rs +++ b/src/util/options.rs @@ -2,11 +2,12 @@ use std::sync::Arc; use crate::db::db::Snapshot; use crate::db::db_format::InternalKeyComparator; use crate::traits::comparator_trait::Comparator; -use crate::traits::filter_policy_trait::FilterPolicy; +use crate::traits::filter_policy_trait::{FilterPolicy, FilterPolicyPtr}; use crate::util::comparator::BytewiseComparatorImpl; use crate::util::env::Env; -pub type OptionRef = Arc>; +/// Options 的 `Arc>` 别名 +pub type OptionsPtr = Arc>; pub enum CompressionType { NoCompression, @@ -103,7 +104,7 @@ pub struct Options { /// If non-null, use the specified filter policy to reduce disk reads. /// Many applications will benefit from passing the result of /// NewBloomFilterPolicy() here. - pub filter_policy: Option>>, + pub filter_policy: Option, } /// Options that control read operations pub struct ReadOptions { -- Gitee