feat(code): finish part 3

Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
Alex Chi
2022-12-24 14:48:57 -05:00
parent 4eb2177a3e
commit 3ed6204400
19 changed files with 517 additions and 101 deletions

View File

@@ -8,5 +8,7 @@ publish = false
anyhow = "1"
arc-swap = "1"
bytes = "1"
crossbeam-epoch = "0.9"
crossbeam-skiplist = "0.1"
parking_lot = "0.12"
ouroboros = "0.15"

View File

@@ -1 +1,54 @@
#![allow(unused_variables)] // TODO(you): remove this lint after implementing this mod
#![allow(dead_code)] // TODO(you): remove this lint after implementing this mod
use crate::iterators::impls::StorageIterator;
use anyhow::Result;
pub struct LsmIterator {}
impl StorageIterator for LsmIterator {
fn is_valid(&self) -> bool {
unimplemented!()
}
fn key(&self) -> &[u8] {
unimplemented!()
}
fn value(&self) -> &[u8] {
unimplemented!()
}
fn next(&mut self) -> Result<()> {
unimplemented!()
}
}
/// A wrapper around existing iterator, will prevent users from calling `next` when the iterator is invalid.
pub struct FusedIterator<I: StorageIterator> {
iter: I,
}
impl<I: StorageIterator> FusedIterator<I> {
pub fn new(iter: I) -> Self {
Self { iter }
}
}
impl<I: StorageIterator> StorageIterator for FusedIterator<I> {
fn is_valid(&self) -> bool {
unimplemented!()
}
fn key(&self) -> &[u8] {
unimplemented!()
}
fn value(&self) -> &[u8] {
unimplemented!()
}
fn next(&mut self) -> Result<()> {
unimplemented!()
}
}

View File

@@ -1,3 +1,6 @@
#![allow(unused_variables)] // TODO(you): remove this lint after implementing this mod
#![allow(dead_code)] // TODO(you): remove this lint after implementing this mod
use std::ops::Bound;
use std::path::Path;
use std::sync::Arc;
@@ -6,20 +9,23 @@ use anyhow::Result;
use arc_swap::ArcSwap;
use bytes::Bytes;
use crate::lsm_iterator::LsmIterator;
use crate::lsm_iterator::{FusedIterator, LsmIterator};
use crate::mem_table::MemTable;
use crate::table::{SsTable, SsTableIterator};
use crate::table::SsTable;
#[derive(Clone)]
pub struct LsmStorageInner {
/// MemTables, from oldest to earliest.
memtables: Vec<Arc<MemTable>>,
sstables: Vec<Arc<SsTable>>,
/// L0 SsTables, from oldest to earliest.
l0_sstables: Vec<Arc<SsTable>>,
}
impl LsmStorageInner {
fn create() -> Self {
Self {
memtables: vec![Arc::new(MemTable::create())],
sstables: vec![],
l0_sstables: vec![],
}
}
}
@@ -37,22 +43,7 @@ impl LsmStorage {
}
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)
unimplemented!()
}
pub fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()> {
@@ -69,7 +60,11 @@ impl LsmStorage {
unimplemented!()
}
pub fn scan(&self, _lower: Bound<&[u8]>, _upper: Bound<&[u8]>) -> Result<LsmIterator> {
pub fn scan(
&self,
_lower: Bound<&[u8]>,
_upper: Bound<&[u8]>,
) -> Result<FusedIterator<LsmIterator>> {
unimplemented!()
}
}

View File

@@ -2,10 +2,12 @@
#![allow(dead_code)] // TODO(you): remove this lint after implementing this mod
use std::ops::Bound;
use std::sync::Arc;
use anyhow::Result;
use bytes::Bytes;
use crossbeam_skiplist::SkipMap;
use ouroboros::self_referencing;
use crate::iterators::impls::StorageIterator;
use crate::table::SsTableBuilder;
@@ -46,17 +48,16 @@ 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> {
_phantom: std::marker::PhantomData<&'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 new(iter: SkipMapRangeIter<'a>) -> Self {
unimplemented!()
}
}
impl StorageIterator for MemTableIterator<'_> {
impl StorageIterator for MemTableIterator {
fn value(&self) -> &[u8] {
unimplemented!()
}
@@ -74,6 +75,4 @@ impl StorageIterator for MemTableIterator<'_> {
}
}
#[cfg(test)]
#[path = "mem_table_test.rs"]
mod tests;

View File

@@ -0,0 +1 @@
//! Please copy `mini-lsm/src/mem_table/tests.rs` here so that you can run tests.

View File

@@ -1 +0,0 @@
//! Please copy `mini-lsm/src/mem_table_test.rs` here so that you can run tests.