more comments, sync check

Signed-off-by: Alex Chi <iskyzh@gmail.com>
This commit is contained in:
Alex Chi
2022-12-23 22:32:30 -05:00
parent 72cee6ac13
commit 3c50c81b69
20 changed files with 298 additions and 46 deletions

View File

@@ -2,15 +2,18 @@ use bytes::BufMut;
use super::{Block, SIZEOF_U16};
/// Builds a block
/// Builds a block.
pub struct BlockBuilder {
/// Offsets of each key-value entries.
offsets: Vec<u16>,
/// All key-value pairs in the block.
data: Vec<u8>,
/// The expected block size.
block_size: usize,
}
impl BlockBuilder {
/// Creates a new block builder
/// Creates a new block builder.
pub fn new(block_size: usize) -> Self {
Self {
offsets: Vec::new(),
@@ -23,7 +26,7 @@ impl BlockBuilder {
self.offsets.len() * SIZEOF_U16 + self.data.len() + SIZEOF_U16
}
/// Adds a key-value pair to the block
/// Adds a key-value pair to the block. Returns false when the block is full.
#[must_use]
pub fn add(&mut self, key: &[u8], value: &[u8]) -> bool {
assert!(!key.is_empty(), "key must not be empty");
@@ -41,11 +44,12 @@ impl BlockBuilder {
true
}
/// Check if there is no key-value pair in the block.
pub fn is_empty(&self) -> bool {
self.offsets.is_empty()
}
/// Builds a block
/// Finalize the block.
pub fn build(self) -> Block {
if self.is_empty() {
panic!("block should not be empty");

View File

@@ -4,6 +4,7 @@ use bytes::Buf;
use super::Block;
/// Iterates on a block.
pub struct BlockIterator {
block: Arc<Block>,
key: Vec<u8>,
@@ -21,45 +22,44 @@ impl BlockIterator {
}
}
/// Creates a block iterator and seek to the first entry.
pub fn create_and_seek_to_first(block: Arc<Block>) -> Self {
let mut iter = Self::new(block);
iter.seek_to_first();
iter
}
/// Creates a block iterator and seek to the first key that >= `key`.
pub fn create_and_seek_to_key(block: Arc<Block>, key: &[u8]) -> Self {
let mut iter = Self::new(block);
iter.seek_to_key(key);
iter
}
/// Returns the key of the current entry.
pub fn key(&self) -> &[u8] {
debug_assert!(!self.key.is_empty(), "invalid iterator");
&self.key
}
/// Returns the value of the current entry.
pub fn value(&self) -> &[u8] {
debug_assert!(!self.key.is_empty(), "invalid iterator");
&self.value
}
/// Returns true if the iterator is valid.
pub fn is_valid(&self) -> bool {
!self.key.is_empty()
}
/// Seeks to the first key in the block.
pub fn seek_to_first(&mut self) {
self.seek_to(0);
}
pub fn len(&self) -> usize {
self.block.offsets.len()
}
pub fn is_empty(&self) -> bool {
self.block.offsets.is_empty()
}
pub fn seek_to(&mut self, idx: usize) {
/// Seeks to the idx-th key in the block.
fn seek_to(&mut self, idx: usize) {
if idx >= self.block.offsets.len() {
self.key.clear();
self.value.clear();
@@ -70,6 +70,7 @@ impl BlockIterator {
self.idx = idx;
}
/// Move to the next key in the block.
pub fn next(&mut self) {
self.idx += 1;
self.seek_to(self.idx);
@@ -89,6 +90,7 @@ impl BlockIterator {
self.value.extend(value);
}
/// Seek to the first key that >= `key`.
pub fn seek_to_key(&mut self, key: &[u8]) {
let mut low = 0;
let mut high = self.block.offsets.len();