From 6fc72def1338b896c2dc9707d90c85b5322d37ec Mon Sep 17 00:00:00 2001 From: HeartLinked Date: Mon, 5 Aug 2024 14:18:26 +0800 Subject: [PATCH] [week1] 1.1 task1 --- mini-lsm-starter/src/mem_table.rs | 18 +++++++++++++++--- mini-lsm-starter/src/tests.rs | 3 +++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/mini-lsm-starter/src/mem_table.rs b/mini-lsm-starter/src/mem_table.rs index dc4fa5a..2bba9f9 100644 --- a/mini-lsm-starter/src/mem_table.rs +++ b/mini-lsm-starter/src/mem_table.rs @@ -38,7 +38,12 @@ pub(crate) fn map_bound(bound: Bound<&[u8]>) -> Bound { 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 @@ -69,7 +74,11 @@ impl MemTable { /// Get a value by key. pub fn get(&self, _key: &[u8]) -> Option { - 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. @@ -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. diff --git a/mini-lsm-starter/src/tests.rs b/mini-lsm-starter/src/tests.rs index 688adfa..283b258 100644 --- a/mini-lsm-starter/src/tests.rs +++ b/mini-lsm-starter/src/tests.rs @@ -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;