2024-01-16 16:30:01 +08:00
|
|
|
use std::collections::HashMap;
|
2024-01-19 11:21:38 +08:00
|
|
|
use std::fs::File;
|
2022-12-24 10:11:06 -05:00
|
|
|
use std::ops::Bound;
|
2022-12-24 18:07:18 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-01-10 14:25:23 +08:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2022-12-24 10:11:06 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use bytes::Bytes;
|
2022-12-24 17:13:52 -05:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2022-12-24 10:11:06 -05:00
|
|
|
|
2022-12-24 18:07:18 -05:00
|
|
|
use crate::block::Block;
|
2024-01-18 17:51:24 +08:00
|
|
|
use crate::compact::{
|
|
|
|
|
CompactionController, CompactionOptions, LeveledCompactionController, LeveledCompactionOptions,
|
|
|
|
|
SimpleLeveledCompactionController, SimpleLeveledCompactionOptions, TieredCompactionController,
|
|
|
|
|
};
|
2022-12-24 14:48:57 -05:00
|
|
|
use crate::iterators::merge_iterator::MergeIterator;
|
|
|
|
|
use crate::iterators::two_merge_iterator::TwoMergeIterator;
|
2022-12-24 18:25:41 -05:00
|
|
|
use crate::iterators::StorageIterator;
|
2022-12-24 14:48:57 -05:00
|
|
|
use crate::lsm_iterator::{FusedIterator, LsmIterator};
|
2024-01-19 11:21:38 +08:00
|
|
|
use crate::manifest::{Manifest, ManifestRecord};
|
2022-12-24 14:48:57 -05:00
|
|
|
use crate::mem_table::{map_bound, MemTable};
|
|
|
|
|
use crate::table::{SsTable, SsTableBuilder, SsTableIterator};
|
2022-12-24 10:11:06 -05:00
|
|
|
|
2022-12-24 18:07:18 -05:00
|
|
|
pub type BlockCache = moka::sync::Cache<(usize, usize), Arc<Block>>;
|
|
|
|
|
|
2022-12-24 14:48:57 -05:00
|
|
|
#[derive(Clone)]
|
2024-01-18 17:51:24 +08:00
|
|
|
pub struct LsmStorageState {
|
2022-12-24 16:27:42 -05:00
|
|
|
/// The current memtable.
|
2024-01-16 16:30:01 +08:00
|
|
|
pub memtable: Arc<MemTable>,
|
2024-01-18 17:51:24 +08:00
|
|
|
/// Immutable memtables, from earliest to latest.
|
2024-01-16 16:30:01 +08:00
|
|
|
pub imm_memtables: Vec<Arc<MemTable>>,
|
2024-01-18 17:51:24 +08:00
|
|
|
/// L0 SSTs, from earliest to latest.
|
2024-01-16 16:30:01 +08:00
|
|
|
pub l0_sstables: Vec<usize>,
|
2024-01-18 17:51:24 +08:00
|
|
|
/// SsTables sorted by key range; L1 - L_max for leveled compaction, or tiers for tiered
|
2024-01-16 16:30:01 +08:00
|
|
|
/// compaction.
|
|
|
|
|
pub levels: Vec<(usize, Vec<usize>)>,
|
2024-01-18 17:51:24 +08:00
|
|
|
/// SST objects.
|
2024-01-16 16:30:01 +08:00
|
|
|
pub sstables: HashMap<usize, Arc<SsTable>>,
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
impl LsmStorageState {
|
|
|
|
|
fn create(options: &LsmStorageOptions) -> Self {
|
2024-01-19 11:21:38 +08:00
|
|
|
let levels = match &options.compaction_options {
|
2024-01-18 17:51:24 +08:00
|
|
|
CompactionOptions::Leveled(LeveledCompactionOptions { max_levels, .. })
|
2024-01-19 11:21:38 +08:00
|
|
|
| CompactionOptions::Simple(SimpleLeveledCompactionOptions { max_levels, .. }) => (1
|
|
|
|
|
..=*max_levels)
|
|
|
|
|
.map(|level| (level, Vec::new()))
|
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
|
CompactionOptions::Tiered(_) | CompactionOptions::NoCompaction => Vec::new(),
|
|
|
|
|
};
|
|
|
|
|
Self {
|
|
|
|
|
memtable: Arc::new(MemTable::create(0)),
|
|
|
|
|
imm_memtables: Vec::new(),
|
|
|
|
|
l0_sstables: Vec::new(),
|
|
|
|
|
levels,
|
|
|
|
|
sstables: Default::default(),
|
2024-01-18 17:51:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct LsmStorageOptions {
|
2024-01-18 19:40:05 +08:00
|
|
|
pub block_size: usize,
|
|
|
|
|
pub target_sst_size: usize,
|
|
|
|
|
pub compaction_options: CompactionOptions,
|
2024-01-19 11:21:38 +08:00
|
|
|
pub enable_wal: bool,
|
2024-01-18 17:51:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LsmStorageOptions {
|
|
|
|
|
pub fn default_for_week1_test() -> Self {
|
2022-12-24 10:11:06 -05:00
|
|
|
Self {
|
2024-01-18 17:51:24 +08:00
|
|
|
block_size: 4096,
|
|
|
|
|
target_sst_size: 2 << 20,
|
|
|
|
|
compaction_options: CompactionOptions::NoCompaction,
|
2024-01-19 11:21:38 +08:00
|
|
|
enable_wal: false,
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The storage interface of the LSM tree.
|
2024-01-18 17:51:24 +08:00
|
|
|
pub(crate) struct LsmStorageInner {
|
|
|
|
|
pub(crate) state: Arc<RwLock<Arc<LsmStorageState>>>,
|
2024-01-18 19:40:05 +08:00
|
|
|
pub(crate) state_lock: Mutex<()>,
|
2022-12-24 18:07:18 -05:00
|
|
|
path: PathBuf,
|
2024-01-10 14:25:23 +08:00
|
|
|
pub(crate) block_cache: Arc<BlockCache>,
|
|
|
|
|
next_sst_id: AtomicUsize,
|
2024-01-18 19:40:05 +08:00
|
|
|
pub(crate) options: Arc<LsmStorageOptions>,
|
|
|
|
|
pub(crate) compaction_controller: CompactionController,
|
2024-01-19 11:21:38 +08:00
|
|
|
pub(crate) manifest: Manifest,
|
2024-01-18 17:51:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct MiniLsm {
|
|
|
|
|
inner: Arc<LsmStorageInner>,
|
2024-01-18 19:40:05 +08:00
|
|
|
compaction_notifier: crossbeam_channel::Sender<()>,
|
2024-01-18 17:51:24 +08:00
|
|
|
compaction_thread: Mutex<Option<std::thread::JoinHandle<()>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for MiniLsm {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.compaction_notifier.send(()).ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MiniLsm {
|
|
|
|
|
pub fn close(&self) -> Result<()> {
|
|
|
|
|
self.compaction_notifier.send(()).ok();
|
|
|
|
|
let mut compaction_thread = self.compaction_thread.lock();
|
|
|
|
|
if let Some(mut compaction_thread) = compaction_thread.take() {
|
|
|
|
|
compaction_thread
|
|
|
|
|
.join()
|
|
|
|
|
.map_err(|e| anyhow::anyhow!("{:?}", e))?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn open(path: impl AsRef<Path>, options: LsmStorageOptions) -> Result<Arc<Self>> {
|
|
|
|
|
let inner = Arc::new(LsmStorageInner::open(path, options)?);
|
2024-01-18 19:40:05 +08:00
|
|
|
let (tx, rx) = crossbeam_channel::unbounded();
|
2024-01-18 17:51:24 +08:00
|
|
|
let compaction_thread = inner.spawn_compaction_thread(rx)?;
|
|
|
|
|
Ok(Arc::new(Self {
|
|
|
|
|
inner,
|
|
|
|
|
compaction_notifier: tx,
|
|
|
|
|
compaction_thread: Mutex::new(compaction_thread),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>> {
|
|
|
|
|
self.inner.get(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
|
|
|
|
self.inner.put(key, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn delete(&self, key: &[u8]) -> Result<()> {
|
|
|
|
|
self.inner.delete(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn scan(
|
|
|
|
|
&self,
|
|
|
|
|
lower: Bound<&[u8]>,
|
|
|
|
|
upper: Bound<&[u8]>,
|
|
|
|
|
) -> Result<FusedIterator<LsmIterator>> {
|
|
|
|
|
self.inner.scan(lower, upper)
|
|
|
|
|
}
|
2024-01-18 19:40:05 +08:00
|
|
|
|
2024-01-19 11:21:38 +08:00
|
|
|
pub fn force_flush(&self) -> Result<()> {
|
|
|
|
|
self.inner.force_freeze_memtable()?;
|
|
|
|
|
self.inner.force_flush_next_imm_memtable()
|
2024-01-18 19:40:05 +08:00
|
|
|
}
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
impl LsmStorageInner {
|
2024-01-10 14:25:23 +08:00
|
|
|
pub(crate) fn next_sst_id(&self) -> usize {
|
|
|
|
|
self.next_sst_id
|
|
|
|
|
.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
pub(crate) fn open(path: impl AsRef<Path>, options: LsmStorageOptions) -> Result<Self> {
|
2024-01-18 19:40:05 +08:00
|
|
|
let path = path.as_ref();
|
|
|
|
|
if !path.exists() {
|
|
|
|
|
std::fs::create_dir_all(path)?;
|
|
|
|
|
}
|
2024-01-19 11:21:38 +08:00
|
|
|
let mut state = LsmStorageState::create(&options);
|
|
|
|
|
if options.enable_wal {
|
|
|
|
|
state.memtable = Arc::new(MemTable::create_with_wal(
|
|
|
|
|
state.memtable.id(),
|
|
|
|
|
Self::path_of_wal_static(path, state.memtable.id()),
|
|
|
|
|
)?);
|
|
|
|
|
}
|
|
|
|
|
let storage = Self {
|
|
|
|
|
state: Arc::new(RwLock::new(Arc::new(state))),
|
2024-01-18 17:51:24 +08:00
|
|
|
state_lock: Mutex::new(()),
|
2024-01-18 19:40:05 +08:00
|
|
|
path: path.to_path_buf(),
|
2024-01-10 14:25:23 +08:00
|
|
|
block_cache: Arc::new(BlockCache::new(1 << 20)), // 4GB block cache,
|
|
|
|
|
next_sst_id: AtomicUsize::new(1),
|
2024-01-18 17:51:24 +08:00
|
|
|
compaction_controller: match &options.compaction_options {
|
|
|
|
|
CompactionOptions::Leveled(options) => {
|
|
|
|
|
CompactionController::Leveled(LeveledCompactionController::new(options.clone()))
|
|
|
|
|
}
|
|
|
|
|
CompactionOptions::Tiered(options) => {
|
|
|
|
|
CompactionController::Tiered(TieredCompactionController::new(options.clone()))
|
|
|
|
|
}
|
|
|
|
|
CompactionOptions::Simple(options) => CompactionController::Simple(
|
|
|
|
|
SimpleLeveledCompactionController::new(options.clone()),
|
|
|
|
|
),
|
|
|
|
|
CompactionOptions::NoCompaction => CompactionController::NoCompaction,
|
|
|
|
|
},
|
2024-01-19 11:21:38 +08:00
|
|
|
manifest: Manifest::create(path.join("MANIFEST"))?,
|
2024-01-18 17:51:24 +08:00
|
|
|
options: options.into(),
|
2024-01-19 11:21:38 +08:00
|
|
|
};
|
|
|
|
|
storage.sync_dir()?;
|
|
|
|
|
|
|
|
|
|
Ok(storage)
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-24 17:13:52 -05:00
|
|
|
/// Get a key from the storage. In day 7, this can be further optimized by using a bloom filter.
|
2022-12-24 10:11:06 -05:00
|
|
|
pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>> {
|
2022-12-24 17:13:52 -05:00
|
|
|
let snapshot = {
|
2024-01-18 17:51:24 +08:00
|
|
|
let guard = self.state.read();
|
2022-12-24 17:13:52 -05:00
|
|
|
Arc::clone(&guard)
|
|
|
|
|
}; // drop global lock here
|
|
|
|
|
|
2022-12-24 16:27:42 -05:00
|
|
|
// Search on the current memtable.
|
|
|
|
|
if let Some(value) = snapshot.memtable.get(key) {
|
|
|
|
|
if value.is_empty() {
|
|
|
|
|
// found tomestone, return key not exists
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
return Ok(Some(value));
|
|
|
|
|
}
|
2024-01-19 11:21:38 +08:00
|
|
|
|
2022-12-24 16:27:42 -05:00
|
|
|
// Search on immutable memtables.
|
|
|
|
|
for memtable in snapshot.imm_memtables.iter().rev() {
|
|
|
|
|
if let Some(value) = memtable.get(key) {
|
2022-12-24 10:11:06 -05:00
|
|
|
if value.is_empty() {
|
|
|
|
|
// found tomestone, return key not exists
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
return Ok(Some(value));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-10 14:12:34 +08:00
|
|
|
let mut iters = Vec::with_capacity(snapshot.l0_sstables.len());
|
2022-12-24 14:48:57 -05:00
|
|
|
for table in snapshot.l0_sstables.iter().rev() {
|
|
|
|
|
iters.push(Box::new(SsTableIterator::create_and_seek_to_key(
|
2024-01-16 16:30:01 +08:00
|
|
|
snapshot.sstables[table].clone(),
|
2022-12-24 14:48:57 -05:00
|
|
|
key,
|
|
|
|
|
)?));
|
|
|
|
|
}
|
|
|
|
|
let iter = MergeIterator::create(iters);
|
2024-01-19 11:21:38 +08:00
|
|
|
if iter.is_valid() && iter.key() == key {
|
2022-12-24 14:48:57 -05:00
|
|
|
return Ok(Some(Bytes::copy_from_slice(iter.value())));
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-24 17:13:52 -05:00
|
|
|
/// Put a key-value pair into the storage by writing into the current memtable.
|
2022-12-24 14:48:57 -05:00
|
|
|
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
2022-12-24 10:11:06 -05:00
|
|
|
assert!(!value.is_empty(), "value cannot be empty");
|
|
|
|
|
assert!(!key.is_empty(), "key cannot be empty");
|
2022-12-24 17:13:52 -05:00
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
let guard = self.state.read();
|
2024-01-19 11:21:38 +08:00
|
|
|
guard.memtable.put(key, value)?;
|
2022-12-24 17:13:52 -05:00
|
|
|
|
2022-12-24 14:48:57 -05:00
|
|
|
Ok(())
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-24 17:13:52 -05:00
|
|
|
/// Remove a key from the storage by writing an empty value.
|
2022-12-24 14:48:57 -05:00
|
|
|
pub fn delete(&self, key: &[u8]) -> Result<()> {
|
2022-12-24 16:27:42 -05:00
|
|
|
assert!(!key.is_empty(), "key cannot be empty");
|
2022-12-24 17:13:52 -05:00
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
let guard = self.state.read();
|
2024-01-19 11:21:38 +08:00
|
|
|
guard.memtable.put(key, b"")?;
|
2022-12-24 17:13:52 -05:00
|
|
|
|
2022-12-24 14:48:57 -05:00
|
|
|
Ok(())
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-10 14:25:23 +08:00
|
|
|
pub(crate) fn path_of_sst(&self, id: usize) -> PathBuf {
|
2022-12-24 18:07:18 -05:00
|
|
|
self.path.join(format!("{:05}.sst", id))
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 11:21:38 +08:00
|
|
|
pub(crate) fn path_of_wal_static(path: impl AsRef<Path>, id: usize) -> PathBuf {
|
|
|
|
|
path.as_ref().join(format!("{:05}.wal", id))
|
2024-01-18 17:51:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 11:21:38 +08:00
|
|
|
pub(crate) fn path_of_wal(&self, id: usize) -> PathBuf {
|
|
|
|
|
Self::path_of_wal_static(&self.path, id)
|
|
|
|
|
}
|
2022-12-24 16:27:42 -05:00
|
|
|
|
2024-01-19 11:21:38 +08:00
|
|
|
fn sync_dir(&self) -> Result<()> {
|
|
|
|
|
File::open(&self.path)?.sync_all()?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2022-12-24 16:27:42 -05:00
|
|
|
|
2024-01-19 11:21:38 +08:00
|
|
|
/// Force freeze the current memetable to an immutable memtable
|
|
|
|
|
pub fn force_freeze_memtable(&self) -> Result<()> {
|
|
|
|
|
let state_lock = self.state_lock.lock();
|
|
|
|
|
|
|
|
|
|
let memtable_id = self.next_sst_id();
|
|
|
|
|
let memtable = Arc::new(if self.options.enable_wal {
|
|
|
|
|
let mt = MemTable::create_with_wal(memtable_id, self.path_of_wal(memtable_id))?;
|
|
|
|
|
self.sync_dir()?;
|
|
|
|
|
mt
|
|
|
|
|
} else {
|
|
|
|
|
MemTable::create(memtable_id)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let old_memtable;
|
2022-12-24 16:27:42 -05:00
|
|
|
{
|
2024-01-18 17:51:24 +08:00
|
|
|
let mut guard = self.state.write();
|
2022-12-24 16:27:42 -05:00
|
|
|
// Swap the current memtable with a new one.
|
|
|
|
|
let mut snapshot = guard.as_ref().clone();
|
2024-01-19 11:21:38 +08:00
|
|
|
old_memtable = std::mem::replace(&mut snapshot.memtable, memtable);
|
2022-12-24 16:27:42 -05:00
|
|
|
// Add the memtable to the immutable memtables.
|
2024-01-19 11:21:38 +08:00
|
|
|
snapshot.imm_memtables.push(old_memtable.clone());
|
2022-12-24 16:27:42 -05:00
|
|
|
// Update the snapshot.
|
2022-12-24 17:13:52 -05:00
|
|
|
*guard = Arc::new(snapshot);
|
2022-12-24 16:27:42 -05:00
|
|
|
}
|
2024-01-19 11:21:38 +08:00
|
|
|
old_memtable.sync_wal()?;
|
|
|
|
|
|
|
|
|
|
self.manifest
|
|
|
|
|
.add_record(&state_lock, ManifestRecord::NewWal(memtable_id))?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Force flush the earliest-created immutable memtable to disk
|
|
|
|
|
pub fn force_flush_next_imm_memtable(&self) -> Result<()> {
|
|
|
|
|
let state_lock = self.state_lock.lock();
|
|
|
|
|
|
|
|
|
|
let flush_memtable;
|
2022-12-24 16:27:42 -05:00
|
|
|
|
2024-01-19 11:21:38 +08:00
|
|
|
{
|
|
|
|
|
let mut guard = self.state.read();
|
|
|
|
|
flush_memtable = guard
|
|
|
|
|
.imm_memtables
|
|
|
|
|
.first()
|
|
|
|
|
.expect("no imm memtables!")
|
|
|
|
|
.clone();
|
|
|
|
|
}
|
2022-12-24 14:48:57 -05:00
|
|
|
|
2024-01-19 11:21:38 +08:00
|
|
|
let mut builder = SsTableBuilder::new(self.options.block_size);
|
2022-12-24 16:27:42 -05:00
|
|
|
flush_memtable.flush(&mut builder)?;
|
2024-01-19 11:21:38 +08:00
|
|
|
let sst_id = flush_memtable.id();
|
2022-12-24 18:07:18 -05:00
|
|
|
let sst = Arc::new(builder.build(
|
|
|
|
|
sst_id,
|
|
|
|
|
Some(self.block_cache.clone()),
|
|
|
|
|
self.path_of_sst(sst_id),
|
|
|
|
|
)?);
|
2022-12-24 16:27:42 -05:00
|
|
|
|
|
|
|
|
// Add the flushed L0 table to the list.
|
|
|
|
|
{
|
2024-01-18 17:51:24 +08:00
|
|
|
let mut guard = self.state.write();
|
2022-12-24 16:27:42 -05:00
|
|
|
let mut snapshot = guard.as_ref().clone();
|
|
|
|
|
// Remove the memtable from the immutable memtables.
|
2024-01-19 11:21:38 +08:00
|
|
|
let mem = snapshot.imm_memtables.remove(0);
|
|
|
|
|
assert_eq!(mem.id(), sst_id);
|
2022-12-24 16:27:42 -05:00
|
|
|
// Add L0 table
|
2024-01-18 17:51:24 +08:00
|
|
|
if self.compaction_controller.flush_to_l0() {
|
|
|
|
|
// In leveled compaction or no compaction, simply flush to L0
|
|
|
|
|
snapshot.l0_sstables.push(sst_id);
|
|
|
|
|
} else {
|
|
|
|
|
// In tiered compaction, create a new tier
|
|
|
|
|
snapshot.levels.insert(0, (sst_id, vec![sst_id]));
|
|
|
|
|
}
|
2024-01-18 19:40:05 +08:00
|
|
|
println!("flushed {}.sst with size={}", sst_id, sst.table_size());
|
2024-01-16 16:30:01 +08:00
|
|
|
snapshot.sstables.insert(sst_id, sst);
|
2022-12-24 16:27:42 -05:00
|
|
|
// Update the snapshot.
|
2022-12-24 17:13:52 -05:00
|
|
|
*guard = Arc::new(snapshot);
|
2022-12-24 16:27:42 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 11:21:38 +08:00
|
|
|
if self.options.enable_wal {
|
|
|
|
|
std::fs::remove_file(self.path_of_wal(sst_id))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.manifest
|
|
|
|
|
.add_record(&state_lock, ManifestRecord::Flush(sst_id))?;
|
|
|
|
|
|
|
|
|
|
self.sync_dir()?;
|
|
|
|
|
|
2022-12-24 14:48:57 -05:00
|
|
|
Ok(())
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-24 17:13:52 -05:00
|
|
|
/// Create an iterator over a range of keys.
|
2022-12-24 14:48:57 -05:00
|
|
|
pub fn scan(
|
|
|
|
|
&self,
|
|
|
|
|
lower: Bound<&[u8]>,
|
|
|
|
|
upper: Bound<&[u8]>,
|
|
|
|
|
) -> Result<FusedIterator<LsmIterator>> {
|
2022-12-24 17:13:52 -05:00
|
|
|
let snapshot = {
|
2024-01-18 17:51:24 +08:00
|
|
|
let guard = self.state.read();
|
2022-12-24 17:13:52 -05:00
|
|
|
Arc::clone(&guard)
|
|
|
|
|
}; // drop global lock here
|
2022-12-24 14:48:57 -05:00
|
|
|
|
2024-01-10 14:12:34 +08:00
|
|
|
let mut memtable_iters = Vec::with_capacity(snapshot.imm_memtables.len() + 1);
|
2022-12-24 17:13:52 -05:00
|
|
|
memtable_iters.push(Box::new(snapshot.memtable.scan(lower, upper)));
|
2022-12-24 16:27:42 -05:00
|
|
|
for memtable in snapshot.imm_memtables.iter().rev() {
|
2022-12-24 17:13:52 -05:00
|
|
|
memtable_iters.push(Box::new(memtable.scan(lower, upper)));
|
2022-12-24 14:48:57 -05:00
|
|
|
}
|
|
|
|
|
let memtable_iter = MergeIterator::create(memtable_iters);
|
|
|
|
|
|
2024-01-10 14:12:34 +08:00
|
|
|
let mut table_iters = Vec::with_capacity(snapshot.l0_sstables.len());
|
2024-01-16 16:30:01 +08:00
|
|
|
for table_id in snapshot.l0_sstables.iter().rev() {
|
|
|
|
|
let table = snapshot.sstables[table_id].clone();
|
2022-12-24 14:48:57 -05:00
|
|
|
let iter = match lower {
|
2024-01-16 16:30:01 +08:00
|
|
|
Bound::Included(key) => SsTableIterator::create_and_seek_to_key(table, key)?,
|
2022-12-24 14:48:57 -05:00
|
|
|
Bound::Excluded(key) => {
|
2024-01-16 16:30:01 +08:00
|
|
|
let mut iter = SsTableIterator::create_and_seek_to_key(table, key)?;
|
2022-12-24 14:48:57 -05:00
|
|
|
if iter.is_valid() && iter.key() == key {
|
|
|
|
|
iter.next()?;
|
|
|
|
|
}
|
|
|
|
|
iter
|
|
|
|
|
}
|
2024-01-16 16:30:01 +08:00
|
|
|
Bound::Unbounded => SsTableIterator::create_and_seek_to_first(table)?,
|
2022-12-24 14:48:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
table_iters.push(Box::new(iter));
|
|
|
|
|
}
|
|
|
|
|
let table_iter = MergeIterator::create(table_iters);
|
|
|
|
|
|
|
|
|
|
let iter = TwoMergeIterator::create(memtable_iter, table_iter)?;
|
|
|
|
|
|
2024-01-18 15:15:51 +08:00
|
|
|
Ok(FusedIterator::new(LsmIterator::new(
|
|
|
|
|
iter,
|
|
|
|
|
map_bound(upper),
|
|
|
|
|
)?))
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
}
|