Skip to content

Commit

Permalink
refactor: sealed memtables
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed May 11, 2024
1 parent 220636c commit 037a057
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a> PrefixIterator<'a> {

let mut iters: Vec<BoxedIterator<'a>> = segment_iters;

for memtable in lock.guard.sealed.values() {
for (_, memtable) in lock.guard.sealed.iter() {
iters.push(Box::new(
memtable
.items
Expand Down
2 changes: 1 addition & 1 deletion src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'a> RangeIterator<'a> {

let mut iters: Vec<BoxedIterator<'a>> = segment_iters;

for memtable in lock.guard.sealed.values() {
for (_, memtable) in lock.guard.sealed.iter() {
iters.push(Box::new(memtable.items.range(range.clone()).map(|entry| {
Ok(Value::from((entry.key().clone(), entry.value().clone())))
})));
Expand Down
8 changes: 4 additions & 4 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Tree {
let mut memtable_lock = self.sealed_memtables.write().expect("lock is poisoned");

for segment in segments {
memtable_lock.remove(&segment.metadata.id);
memtable_lock.remove(segment.metadata.id);
}

// NOTE: Segments are registered, we can unlock the memtable(s) safely
Expand Down Expand Up @@ -276,7 +276,7 @@ impl Tree {
let yanked_memtable = Arc::new(yanked_memtable);

let tmp_memtable_id = self.get_next_segment_id();
sealed_memtables.insert(tmp_memtable_id, yanked_memtable.clone());
sealed_memtables.add(tmp_memtable_id, yanked_memtable.clone());

Some((tmp_memtable_id, yanked_memtable))
}
Expand All @@ -295,7 +295,7 @@ impl Tree {
/// May be used to restore the LSM-tree's in-memory state from some journals.
pub fn add_sealed_memtable(&self, id: MemtableId, memtable: Arc<MemTable>) {
let mut memtable_lock = self.sealed_memtables.write().expect("lock is poisoned");
memtable_lock.insert(id, memtable);
memtable_lock.add(id, memtable);
}

/// Scans the entire tree, returning the amount of items.
Expand Down Expand Up @@ -385,7 +385,7 @@ impl Tree {

// Now look in sealed memtables
let memtable_lock = self.sealed_memtables.read().expect("lock is poisoned");
for memtable in memtable_lock.values().rev() {
for (_, memtable) in memtable_lock.iter().rev() {
if let Some(item) = memtable.get(&key, seqno) {
if evict_tombstone {
return Ok(ignore_tombstone_value(item));
Expand Down
19 changes: 16 additions & 3 deletions src/tree_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
BlockCache,
};
use std::{
collections::BTreeMap,
path::PathBuf,
sync::{atomic::AtomicU64, Arc, RwLock},
};
Expand All @@ -20,8 +19,22 @@ pub type TreeId = u64;

pub type MemtableId = u64;

// TODO: Vec may be enough
pub type SealedMemtables = BTreeMap<MemtableId, Arc<MemTable>>;
#[derive(Default)]
pub struct SealedMemtables(Vec<(MemtableId, Arc<MemTable>)>);

impl SealedMemtables {
pub fn add(&mut self, id: MemtableId, memtable: Arc<MemTable>) {
self.0.push((id, memtable));
}

pub fn remove(&mut self, id_to_remove: MemtableId) {
self.0.retain(|(id, _)| *id != id_to_remove);
}

pub fn iter(&self) -> impl DoubleEndedIterator<Item = &(MemtableId, Arc<MemTable>)> {
self.0.iter()
}
}

pub fn get_next_tree_id() -> TreeId {
static TREE_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
Expand Down

0 comments on commit 037a057

Please sign in to comment.