fix(code): use rwlock in storage

Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
Alex Chi
2022-12-24 17:13:52 -05:00
parent 0aff26af38
commit 86503ac58d
5 changed files with 59 additions and 58 deletions

View File

@@ -1,5 +1,4 @@
use std::ops::Bound;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use anyhow::Result;
@@ -14,7 +13,6 @@ use crate::table::SsTableBuilder;
/// A basic mem-table based on crossbeam-skiplist
pub struct MemTable {
map: Arc<SkipMap<Bytes, Bytes>>,
sealed: AtomicBool,
}
pub(crate) fn map_bound(bound: Bound<&[u8]>) -> Bound<Bytes> {
@@ -30,7 +28,6 @@ impl MemTable {
pub fn create() -> Self {
Self {
map: Arc::new(SkipMap::new()),
sealed: AtomicBool::new(false),
}
}
@@ -39,19 +36,14 @@ impl MemTable {
self.map.get(key).map(|e| e.value().clone())
}
/// Put a key-value pair into the mem-table. If the current mem-table is sealed, return false.
pub fn put(&self, key: &[u8], value: &[u8]) -> bool {
use std::sync::atomic::Ordering;
if self.sealed.load(Ordering::Acquire) {
return false;
}
/// Put a key-value pair into the mem-table.
pub fn put(&self, key: &[u8], value: &[u8]) {
self.map
.insert(Bytes::copy_from_slice(key), Bytes::copy_from_slice(value));
true
}
/// Get an iterator over a range of keys.
pub fn scan(&self, lower: Bound<&[u8]>, upper: Bound<&[u8]>) -> Result<MemTableIterator> {
pub fn scan(&self, lower: Bound<&[u8]>, upper: Bound<&[u8]>) -> MemTableIterator {
let (lower, upper) = (map_bound(lower), map_bound(upper));
let mut iter = MemTableIteratorBuilder {
map: self.map.clone(),
@@ -61,7 +53,7 @@ impl MemTable {
.build();
let entry = iter.with_iter_mut(|iter| MemTableIterator::entry_to_item(iter.next()));
iter.with_mut(|x| *x.item = entry);
Ok(iter)
iter
}
/// Flush the mem-table to SSTable.
@@ -71,12 +63,6 @@ impl MemTable {
}
Ok(())
}
/// Disable writes to this memtable.
pub(crate) fn seal(&self) {
use std::sync::atomic::Ordering;
self.sealed.store(true, Ordering::Release);
}
}
type SkipMapRangeIter<'a> =