@@ -1,23 +1,33 @@
|
||||
use std::ops::Bound;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use bytes::Bytes;
|
||||
use crossbeam_skiplist::map::Entry;
|
||||
use crossbeam_skiplist::SkipMap;
|
||||
use ouroboros::self_referencing;
|
||||
|
||||
use crate::iterators::impls::StorageIterator;
|
||||
use crate::table::SsTableBuilder;
|
||||
|
||||
/// A basic mem-table based on crossbeam-skiplist
|
||||
pub struct MemTable {
|
||||
map: SkipMap<Bytes, Bytes>,
|
||||
map: Arc<SkipMap<Bytes, Bytes>>,
|
||||
}
|
||||
|
||||
pub(crate) fn map_bound(bound: Bound<&[u8]>) -> Bound<Bytes> {
|
||||
match bound {
|
||||
Bound::Included(x) => Bound::Included(Bytes::copy_from_slice(x)),
|
||||
Bound::Excluded(x) => Bound::Excluded(Bytes::copy_from_slice(x)),
|
||||
Bound::Unbounded => Bound::Unbounded,
|
||||
}
|
||||
}
|
||||
|
||||
impl MemTable {
|
||||
/// Create a new mem-table.
|
||||
pub fn create() -> Self {
|
||||
Self {
|
||||
map: SkipMap::new(),
|
||||
map: Arc::new(SkipMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,20 +44,18 @@ impl MemTable {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn map_bound(bound: Bound<&[u8]>) -> Bound<Bytes> {
|
||||
match bound {
|
||||
Bound::Included(x) => Bound::Included(Bytes::copy_from_slice(x)),
|
||||
Bound::Excluded(x) => Bound::Excluded(Bytes::copy_from_slice(x)),
|
||||
Bound::Unbounded => Bound::Unbounded,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get an iterator over a range of keys.
|
||||
pub fn scan(&self, lower: Bound<&[u8]>, upper: Bound<&[u8]>) -> Result<MemTableIterator> {
|
||||
let iter = self
|
||||
.map
|
||||
.range((Self::map_bound(lower), Self::map_bound(upper)));
|
||||
Ok(MemTableIterator::new(iter))
|
||||
let (lower, upper) = (map_bound(lower), map_bound(upper));
|
||||
let mut iter = MemTableIteratorBuilder {
|
||||
map: self.map.clone(),
|
||||
iter_builder: |map| map.range((lower, upper)),
|
||||
item: (Bytes::from_static(&[]), Bytes::from_static(&[])),
|
||||
}
|
||||
.build();
|
||||
let entry = iter.with_iter_mut(|iter| MemTableIterator::entry_to_item(iter.next()));
|
||||
iter.with_mut(|x| *x.item = entry);
|
||||
Ok(iter)
|
||||
}
|
||||
|
||||
/// Flush the mem-table to SSTable.
|
||||
@@ -63,48 +71,42 @@ type SkipMapRangeIter<'a> =
|
||||
crossbeam_skiplist::map::Range<'a, Bytes, (Bound<Bytes>, Bound<Bytes>), Bytes, Bytes>;
|
||||
|
||||
/// An iterator over a range of `SkipMap`.
|
||||
pub struct MemTableIterator<'a> {
|
||||
iter: SkipMapRangeIter<'a>,
|
||||
#[self_referencing]
|
||||
pub struct MemTableIterator {
|
||||
map: Arc<SkipMap<Bytes, Bytes>>,
|
||||
#[borrows(map)]
|
||||
#[not_covariant]
|
||||
iter: SkipMapRangeIter<'this>,
|
||||
item: (Bytes, Bytes),
|
||||
}
|
||||
|
||||
impl<'a> MemTableIterator<'a> {
|
||||
fn entry_to_item(entry: Option<Entry<'a, Bytes, Bytes>>) -> (Bytes, Bytes) {
|
||||
impl MemTableIterator {
|
||||
fn entry_to_item(entry: Option<Entry<'_, Bytes, Bytes>>) -> (Bytes, Bytes) {
|
||||
entry
|
||||
.map(|x| (x.key().clone(), x.value().clone()))
|
||||
.unwrap_or_else(|| (Bytes::from_static(&[]), Bytes::from_static(&[])))
|
||||
}
|
||||
|
||||
fn new(mut iter: SkipMapRangeIter<'a>) -> Self {
|
||||
let entry = iter.next();
|
||||
|
||||
Self {
|
||||
item: Self::entry_to_item(entry),
|
||||
iter,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StorageIterator for MemTableIterator<'_> {
|
||||
impl StorageIterator for MemTableIterator {
|
||||
fn value(&self) -> &[u8] {
|
||||
&self.item.1[..]
|
||||
&self.borrow_item().1[..]
|
||||
}
|
||||
|
||||
fn key(&self) -> &[u8] {
|
||||
&self.item.0[..]
|
||||
&self.borrow_item().0[..]
|
||||
}
|
||||
|
||||
fn is_valid(&self) -> bool {
|
||||
!self.item.0.is_empty()
|
||||
!self.borrow_item().0.is_empty()
|
||||
}
|
||||
|
||||
fn next(&mut self) -> Result<()> {
|
||||
let entry = self.iter.next();
|
||||
self.item = Self::entry_to_item(entry);
|
||||
let entry = self.with_iter_mut(|iter| MemTableIterator::entry_to_item(iter.next()));
|
||||
self.with_mut(|x| *x.item = entry);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "mem_table_test.rs"]
|
||||
mod tests;
|
||||
|
||||
Reference in New Issue
Block a user