more hints for w1d5

Signed-off-by: Alex Chi Z <iskyzh@gmail.com>
This commit is contained in:
Alex Chi Z
2024-01-21 15:36:37 +08:00
parent 8be0a2d475
commit a2d8b3c865
2 changed files with 63 additions and 3 deletions

View File

@@ -73,7 +73,7 @@ In this task, you will need to modify:
src/lsm_storage.rs src/lsm_storage.rs
``` ```
For get requests, it will be processed as lookups in the memtables, and then scans on the SSTs. You can create a merge iterator over all SSTs after probing all memtables. You can seek to the key that the user wants to lookup. There are two possibilities of the seek: the key is the same as what the user probes, and the key is not the same / does not exist. You should only return the value to the user when the key exists and is the same as probed. You should also reduce the critical region of the state lock as in the previous section. For get requests, it will be processed as lookups in the memtables, and then scans on the SSTs. You can create a merge iterator over all SSTs after probing all memtables. You can seek to the key that the user wants to lookup. There are two possibilities of the seek: the key is the same as what the user probes, and the key is not the same / does not exist. You should only return the value to the user when the key exists and is the same as probed. You should also reduce the critical region of the state lock as in the previous section. Also remember to handle deleted keys.
## Test Your Understanding ## Test Your Understanding

View File

@@ -7,7 +7,10 @@ use week1_day5::harness::generate_sst;
use self::harness::{check_iter_result, MockIterator}; use self::harness::{check_iter_result, MockIterator};
use super::*; use super::*;
use crate::{iterators::two_merge_iterator::TwoMergeIterator, lsm_storage::LsmStorageOptions}; use crate::{
iterators::two_merge_iterator::TwoMergeIterator,
lsm_storage::{LsmStorageInner, LsmStorageOptions},
};
#[test] #[test]
fn test_task1_merge_1() { fn test_task1_merge_1() {
@@ -127,7 +130,6 @@ fn test_task1_merge_5() {
#[test] #[test]
fn test_task2_storage_scan() { fn test_task2_storage_scan() {
use crate::lsm_storage::LsmStorageInner;
let dir = tempdir().unwrap(); let dir = tempdir().unwrap();
let storage = LsmStorageInner::open(&dir, LsmStorageOptions::default_for_week1_test()).unwrap(); let storage = LsmStorageInner::open(&dir, LsmStorageOptions::default_for_week1_test()).unwrap();
storage.put(b"1", b"233").unwrap(); storage.put(b"1", b"233").unwrap();
@@ -185,3 +187,61 @@ fn test_task2_storage_scan() {
vec![(Bytes::from("2"), Bytes::from("2333"))], vec![(Bytes::from("2"), Bytes::from("2333"))],
); );
} }
#[test]
fn test_task3_storage_get() {
let dir = tempdir().unwrap();
let storage = LsmStorageInner::open(&dir, LsmStorageOptions::default_for_week1_test()).unwrap();
storage.put(b"1", b"233").unwrap();
storage.put(b"2", b"2333").unwrap();
storage.put(b"00", b"2333").unwrap();
storage
.force_freeze_memtable(&storage.state_lock.lock())
.unwrap();
storage.put(b"3", b"23333").unwrap();
storage.delete(b"1").unwrap();
let sst1 = generate_sst(
10,
dir.path().join("10.sst"),
vec![
(Bytes::from_static(b"0"), Bytes::from_static(b"2333333")),
(Bytes::from_static(b"00"), Bytes::from_static(b"2333333")),
(Bytes::from_static(b"4"), Bytes::from_static(b"23")),
],
Some(storage.block_cache.clone()),
);
let sst2 = generate_sst(
11,
dir.path().join("11.sst"),
vec![(Bytes::from_static(b"4"), Bytes::from_static(b""))],
Some(storage.block_cache.clone()),
);
{
let mut state = storage.state.write();
let mut snapshot = state.as_ref().clone();
snapshot.l0_sstables.push(sst2.sst_id()); // this is the latest SST
snapshot.l0_sstables.push(sst1.sst_id());
snapshot.sstables.insert(sst2.sst_id(), sst2.into());
snapshot.sstables.insert(sst1.sst_id(), sst1.into());
*state = snapshot.into();
}
assert_eq!(
storage.get(b"0").unwrap(),
Some(Bytes::from_static(b"2333333"))
);
assert_eq!(
storage.get(b"00").unwrap(),
Some(Bytes::from_static(b"2333"))
);
assert_eq!(
storage.get(b"2").unwrap(),
Some(Bytes::from_static(b"2333"))
);
assert_eq!(
storage.get(b"3").unwrap(),
Some(Bytes::from_static(b"23333"))
);
assert_eq!(storage.get(b"4").unwrap(), None);
assert_eq!(storage.get(b"--").unwrap(), None);
assert_eq!(storage.get(b"555").unwrap(), None);
}