From fd2f1f57316aff6b8ac2afb6aee8bee2564c8c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E4=BC=9F?= Date: Mon, 29 Jan 2024 11:36:22 +0800 Subject: [PATCH] Add schema to TablePage --- bustubx/src/storage/table_heap.rs | 18 +++++++++--------- bustubx/src/storage/table_page.rs | 21 +++++++++++++-------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/bustubx/src/storage/table_heap.rs b/bustubx/src/storage/table_heap.rs index c68444c..9e24bdf 100644 --- a/bustubx/src/storage/table_heap.rs +++ b/bustubx/src/storage/table_heap.rs @@ -25,7 +25,7 @@ impl TableHeap { .new_page() .expect("Can not new page for table heap"); let first_page_id = first_page.page_id; - let table_page = TablePage::new(INVALID_PAGE_ID); + let table_page = TablePage::new(schema.clone(), INVALID_PAGE_ID); first_page.data = table_page.to_bytes(); buffer_pool_manager.unpin_page(first_page_id, true); @@ -57,7 +57,7 @@ impl TableHeap { .expect("Can not fetch last page"); // Loop until a suitable page is found for inserting the tuple - let mut last_table_page = TablePage::from_bytes(&last_page.data); + let mut last_table_page = TablePage::from_bytes(self.schema.clone(), &last_page.data); loop { if last_table_page.get_next_tuple_offset(meta, tuple).is_some() { break; @@ -76,7 +76,7 @@ impl TableHeap { .new_page() .expect("cannot allocate page"); let next_page_id = next_page.page_id; - let next_table_page = TablePage::new(INVALID_PAGE_ID); + let next_table_page = TablePage::new(self.schema.clone(), INVALID_PAGE_ID); next_page.data = next_table_page.to_bytes(); // Update and release the previous page @@ -106,7 +106,7 @@ impl TableHeap { .buffer_pool_manager .fetch_page_mut(rid.page_id) .expect("Can not fetch page"); - let mut table_page = TablePage::from_bytes(&page.data); + let mut table_page = TablePage::from_bytes(self.schema.clone(), &page.data); table_page.update_tuple_meta(meta, &rid); page.data = table_page.to_bytes(); self.buffer_pool_manager.unpin_page(rid.page_id, true); @@ -117,7 +117,7 @@ impl TableHeap { .buffer_pool_manager .fetch_page_mut(rid.page_id) .expect("Can not fetch page"); - let mut table_page = TablePage::from_bytes(&page.data); + let mut table_page = TablePage::from_bytes(self.schema.clone(), &page.data); let result = table_page.get_tuple(&rid); self.buffer_pool_manager.unpin_page(rid.page_id, false); result @@ -128,7 +128,7 @@ impl TableHeap { .buffer_pool_manager .fetch_page_mut(rid.page_id) .expect("Can not fetch page"); - let mut table_page = TablePage::from_bytes(&page.data); + let mut table_page = TablePage::from_bytes(self.schema.clone(), &page.data); let result = table_page.get_tuple_meta(&rid); self.buffer_pool_manager.unpin_page(rid.page_id, false); result @@ -139,7 +139,7 @@ impl TableHeap { .buffer_pool_manager .fetch_page_mut(self.first_page_id) .expect("Can not fetch page"); - let table_page = TablePage::from_bytes(&page.data); + let table_page = TablePage::from_bytes(self.schema.clone(), &page.data); self.buffer_pool_manager .unpin_page(self.first_page_id, false); if table_page.num_tuples == 0 { @@ -155,7 +155,7 @@ impl TableHeap { .buffer_pool_manager .fetch_page_mut(rid.page_id) .expect("Can not fetch page"); - let table_page = TablePage::from_bytes(&page.data); + let table_page = TablePage::from_bytes(self.schema.clone(), &page.data); self.buffer_pool_manager.unpin_page(rid.page_id, false); let next_rid = table_page.get_next_rid(&rid); if next_rid.is_some() { @@ -169,7 +169,7 @@ impl TableHeap { .buffer_pool_manager .fetch_page_mut(table_page.next_page_id) .expect("Can not fetch page"); - let next_table_page = TablePage::from_bytes(&next_page.data); + let next_table_page = TablePage::from_bytes(self.schema.clone(), &next_page.data); self.buffer_pool_manager .unpin_page(table_page.next_page_id, false); if next_table_page.num_tuples == 0 { diff --git a/bustubx/src/storage/table_page.rs b/bustubx/src/storage/table_page.rs index 73f6110..51ca4e0 100644 --- a/bustubx/src/storage/table_page.rs +++ b/bustubx/src/storage/table_page.rs @@ -1,3 +1,4 @@ +use crate::catalog::SchemaRef; use crate::common::{config::BUSTUBX_PAGE_SIZE, rid::Rid}; use super::{ @@ -26,6 +27,7 @@ pub const TABLE_PAGE_TUPLE_INFO_SIZE: usize = 2 + 2 + (4 + 4 + 4); * */ pub struct TablePage { + pub schema: SchemaRef, pub next_page_id: PageId, pub num_tuples: u16, pub num_deleted_tuples: u16, @@ -37,8 +39,9 @@ pub struct TablePage { } impl TablePage { - pub fn new(next_page_id: PageId) -> Self { + pub fn new(schema: SchemaRef, next_page_id: PageId) -> Self { Self { + schema, next_page_id, num_tuples: 0, num_deleted_tuples: 0, @@ -145,9 +148,9 @@ impl TablePage { } // Parse real data from disk pages into memory pages. - pub fn from_bytes(data: &[u8]) -> Self { + 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 mut table_page = Self::new(next_page_id); + 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]]); @@ -215,14 +218,16 @@ impl TablePage { #[cfg(test)] mod tests { + use crate::catalog::Schema; use crate::{ common::{config::BUSTUBX_PAGE_SIZE, rid::Rid}, storage::tuple::Tuple, }; + use std::sync::Arc; #[test] pub fn test_table_page_insert() { - let mut table_page = super::TablePage::new(0); + let mut table_page = super::TablePage::new(Arc::new(Schema::empty()), 0); let meta = super::TupleMeta { insert_txn_id: 0, delete_txn_id: 0, @@ -255,7 +260,7 @@ mod tests { #[test] pub fn test_table_page_get_tuple() { - let mut table_page = super::TablePage::new(0); + let mut table_page = super::TablePage::new(Arc::new(Schema::empty()), 0); let meta = super::TupleMeta { insert_txn_id: 0, delete_txn_id: 0, @@ -279,7 +284,7 @@ mod tests { #[test] pub fn test_table_page_update_tuple_meta() { - let mut table_page = super::TablePage::new(0); + let mut table_page = super::TablePage::new(Arc::new(Schema::empty()), 0); let meta = super::TupleMeta { insert_txn_id: 0, delete_txn_id: 0, @@ -303,7 +308,7 @@ mod tests { #[test] pub fn test_table_page_from_to_bytes() { - let mut table_page = super::TablePage::new(1); + let mut table_page = super::TablePage::new(Arc::new(Schema::empty()), 1); let meta = super::TupleMeta { insert_txn_id: 0, delete_txn_id: 0, @@ -314,7 +319,7 @@ mod tests { let tuple_id3 = table_page.insert_tuple(&meta, &Tuple::new(vec![3, 3, 3])); let bytes = table_page.to_bytes(); - let table_page2 = super::TablePage::from_bytes(&bytes); + let table_page2 = super::TablePage::from_bytes(Arc::new(Schema::empty()), &bytes); assert_eq!(table_page2.next_page_id, 1); assert_eq!(table_page2.num_tuples, 3); assert_eq!(table_page2.num_deleted_tuples, 0);