finish week 1 day 3+4 block/sst

Signed-off-by: Alex Chi Z <iskyzh@gmail.com>
This commit is contained in:
Alex Chi Z
2024-01-21 14:21:09 +08:00
parent f88394a686
commit 9eb197114d
9 changed files with 237 additions and 39 deletions

View File

@@ -73,8 +73,6 @@ impl BlockMeta {
}
/// A file object.
///
/// Before day 4, it should look like:
pub struct FileObject(Option<File>, u64);
impl FileObject {
@@ -111,7 +109,7 @@ impl FileObject {
pub struct SsTable {
file: FileObject,
block_metas: Vec<BlockMeta>,
block_meta: Vec<BlockMeta>,
block_meta_offset: usize,
id: usize,
block_cache: Option<Arc<BlockCache>>,
@@ -131,12 +129,12 @@ impl SsTable {
let raw_meta_offset = file.read(len - 4, 4)?;
let block_meta_offset = (&raw_meta_offset[..]).get_u32() as u64;
let raw_meta = file.read(block_meta_offset, len - 4 - block_meta_offset)?;
let block_metas = BlockMeta::decode_block_meta(&raw_meta[..]);
let block_meta = BlockMeta::decode_block_meta(&raw_meta[..]);
Ok(Self {
file,
first_key: block_metas.first().unwrap().first_key.clone(),
last_key: block_metas.last().unwrap().last_key.clone(),
block_metas,
first_key: block_meta.first().unwrap().first_key.clone(),
last_key: block_meta.last().unwrap().last_key.clone(),
block_meta,
block_meta_offset: block_meta_offset as usize,
id,
block_cache,
@@ -147,7 +145,7 @@ impl SsTable {
pub fn create_meta_only(id: usize, file_size: u64, first_key: Bytes, last_key: Bytes) -> Self {
Self {
file: FileObject(None, file_size),
block_metas: vec![],
block_meta: vec![],
block_meta_offset: 0,
id,
block_cache: None,
@@ -158,9 +156,9 @@ impl SsTable {
/// Read a block from the disk.
pub fn read_block(&self, block_idx: usize) -> Result<Arc<Block>> {
let offset = self.block_metas[block_idx].offset;
let offset = self.block_meta[block_idx].offset;
let offset_end = self
.block_metas
.block_meta
.get(block_idx + 1)
.map_or(self.block_meta_offset, |x| x.offset);
let block_data = self
@@ -183,14 +181,14 @@ impl SsTable {
/// Find the block that may contain `key`.
pub fn find_block_idx(&self, key: &[u8]) -> usize {
self.block_metas
self.block_meta
.partition_point(|meta| meta.first_key <= key)
.saturating_sub(1)
}
/// Get number of data blocks.
pub fn num_of_blocks(&self) -> usize {
self.block_metas.len()
self.block_meta.len()
}
pub fn first_key(&self) -> &Bytes {

View File

@@ -71,8 +71,7 @@ impl SsTableBuilder {
self.data.extend(encoded_block);
}
/// Builds the SSTable and writes it to the given path. No need to actually write to disk until
/// chapter 4 block cache.
/// Builds the SSTable and writes it to the given path.
pub fn build(
mut self,
id: usize,
@@ -90,7 +89,7 @@ impl SsTableBuilder {
file,
first_key: self.meta.first().unwrap().first_key.clone(),
last_key: self.meta.last().unwrap().last_key.clone(),
block_metas: self.meta,
block_meta: self.meta,
block_meta_offset: meta_offset,
block_cache,
})

View File

@@ -61,9 +61,9 @@ fn test_sst_build_all() {
#[test]
fn test_sst_decode() {
let (_dir, sst) = generate_sst();
let meta = sst.block_metas.clone();
let meta = sst.block_meta.clone();
let new_sst = SsTable::open_for_test(sst.file).unwrap();
assert_eq!(new_sst.block_metas, meta);
assert_eq!(new_sst.block_meta, meta);
}
fn as_bytes(x: &[u8]) -> Bytes {

131
mini-lsm/src/tests/day4.rs Normal file
View File

@@ -0,0 +1,131 @@
use std::sync::Arc;
use bytes::Bytes;
use tempfile::{tempdir, TempDir};
use crate::iterators::StorageIterator;
use crate::table::{SsTable, SsTableBuilder, SsTableIterator};
#[test]
fn test_sst_build_single_key() {
let mut builder = SsTableBuilder::new(16);
builder.add(b"233", b"233333");
let dir = tempdir().unwrap();
builder.build_for_test(dir.path().join("1.sst")).unwrap();
}
#[test]
fn test_sst_build_two_blocks() {
let mut builder = SsTableBuilder::new(16);
builder.add(b"11", b"11");
builder.add(b"22", b"22");
builder.add(b"33", b"11");
builder.add(b"44", b"22");
builder.add(b"55", b"11");
builder.add(b"66", b"22");
assert!(builder.meta.len() >= 2);
let dir = tempdir().unwrap();
builder.build_for_test(dir.path().join("1.sst")).unwrap();
}
fn key_of(idx: usize) -> Vec<u8> {
format!("key_{:03}", idx * 5).into_bytes()
}
fn value_of(idx: usize) -> Vec<u8> {
format!("value_{:010}", idx).into_bytes()
}
fn num_of_keys() -> usize {
100
}
fn generate_sst() -> (TempDir, SsTable) {
let mut builder = SsTableBuilder::new(128);
for idx in 0..num_of_keys() {
let key = key_of(idx);
let value = value_of(idx);
builder.add(&key[..], &value[..]);
}
let dir = tempdir().unwrap();
let path = dir.path().join("1.sst");
(dir, builder.build_for_test(path).unwrap())
}
#[test]
fn test_sst_build_all() {
generate_sst();
}
#[test]
fn test_sst_decode() {
let (_dir, sst) = generate_sst();
let meta = sst.block_meta.clone();
let new_sst = SsTable::open_for_test(sst.file).unwrap();
assert_eq!(new_sst.block_meta, meta);
assert_eq!(new_sst.first_key(), &key_of(0));
assert_eq!(new_sst.last_key(), &key_of(num_of_keys() - 1));
}
fn as_bytes(x: &[u8]) -> Bytes {
Bytes::copy_from_slice(x)
}
#[test]
fn test_sst_iterator() {
let (_dir, sst) = generate_sst();
let sst = Arc::new(sst);
let mut iter = SsTableIterator::create_and_seek_to_first(sst).unwrap();
for _ in 0..5 {
for i in 0..num_of_keys() {
let key = iter.key();
let value = iter.value();
assert_eq!(
key,
key_of(i),
"expected key: {:?}, actual key: {:?}",
as_bytes(&key_of(i)),
as_bytes(key)
);
assert_eq!(
value,
value_of(i),
"expected value: {:?}, actual value: {:?}",
as_bytes(&value_of(i)),
as_bytes(value)
);
iter.next().unwrap();
}
iter.seek_to_first().unwrap();
}
}
#[test]
fn test_sst_seek_key() {
let (_dir, sst) = generate_sst();
let sst = Arc::new(sst);
let mut iter = SsTableIterator::create_and_seek_to_key(sst, &key_of(0)).unwrap();
for offset in 1..=5 {
for i in 0..num_of_keys() {
let key = iter.key();
let value = iter.value();
assert_eq!(
key,
key_of(i),
"expected key: {:?}, actual key: {:?}",
as_bytes(&key_of(i)),
as_bytes(key)
);
assert_eq!(
value,
value_of(i),
"expected value: {:?}, actual value: {:?}",
as_bytes(&value_of(i)),
as_bytes(value)
);
iter.seek_to_key(&format!("key_{:03}", i * 5 + offset).into_bytes())
.unwrap();
}
iter.seek_to_key(b"k").unwrap();
}
}