From 295881988dbbfe775b47de6133020a29246188da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E4=BC=9F?= Date: Fri, 22 Mar 2024 18:08:52 +0800 Subject: [PATCH] Improve code --- bustubx/src/execution/physical_plan/values.rs | 8 ++--- bustubx/src/storage/page/index_page.rs | 3 -- bustubx/src/transaction/mod.rs | 2 -- bustubx/src/transaction/transaction.rs | 32 +++++++++++++++++++ 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/bustubx/src/execution/physical_plan/values.rs b/bustubx/src/execution/physical_plan/values.rs index b51384e..aa5ed30 100644 --- a/bustubx/src/execution/physical_plan/values.rs +++ b/bustubx/src/execution/physical_plan/values.rs @@ -1,12 +1,10 @@ -use std::sync::atomic::AtomicU32; -use std::sync::Arc; +use std::sync::atomic::{AtomicU32, Ordering}; use crate::catalog::SchemaRef; use crate::common::ScalarValue; use crate::expression::{Expr, ExprTrait}; use crate::storage::EMPTY_TUPLE; use crate::{ - catalog::Schema, execution::{ExecutionContext, VolcanoExecutor}, storage::Tuple, BustubxResult, @@ -30,9 +28,7 @@ impl PhysicalValues { } impl VolcanoExecutor for PhysicalValues { fn next(&self, _context: &mut ExecutionContext) -> BustubxResult> { - let cursor = self - .cursor - .fetch_add(1, std::sync::atomic::Ordering::SeqCst) as usize; + let cursor = self.cursor.fetch_add(1, Ordering::SeqCst) as usize; if cursor < self.rows.len() { let values = self.rows[cursor] .iter() diff --git a/bustubx/src/storage/page/index_page.rs b/bustubx/src/storage/page/index_page.rs index 8b3ca6e..dbc1434 100644 --- a/bustubx/src/storage/page/index_page.rs +++ b/bustubx/src/storage/page/index_page.rs @@ -125,9 +125,6 @@ impl BPlusTreeInternalPage { // TODO 可以通过二分查找来插入 pub fn insert(&mut self, key: Tuple, page_id: PageId) { - // if self.header.current_size == 0 && !key.is_null() { - // panic!("First key must be zero"); - // } self.array.push((key, page_id)); self.header.current_size += 1; // 跳过第一个空key diff --git a/bustubx/src/transaction/mod.rs b/bustubx/src/transaction/mod.rs index 5c0d07b..6a31152 100644 --- a/bustubx/src/transaction/mod.rs +++ b/bustubx/src/transaction/mod.rs @@ -2,6 +2,4 @@ mod lock_manager; mod transaction; mod transaction_manager; -pub type TransactionId = u64; - pub use transaction::*; diff --git a/bustubx/src/transaction/transaction.rs b/bustubx/src/transaction/transaction.rs index 8952d63..404ac8b 100644 --- a/bustubx/src/transaction/transaction.rs +++ b/bustubx/src/transaction/transaction.rs @@ -1 +1,33 @@ +use crate::Tuple; + +pub type TransactionId = u64; +pub const INVALID_TRANSACTION_ID: TransactionId = 0; + +pub enum TransactionState { + Running, + Tainted, + Committed, + Aborted, +} + pub struct Transaction {} + +/// Represents a link to a previous version of this tuple +pub struct UndoLink { + prev_txn: TransactionId, + prev_log_idx: u32, +} + +impl UndoLink { + pub fn is_valid(&self) -> bool { + self.prev_txn != INVALID_TRANSACTION_ID + } +} + +pub struct UndoLog { + is_deleted: bool, + modified_fields: Vec, + tuple: Tuple, + timestamp: u64, + prev_version: UndoLink, +}