Skip to content

Commit

Permalink
bump toolchain
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi Z <[email protected]>
  • Loading branch information
skyzh committed Jan 10, 2024
1 parent d59b016 commit e82428c
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"xtask",
"mini-lsm-starter",
]
resolver = "2"

[workspace.package]
version = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions mini-lsm-starter/src/iterators/merge_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl<I: StorageIterator> PartialEq for HeapWrapper<I> {
impl<I: StorageIterator> Eq for HeapWrapper<I> {}

impl<I: StorageIterator> PartialOrd for HeapWrapper<I> {
#[allow(clippy::non_canonical_partial_ord_impl)]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
match self.1.key().cmp(other.1.key()) {
cmp::Ordering::Greater => Some(cmp::Ordering::Greater),
Expand Down
3 changes: 3 additions & 0 deletions mini-lsm/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ impl Block {
}

pub fn decode(data: &[u8]) -> Self {
// get number of elements in the block
let entry_offsets_len = (&data[data.len() - SIZEOF_U16..]).get_u16() as usize;
let data_end = data.len() - SIZEOF_U16 - entry_offsets_len * SIZEOF_U16;
let offsets_raw = &data[data_end..data.len() - SIZEOF_U16];
// get offset array
let offsets = offsets_raw
.chunks(SIZEOF_U16)
.map(|mut x| x.get_u16())
.collect();
// retrieve data
let data = data[0..data_end].to_vec();
Self { data, offsets }
}
Expand Down
16 changes: 10 additions & 6 deletions mini-lsm/src/block/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{Block, SIZEOF_U16};
pub struct BlockBuilder {
/// Offsets of each key-value entries.
offsets: Vec<u16>,
/// All key-value pairs in the block.
/// All serialized key-value pairs in the block.
data: Vec<u8>,
/// The expected block size.
block_size: usize,
Expand All @@ -23,29 +23,33 @@ impl BlockBuilder {
}

fn estimated_size(&self) -> usize {
self.offsets.len() * SIZEOF_U16 + self.data.len() + SIZEOF_U16
SIZEOF_U16 /* number of key-value pairs in the block */ + self.offsets.len() * SIZEOF_U16 /* offsets */ + self.data.len()
/* key-value pairs */
}

/// 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");
// The overhead here is `key_len` + `val_len` + `offset`, each is of type `u16`
if self.estimated_size() + key.len() + value.len() + SIZEOF_U16 * 3 > self.block_size
if self.estimated_size() + key.len() + value.len() + SIZEOF_U16 * 3 /* key_len, value_len and offset */ > self.block_size
&& !self.is_empty()
{
return false;
}
// The offsets should be updated at first, to maintain the correct offset
// Add the offset of the data into the offset array.
self.offsets.push(self.data.len() as u16);
// Encode key length.
self.data.put_u16(key.len() as u16);
// Encode key content.
self.data.put(key);
// Encode value length.
self.data.put_u16(value.len() as u16);
// Encode value content.
self.data.put(value);
true
}

/// Check if there is no key-value pair in the block.
/// Check if there are no key-value pairs in the block.
pub fn is_empty(&self) -> bool {
self.offsets.is_empty()
}
Expand Down
4 changes: 4 additions & 0 deletions mini-lsm/src/block/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ use super::Block;

/// Iterates on a block.
pub struct BlockIterator {
/// reference to the block
block: Arc<Block>,
/// the current key at the iterator position
key: Vec<u8>,
/// the current value at the iterator position
value: Vec<u8>,
/// the current index at the iterator position
idx: usize,
}

Expand Down
1 change: 1 addition & 0 deletions mini-lsm/src/iterators/merge_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl<I: StorageIterator> PartialEq for HeapWrapper<I> {
impl<I: StorageIterator> Eq for HeapWrapper<I> {}

impl<I: StorageIterator> PartialOrd for HeapWrapper<I> {
#[allow(clippy::non_canonical_partial_ord_impl)]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
match self.1.key().cmp(other.1.key()) {
cmp::Ordering::Greater => Some(cmp::Ordering::Greater),
Expand Down
9 changes: 3 additions & 6 deletions mini-lsm/src/lsm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ impl LsmStorage {
return Ok(Some(value));
}
}
let mut iters = Vec::new();
iters.reserve(snapshot.l0_sstables.len());
let mut iters = Vec::with_capacity(snapshot.l0_sstables.len());
for table in snapshot.l0_sstables.iter().rev() {
iters.push(Box::new(SsTableIterator::create_and_seek_to_key(
table.clone(),
Expand Down Expand Up @@ -190,16 +189,14 @@ impl LsmStorage {
Arc::clone(&guard)
}; // drop global lock here

let mut memtable_iters = Vec::new();
memtable_iters.reserve(snapshot.imm_memtables.len() + 1);
let mut memtable_iters = Vec::with_capacity(snapshot.imm_memtables.len() + 1);
memtable_iters.push(Box::new(snapshot.memtable.scan(lower, upper)));
for memtable in snapshot.imm_memtables.iter().rev() {
memtable_iters.push(Box::new(memtable.scan(lower, upper)));
}
let memtable_iter = MergeIterator::create(memtable_iters);

let mut table_iters = Vec::new();
table_iters.reserve(snapshot.l0_sstables.len());
let mut table_iters = Vec::with_capacity(snapshot.l0_sstables.len());
for table in snapshot.l0_sstables.iter().rev() {
let iter = match lower {
Bound::Included(key) => {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.70.0"
channel = "stable"
components = [ "rustfmt", "clippy" ]
profile = "minimal"

0 comments on commit e82428c

Please sign in to comment.