check compaction converge for simple leveled

Signed-off-by: Alex Chi Z <iskyzh@gmail.com>
This commit is contained in:
Alex Chi Z
2024-01-17 16:23:58 +08:00
parent 6649111660
commit e622bee18a
6 changed files with 15 additions and 7 deletions

View File

@@ -64,7 +64,8 @@ impl BlockIterator {
} }
/// Seek to the first key that >= `key`. /// Seek to the first key that >= `key`.
/// Note: You should assume the key-value pairs in the block are sorted when being added by callers. /// Note: You should assume the key-value pairs in the block are sorted when being added by
/// callers.
pub fn seek_to_key(&mut self, key: &[u8]) { pub fn seek_to_key(&mut self, key: &[u8]) {
unimplemented!() unimplemented!()
} }

View File

@@ -22,13 +22,15 @@ impl SsTableBuilder {
} }
/// Adds a key-value pair to SSTable. /// Adds a key-value pair to SSTable.
/// Note: You should split a new block when the current block is full.(`std::mem::replace` may be of help here) /// Note: You should split a new block when the current block is full.(`std::mem::replace` may
/// be of help here)
pub fn add(&mut self, key: &[u8], value: &[u8]) { pub fn add(&mut self, key: &[u8], value: &[u8]) {
unimplemented!() unimplemented!()
} }
/// Get the estimated size of the SSTable. /// Get the estimated size of the SSTable.
/// Since the data blocks contain much more data than meta blocks, just return the size of data blocks here. /// Since the data blocks contain much more data than meta blocks, just return the size of data
/// blocks here.
pub fn estimated_size(&self) -> usize { pub fn estimated_size(&self) -> usize {
unimplemented!() unimplemented!()
} }

View File

@@ -28,7 +28,8 @@ impl SsTableIterator {
} }
/// Seek to the first key-value pair which >= `key`. /// Seek to the first key-value pair which >= `key`.
/// Note: You probably want to review the handout for detailed explanation when implementing this function. /// Note: You probably want to review the handout for detailed explanation when implementing
/// this function.
pub fn seek_to_key(&mut self, key: &[u8]) -> Result<()> { pub fn seek_to_key(&mut self, key: &[u8]) -> Result<()> {
unimplemented!() unimplemented!()
} }

View File

@@ -24,7 +24,7 @@ impl BlockBuilder {
fn estimated_size(&self) -> usize { fn estimated_size(&self) -> usize {
SIZEOF_U16 /* number of key-value pairs in the block */ + self.offsets.len() * SIZEOF_U16 /* offsets */ + self.data.len() SIZEOF_U16 /* number of key-value pairs in the block */ + self.offsets.len() * SIZEOF_U16 /* offsets */ + self.data.len()
/* key-value pairs */ // key-value pairs
} }
/// Adds a key-value pair to the block. Returns false when the block is full. /// Adds a key-value pair to the block. Returns false when the block is full.

View File

@@ -33,7 +33,8 @@ impl BlockMeta {
// The size of actual key // The size of actual key
estimated_size += meta.first_key.len(); estimated_size += meta.first_key.len();
} }
// Reserve the space to improve performance, especially when the size of incoming data is large // Reserve the space to improve performance, especially when the size of incoming data is
// large
buf.reserve(estimated_size); buf.reserve(estimated_size);
let original_len = buf.len(); let original_len = buf.len();
for meta in block_meta { for meta in block_meta {

View File

@@ -160,7 +160,6 @@ fn main() {
} }
let mut num_compactions = 0; let mut num_compactions = 0;
while let Some(task) = controller.generate_compaction_task(&storage.snapshot) { while let Some(task) = controller.generate_compaction_task(&storage.snapshot) {
num_compactions += 1;
println!("--- Compaction Task ---"); println!("--- Compaction Task ---");
let mut sst_ids = Vec::new(); let mut sst_ids = Vec::new();
for file in task for file in task
@@ -194,6 +193,10 @@ fn main() {
} else { } else {
storage.dump_original_id(true); storage.dump_original_id(true);
} }
num_compactions += 1;
if num_compactions >= max_levels * 2 {
panic!("compaction does not converge?");
}
} }
if num_compactions == 0 { if num_compactions == 0 {
println!("no compaction triggered"); println!("no compaction triggered");