Skip to content

Commit

Permalink
Move RecordId to table page
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 26, 2024
1 parent fa39fe6 commit 2d1c6bb
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 109 deletions.
1 change: 0 additions & 1 deletion bustubx/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod bitmap;
pub mod rid;
mod scalar;
mod table_ref;
pub mod util;
Expand Down
14 changes: 0 additions & 14 deletions bustubx/src/common/rid.rs

This file was deleted.

1 change: 0 additions & 1 deletion bustubx/src/planner/logical_planner/plan_create_table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{BustubxError, BustubxResult};
use itertools::Itertools;
use std::collections::HashSet;

use crate::catalog::Column;
Expand Down
7 changes: 3 additions & 4 deletions bustubx/src/storage/codec/index_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ impl BPlusTreeInternalPageHeaderCodec {
#[cfg(test)]
mod tests {
use crate::catalog::{Column, DataType, Schema};
use crate::common::rid::Rid;
use crate::storage::codec::index_page::BPlusTreePageCodec;
use crate::storage::{BPlusTreeInternalPage, BPlusTreeLeafPage, BPlusTreePage};
use crate::storage::{BPlusTreeInternalPage, BPlusTreeLeafPage, BPlusTreePage, RecordId};
use crate::Tuple;
use std::sync::Arc;

Expand All @@ -277,9 +276,9 @@ mod tests {
Column::new("b", DataType::Int32, true),
]));
let tuple1 = Tuple::new(schema.clone(), vec![1i8.into(), 1i32.into()]);
let rid1 = Rid::new(1, 1);
let rid1 = RecordId::new(1, 1);
let tuple2 = Tuple::new(schema.clone(), vec![2i8.into(), 2i32.into()]);
let rid2 = Rid::new(2, 2);
let rid2 = RecordId::new(2, 2);

let mut leaf_page = BPlusTreeLeafPage::new(schema.clone(), 100);
leaf_page.insert(tuple1.clone(), rid1);
Expand Down
12 changes: 7 additions & 5 deletions bustubx/src/storage/codec/table_page.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::buffer::BUSTUBX_PAGE_SIZE;
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::{TablePage, TablePageHeader, TupleInfo, TupleMeta};
use crate::storage::{RecordId, TablePage, TablePageHeader, TupleInfo, TupleMeta};
use crate::{BustubxError, BustubxResult};

pub struct TablePageCodec;
Expand Down Expand Up @@ -123,14 +122,14 @@ impl TablePageHeaderTupleInfoCodec {
pub struct RidCodec;

impl RidCodec {
pub fn encode(rid: &Rid) -> Vec<u8> {
pub fn encode(rid: &RecordId) -> Vec<u8> {
let mut bytes = vec![];
bytes.extend(CommonCodec::encode_u32(rid.page_id));
bytes.extend(CommonCodec::encode_u32(rid.slot_num));
bytes
}

pub fn decode(bytes: &[u8]) -> BustubxResult<DecodedData<Rid>> {
pub fn decode(bytes: &[u8]) -> BustubxResult<DecodedData<RecordId>> {
let mut left_bytes = bytes;

let (page_id, offset) = CommonCodec::decode_u32(left_bytes)?;
Expand All @@ -139,7 +138,10 @@ impl RidCodec {
let (slot_num, offset) = CommonCodec::decode_u32(left_bytes)?;
left_bytes = &left_bytes[offset..];

Ok((Rid::new(page_id, slot_num), bytes.len() - left_bytes.len()))
Ok((
RecordId::new(page_id, slot_num),
bytes.len() - left_bytes.len(),
))
}
}

Expand Down
60 changes: 29 additions & 31 deletions bustubx/src/storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use crate::storage::codec::{
use crate::storage::{InternalKV, LeafKV};
use crate::{
buffer::BufferPoolManager,
common::rid::Rid,
storage::{BPlusTreeInternalPage, BPlusTreeLeafPage, BPlusTreePage},
storage::{BPlusTreeInternalPage, BPlusTreeLeafPage, BPlusTreePage, RecordId},
BustubxError, BustubxResult,
};

Expand Down Expand Up @@ -64,7 +63,7 @@ impl BPlusTreeIndex {
self.root_page_id.load(Ordering::SeqCst) == INVALID_PAGE_ID
}

pub fn insert(&self, key: &Tuple, rid: Rid) -> BustubxResult<()> {
pub fn insert(&self, key: &Tuple, rid: RecordId) -> BustubxResult<()> {
if self.is_empty() {
self.start_new_tree(key, rid)?;
return Ok(());
Expand Down Expand Up @@ -219,15 +218,15 @@ impl BPlusTreeIndex {
Ok(())
}

pub fn scan<R>(&self, range: R) -> Vec<Rid>
pub fn scan<R>(&self, range: R) -> Vec<RecordId>
where
R: RangeBounds<Tuple>,
{
range.start_bound();
unimplemented!()
}

fn start_new_tree(&self, key: &Tuple, rid: Rid) -> BustubxResult<()> {
fn start_new_tree(&self, key: &Tuple, rid: RecordId) -> BustubxResult<()> {
let new_page = self.buffer_pool.new_page()?;
let new_page_id = new_page.read().unwrap().page_id;

Expand All @@ -248,7 +247,7 @@ impl BPlusTreeIndex {
}

// 找到叶子节点上对应的Value
pub fn get(&self, key: &Tuple) -> BustubxResult<Option<Rid>> {
pub fn get(&self, key: &Tuple) -> BustubxResult<Option<RecordId>> {
if self.is_empty() {
return Ok(None);
}
Expand Down Expand Up @@ -645,7 +644,7 @@ impl TreeIndexIterator {
}
}

pub fn next(&mut self) -> BustubxResult<Option<Rid>> {
pub fn next(&mut self) -> BustubxResult<Option<RecordId>> {
if self.started {
match self.end_bound.as_ref() {
Bound::Included(end_tuple) => {
Expand Down Expand Up @@ -767,8 +766,7 @@ mod tests {
use crate::{
buffer::BufferPoolManager,
catalog::{Column, DataType, Schema},
common::rid::Rid,
storage::{DiskManager, Tuple},
storage::{DiskManager, RecordId, Tuple},
};

use super::BPlusTreeIndex;
Expand All @@ -788,67 +786,67 @@ mod tests {
index
.insert(
&Tuple::new(key_schema.clone(), vec![1i8.into(), 1i16.into()]),
Rid::new(1, 1),
RecordId::new(1, 1),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![2i8.into(), 2i16.into()]),
Rid::new(2, 2),
RecordId::new(2, 2),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![3i8.into(), 3i16.into()]),
Rid::new(3, 3),
RecordId::new(3, 3),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![4i8.into(), 4i16.into()]),
Rid::new(4, 4),
RecordId::new(4, 4),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![5i8.into(), 5i16.into()]),
Rid::new(5, 5),
RecordId::new(5, 5),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![6i8.into(), 6i16.into()]),
Rid::new(6, 6),
RecordId::new(6, 6),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![7i8.into(), 7i16.into()]),
Rid::new(7, 7),
RecordId::new(7, 7),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![8i8.into(), 8i16.into()]),
Rid::new(8, 8),
RecordId::new(8, 8),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![9i8.into(), 9i16.into()]),
Rid::new(9, 9),
RecordId::new(9, 9),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![10i8.into(), 10i16.into()]),
Rid::new(10, 10),
RecordId::new(10, 10),
)
.unwrap();
index
.insert(
&Tuple::new(key_schema.clone(), vec![11i8.into(), 11i16.into()]),
Rid::new(11, 11),
RecordId::new(11, 11),
)
.unwrap();
(index, key_schema)
Expand Down Expand Up @@ -952,7 +950,7 @@ B+ Tree Level No.2:
vec![3i8.into(), 3i16.into()],
))
.unwrap(),
Some(Rid::new(3, 3))
Some(RecordId::new(3, 3))
);
assert_eq!(
index
Expand All @@ -961,7 +959,7 @@ B+ Tree Level No.2:
vec![10i8.into(), 10i16.into()],
))
.unwrap(),
Some(Rid::new(10, 10))
Some(RecordId::new(10, 10))
);
}

Expand All @@ -972,16 +970,16 @@ B+ Tree Level No.2:

let end_tuple1 = Tuple::new(key_schema.clone(), vec![3i8.into(), 3i16.into()]);
let mut iterator1 = TreeIndexIterator::new(index.clone(), ..end_tuple1);
assert_eq!(iterator1.next().unwrap(), Some(Rid::new(1, 1)));
assert_eq!(iterator1.next().unwrap(), Some(Rid::new(2, 2)));
assert_eq!(iterator1.next().unwrap(), Some(RecordId::new(1, 1)));
assert_eq!(iterator1.next().unwrap(), Some(RecordId::new(2, 2)));
assert_eq!(iterator1.next().unwrap(), None);

let start_tuple2 = Tuple::new(key_schema.clone(), vec![3i8.into(), 3i16.into()]);
let end_tuple2 = Tuple::new(key_schema.clone(), vec![5i8.into(), 5i16.into()]);
let mut iterator2 = TreeIndexIterator::new(index.clone(), start_tuple2..=end_tuple2);
assert_eq!(iterator2.next().unwrap(), Some(Rid::new(3, 3)));
assert_eq!(iterator2.next().unwrap(), Some(Rid::new(4, 4)));
assert_eq!(iterator2.next().unwrap(), Some(Rid::new(5, 5)));
assert_eq!(iterator2.next().unwrap(), Some(RecordId::new(3, 3)));
assert_eq!(iterator2.next().unwrap(), Some(RecordId::new(4, 4)));
assert_eq!(iterator2.next().unwrap(), Some(RecordId::new(5, 5)));
assert_eq!(iterator2.next().unwrap(), None);

let start_tuple3 = Tuple::new(key_schema.clone(), vec![6i8.into(), 6i16.into()]);
Expand All @@ -990,13 +988,13 @@ B+ Tree Level No.2:
index.clone(),
(Bound::Excluded(start_tuple3), Bound::Excluded(end_tuple3)),
);
assert_eq!(iterator3.next().unwrap(), Some(Rid::new(7, 7)));
assert_eq!(iterator3.next().unwrap(), Some(RecordId::new(7, 7)));

let start_tuple4 = Tuple::new(key_schema.clone(), vec![9i8.into(), 9i16.into()]);
let mut iterator4 = TreeIndexIterator::new(index.clone(), start_tuple4..);
assert_eq!(iterator4.next().unwrap(), Some(Rid::new(9, 9)));
assert_eq!(iterator4.next().unwrap(), Some(Rid::new(10, 10)));
assert_eq!(iterator4.next().unwrap(), Some(Rid::new(11, 11)));
assert_eq!(iterator4.next().unwrap(), Some(RecordId::new(9, 9)));
assert_eq!(iterator4.next().unwrap(), Some(RecordId::new(10, 10)));
assert_eq!(iterator4.next().unwrap(), Some(RecordId::new(11, 11)));
assert_eq!(iterator4.next().unwrap(), None);
assert_eq!(iterator4.next().unwrap(), None);
}
Expand Down
Loading

0 comments on commit 2d1c6bb

Please sign in to comment.