diff --git a/src/table/block_builder.rs b/src/table/block_builder.rs index 63166ce7b39b20261d94993ebe19a4ca680305f1..f24a9956152436e27ef210d98c111ad68425426b 100644 --- a/src/table/block_builder.rs +++ b/src/table/block_builder.rs @@ -39,7 +39,7 @@ impl BlockBuilder { todo!() } - /// 添加数据到block + /// 向datablock增加entry /// /// # Arguments /// @@ -68,7 +68,7 @@ impl BlockBuilder { todo!() } - /// 构造block + /// 追加Restart points /// /// /// # Examples @@ -91,7 +91,7 @@ impl BlockBuilder { todo!() } - /// 估算当前的block大小 + /// 估算当前的block大小, 超过一定大小后,写入文件 /// /// # Examples /// diff --git a/src/table/filter_block.rs b/src/table/filter_block.rs index c24694e120e61ab3a8a99086f45a1b8d68a76ffb..00133829d48b3d8763179b3ac941527325ed275d 100644 --- a/src/table/filter_block.rs +++ b/src/table/filter_block.rs @@ -13,6 +13,7 @@ const FILTER_BASE: usize = 1 << FILTER_BASE_LG; /// /// meta block 构建器 +/// FilterBlock,实质上就是SST文件里面的 meta block /// pub trait FilterBlock { fn new_with_policy(policy: FilterPolicyPtr) -> Self; diff --git a/src/table/format.rs b/src/table/format.rs index 01c9610c998bfc3f3532bf7df37e4868de6d1577..e082810331dc2420b1e28b128bde167fa603fc8f 100644 --- a/src/table/format.rs +++ b/src/table/format.rs @@ -12,26 +12,28 @@ pub const k_max_encoded_length: u32 = 10 + 10; /// of two block handles and a magic number. pub const k_encoded_length: u32 = 2 * k_max_encoded_length + 8; -// // kTableMagicNumber was picked by running -// // echo http://code.google.com/p/leveldb/ | sha1sum -// // and taking the leading 64 bits. -// pub const k_table_magic_number: &str = 0xdb4775248b80fb57ull; +/// Footer 的大小为 48 字节,内容是一个 8 字节的 magic number 和两个 BlockHandle 构成 +/// 在 Footer::EncodeTo 和 Footer::DecodeFrom 中起作用 +/// kTableMagicNumber was picked by running +/// echo http://code.google.com/p/leveldb/ | sha1sum +/// and taking the leading 64 bits. +pub const k_table_magic_number: u64 = 0xdb4775248b80fb57; /// 1-byte type + 32-bit crc pub const k_block_trailer_size: usize = 5; pub struct BlockHandle { // 偏移量 - offset_: u64, + offset: u64, // - size_: u64 + size: u64 } /// Footer encapsulates the fixed information stored at the tail /// end of every table file. pub struct Footer { - metaindex_handle_: BlockHandle, - index_handle_: BlockHandle + meta_index_handle: BlockHandle, + index_handle: BlockHandle } pub struct BlockContents { @@ -45,7 +47,7 @@ pub struct BlockContents { heap_allocated:bool, } -trait BlockTrait { +trait BlockHandleTrait { /// /// The offset of the block in the file. /// @@ -102,7 +104,7 @@ trait BlockTrait { trait FootTrait { // The block handle for the metaindex block of the table - fn metaindex_handle(&self) -> BlockHandle; + fn meta_index_handle(&self) -> BlockHandle; fn set_metaindex_handle(&mut self, h: BlockHandle); @@ -151,21 +153,21 @@ trait BlockContent { } -impl BlockTrait for BlockHandle { +impl BlockHandleTrait for BlockHandle { fn offset(&self) -> u64 { - self.offset_ + self.offset } fn set_offset(&mut self, offset: u64) { - self.offset_ = offset; + self.offset = offset; } fn size(&self) -> u64 { - self.size_ + self.size } fn set_size(&mut self, size: u64) { - self.size_ = size; + self.size = size; } fn encode_to(&self) -> Result { @@ -190,14 +192,15 @@ impl Default for BlockHandle { #[inline] fn default() -> Self { BlockHandle { - offset_: 0, - size_: 0, + offset: 0, + size: 0, } } } impl FootTrait for Footer { - fn metaindex_handle(&self) -> BlockHandle { + /// The block handle for the metaindex block of the table + fn meta_index_handle(&self) -> BlockHandle { todo!() } diff --git a/src/table/table_builder.rs b/src/table/table_builder.rs index 2d7ed0a5e660791d8f05eaee91bc2dbf049a0f04..c2d449260de3aa60f457c410609d47dd5d7f652d 100644 --- a/src/table/table_builder.rs +++ b/src/table/table_builder.rs @@ -10,6 +10,12 @@ use crate::util::slice::Slice; use crate::util::status::Status; use crate::util::unsafe_slice::UnsafeSlice; +/// 在一个 SSTable 中,文件末尾的 Footer 是定长的, +/// 其他数据都被划分成一个个变长的 block: +/// index block(@see format.BlockHandle、Footer#index_handle)、 +/// meta_index block(@see format.BlockHandle、Footer#meta_index_handle)、 +/// meta blocks(@see table.FilterBlock)、 +/// data blocks。 pub struct TableBuilder { rep: Box } @@ -41,6 +47,7 @@ struct Rep<> { pending_index_entry: bool, // Handle to add to index block // pending_handle 记录需要生成数据索引的数据块在 SSTable 中的偏移量和大小 + // 也就是说, pending_handle 主要用于表示当前块的offset及size。 pending_handle: BlockHandle, compressed_output: Slice, @@ -57,18 +64,22 @@ impl TableBuilder { todo!() } + /// 写入 entry pub fn add(&self, key: &UnsafeSlice, value: &UnsafeSlice) { todo!() } + /// flush到文件 pub fn flush(&self) { todo!() } + /// block->Finish、压缩 pub fn write_block(&self, block: &BlockBuilder, handler: &BlockHandle) { todo!() } + /// datablock写入文件,添加压缩方式、crc。 pub fn write_raw_block(&self, block_contents: &UnsafeSlice, compression_type: CompressionType, handler: &BlockHandle) { todo!() } @@ -77,6 +88,7 @@ impl TableBuilder { todo!() } + /// 剩余datablock写入文件,并生成管理区。 pub fn finish(&self) -> Status { todo!() }