diff --git a/mini-lsm-starter/src/debug.rs b/mini-lsm-starter/src/debug.rs new file mode 100644 index 00000000..76702de8 --- /dev/null +++ b/mini-lsm-starter/src/debug.rs @@ -0,0 +1,17 @@ +use crate::lsm_storage::MiniLsm; + +impl MiniLsm { + pub fn dump_structure(&self) { + let snapshot = self.inner.state.read(); + if !snapshot.l0_sstables.is_empty() { + println!( + "L0 ({}): {:?}", + snapshot.l0_sstables.len(), + snapshot.l0_sstables, + ); + } + for (level, files) in &snapshot.levels { + println!("L{level} ({}): {:?}", files.len(), files); + } + } +} diff --git a/mini-lsm-starter/src/lib.rs b/mini-lsm-starter/src/lib.rs index ac8e8ce6..ebfb02af 100644 --- a/mini-lsm-starter/src/lib.rs +++ b/mini-lsm-starter/src/lib.rs @@ -1,5 +1,6 @@ pub mod block; pub mod compact; +pub mod debug; pub mod iterators; pub mod lsm_iterator; pub mod lsm_storage; diff --git a/mini-lsm-starter/src/table.rs b/mini-lsm-starter/src/table.rs index 9dcaa3ac..8173a87a 100644 --- a/mini-lsm-starter/src/table.rs +++ b/mini-lsm-starter/src/table.rs @@ -20,8 +20,10 @@ use crate::lsm_storage::BlockCache; pub struct BlockMeta { /// Offset of this data block. pub offset: usize, - /// The first key of the data block, mainly used for index purpose. + /// The first key of the data block. pub first_key: Bytes, + /// The last key of the data block. + pub last_key: Bytes, } impl BlockMeta { @@ -138,7 +140,23 @@ impl SsTable { /// Get number of data blocks. pub fn num_of_blocks(&self) -> usize { - unimplemented!() + self.block_metas.len() + } + + pub fn first_key(&self) -> &Bytes { + &self.first_key + } + + pub fn last_key(&self) -> &Bytes { + &self.last_key + } + + pub fn table_size(&self) -> u64 { + self.file.1 + } + + pub fn sst_id(&self) -> usize { + self.id } } diff --git a/mini-lsm/src/block.rs b/mini-lsm/src/block.rs index 65935ed1..fe4e0356 100644 --- a/mini-lsm/src/block.rs +++ b/mini-lsm/src/block.rs @@ -5,7 +5,7 @@ pub use builder::BlockBuilder; use bytes::{Buf, BufMut, Bytes}; pub use iterator::BlockIterator; -pub const SIZEOF_U16: usize = std::mem::size_of::(); +pub(crate) const SIZEOF_U16: usize = std::mem::size_of::(); /// A block is the smallest unit of read and caching in LSM tree. It is a collection of sorted /// key-value pairs.