From 3cb2923394210685cbffa07d1a1cb0301077203d Mon Sep 17 00:00:00 2001 From: Linwei Zhang Date: Wed, 28 Feb 2024 01:49:16 +0800 Subject: [PATCH] Simplify tuple meta use --- bustubx/src/catalog/catalog.rs | 22 ++++------- bustubx/src/catalog/information.rs | 2 +- bustubx/src/execution/physical_plan/insert.rs | 14 +++---- bustubx/src/storage/page/table_page.rs | 38 ++++++++----------- bustubx/src/storage/table_heap.rs | 13 ++----- bustubx/src/transaction/lock_manager.rs | 25 +++++++----- bustubx/src/transaction/mod.rs | 2 - .../src/transaction/transaction_manager.rs | 6 +-- 8 files changed, 51 insertions(+), 71 deletions(-) diff --git a/bustubx/src/catalog/catalog.rs b/bustubx/src/catalog/catalog.rs index bf2bc4c..145a995 100644 --- a/bustubx/src/catalog/catalog.rs +++ b/bustubx/src/catalog/catalog.rs @@ -7,7 +7,7 @@ use crate::catalog::{ INFORMATION_SCHEMA_SCHEMAS, INFORMATION_SCHEMA_TABLES, SCHEMAS_SCHMEA, TABLES_SCHMEA, }; use crate::common::TableReference; -use crate::storage::{TupleMeta, BPLUS_INTERNAL_PAGE_MAX_SIZE, BPLUS_LEAF_PAGE_MAX_SIZE}; +use crate::storage::{BPLUS_INTERNAL_PAGE_MAX_SIZE, BPLUS_LEAF_PAGE_MAX_SIZE, EMPTY_TUPLE_META}; use crate::{ buffer::BufferPoolManager, storage::{index::BPlusTreeIndex, TableHeap}, @@ -88,11 +88,6 @@ impl Catalog { )); }; - let tuple_meta = TupleMeta { - insert_txn_id: 0, - delete_txn_id: 0, - is_deleted: false, - }; let tuple = Tuple::new( SCHEMAS_SCHMEA.clone(), vec![ @@ -100,7 +95,9 @@ impl Catalog { schema_name.clone().into(), ], ); - schemas_table.table.insert_tuple(&tuple_meta, &tuple)?; + schemas_table + .table + .insert_tuple(&EMPTY_TUPLE_META, &tuple)?; Ok(()) } @@ -156,11 +153,6 @@ impl Catalog { )); }; - let tuple_meta = TupleMeta { - insert_txn_id: 0, - delete_txn_id: 0, - is_deleted: false, - }; let tuple = Tuple::new( TABLES_SCHMEA.clone(), vec![ @@ -171,7 +163,7 @@ impl Catalog { (table_heap.last_page_id.load(Ordering::SeqCst)).into(), ], ); - tables_table.table.insert_tuple(&tuple_meta, &tuple)?; + tables_table.table.insert_tuple(&EMPTY_TUPLE_META, &tuple)?; let Some(columns_table) = information_schema .tables @@ -194,7 +186,9 @@ impl Catalog { col.nullable.into(), ], ); - columns_table.table.insert_tuple(&tuple_meta, &tuple)?; + columns_table + .table + .insert_tuple(&EMPTY_TUPLE_META, &tuple)?; } Ok(table_heap) diff --git a/bustubx/src/catalog/information.rs b/bustubx/src/catalog/information.rs index f13189b..897fe4d 100644 --- a/bustubx/src/catalog/information.rs +++ b/bustubx/src/catalog/information.rs @@ -7,7 +7,7 @@ use crate::common::{ScalarValue, TableReference}; use crate::storage::codec::TablePageCodec; use crate::storage::TableHeap; use crate::{BustubxError, BustubxResult, Database}; -use std::collections::HashMap; + use std::sync::Arc; pub static INFORMATION_SCHEMA_NAME: &str = "information_schema"; diff --git a/bustubx/src/execution/physical_plan/insert.rs b/bustubx/src/execution/physical_plan/insert.rs index 3cef75e..632934a 100644 --- a/bustubx/src/execution/physical_plan/insert.rs +++ b/bustubx/src/execution/physical_plan/insert.rs @@ -1,12 +1,14 @@ use log::debug; +use std::sync::atomic::Ordering; use std::sync::{atomic::AtomicU32, Arc}; use crate::catalog::{SchemaRef, INSERT_OUTPUT_SCHEMA_REF}; use crate::common::TableReference; +use crate::storage::EMPTY_TUPLE_META; use crate::{ common::ScalarValue, execution::{ExecutionContext, VolcanoExecutor}, - storage::{Tuple, TupleMeta}, + storage::Tuple, BustubxResult, }; @@ -78,12 +80,7 @@ impl VolcanoExecutor for PhysicalInsert { let tuple = Tuple::new(self.table_schema.clone(), casted_data); let table_heap = context.catalog.table_heap(&self.table)?; - let tuple_meta = TupleMeta { - insert_txn_id: 0, - delete_txn_id: 0, - is_deleted: false, - }; - let rid = table_heap.insert_tuple(&tuple_meta, &tuple)?; + let rid = table_heap.insert_tuple(&EMPTY_TUPLE_META, &tuple)?; let indexes = context.catalog.table_indexes(&self.table)?; for index in indexes { @@ -92,8 +89,7 @@ impl VolcanoExecutor for PhysicalInsert { } } - self.insert_rows - .fetch_add(1, std::sync::atomic::Ordering::SeqCst); + self.insert_rows.fetch_add(1, Ordering::SeqCst); } } diff --git a/bustubx/src/storage/page/table_page.rs b/bustubx/src/storage/page/table_page.rs index 66b65d5..e134cca 100644 --- a/bustubx/src/storage/page/table_page.rs +++ b/bustubx/src/storage/page/table_page.rs @@ -4,15 +4,17 @@ use crate::storage::codec::{TablePageHeaderCodec, TablePageHeaderTupleInfoCodec, use crate::transaction::TransactionId; use crate::{BustubxError, BustubxResult, Tuple}; +pub static EMPTY_TUPLE_META: TupleMeta = TupleMeta { + insert_txn_id: 0, + delete_txn_id: 0, + is_deleted: false, +}; + lazy_static::lazy_static! { pub static ref EMPTY_TUPLE_INFO: TupleInfo = TupleInfo { offset: 0, size: 0, - meta: TupleMeta { - insert_txn_id: 0, - delete_txn_id: 0, - is_deleted: false, - } + meta: EMPTY_TUPLE_META, }; } @@ -209,7 +211,7 @@ impl TablePage { #[cfg(test)] mod tests { use crate::catalog::{Column, DataType, Schema}; - use crate::storage::Tuple; + use crate::storage::{Tuple, EMPTY_TUPLE_META}; use std::sync::Arc; #[test] @@ -219,35 +221,30 @@ mod tests { Column::new("b", DataType::Int16, false), ])); let mut table_page = super::TablePage::new(schema.clone(), 0); - let meta = super::TupleMeta { - insert_txn_id: 0, - delete_txn_id: 0, - is_deleted: false, - }; let tuple_id = table_page .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![1i8.into(), 1i16.into()]), ) .unwrap(); assert_eq!(tuple_id, 0); let tuple_id = table_page .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()]), ) .unwrap(); assert_eq!(tuple_id, 1); let tuple_id = table_page .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![3i8.into(), 3i16.into()]), ) .unwrap(); assert_eq!(tuple_id, 2); let (tuple_meta, tuple) = table_page.tuple(0).unwrap(); - assert_eq!(tuple_meta, meta); + assert_eq!(tuple_meta, EMPTY_TUPLE_META); assert_eq!(tuple.data, vec![1i8.into(), 1i16.into()]); let (_tuple_meta, tuple) = table_page.tuple(1).unwrap(); assert_eq!(tuple.data, vec![2i8.into(), 2i16.into()]); @@ -262,26 +259,21 @@ mod tests { Column::new("b", DataType::Int16, false), ])); let mut table_page = super::TablePage::new(schema.clone(), 0); - let meta = super::TupleMeta { - insert_txn_id: 0, - delete_txn_id: 0, - is_deleted: false, - }; let _tuple_id = table_page .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![1i8.into(), 1i16.into()]), ) .unwrap(); let _tuple_id = table_page .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()]), ) .unwrap(); let _tuple_id = table_page .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![3i8.into(), 3i16.into()]), ) .unwrap(); diff --git a/bustubx/src/storage/table_heap.rs b/bustubx/src/storage/table_heap.rs index 86a75e7..52a3f17 100644 --- a/bustubx/src/storage/table_heap.rs +++ b/bustubx/src/storage/table_heap.rs @@ -275,7 +275,7 @@ mod tests { use tempfile::TempDir; use crate::catalog::{Column, DataType, Schema}; - use crate::storage::TableIterator; + use crate::storage::{TableIterator, EMPTY_TUPLE_META}; use crate::{ buffer::BufferPoolManager, storage::{table_heap::TableHeap, DiskManager, Tuple}, @@ -293,27 +293,22 @@ mod tests { let disk_manager = DiskManager::try_new(temp_path).unwrap(); let buffer_pool = Arc::new(BufferPoolManager::new(1000, Arc::new(disk_manager))); let table_heap = TableHeap::try_new(schema.clone(), buffer_pool).unwrap(); - let meta = super::TupleMeta { - insert_txn_id: 0, - delete_txn_id: 0, - is_deleted: false, - }; let _rid1 = table_heap .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![1i8.into(), 1i16.into()]), ) .unwrap(); let rid2 = table_heap .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()]), ) .unwrap(); let _rid3 = table_heap .insert_tuple( - &meta, + &EMPTY_TUPLE_META, &Tuple::new(schema.clone(), vec![3i8.into(), 3i16.into()]), ) .unwrap(); diff --git a/bustubx/src/transaction/lock_manager.rs b/bustubx/src/transaction/lock_manager.rs index d70085b..899c427 100644 --- a/bustubx/src/transaction/lock_manager.rs +++ b/bustubx/src/transaction/lock_manager.rs @@ -26,30 +26,35 @@ pub struct LockManager { } impl LockManager { - pub fn lock_table(&self, txn: Transaction, mode: LockMode, table_ref: TableReference) -> bool { + pub fn lock_table( + &self, + _txn: Transaction, + _mode: LockMode, + _table_ref: TableReference, + ) -> bool { todo!() } - pub fn unlock_table(&self, txn: Transaction, table_ref: TableReference) -> bool { + pub fn unlock_table(&self, _txn: Transaction, _table_ref: TableReference) -> bool { todo!() } pub fn lock_row( &self, - txn: Transaction, - mode: LockMode, - table_ref: TableReference, - rid: RecordId, + _txn: Transaction, + _mode: LockMode, + _table_ref: TableReference, + _rid: RecordId, ) -> bool { todo!() } pub fn unlock_row( &self, - txn: Transaction, - table_ref: TableReference, - rid: RecordId, - force: bool, + _txn: Transaction, + _table_ref: TableReference, + _rid: RecordId, + _force: bool, ) -> bool { todo!() } diff --git a/bustubx/src/transaction/mod.rs b/bustubx/src/transaction/mod.rs index 2edfe1e..5c0d07b 100644 --- a/bustubx/src/transaction/mod.rs +++ b/bustubx/src/transaction/mod.rs @@ -4,6 +4,4 @@ mod transaction_manager; pub type TransactionId = u64; -pub use lock_manager::*; pub use transaction::*; -pub use transaction_manager::*; diff --git a/bustubx/src/transaction/transaction_manager.rs b/bustubx/src/transaction/transaction_manager.rs index e5491fb..4dd64bb 100644 --- a/bustubx/src/transaction/transaction_manager.rs +++ b/bustubx/src/transaction/transaction_manager.rs @@ -9,15 +9,15 @@ pub enum IsolationLevel { pub struct TransactionManager {} impl TransactionManager { - pub fn begin(&self, isolation_level: IsolationLevel) -> Transaction { + pub fn begin(&self, _isolation_level: IsolationLevel) -> Transaction { todo!() } - pub fn commit(&self, txn: Transaction) -> bool { + pub fn commit(&self, _txn: Transaction) -> bool { todo!() } - pub fn abort(&self, txn: Transaction) { + pub fn abort(&self, _txn: Transaction) { todo!() } }