138
mini-lsm-starter/src/bin/mini-lsm-cli.rs
Normal file
138
mini-lsm-starter/src/bin/mini-lsm-cli.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
mod wrapper;
|
||||
use wrapper::mini_lsm_wrapper;
|
||||
|
||||
use anyhow::Result;
|
||||
use bytes::Bytes;
|
||||
use clap::{Parser, ValueEnum};
|
||||
use mini_lsm_wrapper::compact::{
|
||||
CompactionOptions, LeveledCompactionOptions, SimpleLeveledCompactionOptions,
|
||||
TieredCompactionOptions,
|
||||
};
|
||||
use mini_lsm_wrapper::iterators::StorageIterator;
|
||||
use mini_lsm_wrapper::lsm_storage::{LsmStorageOptions, MiniLsm};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone, ValueEnum)]
|
||||
enum CompactionStrategy {
|
||||
Simple,
|
||||
Leveled,
|
||||
Tiered,
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[arg(long, default_value = "lsm.db")]
|
||||
path: PathBuf,
|
||||
#[arg(long, default_value = "leveled")]
|
||||
compaction: CompactionStrategy,
|
||||
#[arg(long, default_value = "true")]
|
||||
enable_wal: bool,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
let lsm = MiniLsm::open(
|
||||
args.path,
|
||||
LsmStorageOptions {
|
||||
block_size: 4096,
|
||||
target_sst_size: 2 << 20, // 2MB
|
||||
num_memtable_limit: 3,
|
||||
compaction_options: match args.compaction {
|
||||
CompactionStrategy::None => CompactionOptions::NoCompaction,
|
||||
CompactionStrategy::Simple => {
|
||||
CompactionOptions::Simple(SimpleLeveledCompactionOptions {
|
||||
size_ratio_percent: 200,
|
||||
level0_file_num_compaction_trigger: 2,
|
||||
max_levels: 4,
|
||||
})
|
||||
}
|
||||
CompactionStrategy::Tiered => CompactionOptions::Tiered(TieredCompactionOptions {
|
||||
num_tiers: 3,
|
||||
max_size_amplification_percent: 200,
|
||||
size_ratio: 1,
|
||||
min_merge_width: 2,
|
||||
}),
|
||||
CompactionStrategy::Leveled => {
|
||||
CompactionOptions::Leveled(LeveledCompactionOptions {
|
||||
level0_file_num_compaction_trigger: 2,
|
||||
max_levels: 4,
|
||||
base_level_size_mb: 128,
|
||||
level_size_multiplier: 2,
|
||||
})
|
||||
}
|
||||
},
|
||||
enable_wal: args.enable_wal,
|
||||
},
|
||||
)?;
|
||||
let mut epoch = 0;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
std::io::stdin().read_line(&mut line)?;
|
||||
let line = line.trim().to_string();
|
||||
if line.starts_with("fill ") {
|
||||
let Some((_, options)) = line.split_once(' ') else {
|
||||
println!("invalid command");
|
||||
continue;
|
||||
};
|
||||
let Some((begin, end)) = options.split_once(' ') else {
|
||||
println!("invalid command");
|
||||
continue;
|
||||
};
|
||||
let begin = begin.parse::<u64>()?;
|
||||
let end = end.parse::<u64>()?;
|
||||
|
||||
for i in begin..=end {
|
||||
lsm.put(
|
||||
format!("{}", i).as_bytes(),
|
||||
format!("value{}@{}", i, epoch).as_bytes(),
|
||||
)?;
|
||||
}
|
||||
|
||||
println!("{} values filled with epoch {}", end - begin + 1, epoch);
|
||||
} else if line.starts_with("get ") {
|
||||
let Some((_, key)) = line.split_once(' ') else {
|
||||
println!("invalid command");
|
||||
continue;
|
||||
};
|
||||
if let Some(value) = lsm.get(key.as_bytes())? {
|
||||
println!("{}={:?}", key, value);
|
||||
} else {
|
||||
println!("{} not exist", key);
|
||||
}
|
||||
} else if line.starts_with("scan ") {
|
||||
let Some((_, rest)) = line.split_once(' ') else {
|
||||
println!("invalid command");
|
||||
continue;
|
||||
};
|
||||
let Some((begin_key, end_key)) = rest.split_once(' ') else {
|
||||
println!("invalid command");
|
||||
continue;
|
||||
};
|
||||
let mut iter = lsm.scan(
|
||||
std::ops::Bound::Included(begin_key.as_bytes()),
|
||||
std::ops::Bound::Included(end_key.as_bytes()),
|
||||
)?;
|
||||
while iter.is_valid() {
|
||||
println!(
|
||||
"{:?}={:?}",
|
||||
Bytes::copy_from_slice(iter.key()),
|
||||
Bytes::copy_from_slice(iter.value()),
|
||||
);
|
||||
iter.next()?;
|
||||
}
|
||||
} else if line == "dump" {
|
||||
lsm.dump_structure();
|
||||
} else if line == "flush" {
|
||||
lsm.force_flush()?;
|
||||
} else if line == "quit" {
|
||||
lsm.close()?;
|
||||
break;
|
||||
} else {
|
||||
println!("invalid command: {}", line);
|
||||
}
|
||||
epoch += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
6
mini-lsm-starter/src/bin/wrapper.rs
Normal file
6
mini-lsm-starter/src/bin/wrapper.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub mod mini_lsm_wrapper {
|
||||
pub use mini_lsm_starter::*;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn main() {}
|
||||
@@ -140,4 +140,27 @@ impl LsmStorageInner {
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn trigger_flush(&self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn spawn_flush_thread(
|
||||
self: &Arc<Self>,
|
||||
rx: crossbeam_channel::Receiver<()>,
|
||||
) -> Result<Option<std::thread::JoinHandle<()>>> {
|
||||
let this = self.clone();
|
||||
let handle = std::thread::spawn(move || {
|
||||
let ticker = crossbeam_channel::tick(Duration::from_millis(50));
|
||||
loop {
|
||||
crossbeam_channel::select! {
|
||||
recv(ticker) -> _ => if let Err(e) = this.trigger_flush() {
|
||||
eprintln!("flush failed: {}", e);
|
||||
},
|
||||
recv(rx) -> _ => return
|
||||
}
|
||||
}
|
||||
});
|
||||
return Ok(Some(handle));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,4 +13,9 @@ pub trait StorageIterator {
|
||||
|
||||
/// Move to the next position.
|
||||
fn next(&mut self) -> anyhow::Result<()>;
|
||||
|
||||
/// Number of underlying active iterators for this iterator.
|
||||
fn num_active_iterators(&self) -> usize {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,16 @@ impl LsmStorageOptions {
|
||||
num_memtable_limit: 50,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_for_week1_day6_test() -> Self {
|
||||
Self {
|
||||
block_size: 4096,
|
||||
target_sst_size: 2 << 20,
|
||||
compaction_options: CompactionOptions::NoCompaction,
|
||||
enable_wal: false,
|
||||
num_memtable_limit: 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The storage interface of the LSM tree.
|
||||
@@ -96,6 +106,10 @@ pub(crate) struct LsmStorageInner {
|
||||
/// A thin wrapper for `LsmStorageInner` and the user interface for MiniLSM.
|
||||
pub struct MiniLsm {
|
||||
pub(crate) inner: Arc<LsmStorageInner>,
|
||||
/// Notifies the L0 flush thread to stop working. (In week 1 day 6)
|
||||
flush_notifier: crossbeam_channel::Sender<()>,
|
||||
/// The handle for the compaction thread. (In week 1 day 6)
|
||||
flush_thread: Mutex<Option<std::thread::JoinHandle<()>>>,
|
||||
/// Notifies the compaction thread to stop working. (In week 2)
|
||||
compaction_notifier: crossbeam_channel::Sender<()>,
|
||||
/// The handle for the compaction thread. (In week 2)
|
||||
@@ -105,6 +119,7 @@ pub struct MiniLsm {
|
||||
impl Drop for MiniLsm {
|
||||
fn drop(&mut self) {
|
||||
self.compaction_notifier.send(()).ok();
|
||||
self.flush_notifier.send(()).ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,11 +132,15 @@ impl MiniLsm {
|
||||
/// not exist.
|
||||
pub fn open(path: impl AsRef<Path>, options: LsmStorageOptions) -> Result<Arc<Self>> {
|
||||
let inner = Arc::new(LsmStorageInner::open(path, options)?);
|
||||
let (tx, rx) = crossbeam_channel::unbounded();
|
||||
let (tx1, rx) = crossbeam_channel::unbounded();
|
||||
let compaction_thread = inner.spawn_compaction_thread(rx)?;
|
||||
let (tx2, rx) = crossbeam_channel::unbounded();
|
||||
let flush_thread = inner.spawn_flush_thread(rx)?;
|
||||
Ok(Arc::new(Self {
|
||||
inner,
|
||||
compaction_notifier: tx,
|
||||
flush_notifier: tx2,
|
||||
flush_thread: Mutex::new(flush_thread),
|
||||
compaction_notifier: tx1,
|
||||
compaction_thread: Mutex::new(compaction_thread),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ impl MemTable {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
/// Flush the mem-table to SSTable.
|
||||
/// Flush the mem-table to SSTable. Implement in week 1 day 6.
|
||||
pub fn flush(&self, _builder: &mut SsTableBuilder) -> Result<()> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
pub struct Storage {}
|
||||
Reference in New Issue
Block a user