Skip to content

Commit

Permalink
Create page mod and rename buffer_pool_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 20, 2024
1 parent 5022573 commit 3b457c9
Show file tree
Hide file tree
Showing 23 changed files with 239 additions and 321 deletions.
1 change: 1 addition & 0 deletions bustubx-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
109 changes: 47 additions & 62 deletions bustubx/src/buffer/buffer_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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());
}

Expand All @@ -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]
Expand All @@ -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);
}
}
26 changes: 13 additions & 13 deletions bustubx/src/catalog/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ pub struct IndexInfo {
pub struct Catalog {
pub tables: HashMap<FullTableRef, TableInfo>,
pub indexes: HashMap<FullIndexRef, IndexInfo>,
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,
}
}

Expand All @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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![
Expand Down Expand Up @@ -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![
Expand Down
15 changes: 5 additions & 10 deletions bustubx/src/catalog/information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions bustubx/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ pub struct Database {
impl Database {
pub fn new_on_disk(db_path: &str) -> BustubxResult<Self> {
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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/execution/physical_plan/values.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 0 additions & 1 deletion bustubx/src/expression/aggr/avg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_planner/plan_insert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{BustubxError, BustubxResult};
use crate::BustubxResult;
use std::sync::Arc;

use crate::planner::logical_plan::{Insert, LogicalPlan};
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/planner/logical_planner/plan_set_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl LogicalPlanner<'_> {
.collect::<BustubxResult<Vec<Column>>>()?,
);
Ok(LogicalPlan::Aggregate(Aggregate {
input: Arc::new((input)),
input: Arc::new(input),
group_exprs,
aggr_exprs,
schema: Arc::new(Schema::new(columns)),
Expand Down
3 changes: 1 addition & 2 deletions bustubx/src/storage/codec/freelist_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions bustubx/src/storage/codec/index_page.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/storage/codec/meta_page.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions bustubx/src/storage/codec/table_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions bustubx/src/storage/disk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
Loading

0 comments on commit 3b457c9

Please sign in to comment.