Skip to content

Commit

Permalink
memtable: update memtable view (#154)
Browse files Browse the repository at this point in the history
* update memtable view

Signed-off-by: GanZiheng <[email protected]>
  • Loading branch information
GanZiheng committed Jun 15, 2022
1 parent 6b5c341 commit 6972d08
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/memtable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{
collections::VecDeque,
mem::{self, ManuallyDrop, MaybeUninit},
ptr,
sync::{atomic::AtomicBool, Arc, RwLock},
};

Expand All @@ -17,8 +15,6 @@ use crate::{
AgateOptions, Result,
};

const MEMTABLE_VIEW_MAX: usize = 20;

/// MemTableInner guards WAL and max_version.
/// These data will only be modified on memtable put.
/// Therefore, separating wal and max_version enables
Expand Down Expand Up @@ -153,23 +149,12 @@ impl Drop for MemTable {
}

pub struct MemTablesView {
tables: ManuallyDrop<[Skiplist<Comparator>; MEMTABLE_VIEW_MAX]>,
len: usize,
tables: Vec<Skiplist<Comparator>>,
}

impl MemTablesView {
pub fn tables(&self) -> &[Skiplist<Comparator>] {
&self.tables[0..self.len]
}
}

impl Drop for MemTablesView {
fn drop(&mut self) {
for i in 0..self.len {
unsafe {
ptr::drop_in_place(&mut self.tables[i]);
}
}
&self.tables[..]
}
}

Expand All @@ -186,17 +171,16 @@ impl MemTables {
/// Get view of all current memtables
pub fn view(&self) -> MemTablesView {
// Maybe flush is better.
assert!(self.immutable.len() < MEMTABLE_VIEW_MAX);
let mut array: [MaybeUninit<Skiplist<Comparator>>; MEMTABLE_VIEW_MAX] =
unsafe { MaybeUninit::uninit().assume_init() };
array[0] = MaybeUninit::new(self.mutable.skl.clone());
for (i, s) in self.immutable.iter().enumerate() {
array[i + 1] = MaybeUninit::new(s.skl.clone());
}
MemTablesView {
tables: unsafe { ManuallyDrop::new(mem::transmute(array)) },
len: self.immutable.len() + 1,
let len = self.immutable.len() + 1;

let mut tables: Vec<Skiplist<Comparator>> = Vec::with_capacity(len);

tables.push(self.mutable.skl.clone());
for s in self.immutable.iter() {
tables.push(s.skl.clone());
}

MemTablesView { tables }
}

/// Get mutable memtable
Expand Down

0 comments on commit 6972d08

Please sign in to comment.