Skip to content

Commit

Permalink
Add Serializable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 6, 2024
1 parent 3631dda commit 486695f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bustubx/src/catalog/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Catalog {
TABLE_HEAP_BUFFER_POOL_SIZE,
self.buffer_pool_manager.disk_manager.clone(),
);
let table_heap = TableHeap::new(schema.clone(), buffer_pool_manager);
let table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager);
let table_oid = self
.next_table_oid
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
Expand Down
3 changes: 3 additions & 0 deletions bustubx/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ pub enum BustubxError {

#[error("Execution error: {0}")]
Execution(String),

#[error("Storage error: {0}")]
Storage(String),
}
1 change: 0 additions & 1 deletion bustubx/src/storage/disk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl DiskManager {

pub fn deallocate_page(&self, page_id: PageId) -> BustubxResult<()> {
// TODO 利用pageId或者释放的空间
// TODO 添加单测
let mut guard = self.db_file.lock().unwrap();

// Write an empty page (all zeros) to the deallocated page.
Expand Down
2 changes: 2 additions & 0 deletions bustubx/src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod disk_manager;
pub mod index;
pub mod index_page;
mod serialize;
mod table_heap;
mod table_page;
mod tuple;

pub use disk_manager::DiskManager;
pub use serialize::Serializable;
pub use table_heap::{TableHeap, TableIterator};
pub use table_page::TablePage;

Check warning on line 12 in bustubx/src/storage/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `table_page::TablePage`
pub use tuple::{Tuple, TupleMeta};
44 changes: 44 additions & 0 deletions bustubx/src/storage/serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{BustubxError, BustubxResult};
use std::u16;

pub trait Serializable: Sized {
fn serialize(&self) -> BustubxResult<Vec<u8>>;

fn deserialize(bytes: &[u8]) -> BustubxResult<(Self, &[u8])>;
}

impl Serializable for u16 {
fn serialize(&self) -> BustubxResult<Vec<u8>> {
Ok(self.to_be_bytes().to_vec())
}

fn deserialize(bytes: &[u8]) -> BustubxResult<(Self, &[u8])> {
if bytes.len() < 2 {
return Err(BustubxError::Storage(format!(
"bytes length {} is less than {}",
bytes.len(),
2
)));
}
let data = [bytes[0], bytes[1]];
Ok((u16::from_be_bytes(data), &bytes[2..]))
}
}

impl Serializable for u32 {
fn serialize(&self) -> BustubxResult<Vec<u8>> {
Ok(self.to_ne_bytes().to_vec())
}

fn deserialize(bytes: &[u8]) -> BustubxResult<(Self, &[u8])> {
if bytes.len() < 4 {
return Err(BustubxError::Storage(format!(
"bytes length {} is less than {}",
bytes.len(),
4
)));
}
let data = [bytes[0], bytes[1], bytes[2], bytes[3]];
Ok((u32::from_ne_bytes(data), &bytes[4..]))
}
}
12 changes: 6 additions & 6 deletions bustubx/src/storage/table_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct TableHeap {
}

impl TableHeap {
pub fn new(schema: SchemaRef, mut buffer_pool_manager: BufferPoolManager) -> Self {
pub fn try_new(schema: SchemaRef, mut buffer_pool_manager: BufferPoolManager) -> Self {
// new a page and initialize
let first_page = buffer_pool_manager
.new_page()
Expand Down Expand Up @@ -224,7 +224,7 @@ mod tests {

let disk_manager = DiskManager::try_new(&temp_path).unwrap();
let buffer_pool_manager = BufferPoolManager::new(10, Arc::new(disk_manager));
let table_heap = TableHeap::new(Arc::new(Schema::empty()), buffer_pool_manager);
let table_heap = TableHeap::try_new(Arc::new(Schema::empty()), buffer_pool_manager);
assert_eq!(table_heap.first_page_id, 0);
assert_eq!(table_heap.last_page_id, 0);
}
Expand All @@ -240,7 +240,7 @@ mod tests {
]));
let disk_manager = DiskManager::try_new(&temp_path).unwrap();
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
let mut table_heap = TableHeap::new(schema.clone(), buffer_pool_manager);
let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager);
let meta = super::TupleMeta {
insert_txn_id: 0,
delete_txn_id: 0,
Expand Down Expand Up @@ -280,7 +280,7 @@ mod tests {
]));
let disk_manager = DiskManager::try_new(&temp_path).unwrap();
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
let mut table_heap = TableHeap::new(schema.clone(), buffer_pool_manager);
let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager);
let meta = super::TupleMeta {
insert_txn_id: 0,
delete_txn_id: 0,
Expand Down Expand Up @@ -329,7 +329,7 @@ mod tests {
]));
let disk_manager = DiskManager::try_new(&temp_path).unwrap();
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
let mut table_heap = TableHeap::new(schema.clone(), buffer_pool_manager);
let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager);

let meta1 = super::TupleMeta {
insert_txn_id: 1,
Expand Down Expand Up @@ -390,7 +390,7 @@ mod tests {

let disk_manager = DiskManager::try_new(&temp_path).unwrap();
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
let mut table_heap = TableHeap::new(schema.clone(), buffer_pool_manager);
let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager);

let meta1 = super::TupleMeta {
insert_txn_id: 1,
Expand Down
14 changes: 10 additions & 4 deletions bustubx/src/storage/table_page.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::buffer::{PageId, BUSTUBX_PAGE_SIZE};
use crate::catalog::SchemaRef;
use crate::common::rid::Rid;
use crate::storage::Serializable;
use crate::BustubxResult;

Check warning on line 5 in bustubx/src/storage/table_page.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `crate::BustubxResult`

use super::tuple::{Tuple, TupleMeta};

Expand Down Expand Up @@ -36,6 +38,10 @@ pub struct TablePage {
pub data: [u8; BUSTUBX_PAGE_SIZE],
}

pub struct TablePageHeader {
pub next_page_id: PageId,
}

impl TablePage {
pub fn new(schema: SchemaRef, next_page_id: PageId) -> Self {
Self {
Expand Down Expand Up @@ -150,7 +156,7 @@ impl TablePage {

// Parse real data from disk pages into memory pages.
pub fn from_bytes(schema: SchemaRef, data: &[u8]) -> Self {
let next_page_id = u32::from_be_bytes([data[0], data[1], data[2], data[3]]);
let (next_page_id, _) = PageId::deserialize(data).unwrap();
let mut table_page = Self::new(schema, next_page_id);
table_page.num_tuples = u16::from_be_bytes([data[4], data[5]]);
table_page.num_deleted_tuples = u16::from_be_bytes([data[6], data[7]]);
Expand Down Expand Up @@ -195,9 +201,9 @@ impl TablePage {

pub fn to_bytes(&self) -> [u8; BUSTUBX_PAGE_SIZE] {
let mut bytes = [0; BUSTUBX_PAGE_SIZE];
bytes[0..4].copy_from_slice(&self.next_page_id.to_be_bytes());
bytes[4..6].copy_from_slice(&self.num_tuples.to_be_bytes());
bytes[6..8].copy_from_slice(&self.num_deleted_tuples.to_be_bytes());
bytes[0..4].copy_from_slice(self.next_page_id.serialize().unwrap().as_slice());
bytes[4..6].copy_from_slice(self.num_tuples.serialize().unwrap().as_slice());
bytes[6..8].copy_from_slice(self.num_deleted_tuples.serialize().unwrap().as_slice());
for i in 0..self.num_tuples as usize {
let offset = 8 + i * TABLE_PAGE_TUPLE_INFO_SIZE;
let (tuple_offset, tuple_size, meta) = self.tuple_info[i];
Expand Down

0 comments on commit 486695f

Please sign in to comment.