diff --git a/bustubx-cli/Cargo.toml b/bustubx-cli/Cargo.toml index d74da56..27d0f42 100644 --- a/bustubx-cli/Cargo.toml +++ b/bustubx-cli/Cargo.toml @@ -8,6 +8,7 @@ keywords.workspace = true license.workspace = true repository.workspace = true homepage.workspace = true +readme = "../README.md" [dependencies] bustubx = { path = "../bustubx", version = "0.2.0" } diff --git a/bustubx/src/buffer/buffer_pool.rs b/bustubx/src/buffer/buffer_pool.rs index 45ace32..7cb8393 100644 --- a/bustubx/src/buffer/buffer_pool.rs +++ b/bustubx/src/buffer/buffer_pool.rs @@ -214,44 +214,29 @@ mod tests { let temp_path = temp_dir.path().join("test.db"); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let mut buffer_pool_manager = BufferPoolManager::new(3, Arc::new(disk_manager)); - let page1 = buffer_pool_manager.new_page().unwrap().clone(); + let mut buffer_pool = BufferPoolManager::new(3, Arc::new(disk_manager)); + let page1 = buffer_pool.new_page().unwrap().clone(); let page1_id = page1.read().unwrap().page_id; - assert_eq!( - buffer_pool_manager.pool[0].read().unwrap().page_id, - page1_id, - ); - assert_eq!( - buffer_pool_manager.page_table[&page1.read().unwrap().page_id], - 0 - ); - assert_eq!(buffer_pool_manager.free_list.len(), 2); - assert_eq!(buffer_pool_manager.replacer.size(), 0); - - let page2 = buffer_pool_manager.new_page().unwrap(); + assert_eq!(buffer_pool.pool[0].read().unwrap().page_id, page1_id,); + assert_eq!(buffer_pool.page_table[&page1.read().unwrap().page_id], 0); + assert_eq!(buffer_pool.free_list.len(), 2); + assert_eq!(buffer_pool.replacer.size(), 0); + + let page2 = buffer_pool.new_page().unwrap(); let page2_id = page2.read().unwrap().page_id; - assert_eq!( - buffer_pool_manager.pool[1].read().unwrap().page_id, - page2_id, - ); + assert_eq!(buffer_pool.pool[1].read().unwrap().page_id, page2_id,); - let page3 = buffer_pool_manager.new_page().unwrap(); + let page3 = buffer_pool.new_page().unwrap(); let page3_id = page3.read().unwrap().page_id; - assert_eq!( - buffer_pool_manager.pool[2].read().unwrap().page_id, - page3_id, - ); + assert_eq!(buffer_pool.pool[2].read().unwrap().page_id, page3_id,); - let page4 = buffer_pool_manager.new_page(); + let page4 = buffer_pool.new_page(); assert!(page4.is_err()); - buffer_pool_manager.unpin_page(page1_id, false).unwrap(); - let page5 = buffer_pool_manager.new_page().unwrap(); + buffer_pool.unpin_page(page1_id, false).unwrap(); + let page5 = buffer_pool.new_page().unwrap(); let page5_id = page5.read().unwrap().page_id; - assert_eq!( - buffer_pool_manager.pool[0].read().unwrap().page_id, - page5_id, - ); + assert_eq!(buffer_pool.pool[0].read().unwrap().page_id, page5_id,); } #[test] @@ -260,17 +245,17 @@ mod tests { let temp_path = temp_dir.path().join("test.db"); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let mut buffer_pool_manager = BufferPoolManager::new(3, Arc::new(disk_manager)); + let mut buffer_pool = BufferPoolManager::new(3, Arc::new(disk_manager)); - let page1 = buffer_pool_manager.new_page().unwrap(); + let page1 = buffer_pool.new_page().unwrap(); let page1_id = page1.read().unwrap().page_id; - let page2 = buffer_pool_manager.new_page().unwrap(); - let page3 = buffer_pool_manager.new_page().unwrap(); - let page4 = buffer_pool_manager.new_page(); + let page2 = buffer_pool.new_page().unwrap(); + let page3 = buffer_pool.new_page().unwrap(); + let page4 = buffer_pool.new_page(); assert!(page4.is_err()); - buffer_pool_manager.unpin_page(page1_id, true).unwrap(); - let page5 = buffer_pool_manager.new_page(); + buffer_pool.unpin_page(page1_id, true).unwrap(); + let page5 = buffer_pool.new_page(); assert!(page5.is_ok()); } @@ -280,29 +265,29 @@ mod tests { let temp_path = temp_dir.path().join("test.db"); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let mut buffer_pool_manager = BufferPoolManager::new(3, Arc::new(disk_manager)); + let mut buffer_pool = BufferPoolManager::new(3, Arc::new(disk_manager)); - let page1 = buffer_pool_manager.new_page().unwrap(); + let page1 = buffer_pool.new_page().unwrap(); let page1_id = page1.read().unwrap().page_id; - buffer_pool_manager.unpin_page(page1_id, true).unwrap(); + buffer_pool.unpin_page(page1_id, true).unwrap(); - let page2 = buffer_pool_manager.new_page().unwrap(); + let page2 = buffer_pool.new_page().unwrap(); let page2_id = page2.read().unwrap().page_id; - buffer_pool_manager.unpin_page(page2_id, false).unwrap(); + buffer_pool.unpin_page(page2_id, false).unwrap(); - let page3 = buffer_pool_manager.new_page().unwrap(); + let page3 = buffer_pool.new_page().unwrap(); let page3_id = page3.read().unwrap().page_id; - buffer_pool_manager.unpin_page(page3_id, false).unwrap(); + buffer_pool.unpin_page(page3_id, false).unwrap(); - let page = buffer_pool_manager.fetch_page(page1_id).unwrap(); + let page = buffer_pool.fetch_page(page1_id).unwrap(); assert_eq!(page.read().unwrap().page_id, page1_id); - buffer_pool_manager.unpin_page(page1_id, false).unwrap(); + buffer_pool.unpin_page(page1_id, false).unwrap(); - let page = buffer_pool_manager.fetch_page(page2_id).unwrap(); + let page = buffer_pool.fetch_page(page2_id).unwrap(); assert_eq!(page.read().unwrap().page_id, page2_id); - buffer_pool_manager.unpin_page(page2_id, false).unwrap(); + buffer_pool.unpin_page(page2_id, false).unwrap(); - assert_eq!(buffer_pool_manager.replacer.size(), 3); + assert_eq!(buffer_pool.replacer.size(), 3); } #[test] @@ -311,28 +296,28 @@ mod tests { let temp_path = temp_dir.path().join("test.db"); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let mut buffer_pool_manager = BufferPoolManager::new(3, Arc::new(disk_manager)); + let mut buffer_pool = BufferPoolManager::new(3, Arc::new(disk_manager)); - let page1 = buffer_pool_manager.new_page().unwrap(); + let page1 = buffer_pool.new_page().unwrap(); let page1_id = page1.read().unwrap().page_id; - buffer_pool_manager.unpin_page(page1_id, true).unwrap(); + buffer_pool.unpin_page(page1_id, true).unwrap(); - let page2 = buffer_pool_manager.new_page().unwrap(); + let page2 = buffer_pool.new_page().unwrap(); let page2_id = page2.read().unwrap().page_id; - buffer_pool_manager.unpin_page(page2_id, true).unwrap(); + buffer_pool.unpin_page(page2_id, true).unwrap(); - let page3 = buffer_pool_manager.new_page().unwrap(); + let page3 = buffer_pool.new_page().unwrap(); let page3_id = page3.read().unwrap().page_id; - buffer_pool_manager.unpin_page(page3_id, false).unwrap(); + buffer_pool.unpin_page(page3_id, false).unwrap(); - let res = buffer_pool_manager.delete_page(page1_id).unwrap(); + let res = buffer_pool.delete_page(page1_id).unwrap(); assert!(res); - assert_eq!(buffer_pool_manager.pool.len(), 3); - assert_eq!(buffer_pool_manager.free_list.len(), 1); - assert_eq!(buffer_pool_manager.replacer.size(), 2); - assert_eq!(buffer_pool_manager.page_table.len(), 2); + assert_eq!(buffer_pool.pool.len(), 3); + assert_eq!(buffer_pool.free_list.len(), 1); + assert_eq!(buffer_pool.replacer.size(), 2); + assert_eq!(buffer_pool.page_table.len(), 2); - let page = buffer_pool_manager.fetch_page(page1_id).unwrap(); + let page = buffer_pool.fetch_page(page1_id).unwrap(); assert_eq!(page.read().unwrap().page_id, page1_id); } } diff --git a/bustubx/src/catalog/catalog.rs b/bustubx/src/catalog/catalog.rs index a4d0cba..ba25de3 100644 --- a/bustubx/src/catalog/catalog.rs +++ b/bustubx/src/catalog/catalog.rs @@ -37,15 +37,15 @@ pub struct IndexInfo { pub struct Catalog { pub tables: HashMap, pub indexes: HashMap, - pub buffer_pool_manager: BufferPoolManager, + pub buffer_pool: BufferPoolManager, } impl Catalog { - pub fn new(buffer_pool_manager: BufferPoolManager) -> Self { + pub fn new(buffer_pool: BufferPoolManager) -> Self { Self { tables: HashMap::new(), indexes: HashMap::new(), - buffer_pool_manager, + buffer_pool, } } @@ -57,11 +57,11 @@ impl Catalog { let full_table_ref = table_ref.extend_to_full(); if !self.tables.contains_key(&full_table_ref) { // 一个table对应一个buffer pool manager - let buffer_pool_manager = BufferPoolManager::new( + let buffer_pool = BufferPoolManager::new( TABLE_HEAP_BUFFER_POOL_SIZE, - self.buffer_pool_manager.disk_manager.clone(), + self.buffer_pool.disk_manager.clone(), ); - let table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager)?; + let table_heap = TableHeap::try_new(schema.clone(), buffer_pool)?; let table_info = TableInfo { schema, name: table_ref.table().to_string(), @@ -114,12 +114,12 @@ impl Catalog { key_attrs, ); // one buffer pool manager for one index - let buffer_pool_manager = BufferPoolManager::new( + let buffer_pool = BufferPoolManager::new( TABLE_HEAP_BUFFER_POOL_SIZE, - self.buffer_pool_manager.disk_manager.clone(), + self.buffer_pool.disk_manager.clone(), ); // TODO compute leaf_max_size and internal_max_size - let b_plus_tree_index = BPlusTreeIndex::new(index_metadata, buffer_pool_manager, 10, 10); + let b_plus_tree_index = BPlusTreeIndex::new(index_metadata, buffer_pool, 10, 10); let index_info = IndexInfo { key_schema, @@ -162,8 +162,8 @@ mod tests { let temp_path = temp_dir.path().join("test.db"); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager)); - let mut catalog = super::Catalog::new(buffer_pool_manager); + let buffer_pool = BufferPoolManager::new(1000, Arc::new(disk_manager)); + let mut catalog = super::Catalog::new(buffer_pool); let table_ref1 = TableReference::bare("test_table1".to_string()); let schema = Arc::new(Schema::new(vec![ @@ -204,8 +204,8 @@ mod tests { let temp_path = temp_dir.path().join("test.db"); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager)); - let mut catalog = super::Catalog::new(buffer_pool_manager); + let buffer_pool = BufferPoolManager::new(1000, Arc::new(disk_manager)); + let mut catalog = super::Catalog::new(buffer_pool); let table_ref = TableReference::bare("test_table1".to_string()); let schema = Arc::new(Schema::new(vec![ diff --git a/bustubx/src/catalog/information.rs b/bustubx/src/catalog/information.rs index f876164..5fa2008 100644 --- a/bustubx/src/catalog/information.rs +++ b/bustubx/src/catalog/information.rs @@ -49,20 +49,15 @@ pub fn load_catalog_data(catalog: &mut Catalog) -> BustubxResult<()> { } fn load_information_schema(catalog: &mut Catalog) -> BustubxResult<()> { - let meta = catalog - .buffer_pool_manager - .disk_manager - .meta - .read() - .unwrap(); + let meta = catalog.buffer_pool.disk_manager.meta.read().unwrap(); let tables_table = TableInfo { schema: TABLES_SCHMEA.clone(), name: INFORMATION_SCHEMA_TABLES.to_string(), table: TableHeap { schema: TABLES_SCHMEA.clone(), - buffer_pool_manager: BufferPoolManager::new( + buffer_pool: BufferPoolManager::new( TABLE_HEAP_BUFFER_POOL_SIZE, - catalog.buffer_pool_manager.disk_manager.clone(), + catalog.buffer_pool.disk_manager.clone(), ), first_page_id: meta.information_schema_tables_first_page_id, last_page_id: meta.information_schema_tables_last_page_id, @@ -77,9 +72,9 @@ fn load_information_schema(catalog: &mut Catalog) -> BustubxResult<()> { name: INFORMATION_SCHEMA_COLUMNS.to_string(), table: TableHeap { schema: COLUMNS_SCHMEA.clone(), - buffer_pool_manager: BufferPoolManager::new( + buffer_pool: BufferPoolManager::new( TABLE_HEAP_BUFFER_POOL_SIZE, - catalog.buffer_pool_manager.disk_manager.clone(), + catalog.buffer_pool.disk_manager.clone(), ), first_page_id: meta.information_schema_columns_first_page_id, last_page_id: meta.information_schema_columns_last_page_id, diff --git a/bustubx/src/database.rs b/bustubx/src/database.rs index 63bcb7a..70501ca 100644 --- a/bustubx/src/database.rs +++ b/bustubx/src/database.rs @@ -25,11 +25,10 @@ pub struct Database { impl Database { pub fn new_on_disk(db_path: &str) -> BustubxResult { let disk_manager = Arc::new(DiskManager::try_new(&db_path)?); - let buffer_pool_manager = - BufferPoolManager::new(TABLE_HEAP_BUFFER_POOL_SIZE, disk_manager.clone()); + let buffer_pool = BufferPoolManager::new(TABLE_HEAP_BUFFER_POOL_SIZE, disk_manager.clone()); // TODO load catalog from disk - let mut catalog = Catalog::new(buffer_pool_manager); + let mut catalog = Catalog::new(buffer_pool); load_catalog_data(&mut catalog)?; Ok(Self { @@ -46,10 +45,9 @@ impl Database { Arc::new(DiskManager::try_new(temp_path.to_str().ok_or( BustubxError::Internal("Invalid temp path".to_string()), )?)?); - let buffer_pool_manager = - BufferPoolManager::new(TABLE_HEAP_BUFFER_POOL_SIZE, disk_manager.clone()); + let buffer_pool = BufferPoolManager::new(TABLE_HEAP_BUFFER_POOL_SIZE, disk_manager.clone()); - let mut catalog = Catalog::new(buffer_pool_manager); + let mut catalog = Catalog::new(buffer_pool); load_catalog_data(&mut catalog)?; Ok(Self { diff --git a/bustubx/src/execution/physical_plan/values.rs b/bustubx/src/execution/physical_plan/values.rs index dfde929..33c7e2f 100644 --- a/bustubx/src/execution/physical_plan/values.rs +++ b/bustubx/src/execution/physical_plan/values.rs @@ -1,7 +1,7 @@ use std::sync::atomic::AtomicU32; use std::sync::Arc; -use crate::catalog::{ColumnRef, SchemaRef}; +use crate::catalog::SchemaRef; use crate::expression::{Expr, ExprTrait}; use crate::{ catalog::Schema, diff --git a/bustubx/src/expression/aggr/avg.rs b/bustubx/src/expression/aggr/avg.rs index 23f78b6..d432703 100644 --- a/bustubx/src/expression/aggr/avg.rs +++ b/bustubx/src/expression/aggr/avg.rs @@ -2,7 +2,6 @@ use crate::catalog::DataType; use crate::common::ScalarValue; use crate::expression::Accumulator; use crate::{BustubxError, BustubxResult}; -use std::fs::read; #[derive(Debug)] pub struct AvgAccumulator { diff --git a/bustubx/src/planner/logical_planner/plan_insert.rs b/bustubx/src/planner/logical_planner/plan_insert.rs index 72a0efd..ef132a3 100644 --- a/bustubx/src/planner/logical_planner/plan_insert.rs +++ b/bustubx/src/planner/logical_planner/plan_insert.rs @@ -1,4 +1,4 @@ -use crate::{BustubxError, BustubxResult}; +use crate::BustubxResult; use std::sync::Arc; use crate::planner::logical_plan::{Insert, LogicalPlan}; diff --git a/bustubx/src/planner/logical_planner/plan_set_expr.rs b/bustubx/src/planner/logical_planner/plan_set_expr.rs index 3248887..b013383 100644 --- a/bustubx/src/planner/logical_planner/plan_set_expr.rs +++ b/bustubx/src/planner/logical_planner/plan_set_expr.rs @@ -64,7 +64,7 @@ impl LogicalPlanner<'_> { .collect::>>()?, ); Ok(LogicalPlan::Aggregate(Aggregate { - input: Arc::new((input)), + input: Arc::new(input), group_exprs, aggr_exprs, schema: Arc::new(Schema::new(columns)), diff --git a/bustubx/src/storage/codec/freelist_page.rs b/bustubx/src/storage/codec/freelist_page.rs index e8769f3..7d232f4 100644 --- a/bustubx/src/storage/codec/freelist_page.rs +++ b/bustubx/src/storage/codec/freelist_page.rs @@ -72,8 +72,7 @@ impl FreelistPageCodec { #[cfg(test)] mod tests { use crate::storage::codec::FreelistPageCodec; - use crate::storage::freelist_page::FREELIST_PAGE_MAX_SIZE; - use crate::storage::{FreelistPage, FreelistPageHeader}; + use crate::storage::{FreelistPage, FreelistPageHeader, FREELIST_PAGE_MAX_SIZE}; #[test] fn freelist_page_codec() { diff --git a/bustubx/src/storage/codec/index_page.rs b/bustubx/src/storage/codec/index_page.rs index 0228ca0..0182d8a 100644 --- a/bustubx/src/storage/codec/index_page.rs +++ b/bustubx/src/storage/codec/index_page.rs @@ -1,7 +1,7 @@ use crate::buffer::BUSTUBX_PAGE_SIZE; use crate::catalog::SchemaRef; use crate::storage::codec::{CommonCodec, DecodedData, RidCodec, TupleCodec}; -use crate::storage::index_page::{ +use crate::storage::{ BPlusTreeInternalPage, BPlusTreeInternalPageHeader, BPlusTreeLeafPage, BPlusTreeLeafPageHeader, BPlusTreePage, BPlusTreePageType, }; @@ -266,7 +266,7 @@ mod tests { use crate::catalog::{Column, DataType, Schema}; use crate::common::rid::Rid; use crate::storage::codec::index_page::BPlusTreePageCodec; - use crate::storage::index_page::{BPlusTreeInternalPage, BPlusTreeLeafPage, BPlusTreePage}; + use crate::storage::{BPlusTreeInternalPage, BPlusTreeLeafPage, BPlusTreePage}; use crate::Tuple; use std::sync::Arc; diff --git a/bustubx/src/storage/codec/meta_page.rs b/bustubx/src/storage/codec/meta_page.rs index 8f6c25d..380d4b1 100644 --- a/bustubx/src/storage/codec/meta_page.rs +++ b/bustubx/src/storage/codec/meta_page.rs @@ -1,5 +1,5 @@ use crate::storage::codec::{CommonCodec, DecodedData}; -use crate::storage::meta_page::MetaPage; +use crate::storage::MetaPage; use crate::BustubxResult; pub struct MetaPageCodec; diff --git a/bustubx/src/storage/codec/table_page.rs b/bustubx/src/storage/codec/table_page.rs index 06814b1..99c5657 100644 --- a/bustubx/src/storage/codec/table_page.rs +++ b/bustubx/src/storage/codec/table_page.rs @@ -3,8 +3,7 @@ use crate::catalog::SchemaRef; use crate::common::rid::Rid; use crate::common::util::page_bytes_to_array; use crate::storage::codec::{CommonCodec, DecodedData}; -use crate::storage::table_page::{TablePageHeader, TupleInfo}; -use crate::storage::{TablePage, TupleMeta}; +use crate::storage::{TablePage, TablePageHeader, TupleInfo, TupleMeta}; use crate::{BustubxError, BustubxResult}; pub struct TablePageCodec; diff --git a/bustubx/src/storage/disk_manager.rs b/bustubx/src/storage/disk_manager.rs index 17c5cb9..01c495d 100644 --- a/bustubx/src/storage/disk_manager.rs +++ b/bustubx/src/storage/disk_manager.rs @@ -12,8 +12,7 @@ use crate::error::{BustubxError, BustubxResult}; use crate::buffer::{PageId, BUSTUBX_PAGE_SIZE, INVALID_PAGE_ID}; use crate::storage::codec::{FreelistPageCodec, MetaPageCodec}; -use crate::storage::meta_page::MetaPage; -use crate::storage::{FreelistPage, META_PAGE_SIZE}; +use crate::storage::{FreelistPage, MetaPage, META_PAGE_SIZE}; static EMPTY_PAGE: [u8; BUSTUBX_PAGE_SIZE] = [0; BUSTUBX_PAGE_SIZE]; diff --git a/bustubx/src/storage/index.rs b/bustubx/src/storage/index.rs index 72d0851..e98b228 100644 --- a/bustubx/src/storage/index.rs +++ b/bustubx/src/storage/index.rs @@ -6,17 +6,15 @@ use crate::common::util::page_bytes_to_array; use crate::storage::codec::{ BPlusTreeInternalPageCodec, BPlusTreeLeafPageCodec, BPlusTreePageCodec, }; +use crate::storage::{InternalKV, LeafKV}; use crate::{ buffer::BufferPoolManager, common::rid::Rid, - storage::index_page::{BPlusTreeInternalPage, BPlusTreeLeafPage, BPlusTreePage}, + storage::{BPlusTreeInternalPage, BPlusTreeLeafPage, BPlusTreePage}, BustubxResult, }; -use super::{ - index_page::{InternalKV, LeafKV}, - tuple::Tuple, -}; +use super::tuple::Tuple; // 索引元信息 #[derive(Debug, Clone)] @@ -62,7 +60,7 @@ impl Context { // B+树索引 pub struct BPlusTreeIndex { pub index_metadata: IndexMetadata, - pub buffer_pool_manager: BufferPoolManager, + pub buffer_pool: BufferPoolManager, pub leaf_max_size: u32, pub internal_max_size: u32, pub root_page_id: PageId, @@ -70,14 +68,14 @@ pub struct BPlusTreeIndex { impl BPlusTreeIndex { pub fn new( index_metadata: IndexMetadata, - buffer_pool_manager: BufferPoolManager, + buffer_pool: BufferPoolManager, leaf_max_size: u32, internal_max_size: u32, ) -> Self { // TODO 计算页容量是否能存放下这么多的kv对 Self { index_metadata, - buffer_pool_manager, + buffer_pool, leaf_max_size, internal_max_size, root_page_id: INVALID_PAGE_ID, @@ -97,7 +95,7 @@ impl BPlusTreeIndex { // 找到leaf page let leaf_page_id = self.find_leaf_page(key, &mut context); let page = self - .buffer_pool_manager + .buffer_pool .fetch_page(leaf_page_id) .expect("Leaf page can not be fetched"); let (mut leaf_page, _) = BPlusTreeLeafPageCodec::decode( @@ -116,18 +114,16 @@ impl BPlusTreeIndex { // 向右分裂出一个新page let internalkv = self.split(&mut curr_page); - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( curr_page_id, page_bytes_to_array(&BPlusTreePageCodec::encode(&curr_page)), ); - self.buffer_pool_manager - .unpin_page(curr_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(curr_page_id, true).unwrap(); if let Some(page_id) = context.read_set.pop_back() { // 更新父节点 let page = self - .buffer_pool_manager + .buffer_pool .fetch_page(page_id) .expect("Page can not be fetched"); let (mut tree_page, _) = BPlusTreePageCodec::decode( @@ -135,17 +131,14 @@ impl BPlusTreeIndex { self.index_metadata.key_schema.clone(), ) .unwrap(); - self.buffer_pool_manager.unpin_page(page_id, false).unwrap(); + self.buffer_pool.unpin_page(page_id, false).unwrap(); tree_page.insert_internalkv(internalkv, &self.index_metadata.key_schema); curr_page = tree_page; curr_page_id = page_id; } else if curr_page_id == self.root_page_id { // new 一个新的root page - let new_root_page = self - .buffer_pool_manager - .new_page() - .expect("can not new root page"); + let new_root_page = self.buffer_pool.new_page().expect("can not new root page"); let new_root_page_id = new_root_page.read().unwrap().page_id; let mut new_internal_page = BPlusTreeInternalPage::new( self.index_metadata.key_schema.clone(), @@ -166,9 +159,7 @@ impl BPlusTreeIndex { new_root_page.write().unwrap().data = page_bytes_to_array(&BPlusTreeInternalPageCodec::encode(&new_internal_page)); - self.buffer_pool_manager - .unpin_page(new_root_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(new_root_page_id, true).unwrap(); // 更新root page id self.root_page_id = new_root_page_id; @@ -178,13 +169,11 @@ impl BPlusTreeIndex { } } - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( curr_page_id, page_bytes_to_array(&BPlusTreePageCodec::encode(&curr_page)), ); - self.buffer_pool_manager - .unpin_page(curr_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(curr_page_id, true).unwrap(); return true; } @@ -196,7 +185,7 @@ impl BPlusTreeIndex { // 找到leaf page let leaf_page_id = self.find_leaf_page(key, &mut context); let page = self - .buffer_pool_manager + .buffer_pool .fetch_page(leaf_page_id) .expect("Leaf page can not be fetched"); let (mut leaf_page, _) = BPlusTreeLeafPageCodec::decode( @@ -219,7 +208,7 @@ impl BPlusTreeIndex { // 尝试从左兄弟借一个 if let Some(left_sibling_page_id) = left_sibling_page_id { let left_sibling_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(left_sibling_page_id) .expect("Left sibling page can not be fetched"); let (mut left_sibling_tree_page, _) = BPlusTreePageCodec::decode( @@ -285,19 +274,19 @@ impl BPlusTreeIndex { } }; // 更新兄弟节点 - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( left_sibling_page_id, page_bytes_to_array(&BPlusTreePageCodec::encode( &left_sibling_tree_page, )), ); - self.buffer_pool_manager + self.buffer_pool .unpin_page(left_sibling_page_id, true) .unwrap(); // 更新父节点 let parent_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(parent_page_id) .expect("Parent page can not be fetched"); let (mut parent_internal_page, _) = BPlusTreeInternalPageCodec::decode( @@ -314,13 +303,11 @@ impl BPlusTreeIndex { parent_page.write().unwrap().data = page_bytes_to_array( &BPlusTreeInternalPageCodec::encode(&parent_internal_page), ); - self.buffer_pool_manager - .unpin_page(parent_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(parent_page_id, true).unwrap(); break; } - self.buffer_pool_manager + self.buffer_pool .unpin_page(left_sibling_page_id, false) .unwrap(); } @@ -328,7 +315,7 @@ impl BPlusTreeIndex { // 尝试从右兄弟借一个 if let Some(right_sibling_page_id) = right_sibling_page_id { let right_sibling_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(right_sibling_page_id) .expect("Right sibling page can not be fetched"); let (mut right_sibling_tree_page, _) = BPlusTreePageCodec::decode( @@ -374,19 +361,19 @@ impl BPlusTreeIndex { } }; // 更新兄弟节点 - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( right_sibling_page_id, page_bytes_to_array(&BPlusTreePageCodec::encode( &right_sibling_tree_page, )), ); - self.buffer_pool_manager + self.buffer_pool .unpin_page(right_sibling_page_id, true) .unwrap(); // 更新父节点 let parent_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(parent_page_id) .expect("Parent page can not be fetched"); let (mut parent_internal_page, _) = BPlusTreeInternalPageCodec::decode( @@ -403,13 +390,11 @@ impl BPlusTreeIndex { parent_page.write().unwrap().data = page_bytes_to_array( &BPlusTreeInternalPageCodec::encode(&parent_internal_page), ); - self.buffer_pool_manager - .unpin_page(parent_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(parent_page_id, true).unwrap(); break; } - self.buffer_pool_manager + self.buffer_pool .unpin_page(right_sibling_page_id, false) .unwrap(); } @@ -417,7 +402,7 @@ impl BPlusTreeIndex { // 跟左兄弟合并 if let Some(left_sibling_page_id) = left_sibling_page_id { let left_sibling_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(left_sibling_page_id) .expect("Left sibling page can not be fetched"); let (mut left_sibling_tree_page, _) = BPlusTreePageCodec::decode( @@ -456,19 +441,15 @@ impl BPlusTreeIndex { } }; - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( left_sibling_page_id, page_bytes_to_array(&BPlusTreePageCodec::encode(&left_sibling_tree_page)), ); // 删除当前页 let deleted_page_id = curr_page_id; - self.buffer_pool_manager - .unpin_page(deleted_page_id, false) - .unwrap(); - self.buffer_pool_manager - .delete_page(deleted_page_id) - .unwrap(); + self.buffer_pool.unpin_page(deleted_page_id, false).unwrap(); + self.buffer_pool.delete_page(deleted_page_id).unwrap(); // 更新当前页为左兄弟页 curr_page_id = left_sibling_page_id; @@ -476,7 +457,7 @@ impl BPlusTreeIndex { // 更新父节点 let parent_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(parent_page_id) .expect("Parent page can not be fetched"); let (mut parent_internal_page, _) = BPlusTreeInternalPageCodec::decode( @@ -491,19 +472,13 @@ impl BPlusTreeIndex { { self.root_page_id = curr_page_id; // 删除旧的根节点 - self.buffer_pool_manager - .unpin_page(parent_page_id, false) - .unwrap(); - self.buffer_pool_manager - .delete_page(parent_page_id) - .unwrap(); + self.buffer_pool.unpin_page(parent_page_id, false).unwrap(); + self.buffer_pool.delete_page(parent_page_id).unwrap(); } else { parent_page.write().unwrap().data = page_bytes_to_array( &BPlusTreeInternalPageCodec::encode(&parent_internal_page), ); - self.buffer_pool_manager - .unpin_page(curr_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(curr_page_id, true).unwrap(); curr_page = BPlusTreePage::Internal(parent_internal_page); curr_page_id = parent_page_id; } @@ -513,7 +488,7 @@ impl BPlusTreeIndex { // 跟右兄弟合并 if let Some(right_sibling_page_id) = right_sibling_page_id { let right_sibling_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(right_sibling_page_id) .expect("Right sibling page can not be fetched"); let (mut right_sibling_tree_page, _) = BPlusTreePageCodec::decode( @@ -554,23 +529,19 @@ impl BPlusTreeIndex { } }; - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( curr_page_id, page_bytes_to_array(&BPlusTreePageCodec::encode(&curr_page)), ); // 删除右兄弟页 let deleted_page_id = right_sibling_page_id; - self.buffer_pool_manager - .unpin_page(deleted_page_id, false) - .unwrap(); - self.buffer_pool_manager - .delete_page(deleted_page_id) - .unwrap(); + self.buffer_pool.unpin_page(deleted_page_id, false).unwrap(); + self.buffer_pool.delete_page(deleted_page_id).unwrap(); // 更新父节点 let parent_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(parent_page_id) .expect("Parent page can not be fetched"); let (mut parent_internal_page, _) = BPlusTreeInternalPageCodec::decode( @@ -585,19 +556,13 @@ impl BPlusTreeIndex { { self.root_page_id = curr_page_id; // 删除旧的根节点 - self.buffer_pool_manager - .unpin_page(parent_page_id, false) - .unwrap(); - self.buffer_pool_manager - .delete_page(parent_page_id) - .unwrap(); + self.buffer_pool.unpin_page(parent_page_id, false).unwrap(); + self.buffer_pool.delete_page(parent_page_id).unwrap(); } else { parent_page.write().unwrap().data = page_bytes_to_array( &BPlusTreeInternalPageCodec::encode(&parent_internal_page), ); - self.buffer_pool_manager - .unpin_page(curr_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(curr_page_id, true).unwrap(); curr_page = BPlusTreePage::Internal(parent_internal_page); curr_page_id = parent_page_id; } @@ -606,13 +571,11 @@ impl BPlusTreeIndex { } } - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( curr_page_id, page_bytes_to_array(&BPlusTreePageCodec::encode(&curr_page)), ); - self.buffer_pool_manager - .unpin_page(curr_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(curr_page_id, true).unwrap(); } pub fn scan(&self, key: &Tuple) -> Vec { @@ -620,7 +583,7 @@ impl BPlusTreeIndex { } fn start_new_tree(&mut self, key: &Tuple, rid: Rid) -> BustubxResult<()> { - let new_page = self.buffer_pool_manager.new_page()?; + let new_page = self.buffer_pool.new_page()?; let new_page_id = new_page.read().unwrap().page_id; let mut leaf_page = @@ -633,7 +596,7 @@ impl BPlusTreeIndex { // 更新root page id self.root_page_id = new_page_id; - self.buffer_pool_manager.unpin_page(new_page_id, true)?; + self.buffer_pool.unpin_page(new_page_id, true)?; Ok(()) } @@ -651,7 +614,7 @@ impl BPlusTreeIndex { } let leaf_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(leaf_page_id) .expect("Leaf page can not be fetched"); let (leaf_page, _) = BPlusTreeLeafPageCodec::decode( @@ -660,9 +623,7 @@ impl BPlusTreeIndex { ) .unwrap(); let result = leaf_page.look_up(key, &self.index_metadata.key_schema); - self.buffer_pool_manager - .unpin_page(leaf_page_id, false) - .unwrap(); + self.buffer_pool.unpin_page(leaf_page_id, false).unwrap(); return result; } @@ -670,10 +631,7 @@ impl BPlusTreeIndex { if self.is_empty() { return INVALID_PAGE_ID; } - let curr_page = self - .buffer_pool_manager - .fetch_page(self.root_page_id) - .unwrap(); + let curr_page = self.buffer_pool.fetch_page(self.root_page_id).unwrap(); let mut curr_page_id = curr_page.read().unwrap().page_id; let (mut curr_page, _) = BPlusTreePageCodec::decode( &curr_page.read().unwrap().data, @@ -687,13 +645,11 @@ impl BPlusTreeIndex { BPlusTreePage::Internal(internal_page) => { context.read_set.push_back(curr_page_id); // 释放上一页 - self.buffer_pool_manager - .unpin_page(curr_page_id, false) - .unwrap(); + self.buffer_pool.unpin_page(curr_page_id, false).unwrap(); // 查找下一页 let next_page_id = internal_page.look_up(key, &self.index_metadata.key_schema); let next_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(next_page_id) .expect("Next page can not be fetched"); let (next_page, _) = BPlusTreePageCodec::decode( @@ -705,9 +661,7 @@ impl BPlusTreeIndex { curr_page = next_page; } BPlusTreePage::Leaf(leaf_page) => { - self.buffer_pool_manager - .unpin_page(curr_page_id, false) - .unwrap(); + self.buffer_pool.unpin_page(curr_page_id, false).unwrap(); return curr_page_id; } } @@ -719,7 +673,7 @@ impl BPlusTreeIndex { match page { BPlusTreePage::Leaf(leaf_page) => { let new_page = self - .buffer_pool_manager + .buffer_pool .new_page() .expect("failed to split leaf page"); let new_page_id = new_page.read().unwrap().page_id; @@ -740,15 +694,13 @@ impl BPlusTreeIndex { new_page.write().unwrap().data = page_bytes_to_array(&BPlusTreeLeafPageCodec::encode(&new_leaf_page)); - self.buffer_pool_manager - .unpin_page(new_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(new_page_id, true).unwrap(); return (new_leaf_page.key_at(0).clone(), new_page_id); } BPlusTreePage::Internal(internal_page) => { let new_page = self - .buffer_pool_manager + .buffer_pool .new_page() .expect("failed to split internal page"); let new_page_id = new_page.read().unwrap().page_id; @@ -765,9 +717,7 @@ impl BPlusTreeIndex { new_page.write().unwrap().data = page_bytes_to_array(&BPlusTreeInternalPageCodec::encode(&new_internal_page)); - self.buffer_pool_manager - .unpin_page(new_page_id, true) - .unwrap(); + self.buffer_pool.unpin_page(new_page_id, true).unwrap(); let min_leafkv = self.find_subtree_min_leafkv(new_page_id).unwrap(); return (min_leafkv.0, new_page_id); @@ -784,12 +734,12 @@ impl BPlusTreeIndex { parent_page_id: PageId, child_page_id: PageId, ) -> BustubxResult<(Option, Option)> { - let parent_page = self.buffer_pool_manager.fetch_page(parent_page_id)?; + let parent_page = self.buffer_pool.fetch_page(parent_page_id)?; let (parent_page, _) = BPlusTreeInternalPageCodec::decode( &parent_page.read().unwrap().data, self.index_metadata.key_schema.clone(), )?; - self.buffer_pool_manager.unpin_page(parent_page_id, false)?; + self.buffer_pool.unpin_page(parent_page_id, false)?; Ok(parent_page.sibling_page_ids(child_page_id)) } @@ -808,12 +758,12 @@ impl BPlusTreeIndex { } fn find_subtree_leafkv(&mut self, page_id: PageId, min_or_max: bool) -> BustubxResult { - let curr_page = self.buffer_pool_manager.fetch_page(page_id)?; + let curr_page = self.buffer_pool.fetch_page(page_id)?; let (mut curr_page, _) = BPlusTreePageCodec::decode( &curr_page.read().unwrap().data, self.index_metadata.key_schema.clone(), )?; - self.buffer_pool_manager.unpin_page(page_id, false)?; + self.buffer_pool.unpin_page(page_id, false)?; loop { match curr_page { BPlusTreePage::Internal(internal_page) => { @@ -823,13 +773,13 @@ impl BPlusTreeIndex { internal_page.header.current_size as usize - 1 }; let page_id = internal_page.value_at(index); - let page = self.buffer_pool_manager.fetch_page(page_id)?; + let page = self.buffer_pool.fetch_page(page_id)?; curr_page = BPlusTreePageCodec::decode( &page.read().unwrap().data, self.index_metadata.key_schema.clone(), )? .0; - self.buffer_pool_manager.unpin_page(page_id, false)?; + self.buffer_pool.unpin_page(page_id, false)?; } BPlusTreePage::Leaf(leaf_page) => { let index = if min_or_max { @@ -862,7 +812,7 @@ impl BPlusTreeIndex { println!("B+树第{}层: ", level_index); while let Some(page_id) = curr_queue.pop_front() { let page = self - .buffer_pool_manager + .buffer_pool .fetch_page(page_id) .expect("Page can not be fetched"); let (curr_page, _) = BPlusTreePageCodec::decode( @@ -870,7 +820,7 @@ impl BPlusTreeIndex { self.index_metadata.key_schema.clone(), ) .unwrap(); - self.buffer_pool_manager.unpin_page(page_id, false).unwrap(); + self.buffer_pool.unpin_page(page_id, false).unwrap(); match curr_page { BPlusTreePage::Internal(internal_page) => { internal_page.print_page(page_id, &self.index_metadata.key_schema); @@ -921,8 +871,8 @@ mod tests { vec![0, 1], ); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager)); - let mut index = BPlusTreeIndex::new(index_metadata, buffer_pool_manager, 2, 3); + let buffer_pool = BufferPoolManager::new(1000, Arc::new(disk_manager)); + let mut index = BPlusTreeIndex::new(index_metadata, buffer_pool, 2, 3); index.insert( &Tuple::new(schema.clone(), vec![1i8.into(), 2i16.into()]), @@ -935,7 +885,7 @@ mod tests { Rid::new(1, 1) ); assert_eq!(index.root_page_id, 4); - assert_eq!(index.buffer_pool_manager.replacer.size(), 1); + assert_eq!(index.buffer_pool.replacer.size(), 1); index.insert( &Tuple::new(schema.clone(), vec![2i8.into(), 4i16.into()]), @@ -948,7 +898,7 @@ mod tests { Rid::new(2, 2) ); assert_eq!(index.root_page_id, 4); - assert_eq!(index.buffer_pool_manager.replacer.size(), 1); + assert_eq!(index.buffer_pool.replacer.size(), 1); index.insert( &Tuple::new(schema.clone(), vec![3i8.into(), 6i16.into()]), @@ -961,7 +911,7 @@ mod tests { Rid::new(3, 3) ); assert_eq!(index.root_page_id, 6); - assert_eq!(index.buffer_pool_manager.replacer.size(), 3); + assert_eq!(index.buffer_pool.replacer.size(), 3); index.insert( &Tuple::new(schema.clone(), vec![4i8.into(), 8i16.into()]), @@ -974,7 +924,7 @@ mod tests { Rid::new(4, 4) ); assert_eq!(index.root_page_id, 6); - assert_eq!(index.buffer_pool_manager.replacer.size(), 4); + assert_eq!(index.buffer_pool.replacer.size(), 4); index.insert( &Tuple::new(schema.clone(), vec![5i8.into(), 10i16.into()]), @@ -987,7 +937,7 @@ mod tests { Rid::new(5, 5) ); assert_eq!(index.root_page_id, 10); - assert_eq!(index.buffer_pool_manager.replacer.size(), 7); + assert_eq!(index.buffer_pool.replacer.size(), 7); } #[test] @@ -1007,8 +957,8 @@ mod tests { vec![0, 1], ); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager)); - let mut index = BPlusTreeIndex::new(index_metadata, buffer_pool_manager, 4, 5); + let buffer_pool = BufferPoolManager::new(1000, Arc::new(disk_manager)); + let mut index = BPlusTreeIndex::new(index_metadata, buffer_pool, 4, 5); index.insert( &Tuple::new(schema.clone(), vec![1i8.into(), 1i16.into()]), @@ -1050,7 +1000,7 @@ mod tests { &Tuple::new(schema.clone(), vec![10i8.into(), 10i16.into()]), Rid::new(10, 10), ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 5); + assert_eq!(index.buffer_pool.replacer.size(), 5); assert_eq!(index.root_page_id, 6); index.print_tree(); @@ -1060,7 +1010,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![1i8.into(), 1i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 4); + assert_eq!(index.buffer_pool.replacer.size(), 4); index.delete(&Tuple::new(schema.clone(), vec![3i8.into(), 3i16.into()])); assert_eq!(index.root_page_id, 6); @@ -1068,7 +1018,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![3i8.into(), 3i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 4); + assert_eq!(index.buffer_pool.replacer.size(), 4); index.delete(&Tuple::new(schema.clone(), vec![5i8.into(), 5i16.into()])); assert_eq!(index.root_page_id, 6); @@ -1076,7 +1026,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![5i8.into(), 5i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 4); + assert_eq!(index.buffer_pool.replacer.size(), 4); index.delete(&Tuple::new(schema.clone(), vec![7i8.into(), 7i16.into()])); assert_eq!(index.root_page_id, 6); @@ -1084,7 +1034,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![7i8.into(), 7i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 4); + assert_eq!(index.buffer_pool.replacer.size(), 4); index.delete(&Tuple::new(schema.clone(), vec![9i8.into(), 9i16.into()])); assert_eq!(index.root_page_id, 6); @@ -1092,7 +1042,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![9i8.into(), 9i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 3); + assert_eq!(index.buffer_pool.replacer.size(), 3); index.delete(&Tuple::new(schema.clone(), vec![10i8.into(), 10i16.into()])); assert_eq!(index.root_page_id, 6); @@ -1100,7 +1050,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![10i8.into(), 10i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 3); + assert_eq!(index.buffer_pool.replacer.size(), 3); index.delete(&Tuple::new(schema.clone(), vec![8i8.into(), 8i16.into()])); assert_eq!(index.root_page_id, 4); @@ -1108,7 +1058,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![8i8.into(), 8i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 1); + assert_eq!(index.buffer_pool.replacer.size(), 1); index.delete(&Tuple::new(schema.clone(), vec![6i8.into(), 6i16.into()])); assert_eq!(index.root_page_id, 4); @@ -1116,7 +1066,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![6i8.into(), 6i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 1); + assert_eq!(index.buffer_pool.replacer.size(), 1); index.delete(&Tuple::new(schema.clone(), vec![4i8.into(), 4i16.into()])); assert_eq!(index.root_page_id, 4); @@ -1124,7 +1074,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![4i8.into(), 4i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 1); + assert_eq!(index.buffer_pool.replacer.size(), 1); index.delete(&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()])); assert_eq!(index.root_page_id, 4); @@ -1132,7 +1082,7 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 1); + assert_eq!(index.buffer_pool.replacer.size(), 1); index.delete(&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()])); assert_eq!(index.root_page_id, 4); @@ -1140,6 +1090,6 @@ mod tests { index.get(&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()])), None ); - assert_eq!(index.buffer_pool_manager.replacer.size(), 1); + assert_eq!(index.buffer_pool.replacer.size(), 1); } } diff --git a/bustubx/src/storage/mod.rs b/bustubx/src/storage/mod.rs index 5c299d8..b243e89 100644 --- a/bustubx/src/storage/mod.rs +++ b/bustubx/src/storage/mod.rs @@ -1,16 +1,11 @@ mod codec; mod disk_manager; -mod freelist_page; pub mod index; -pub mod index_page; -mod meta_page; +mod page; mod table_heap; -mod table_page; mod tuple; pub use disk_manager::DiskManager; -pub use freelist_page::*; -pub use meta_page::{MetaPage, EMPTY_META_PAGE, META_PAGE_SIZE}; +pub use page::*; pub use table_heap::{TableHeap, TableIterator}; -pub use table_page::TablePage; -pub use tuple::{Tuple, TupleMeta}; +pub use tuple::Tuple; diff --git a/bustubx/src/storage/freelist_page.rs b/bustubx/src/storage/page/freelist_page.rs similarity index 100% rename from bustubx/src/storage/freelist_page.rs rename to bustubx/src/storage/page/freelist_page.rs diff --git a/bustubx/src/storage/index_page.rs b/bustubx/src/storage/page/index_page.rs similarity index 99% rename from bustubx/src/storage/index_page.rs rename to bustubx/src/storage/page/index_page.rs index ff032a8..611bb9d 100644 --- a/bustubx/src/storage/index_page.rs +++ b/bustubx/src/storage/page/index_page.rs @@ -1,7 +1,6 @@ -use super::Tuple; use crate::buffer::{PageId, INVALID_PAGE_ID}; use crate::catalog::SchemaRef; -use crate::{catalog::Schema, common::rid::Rid}; +use crate::{catalog::Schema, common::rid::Rid, Tuple}; #[derive(Debug, Clone, Eq, PartialEq)] pub enum BPlusTreePage { @@ -464,13 +463,11 @@ impl BPlusTreeLeafPage { #[cfg(test)] mod tests { use crate::common::ScalarValue; + use crate::storage::{BPlusTreeInternalPage, BPlusTreeLeafPage}; use crate::{ catalog::{Column, DataType, Schema}, common::rid::Rid, - storage::{ - index_page::{BPlusTreeInternalPage, BPlusTreeLeafPage}, - Tuple, - }, + storage::Tuple, }; use std::sync::Arc; diff --git a/bustubx/src/storage/meta_page.rs b/bustubx/src/storage/page/meta_page.rs similarity index 100% rename from bustubx/src/storage/meta_page.rs rename to bustubx/src/storage/page/meta_page.rs diff --git a/bustubx/src/storage/page/mod.rs b/bustubx/src/storage/page/mod.rs new file mode 100644 index 0000000..eb01d96 --- /dev/null +++ b/bustubx/src/storage/page/mod.rs @@ -0,0 +1,9 @@ +mod freelist_page; +mod index_page; +mod meta_page; +mod table_page; + +pub use freelist_page::*; +pub use index_page::*; +pub use meta_page::*; +pub use table_page::*; diff --git a/bustubx/src/storage/table_page.rs b/bustubx/src/storage/page/table_page.rs similarity index 97% rename from bustubx/src/storage/table_page.rs rename to bustubx/src/storage/page/table_page.rs index a84b9f9..d36cb73 100644 --- a/bustubx/src/storage/table_page.rs +++ b/bustubx/src/storage/page/table_page.rs @@ -1,10 +1,9 @@ use crate::buffer::{PageId, BUSTUBX_PAGE_SIZE}; use crate::catalog::SchemaRef; use crate::common::rid::Rid; +use crate::common::TransactionId; use crate::storage::codec::{TablePageHeaderCodec, TablePageHeaderTupleInfoCodec, TupleCodec}; -use crate::{BustubxError, BustubxResult}; - -use super::tuple::{Tuple, TupleMeta}; +use crate::{BustubxError, BustubxResult, Tuple}; lazy_static::lazy_static! { pub static ref EMPTY_TUPLE_INFO: TupleInfo = TupleInfo { @@ -43,6 +42,29 @@ pub struct TablePage { pub data: [u8; BUSTUBX_PAGE_SIZE], } +// TODO do we need pre_page_id? +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct TablePageHeader { + pub next_page_id: PageId, + pub num_tuples: u16, + pub num_deleted_tuples: u16, + pub tuple_infos: Vec, +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct TupleInfo { + pub offset: u16, + pub size: u16, + pub meta: TupleMeta, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TupleMeta { + pub insert_txn_id: TransactionId, + pub delete_txn_id: TransactionId, + pub is_deleted: bool, +} + impl TablePage { pub fn new(schema: SchemaRef, next_page_id: PageId) -> Self { Self { @@ -174,22 +196,6 @@ impl TablePage { } } -// TODO do we need pre_page_id? -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct TablePageHeader { - pub next_page_id: PageId, - pub num_tuples: u16, - pub num_deleted_tuples: u16, - pub tuple_infos: Vec, -} - -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct TupleInfo { - pub offset: u16, - pub size: u16, - pub meta: TupleMeta, -} - #[cfg(test)] mod tests { use crate::catalog::{Column, DataType, Schema}; diff --git a/bustubx/src/storage/table_heap.rs b/bustubx/src/storage/table_heap.rs index f5a04c5..576ebb0 100644 --- a/bustubx/src/storage/table_heap.rs +++ b/bustubx/src/storage/table_heap.rs @@ -1,38 +1,33 @@ -use crate::buffer::{PageId, BUSTUBX_PAGE_SIZE, INVALID_PAGE_ID}; +use crate::buffer::{PageId, INVALID_PAGE_ID}; use crate::catalog::SchemaRef; use crate::common::util::page_bytes_to_array; use crate::storage::codec::TablePageCodec; +use crate::storage::{TablePage, TupleMeta}; use crate::{buffer::BufferPoolManager, common::rid::Rid, BustubxResult}; -use super::{ - table_page::TablePage, - tuple::{Tuple, TupleMeta}, -}; +use super::tuple::Tuple; #[derive(Debug)] pub struct TableHeap { pub schema: SchemaRef, - pub buffer_pool_manager: BufferPoolManager, + pub buffer_pool: BufferPoolManager, pub first_page_id: PageId, pub last_page_id: PageId, } impl TableHeap { - pub fn try_new( - schema: SchemaRef, - mut buffer_pool_manager: BufferPoolManager, - ) -> BustubxResult { + pub fn try_new(schema: SchemaRef, mut buffer_pool: BufferPoolManager) -> BustubxResult { // new a page and initialize - let first_page = buffer_pool_manager.new_page()?; + let first_page = buffer_pool.new_page()?; let first_page_id = first_page.read().unwrap().page_id; let table_page = TablePage::new(schema.clone(), INVALID_PAGE_ID); first_page.write().unwrap().data = page_bytes_to_array(&TablePageCodec::encode(&table_page)); - buffer_pool_manager.unpin_page(first_page_id, true)?; + buffer_pool.unpin_page(first_page_id, true)?; Ok(Self { schema, - buffer_pool_manager, + buffer_pool, first_page_id, last_page_id: first_page_id, }) @@ -52,7 +47,7 @@ impl TableHeap { /// An `Option` containing the `Rid` of the inserted tuple if successful, otherwise `None`. pub fn insert_tuple(&mut self, meta: &TupleMeta, tuple: &Tuple) -> BustubxResult { let mut last_page_id = self.last_page_id; - let last_page = self.buffer_pool_manager.fetch_page(self.last_page_id)?; + let last_page = self.buffer_pool.fetch_page(self.last_page_id)?; // Loop until a suitable page is found for inserting the tuple let (mut last_table_page, _) = @@ -70,7 +65,7 @@ impl TableHeap { ); // Allocate a new page if no more table pages are available. - let next_page = self.buffer_pool_manager.new_page()?; + let next_page = self.buffer_pool.new_page()?; let next_page_id = next_page.read().unwrap().page_id; let next_table_page = TablePage::new(self.schema.clone(), INVALID_PAGE_ID); next_page.write().unwrap().data = @@ -79,11 +74,11 @@ impl TableHeap { // Update and release the previous page last_table_page.header.next_page_id = next_page_id; - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( last_page_id, page_bytes_to_array(&TablePageCodec::encode(&last_table_page)), ); - self.buffer_pool_manager.unpin_page(last_page_id, true)?; + self.buffer_pool.unpin_page(last_page_id, true)?; // Update last_page_id. last_page_id = next_page_id; @@ -94,53 +89,53 @@ impl TableHeap { // Insert the tuple into the chosen page let slot_id = last_table_page.insert_tuple(meta, tuple)?; - self.buffer_pool_manager.write_page( + self.buffer_pool.write_page( last_page_id, page_bytes_to_array(&TablePageCodec::encode(&last_table_page)), ); - self.buffer_pool_manager.unpin_page(last_page_id, true)?; + self.buffer_pool.unpin_page(last_page_id, true)?; // Map the slot_id to a Rid and return Ok(Rid::new(last_page_id, slot_id as u32)) } pub fn update_tuple_meta(&mut self, meta: &TupleMeta, rid: Rid) -> BustubxResult<()> { - let page = self.buffer_pool_manager.fetch_page(rid.page_id)?; + let page = self.buffer_pool.fetch_page(rid.page_id)?; let (mut table_page, _) = TablePageCodec::decode(&page.read().unwrap().data, self.schema.clone())?; table_page.update_tuple_meta(meta, rid.slot_num as u16)?; page.write().unwrap().data = page_bytes_to_array(&TablePageCodec::encode(&table_page)); - self.buffer_pool_manager.unpin_page(rid.page_id, true)?; + self.buffer_pool.unpin_page(rid.page_id, true)?; Ok(()) } pub fn tuple(&mut self, rid: Rid) -> BustubxResult<(TupleMeta, Tuple)> { - let page = self.buffer_pool_manager.fetch_page(rid.page_id)?; + let page = self.buffer_pool.fetch_page(rid.page_id)?; let (table_page, _) = TablePageCodec::decode(&page.read().unwrap().data, self.schema.clone())?; let result = table_page.tuple(rid.slot_num as u16)?; - self.buffer_pool_manager.unpin_page(rid.page_id, false)?; + self.buffer_pool.unpin_page(rid.page_id, false)?; Ok(result) } pub fn tuple_meta(&mut self, rid: Rid) -> BustubxResult { - let page = self.buffer_pool_manager.fetch_page(rid.page_id)?; + let page = self.buffer_pool.fetch_page(rid.page_id)?; let (table_page, _) = TablePageCodec::decode(&page.read().unwrap().data, self.schema.clone())?; let result = table_page.tuple_meta(rid.slot_num as u16)?; - self.buffer_pool_manager.unpin_page(rid.page_id, false)?; + self.buffer_pool.unpin_page(rid.page_id, false)?; Ok(result) } pub fn get_first_rid(&mut self) -> Option { let page = self - .buffer_pool_manager + .buffer_pool .fetch_page(self.first_page_id) .expect("Can not fetch page"); let (table_page, _) = TablePageCodec::decode(&page.read().unwrap().data, self.schema.clone()).unwrap(); - self.buffer_pool_manager + self.buffer_pool .unpin_page(self.first_page_id, false) .unwrap(); if table_page.header.num_tuples == 0 { @@ -153,14 +148,12 @@ impl TableHeap { pub fn get_next_rid(&mut self, rid: Rid) -> Option { let page = self - .buffer_pool_manager + .buffer_pool .fetch_page(rid.page_id) .expect("Can not fetch page"); let (table_page, _) = TablePageCodec::decode(&page.read().unwrap().data, self.schema.clone()).unwrap(); - self.buffer_pool_manager - .unpin_page(rid.page_id, false) - .unwrap(); + self.buffer_pool.unpin_page(rid.page_id, false).unwrap(); let next_rid = table_page.get_next_rid(&rid); if next_rid.is_some() { return next_rid; @@ -170,12 +163,12 @@ impl TableHeap { return None; } let next_page = self - .buffer_pool_manager + .buffer_pool .fetch_page(table_page.header.next_page_id) .expect("Can not fetch page"); let (next_table_page, _) = TablePageCodec::decode(&next_page.read().unwrap().data, self.schema.clone()).unwrap(); - self.buffer_pool_manager + self.buffer_pool .unpin_page(table_page.header.next_page_id, false) .unwrap(); if next_table_page.header.num_tuples == 0 { @@ -236,8 +229,8 @@ mod tests { Column::new("b".to_string(), DataType::Int16, false), ])); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager)); - let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager).unwrap(); + let buffer_pool = BufferPoolManager::new(1000, Arc::new(disk_manager)); + let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool).unwrap(); let meta = super::TupleMeta { insert_txn_id: 0, delete_txn_id: 0, @@ -285,8 +278,8 @@ mod tests { Column::new("b".to_string(), DataType::Int16, false), ])); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager)); - let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager).unwrap(); + let buffer_pool = BufferPoolManager::new(1000, Arc::new(disk_manager)); + let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool).unwrap(); let meta1 = super::TupleMeta { insert_txn_id: 1, @@ -346,8 +339,8 @@ mod tests { ])); let disk_manager = DiskManager::try_new(&temp_path).unwrap(); - let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager)); - let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool_manager).unwrap(); + let buffer_pool = BufferPoolManager::new(1000, Arc::new(disk_manager)); + let mut table_heap = TableHeap::try_new(schema.clone(), buffer_pool).unwrap(); let meta1 = super::TupleMeta { insert_txn_id: 1, diff --git a/bustubx/src/storage/tuple.rs b/bustubx/src/storage/tuple.rs index 59d50a5..4a1206a 100644 --- a/bustubx/src/storage/tuple.rs +++ b/bustubx/src/storage/tuple.rs @@ -1,15 +1,8 @@ -use crate::catalog::{ColumnRef, SchemaRef}; +use crate::catalog::SchemaRef; use crate::common::{TableReference, TransactionId}; use crate::{catalog::Schema, common::ScalarValue, BustubxError, BustubxResult}; use std::sync::Arc; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct TupleMeta { - pub insert_txn_id: TransactionId, - pub delete_txn_id: TransactionId, - pub is_deleted: bool, -} - #[derive(Debug, Clone, Eq, PartialEq)] pub struct Tuple { pub schema: SchemaRef,