feat(code): part 3 iterators

Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
Alex Chi
2022-12-24 10:11:06 -05:00
parent b263ea4fac
commit 4eb2177a3e
39 changed files with 1304 additions and 73 deletions

View File

@@ -0,0 +1,75 @@
use std::ops::Bound;
use std::path::Path;
use std::sync::Arc;
use anyhow::Result;
use arc_swap::ArcSwap;
use bytes::Bytes;
use crate::lsm_iterator::LsmIterator;
use crate::mem_table::MemTable;
use crate::table::{SsTable, SsTableIterator};
pub struct LsmStorageInner {
memtables: Vec<Arc<MemTable>>,
sstables: Vec<Arc<SsTable>>,
}
impl LsmStorageInner {
fn create() -> Self {
Self {
memtables: vec![Arc::new(MemTable::create())],
sstables: vec![],
}
}
}
/// The storage interface of the LSM tree.
pub struct LsmStorage {
inner: ArcSwap<LsmStorageInner>,
}
impl LsmStorage {
pub fn open(_path: &Path) -> Result<Self> {
Ok(Self {
inner: ArcSwap::from_pointee(LsmStorageInner::create()),
})
}
pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>> {
let snapshot = self.inner.load();
for memtable in &snapshot.memtables {
if let Some(value) = memtable.get(key)? {
if value.is_empty() {
// found tomestone, return key not exists
return Ok(None);
}
return Ok(Some(value));
}
}
let mut iters = Vec::new();
iters.reserve(snapshot.sstables.len());
for table in snapshot.sstables.iter().rev() {
iters.push(SsTableIterator::create_and_seek_to_key(table.clone(), key)?);
}
Ok(None)
}
pub fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
assert!(!value.is_empty(), "value cannot be empty");
assert!(!key.is_empty(), "key cannot be empty");
unimplemented!()
}
pub fn delete(&mut self, _key: &[u8]) -> Result<()> {
unimplemented!()
}
pub fn sync(&mut self) -> Result<()> {
unimplemented!()
}
pub fn scan(&self, _lower: Bound<&[u8]>, _upper: Bound<&[u8]>) -> Result<LsmIterator> {
unimplemented!()
}
}