-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add compaction skeleton Signed-off-by: Alex Chi <[email protected]> * remove tombstone when compact to bottom-most level Signed-off-by: Alex Chi <[email protected]> * new plan Signed-off-by: Alex Chi Z <[email protected]> --------- Signed-off-by: Alex Chi <[email protected]> Signed-off-by: Alex Chi Z <[email protected]>
- Loading branch information
Showing
7 changed files
with
130 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[alias] | ||
xtask = "run --package mini-lsm-xtask --" | ||
x = "run --package mini-lsm-xtask --" | ||
test = "nextest run" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::{ | ||
iterators::{merge_iterator::MergeIterator, StorageIterator}, | ||
lsm_storage::LsmStorage, | ||
table::{SsTable, SsTableBuilder, SsTableIterator}, | ||
}; | ||
|
||
struct CompactOptions { | ||
block_size: usize, | ||
target_sst_size: usize, | ||
compact_to_bottom_level: bool, | ||
} | ||
|
||
impl LsmStorage { | ||
#[allow(dead_code)] | ||
fn compact( | ||
&self, | ||
tables: Vec<Arc<SsTable>>, | ||
options: CompactOptions, | ||
) -> Result<Vec<Arc<SsTable>>> { | ||
let mut iters = Vec::new(); | ||
iters.reserve(tables.len()); | ||
for table in tables.iter() { | ||
iters.push(Box::new(SsTableIterator::create_and_seek_to_first( | ||
table.clone(), | ||
)?)); | ||
} | ||
let mut iter = MergeIterator::create(iters); | ||
|
||
let mut builder = None; | ||
let mut new_sst = vec![]; | ||
|
||
while iter.is_valid() { | ||
if builder.is_none() { | ||
builder = Some(SsTableBuilder::new(options.block_size)); | ||
} | ||
let builder_inner = builder.as_mut().unwrap(); | ||
if options.compact_to_bottom_level { | ||
if !iter.value().is_empty() { | ||
builder_inner.add(iter.key(), iter.value()); | ||
} | ||
} else { | ||
builder_inner.add(iter.key(), iter.value()); | ||
} | ||
iter.next()?; | ||
|
||
if builder_inner.estimated_size() >= options.target_sst_size { | ||
let sst_id = self.next_sst_id(); // lock dropped here | ||
let builder = builder.take().unwrap(); | ||
let sst = Arc::new(builder.build( | ||
sst_id, | ||
Some(self.block_cache.clone()), | ||
self.path_of_sst(sst_id), | ||
)?); | ||
new_sst.push(sst); | ||
} | ||
} | ||
if let Some(builder) = builder { | ||
let sst_id = self.next_sst_id(); // lock dropped here | ||
let sst = Arc::new(builder.build( | ||
sst_id, | ||
Some(self.block_cache.clone()), | ||
self.path_of_sst(sst_id), | ||
)?); | ||
new_sst.push(sst); | ||
} | ||
Ok(new_sst) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod block; | ||
mod compact; | ||
pub mod iterators; | ||
pub mod lsm_iterator; | ||
pub mod lsm_storage; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
comment_width = 120 | ||
format_code_in_doc_comments = true | ||
format_macro_bodies = true | ||
format_macro_matchers = true | ||
normalize_comments = true | ||
normalize_doc_attributes = true | ||
imports_granularity = "Module" | ||
group_imports = "StdExternalCrate" | ||
reorder_impl_items = true | ||
reorder_imports = true | ||
tab_spaces = 4 | ||
wrap_comments = true |