add wal and manifest

Signed-off-by: Alex Chi Z <iskyzh@gmail.com>
This commit is contained in:
Alex Chi Z
2024-01-19 11:21:38 +08:00
parent 643734f032
commit 49c22152ae
17 changed files with 236 additions and 71 deletions

View File

@@ -1,4 +1,6 @@
use std::ops::Bound;
use std::path::Path;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use anyhow::Result;
@@ -9,10 +11,13 @@ use ouroboros::self_referencing;
use crate::iterators::StorageIterator;
use crate::table::SsTableBuilder;
use crate::wal::Wal;
/// A basic mem-table based on crossbeam-skiplist
pub struct MemTable {
map: Arc<SkipMap<Bytes, Bytes>>,
wal: Option<Wal>,
id: usize,
}
pub(crate) fn map_bound(bound: Bound<&[u8]>) -> Bound<Bytes> {
@@ -25,21 +30,47 @@ pub(crate) fn map_bound(bound: Bound<&[u8]>) -> Bound<Bytes> {
impl MemTable {
/// Create a new mem-table.
pub fn create() -> Self {
pub fn create(id: usize) -> Self {
Self {
id,
map: Arc::new(SkipMap::new()),
wal: None,
}
}
/// Create a new mem-table with WAL
pub fn create_with_wal(id: usize, path: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
id,
map: Arc::new(SkipMap::new()),
wal: Some(Wal::create(path.as_ref())?),
})
}
pub fn recover_from_wal(id: usize, path: impl AsRef<Path>) -> Result<Self> {
unimplemented!()
}
/// Get a value by key.
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]) {
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
self.map
.insert(Bytes::copy_from_slice(key), Bytes::copy_from_slice(value));
if let Some(ref wal) = self.wal {
wal.put(key, value)?;
}
Ok(())
}
pub fn sync_wal(&self) -> Result<()> {
if let Some(ref wal) = self.wal {
wal.sync()?;
}
Ok(())
}
/// Get an iterator over a range of keys.
@@ -63,6 +94,10 @@ impl MemTable {
}
Ok(())
}
pub fn id(&self) -> usize {
self.id
}
}
type SkipMapRangeIter<'a> =