Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wcy-fdu committed Mar 16, 2023
1 parent 4e09c25 commit d3f65d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
14 changes: 14 additions & 0 deletions src/storage/hummock_sdk/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,20 @@ impl<'a> FullKey<&'a [u8]> {
}
}

/// Construct a [`FullKey`] from a byte slice without `table_id` encoded.
pub fn from_slice_without_table_id(
table_id: TableId,
slice_without_table_id: &'a [u8],
) -> Self {
let epoch_pos = slice_without_table_id.len() - EPOCH_LEN;
let epoch = (&slice_without_table_id[epoch_pos..]).get_u64();

Self {
user_key: UserKey::new(table_id, TableKey(&slice_without_table_id[..epoch_pos])),
epoch,
}
}

/// Construct a [`FullKey`] from a byte slice.
pub fn decode_reverse_epoch(slice: &'a [u8]) -> Self {
let epoch_pos = slice.len() - EPOCH_LEN;
Expand Down
15 changes: 10 additions & 5 deletions src/storage/src/hummock/sstable/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::mem::size_of;
use std::ops::Range;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use risingwave_common::catalog::TableId;
use risingwave_hummock_sdk::key::FullKey;
use risingwave_hummock_sdk::KeyComparator;
use {lz4, zstd};
Expand Down Expand Up @@ -142,9 +143,11 @@ pub struct Block {
pub data: Bytes,
/// Uncompressed entried data length.
data_len: usize,
/// Restart points.
table_id: u32,

/// Table id of this block.
table_id: TableId,

/// Restart points.
restart_points: Vec<RestartPoint>,
}

Expand Down Expand Up @@ -237,7 +240,7 @@ impl Block {
data: buf,
data_len,
restart_points,
table_id,
table_id: TableId::new(table_id),
}
}

Expand All @@ -249,10 +252,12 @@ impl Block {
}

pub fn capacity(&self) -> usize {
self.data.len() + self.restart_points.capacity() * std::mem::size_of::<u32>() + 4
self.data.len()
+ self.restart_points.capacity() * std::mem::size_of::<u32>()
+ std::mem::size_of::<u32>()
}

pub fn table_id(&self) -> u32 {
pub fn table_id(&self) -> TableId {
self.table_id
}

Expand Down
18 changes: 6 additions & 12 deletions src/storage/src/hummock/sstable/block_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
use std::cmp::Ordering;
use std::ops::Range;

use bytes::{Buf, BufMut, BytesMut};
use risingwave_common::catalog::TableId;
use risingwave_hummock_sdk::key::{FullKey, TableKey, UserKey, EPOCH_LEN};
use bytes::BytesMut;
use risingwave_hummock_sdk::key::FullKey;

use super::{KeyPrefix, LenType, RestartPoint};
use crate::hummock::BlockHolder;
Expand Down Expand Up @@ -77,11 +76,8 @@ impl BlockIterator {

pub fn key(&self) -> FullKey<&[u8]> {
assert!(self.is_valid());
let table_id = TableId::new(self.block.table_id());
let epoch_pos = self.key[..].len() - EPOCH_LEN;
let epoch = (&self.key[epoch_pos..]).get_u64();
let user_key = UserKey::new(table_id, TableKey(&self.key[..epoch_pos]));
FullKey::from_user_key(user_key, epoch)

FullKey::from_slice_without_table_id(self.block.table_id(), &self.key[..])
}

pub fn value(&self) -> &[u8] {
Expand Down Expand Up @@ -252,10 +248,8 @@ impl BlockIterator {
let prefix =
self.decode_prefix_at(probe as usize, key_len_type, value_len_type);
let probe_key = &self.block.data()[prefix.diff_key_range()];
let mut buf: BytesMut = BytesMut::default();
buf.put_u32(self.block.table_id());
buf.put_slice(probe_key);
let full_probe_key = FullKey::decode(&buf[..]);
let full_probe_key =
FullKey::from_slice_without_table_id(self.block.table_id(), probe_key);
match full_probe_key.cmp(&key) {
Ordering::Less | Ordering::Equal => true,
Ordering::Greater => false,
Expand Down

0 comments on commit d3f65d5

Please sign in to comment.