mod blob_block_builder;
mod blob_block_iterator;
mod char_block_builder;
mod dict_block_builder;
mod dict_block_iterator;
mod fake_block_iterator;
mod nullable_block_builder;
mod nullable_block_iterator;
mod primitive_block_builder;
mod primitive_block_iterator;
mod rle_block_builder;
mod rle_block_iterator;
use bitvec::prelude::{BitVec, Lsb0};
pub use blob_block_builder::*;
pub use blob_block_iterator::*;
pub use char_block_builder::*;
pub use fake_block_iterator::*;
pub use nullable_block_builder::*;
pub use primitive_block_builder::*;
pub use primitive_block_iterator::*;
use risinglight_proto::rowset::BlockStatistics;
mod char_block_iterator;
pub use char_block_iterator::*;
pub use dict_block_builder::*;
pub use dict_block_iterator::*;
pub use nullable_block_iterator::*;
pub use rle_block_builder::*;
pub use rle_block_iterator::*;
mod block_index_builder;
pub use block_index_builder::*;
use bytes::{Buf, BufMut, Bytes};
use risinglight_proto::rowset::block_checksum::ChecksumType;
use risinglight_proto::rowset::block_index::BlockType;
use super::StorageResult;
use crate::array::Array;
use crate::storage::TracedStorageError;
pub type Block = Bytes;
pub trait BlockBuilder<A: Array> {
fn append(&mut self, item: Option<&A::Item>);
fn estimated_size(&self) -> usize;
fn get_statistics(&self) -> Vec<BlockStatistics>;
fn should_finish(&self, next_item: &Option<&A::Item>) -> bool;
fn finish(self) -> Vec<u8>;
fn get_target_size(&self) -> usize;
}
pub trait NonNullableBlockBuilder<A: Array> {
fn append_value(&mut self, item: &A::Item);
fn append_default(&mut self);
fn get_statistics_with_bitmap(&self, selection: &BitVec<u8, Lsb0>) -> Vec<BlockStatistics>;
fn estimated_size_with_next_item(&self, next_item: &Option<&A::Item>) -> usize;
fn is_empty(&self) -> bool;
}
pub trait BlockIterator<A: Array> {
fn next_batch(&mut self, expected_size: Option<usize>, builder: &mut A::Builder) -> usize;
fn skip(&mut self, cnt: usize);
fn remaining_items(&self) -> usize;
}
pub trait NonNullableBlockIterator<A: Array> {
fn next_batch_non_null(
&mut self,
expected_size: Option<usize>,
builder: &mut A::Builder,
) -> usize;
}
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
pub struct BlockCacheKey {
pub rowset_id: u32,
pub storage_column_id: u32,
pub block_id: u32,
}
impl BlockCacheKey {
pub fn block(mut self, block_id: u32) -> Self {
self.block_id = block_id;
self
}
pub fn column(mut self, storage_column_id: u32) -> Self {
self.storage_column_id = storage_column_id;
self
}
pub fn rowset(mut self, rowset_id: u32) -> Self {
self.rowset_id = rowset_id;
self
}
}
#[derive(Default, Debug, Clone)]
pub struct BlockMeta {
pub block_type: BlockType,
pub checksum_type: ChecksumType,
pub checksum: u64,
}
pub const BLOCK_META_NON_CHECKSUM_SIZE: usize = 4;
pub const BLOCK_META_CHECKSUM_SIZE: usize = 4 + 8;
pub const BLOCK_META_SIZE: usize = BLOCK_META_NON_CHECKSUM_SIZE + BLOCK_META_CHECKSUM_SIZE;
impl BlockMeta {
pub fn encode_except_checksum(&self, buf: &mut impl BufMut) {
buf.put_i32(self.block_type.into());
}
pub fn encode_checksum(&self, buf: &mut impl BufMut) {
buf.put_i32(self.checksum_type.into());
buf.put_u64(self.checksum);
}
pub fn decode(&mut self, buf: &mut impl Buf) -> StorageResult<()> {
if buf.remaining() < 4 + 4 + 8 {
return Err(TracedStorageError::decode("expected 16 bytes"));
}
self.block_type = BlockType::from_i32(buf.get_i32())
.ok_or_else(|| TracedStorageError::decode("expected valid block type"))?;
self.checksum_type = ChecksumType::from_i32(buf.get_i32())
.ok_or_else(|| TracedStorageError::decode("expected valid checksum type"))?;
self.checksum = buf.get_u64();
Ok(())
}
}