@@ -1,4 +1,5 @@
|
||||
use std::ops::Bound;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
@@ -13,6 +14,7 @@ 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> {
|
||||
@@ -28,20 +30,24 @@ impl MemTable {
|
||||
pub fn create() -> Self {
|
||||
Self {
|
||||
map: Arc::new(SkipMap::new()),
|
||||
sealed: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a value by key.
|
||||
pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>> {
|
||||
let entry = self.map.get(key).map(|e| e.value().clone());
|
||||
Ok(entry)
|
||||
pub fn get(&self, key: &[u8]) -> Option<Bytes> {
|
||||
self.map.get(key).map(|e| e.value().clone())
|
||||
}
|
||||
|
||||
/// Put a key-value pair into the mem-table.
|
||||
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||
/// 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;
|
||||
}
|
||||
self.map
|
||||
.insert(Bytes::copy_from_slice(key), Bytes::copy_from_slice(value));
|
||||
Ok(())
|
||||
true
|
||||
}
|
||||
|
||||
/// Get an iterator over a range of keys.
|
||||
@@ -65,6 +71,12 @@ 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> =
|
||||
|
||||
Reference in New Issue
Block a user