Skip to content

Commit

Permalink
Add schema to TablePage
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Jan 29, 2024
1 parent c4ab4f2 commit fd2f1f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
18 changes: 9 additions & 9 deletions bustubx/src/storage/table_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down
21 changes: 13 additions & 8 deletions bustubx/src/storage/table_page.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::catalog::SchemaRef;
use crate::common::{config::BUSTUBX_PAGE_SIZE, rid::Rid};

use super::{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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]]);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit fd2f1f5

Please sign in to comment.