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

@@ -79,18 +79,14 @@ impl FileObject {
}
}
/// -------------------------------------------------------------------------------------------------------
/// | Data Block | Meta Block | Extra |
/// -------------------------------------------------------------------------------------------------------
/// | Data Block #1 | ... | Data Block #N | Meta Block #1 | ... | Meta Block #N | Meta Block Offset (u32) |
/// -------------------------------------------------------------------------------------------------------
/// An SSTable.
pub struct SsTable {
/// The actual storage unit of SsTable, the format is as above.
file: FileObject,
pub(crate) file: FileObject,
/// The meta blocks that hold info for data blocks.
block_metas: Vec<BlockMeta>,
pub(crate) block_meta: Vec<BlockMeta>,
/// The offset that indicates the start point of meta blocks in `file`.
block_meta_offset: usize,
pub(crate) block_meta_offset: usize,
id: usize,
block_cache: Option<Arc<BlockCache>>,
first_key: Bytes,
@@ -112,7 +108,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,
@@ -140,7 +136,7 @@ impl SsTable {
/// 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

@@ -7,12 +7,16 @@ use std::sync::Arc;
use anyhow::Result;
use super::{BlockMeta, SsTable};
use crate::lsm_storage::BlockCache;
use crate::{block::BlockBuilder, lsm_storage::BlockCache};
/// Builds an SSTable from key-value pairs.
pub struct SsTableBuilder {
pub(super) meta: Vec<BlockMeta>,
// Add other fields you need.
builder: BlockBuilder,
first_key: Vec<u8>,
last_key: Vec<u8>,
data: Vec<u8>,
pub(crate) meta: Vec<BlockMeta>,
block_size: usize,
}
impl SsTableBuilder {
@@ -22,21 +26,22 @@ impl SsTableBuilder {
}
/// Adds a key-value pair to SSTable.
///
/// Note: You should split a new block when the current block is full.(`std::mem::replace` may
/// be of help here)
/// be helpful here)
pub fn add(&mut self, key: &[u8], value: &[u8]) {
unimplemented!()
}
/// Get the estimated size of the SSTable.
///
/// Since the data blocks contain much more data than meta blocks, just return the size of data
/// blocks here.
pub fn estimated_size(&self) -> usize {
unimplemented!()
}
/// 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. Use the `FileObject` structure to manipulate the disk objects.
pub fn build(
self,
id: usize,

View File

@@ -6,10 +6,14 @@ use std::sync::Arc;
use anyhow::Result;
use super::SsTable;
use crate::iterators::StorageIterator;
use crate::{block::BlockIterator, iterators::StorageIterator};
/// An iterator over the contents of an SSTable.
pub struct SsTableIterator {}
pub struct SsTableIterator {
table: Arc<SsTable>,
blk_iter: BlockIterator,
blk_idx: usize,
}
impl SsTableIterator {
/// Create a new iterator and seek to the first key-value pair in the first data block.