From a3cadce1652e93c0c4a19d87632f48427cd43554 Mon Sep 17 00:00:00 2001 From: summerxwu Date: Fri, 25 Aug 2023 17:02:07 +0800 Subject: [PATCH] Add feature for SSTable --- src/blocks/block_builder.rs | 5 ++++- src/sstable/sstable_builder.rs | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/blocks/block_builder.rs b/src/blocks/block_builder.rs index fb3cc88..4fc2f5f 100644 --- a/src/blocks/block_builder.rs +++ b/src/blocks/block_builder.rs @@ -92,6 +92,9 @@ impl BlockBuilder { pub fn clean_up(&mut self) { self.data.clear(); self.offsets.clear(); - self.amount = 0; + self.amount = 2; + } + pub fn is_empty(&self) ->bool{ + self.amount == 2 } } diff --git a/src/sstable/sstable_builder.rs b/src/sstable/sstable_builder.rs index 6b4c1af..f37149d 100644 --- a/src/sstable/sstable_builder.rs +++ b/src/sstable/sstable_builder.rs @@ -19,7 +19,7 @@ impl SSTableBuilder { pub fn approximate_size_after_add(&self, key: &[u8], value: &[u8]) -> usize { // TODO(summerxwu): accumulate the data size of current builder memory - 1 + SSTABLE_SIZE_LIMIT } /// [`add`] function append user specified key and value pair to current builder. @@ -42,7 +42,12 @@ impl SSTableBuilder { return Ok(()); } /// build will return the `SSTable` object and serializable the content to disk file - pub fn build(&self) -> Result { + pub fn build(&mut self) -> Result { + if !self.block_builder.is_empty(){ + let data_block_holder = self.block_builder.build(); + self.data_blocks.push(data_block_holder); + self.block_builder.clean_up(); + } let seq = get_global_sequence_number(); let mut file_obj = FileObject::create(sstfile_path(seq as usize).as_str())?;