Skip to content

Commit

Permalink
Refactor tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Jan 30, 2024
1 parent ee1c0f9 commit 877bff7
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 225 deletions.
6 changes: 6 additions & 0 deletions bustubx/src/catalog/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ pub struct Column {
pub column_offset: usize,
}

impl PartialEq for Column {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.data_type == other.data_type
}
}

impl Column {
pub fn new(name: String, data_type: DataType) -> Self {
Self {
Expand Down
22 changes: 22 additions & 0 deletions bustubx/src/common/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,25 @@ impl std::fmt::Display for ScalarValue {
}
}
}

macro_rules! impl_from_for_scalar {
($ty:ty, $scalar:tt) => {
impl From<$ty> for ScalarValue {
fn from(value: $ty) -> Self {
ScalarValue::$scalar(Some(value))
}
}

impl From<Option<$ty>> for ScalarValue {
fn from(value: Option<$ty>) -> Self {
ScalarValue::$scalar(value)
}
}
};
}

impl_from_for_scalar!(i8, Int8);
impl_from_for_scalar!(i16, Int16);
impl_from_for_scalar!(i32, Int32);
impl_from_for_scalar!(i64, Int64);
impl_from_for_scalar!(bool, Boolean);
2 changes: 1 addition & 1 deletion bustubx/src/planner/physical_plan/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl VolcanoExecutor for PhysicalInsert {
let insert_rows = self.insert_rows.load(std::sync::atomic::Ordering::SeqCst);
self.insert_rows
.store(0, std::sync::atomic::Ordering::SeqCst);
return Some(Tuple::from_values(
return Some(Tuple::new(
Arc::new(self.output_schema()),
vec![ScalarValue::Int32(Some(insert_rows as i32))],
));
Expand Down
5 changes: 1 addition & 4 deletions bustubx/src/planner/physical_plan/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ impl VolcanoExecutor for PhysicalProject {
for expr in &self.expressions {
new_values.push(expr.evaluate(next_tuple.as_ref(), Some(&self.input.output_schema())));
}
return Some(Tuple::from_values(
Arc::new(self.output_schema()),
new_values,
));
return Some(Tuple::new(Arc::new(self.output_schema()), new_values));
}
}
2 changes: 1 addition & 1 deletion bustubx/src/planner/physical_plan/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl VolcanoExecutor for PhysicalValues {
.fetch_add(1, std::sync::atomic::Ordering::SeqCst) as usize;
if cursor < self.tuples.len() {
let values = self.tuples[cursor].clone();
return Some(Tuple::from_values(Arc::new(self.output_schema()), values));
return Some(Tuple::new(Arc::new(self.output_schema()), values));
} else {
return None;
}
Expand Down
161 changes: 115 additions & 46 deletions bustubx/src/storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ impl BPlusTreeIndex {

// internal page第一个kv对的key为空
new_internal_page.insert(
Tuple::empty(
self.index_metadata.key_schema.clone(),
self.index_metadata.key_schema.fixed_len(),
),
Tuple::empty(self.index_metadata.key_schema.clone()),
self.root_page_id,
&self.index_metadata.key_schema,
);
Expand Down Expand Up @@ -835,50 +832,65 @@ mod tests {
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
let mut index = BPlusTreeIndex::new(index_metadata, buffer_pool_manager, 2, 3);

index.insert(&Tuple::new(schema.clone(), vec![1, 1, 1]), Rid::new(1, 1));
index.insert(
&Tuple::new(schema.clone(), vec![1i8.into(), 2i16.into()]),
Rid::new(1, 1),
);
assert_eq!(
index
.get(&Tuple::new(schema.clone(), vec![1, 1, 1]))
.get(&Tuple::new(schema.clone(), vec![1i8.into(), 2i16.into()]))
.unwrap(),
Rid::new(1, 1)
);
assert_eq!(index.root_page_id, 0);
assert_eq!(index.buffer_pool_manager.replacer.size(), 1);

index.insert(&Tuple::new(schema.clone(), vec![2, 2, 2]), Rid::new(2, 2));
index.insert(
&Tuple::new(schema.clone(), vec![2i8.into(), 4i16.into()]),
Rid::new(2, 2),
);
assert_eq!(
index
.get(&Tuple::new(schema.clone(), vec![2, 2, 2]))
.get(&Tuple::new(schema.clone(), vec![2i8.into(), 4i16.into()]))
.unwrap(),
Rid::new(2, 2)
);
assert_eq!(index.root_page_id, 0);
assert_eq!(index.buffer_pool_manager.replacer.size(), 1);

index.insert(&Tuple::new(schema.clone(), vec![3, 3, 3]), Rid::new(3, 3));
index.insert(
&Tuple::new(schema.clone(), vec![3i8.into(), 6i16.into()]),
Rid::new(3, 3),
);
assert_eq!(
index
.get(&Tuple::new(schema.clone(), vec![3, 3, 3]))
.get(&Tuple::new(schema.clone(), vec![3i8.into(), 6i16.into()]))
.unwrap(),
Rid::new(3, 3)
);
assert_eq!(index.root_page_id, 2);
assert_eq!(index.buffer_pool_manager.replacer.size(), 3);

index.insert(&Tuple::new(schema.clone(), vec![4, 4, 4]), Rid::new(4, 4));
index.insert(
&Tuple::new(schema.clone(), vec![4i8.into(), 8i16.into()]),
Rid::new(4, 4),
);
assert_eq!(
index
.get(&Tuple::new(schema.clone(), vec![4, 4, 4]))
.get(&Tuple::new(schema.clone(), vec![4i8.into(), 8i16.into()]))
.unwrap(),
Rid::new(4, 4)
);
assert_eq!(index.root_page_id, 2);
assert_eq!(index.buffer_pool_manager.replacer.size(), 4);

index.insert(&Tuple::new(schema.clone(), vec![5, 5, 5]), Rid::new(5, 5));
index.insert(
&Tuple::new(schema.clone(), vec![5i8.into(), 10i16.into()]),
Rid::new(5, 5),
);
assert_eq!(
index
.get(&Tuple::new(schema.clone(), vec![5, 5, 5]))
.get(&Tuple::new(schema.clone(), vec![5i8.into(), 10i16.into()]))
.unwrap(),
Rid::new(5, 5)
);
Expand All @@ -905,79 +917,136 @@ mod tests {
let buffer_pool_manager = BufferPoolManager::new(1000, Arc::new(disk_manager));
let mut index = BPlusTreeIndex::new(index_metadata, buffer_pool_manager, 4, 5);

index.insert(&Tuple::new(schema.clone(), vec![1, 1, 1]), Rid::new(1, 1));
index.insert(&Tuple::new(schema.clone(), vec![2, 2, 2]), Rid::new(2, 2));
index.insert(&Tuple::new(schema.clone(), vec![3, 3, 3]), Rid::new(3, 3));
index.insert(&Tuple::new(schema.clone(), vec![4, 4, 4]), Rid::new(4, 4));
index.insert(&Tuple::new(schema.clone(), vec![5, 5, 5]), Rid::new(5, 5));
index.insert(&Tuple::new(schema.clone(), vec![6, 6, 6]), Rid::new(6, 6));
index.insert(&Tuple::new(schema.clone(), vec![7, 7, 7]), Rid::new(7, 7));
index.insert(&Tuple::new(schema.clone(), vec![8, 8, 8]), Rid::new(8, 8));
index.insert(&Tuple::new(schema.clone(), vec![9, 9, 9]), Rid::new(9, 9));
index.insert(
&Tuple::new(schema.clone(), vec![10, 10, 10]),
&Tuple::new(schema.clone(), vec![1i8.into(), 1i16.into()]),
Rid::new(1, 1),
);
index.insert(
&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()]),
Rid::new(2, 2),
);
index.insert(
&Tuple::new(schema.clone(), vec![3i8.into(), 3i16.into()]),
Rid::new(3, 3),
);
index.insert(
&Tuple::new(schema.clone(), vec![4i8.into(), 4i16.into()]),
Rid::new(4, 4),
);
index.insert(
&Tuple::new(schema.clone(), vec![5i8.into(), 5i16.into()]),
Rid::new(5, 5),
);
index.insert(
&Tuple::new(schema.clone(), vec![6i8.into(), 6i16.into()]),
Rid::new(6, 6),
);
index.insert(
&Tuple::new(schema.clone(), vec![7i8.into(), 7i16.into()]),
Rid::new(7, 7),
);
index.insert(
&Tuple::new(schema.clone(), vec![8i8.into(), 8i16.into()]),
Rid::new(8, 8),
);
index.insert(
&Tuple::new(schema.clone(), vec![9i8.into(), 9i16.into()]),
Rid::new(9, 9),
);
index.insert(
&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.root_page_id, 2);
index.print_tree();

index.delete(&Tuple::new(schema.clone(), vec![1, 1, 1]));
index.delete(&Tuple::new(schema.clone(), vec![1i8.into(), 1i16.into()]));
assert_eq!(index.root_page_id, 2);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![1, 1, 1])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![1i8.into(), 1i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 4);

index.delete(&Tuple::new(schema.clone(), vec![3, 3, 3]));
index.delete(&Tuple::new(schema.clone(), vec![3i8.into(), 3i16.into()]));
assert_eq!(index.root_page_id, 2);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![3, 3, 3])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![3i8.into(), 3i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 4);

index.delete(&Tuple::new(schema.clone(), vec![5, 5, 5]));
index.delete(&Tuple::new(schema.clone(), vec![5i8.into(), 5i16.into()]));
assert_eq!(index.root_page_id, 2);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![5, 5, 5])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![5i8.into(), 5i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 4);

index.delete(&Tuple::new(schema.clone(), vec![7, 7, 7]));
index.delete(&Tuple::new(schema.clone(), vec![7i8.into(), 7i16.into()]));
assert_eq!(index.root_page_id, 2);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![7, 7, 7])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![7i8.into(), 7i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 4);

index.delete(&Tuple::new(schema.clone(), vec![9, 9, 9]));
index.delete(&Tuple::new(schema.clone(), vec![9i8.into(), 9i16.into()]));
assert_eq!(index.root_page_id, 2);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![9, 9, 9])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![9i8.into(), 9i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 3);

index.delete(&Tuple::new(schema.clone(), vec![10, 10, 10]));
index.delete(&Tuple::new(schema.clone(), vec![10i8.into(), 10i16.into()]));
assert_eq!(index.root_page_id, 2);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![10, 10, 10])),
index.get(&Tuple::new(schema.clone(), vec![10i8.into(), 10i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 3);

index.delete(&Tuple::new(schema.clone(), vec![8, 8, 8]));
index.delete(&Tuple::new(schema.clone(), vec![8i8.into(), 8i16.into()]));
assert_eq!(index.root_page_id, 0);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![8, 8, 8])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![8i8.into(), 8i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 1);

index.delete(&Tuple::new(schema.clone(), vec![6, 6, 6]));
index.delete(&Tuple::new(schema.clone(), vec![6i8.into(), 6i16.into()]));
assert_eq!(index.root_page_id, 0);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![6, 6, 6])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![6i8.into(), 6i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 1);

index.delete(&Tuple::new(schema.clone(), vec![4, 4, 4]));
index.delete(&Tuple::new(schema.clone(), vec![4i8.into(), 4i16.into()]));
assert_eq!(index.root_page_id, 0);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![4, 4, 4])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![4i8.into(), 4i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 1);

index.delete(&Tuple::new(schema.clone(), vec![2, 2, 2]));
index.delete(&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()]));
assert_eq!(index.root_page_id, 0);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![2, 2, 2])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 1);

index.delete(&Tuple::new(schema.clone(), vec![2, 2, 2]));
index.delete(&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()]));
assert_eq!(index.root_page_id, 0);
assert_eq!(index.get(&Tuple::new(schema.clone(), vec![2, 2, 2])), None);
assert_eq!(
index.get(&Tuple::new(schema.clone(), vec![2i8.into(), 2i16.into()])),
None
);
assert_eq!(index.buffer_pool_manager.replacer.size(), 1);
}
}
Loading

0 comments on commit 877bff7

Please sign in to comment.