2022-12-23 14:29:32 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
use bytes::Buf;
|
|
|
|
|
|
|
|
|
|
use super::Block;
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Iterates on a block.
|
2022-12-23 14:29:32 -05:00
|
|
|
pub struct BlockIterator {
|
2024-01-10 14:12:34 +08:00
|
|
|
/// reference to the block
|
2022-12-23 14:29:32 -05:00
|
|
|
block: Arc<Block>,
|
2024-01-10 14:12:34 +08:00
|
|
|
/// the current key at the iterator position
|
2022-12-23 14:29:32 -05:00
|
|
|
key: Vec<u8>,
|
2024-01-10 14:12:34 +08:00
|
|
|
/// the current value at the iterator position
|
2022-12-23 14:29:32 -05:00
|
|
|
value: Vec<u8>,
|
2024-01-10 14:12:34 +08:00
|
|
|
/// the current index at the iterator position
|
2022-12-23 14:29:32 -05:00
|
|
|
idx: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BlockIterator {
|
|
|
|
|
fn new(block: Arc<Block>) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
block,
|
|
|
|
|
key: Vec::new(),
|
|
|
|
|
value: Vec::new(),
|
|
|
|
|
idx: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Creates a block iterator and seek to the first entry.
|
2022-12-23 14:29:32 -05:00
|
|
|
pub fn create_and_seek_to_first(block: Arc<Block>) -> Self {
|
|
|
|
|
let mut iter = Self::new(block);
|
|
|
|
|
iter.seek_to_first();
|
|
|
|
|
iter
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Creates a block iterator and seek to the first key that >= `key`.
|
2022-12-23 14:29:32 -05:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Returns the key of the current entry.
|
2022-12-23 14:29:32 -05:00
|
|
|
pub fn key(&self) -> &[u8] {
|
|
|
|
|
debug_assert!(!self.key.is_empty(), "invalid iterator");
|
|
|
|
|
&self.key
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Returns the value of the current entry.
|
2022-12-23 14:29:32 -05:00
|
|
|
pub fn value(&self) -> &[u8] {
|
|
|
|
|
debug_assert!(!self.key.is_empty(), "invalid iterator");
|
|
|
|
|
&self.value
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Returns true if the iterator is valid.
|
2022-12-23 14:29:32 -05:00
|
|
|
pub fn is_valid(&self) -> bool {
|
|
|
|
|
!self.key.is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Seeks to the first key in the block.
|
2022-12-23 14:29:32 -05:00
|
|
|
pub fn seek_to_first(&mut self) {
|
|
|
|
|
self.seek_to(0);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Seeks to the idx-th key in the block.
|
|
|
|
|
fn seek_to(&mut self, idx: usize) {
|
2022-12-23 14:29:32 -05:00
|
|
|
if idx >= self.block.offsets.len() {
|
|
|
|
|
self.key.clear();
|
|
|
|
|
self.value.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let offset = self.block.offsets[idx] as usize;
|
|
|
|
|
self.seek_to_offset(offset);
|
|
|
|
|
self.idx = idx;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 22:32:30 -05:00
|
|
|
/// Move to the next key in the block.
|
2022-12-23 14:29:32 -05:00
|
|
|
pub fn next(&mut self) {
|
|
|
|
|
self.idx += 1;
|
|
|
|
|
self.seek_to(self.idx);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 12:05:34 +08:00
|
|
|
/// Seek to the specified position and update the current `key` and `value`
|
|
|
|
|
/// Index update will be handled by caller
|
2022-12-23 14:29:32 -05:00
|
|
|
fn seek_to_offset(&mut self, offset: usize) {
|
|
|
|
|
let mut entry = &self.block.data[offset..];
|
2023-07-11 12:05:34 +08:00
|
|
|
// Since `get_u16()` will automatically move the ptr 2 bytes ahead here,
|
|
|
|
|
// we don't need to manually advance it
|
2022-12-23 14:29:32 -05:00
|
|
|
let key_len = entry.get_u16() as usize;
|
|
|
|
|
let key = entry[..key_len].to_vec();
|
|
|
|
|
entry.advance(key_len);
|
|
|
|
|
self.key.clear();
|
|
|
|
|
self.key.extend(key);
|
|
|
|
|
let value_len = entry.get_u16() as usize;
|
|
|
|
|
let value = entry[..value_len].to_vec();
|
|
|
|
|
entry.advance(value_len);
|
|
|
|
|
self.value.clear();
|
|
|
|
|
self.value.extend(value);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 12:05:34 +08:00
|
|
|
/// Seek to the first key that is >= `key`.
|
2022-12-23 14:29:32 -05:00
|
|
|
pub fn seek_to_key(&mut self, key: &[u8]) {
|
|
|
|
|
let mut low = 0;
|
|
|
|
|
let mut high = self.block.offsets.len();
|
|
|
|
|
while low < high {
|
|
|
|
|
let mid = low + (high - low) / 2;
|
|
|
|
|
self.seek_to(mid);
|
|
|
|
|
assert!(self.is_valid());
|
|
|
|
|
match self.key().cmp(key) {
|
|
|
|
|
std::cmp::Ordering::Less => low = mid + 1,
|
|
|
|
|
std::cmp::Ordering::Greater => high = mid,
|
|
|
|
|
std::cmp::Ordering::Equal => return,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-23 21:14:11 -05:00
|
|
|
self.seek_to(low);
|
2022-12-23 14:29:32 -05:00
|
|
|
}
|
|
|
|
|
}
|