Skip to content

Commit

Permalink
[week1] 1.1 task1
Browse files Browse the repository at this point in the history
  • Loading branch information
HeartLinked committed Aug 5, 2024
1 parent b84dd38 commit cfdfd8e
Show file tree
Hide file tree
Showing 4 changed files with 608 additions and 3 deletions.
18 changes: 15 additions & 3 deletions mini-lsm-starter/src/mem_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ pub(crate) fn map_bound(bound: Bound<&[u8]>) -> Bound<Bytes> {
impl MemTable {
/// Create a new mem-table.
pub fn create(_id: usize) -> Self {
unimplemented!()
Self {
map: Arc::new(SkipMap::new()),
wal: None, // 假设 Wal 在初始化时为 None
id: _id,
approximate_size: Arc::new(AtomicUsize::new(0)),
}
}

/// Create a new mem-table with WAL
Expand Down Expand Up @@ -69,7 +74,11 @@ impl MemTable {

/// Get a value by key.
pub fn get(&self, _key: &[u8]) -> Option<Bytes> {
unimplemented!()
let get_entry = (*self.map).get(_key);
match get_entry {
Some(entry) => Some(entry.value().clone()),
None => None,
}
}

/// Put a key-value pair into the mem-table.
Expand All @@ -78,7 +87,10 @@ impl MemTable {
/// In week 2, day 6, also flush the data to WAL.
/// In week 3, day 5, modify the function to use the batch API.
pub fn put(&self, _key: &[u8], _value: &[u8]) -> Result<()> {
unimplemented!()
let key = Bytes::copy_from_slice(_key);
let value = Bytes::copy_from_slice(_value);
self.map.insert(key, value);
Ok(())
}

/// Implement this in week 3, day 5.
Expand Down
3 changes: 3 additions & 0 deletions mini-lsm-starter/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
//! DO NOT MODIFY -- Mini-LSM tests modules
//! This file will be automatically rewritten by the copy-test command.
mod harness;
mod week1_day1;
Loading

0 comments on commit cfdfd8e

Please sign in to comment.