Skip to content

Commit

Permalink
Simplify tuple meta use
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 27, 2024
1 parent 304a4fc commit 3cb2923
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 71 deletions.
22 changes: 8 additions & 14 deletions bustubx/src/catalog/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -88,19 +88,16 @@ 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![
DEFAULT_CATALOG_NAME.to_string().into(),
schema_name.clone().into(),
],
);
schemas_table.table.insert_tuple(&tuple_meta, &tuple)?;
schemas_table
.table
.insert_tuple(&EMPTY_TUPLE_META, &tuple)?;
Ok(())
}

Expand Down Expand Up @@ -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![
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/catalog/information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
14 changes: 5 additions & 9 deletions bustubx/src/execution/physical_plan/insert.rs
Original file line number Diff line number Diff line change
@@ -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,
};

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

Expand Down
38 changes: 15 additions & 23 deletions bustubx/src/storage/page/table_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down Expand Up @@ -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]
Expand All @@ -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()]);
Expand All @@ -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();
Expand Down
13 changes: 4 additions & 9 deletions bustubx/src/storage/table_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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();
Expand Down
25 changes: 15 additions & 10 deletions bustubx/src/transaction/lock_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
}
Expand Down
2 changes: 0 additions & 2 deletions bustubx/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ mod transaction_manager;

pub type TransactionId = u64;

pub use lock_manager::*;
pub use transaction::*;
pub use transaction_manager::*;
6 changes: 3 additions & 3 deletions bustubx/src/transaction/transaction_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
}
}

0 comments on commit 3cb2923

Please sign in to comment.