Skip to content

Commit

Permalink
Add BPlusTreePageTypeCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 7, 2024
1 parent e93bff7 commit 47e8d7d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
23 changes: 23 additions & 0 deletions bustubx/src/storage/codec/index_page.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::storage::codec::{CommonCodec, DecodedData};
use crate::storage::index_page::BPlusTreePageType;
use crate::{BustubxError, BustubxResult};

pub struct BPlusTreePageTypeCodec;

impl BPlusTreePageTypeCodec {
pub fn encode(page_type: &BPlusTreePageType) -> Vec<u8> {
match page_type {
BPlusTreePageType::LeafPage => CommonCodec::encode_u32(1),
BPlusTreePageType::InternalPage => CommonCodec::encode_u32(2),
}
}

pub fn decode(bytes: &[u8]) -> BustubxResult<DecodedData<BPlusTreePageType>> {
let (flag, offset) = CommonCodec::decode_u32(bytes)?;
match flag {
1 => Ok((BPlusTreePageType::LeafPage, offset)),
2 => Ok((BPlusTreePageType::InternalPage, offset)),
_ => Err(BustubxError::Storage("Invalid page type".to_string())),
}
}
}
2 changes: 2 additions & 0 deletions bustubx/src/storage/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod common;
mod index_page;
mod scalar;
mod table_page;
mod tuple;

pub use common::CommonCodec;
pub use index_page::BPlusTreePageTypeCodec;
pub use scalar::ScalarValueCodec;
pub use table_page::{TablePageCodec, TablePageHeaderCodec, TablePageHeaderTupleInfoCodec};
pub use tuple::TupleCodec;
Expand Down
9 changes: 8 additions & 1 deletion bustubx/src/storage/codec/table_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::catalog::SchemaRef;
use crate::storage::codec::{CommonCodec, DecodedData};
use crate::storage::table_page::{TablePageHeader, TupleInfo};
use crate::storage::{TablePage, TupleMeta};
use crate::BustubxResult;
use crate::{BustubxError, BustubxResult};

pub struct TablePageCodec;

Expand All @@ -16,6 +16,13 @@ impl TablePageCodec {
}

pub fn decode(bytes: &[u8], schema: SchemaRef) -> BustubxResult<DecodedData<TablePage>> {
if bytes.len() != BUSTUBX_PAGE_SIZE {
return Err(BustubxError::Storage(format!(
"Table page size is not {} instead of {}",
BUSTUBX_PAGE_SIZE,
bytes.len()
)));
}
let (header, offset) = TablePageHeaderCodec::decode(bytes)?;
let mut data = [0u8; BUSTUBX_PAGE_SIZE];
data.copy_from_slice(&bytes[0..BUSTUBX_PAGE_SIZE]);
Expand Down
9 changes: 3 additions & 6 deletions bustubx/src/storage/index_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::mem::size_of;
use super::Tuple;
use crate::buffer::{PageId, BUSTUBX_PAGE_SIZE, INVALID_PAGE_ID};
use crate::catalog::SchemaRef;
use crate::storage::codec::BPlusTreePageTypeCodec;
use crate::{catalog::Schema, common::rid::Rid};

pub const INTERNAL_PAGE_HEADER_SIZE: usize = 4 + 4 + 4;
Expand All @@ -25,7 +26,6 @@ impl BPlusTreePage {
BPlusTreePageType::LeafPage => {
Self::Leaf(BPlusTreeLeafPage::from_bytes(raw, key_schema.clone()))
}
BPlusTreePageType::InvalidPage => panic!("Invalid b+ tree page type"),
};
}
pub fn to_bytes(&self) -> [u8; BUSTUBX_PAGE_SIZE] {
Expand Down Expand Up @@ -71,22 +71,19 @@ impl BPlusTreePage {

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BPlusTreePageType {
InvalidPage,
LeafPage,
InternalPage,
}
impl BPlusTreePageType {
pub fn from_bytes(raw: &[u8; 4]) -> Self {
match u32::from_be_bytes(*raw) {
0 => Self::InvalidPage,
1 => Self::LeafPage,
2 => Self::InternalPage,
_ => panic!("Invalid page type"),
}
}
pub fn to_bytes(&self) -> [u8; 4] {
match self {
Self::InvalidPage => 0u32.to_be_bytes(),
Self::LeafPage => 1u32.to_be_bytes(),
Self::InternalPage => 2u32.to_be_bytes(),
}
Expand Down Expand Up @@ -322,7 +319,7 @@ impl BPlusTreeInternalPage {
}

pub fn from_bytes(raw: &[u8; BUSTUBX_PAGE_SIZE], key_schema: SchemaRef) -> Self {
let page_type = BPlusTreePageType::from_bytes(&raw[0..4].try_into().unwrap());
let (page_type, _) = BPlusTreePageTypeCodec::decode(raw).unwrap();
let current_size = u32::from_be_bytes(raw[4..8].try_into().unwrap());
let max_size = u32::from_be_bytes(raw[8..12].try_into().unwrap());
let mut array = Vec::with_capacity(max_size as usize);
Expand All @@ -347,7 +344,7 @@ impl BPlusTreeInternalPage {

pub fn to_bytes(&self) -> [u8; BUSTUBX_PAGE_SIZE] {
let mut buf = [0; BUSTUBX_PAGE_SIZE];
buf[0..4].copy_from_slice(&self.page_type.to_bytes());
buf[0..4].copy_from_slice(&BPlusTreePageTypeCodec::encode(&self.page_type));
buf[4..8].copy_from_slice(&self.current_size.to_be_bytes());
buf[8..12].copy_from_slice(&self.max_size.to_be_bytes());
if self.current_size == 0 {
Expand Down
1 change: 0 additions & 1 deletion bustubx/src/storage/table_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub struct TablePage {
pub schema: SchemaRef,
pub header: TablePageHeader,
// 整个页原始数据
// TODO 可以通过memmove、memcpy优化,参考bustub
pub data: [u8; BUSTUBX_PAGE_SIZE],
}

Expand Down

0 comments on commit 47e8d7d

Please sign in to comment.