Skip to content

Commit

Permalink
Implement index iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 24, 2024
1 parent ef5f736 commit fbfde65
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 59 deletions.
26 changes: 7 additions & 19 deletions bustubx/src/catalog/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@ pub static DEFAULT_SCHEMA_NAME: &str = "public";
/// catalog, schema, table, index
pub type FullIndexRef = (String, String, String, String);

// index元信息
pub struct IndexInfo {
pub key_schema: SchemaRef,
pub index: BPlusTreeIndex,
pub table_name: String,
}

pub struct Catalog {
pub tables: HashMap<FullTableRef, Arc<TableHeap>>,
pub indexes: HashMap<FullIndexRef, IndexInfo>,
pub indexes: HashMap<FullIndexRef, Arc<BPlusTreeIndex>>,
pub buffer_pool: Arc<BufferPoolManager>,
}

Expand Down Expand Up @@ -124,7 +117,7 @@ impl Catalog {
index_name: String,
table_ref: &TableReference,
key_attrs: Vec<usize>,
) -> BustubxResult<&IndexInfo> {
) -> BustubxResult<Arc<BPlusTreeIndex>> {
let (catalog, schema, table) = table_ref.extend_to_full();
let full_index_ref = (catalog, schema, table, index_name.clone());

Expand All @@ -139,25 +132,22 @@ impl Catalog {
BPLUS_INTERNAL_PAGE_MAX_SIZE as u32,
);

let index_info = IndexInfo {
key_schema,
index: b_plus_tree_index,
table_name: table_ref.table().to_string(),
};
self.indexes.insert(full_index_ref.clone(), index_info);
self.indexes
.insert(full_index_ref.clone(), Arc::new(b_plus_tree_index));
self.indexes
.get(&full_index_ref)
.cloned()
.ok_or(BustubxError::Internal("Failed to create table".to_string()))
}

pub fn get_index_by_name(
&self,
table_ref: &TableReference,
index_name: &str,
) -> Option<&IndexInfo> {
) -> Option<Arc<BPlusTreeIndex>> {
let (catalog, schema, table) = table_ref.extend_to_full();
let full_index_ref = (catalog, schema, table, index_name.to_string());
self.indexes.get(&full_index_ref)
self.indexes.get(&full_index_ref).cloned()
}
}

Expand Down Expand Up @@ -231,7 +221,6 @@ mod tests {
let index_info = catalog
.create_index(index_name1.clone(), &table_ref, key_attrs)
.unwrap();
assert_eq!(index_info.table_name, table_ref.table());
assert_eq!(index_info.key_schema.column_count(), 2);
assert_eq!(
index_info.key_schema.column_with_index(0).unwrap().name,
Expand Down Expand Up @@ -263,7 +252,6 @@ mod tests {
let index_info = catalog
.create_index(index_name2.clone(), &table_ref, key_attrs)
.unwrap();
assert_eq!(index_info.table_name, table_ref.table());
assert_eq!(index_info.key_schema.column_count(), 1);
assert_eq!(
index_info.key_schema.column_with_index(0).unwrap().name,
Expand Down
3 changes: 2 additions & 1 deletion bustubx/src/common/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::storage::index::BPlusTreeIndex;
use crate::BustubxResult;
use comfy_table::Cell;
use std::collections::VecDeque;
use std::sync::atomic::Ordering;

use crate::storage::{BPlusTreePage, Tuple};

Expand Down Expand Up @@ -79,7 +80,7 @@ pub(crate) fn pretty_format_index_tree(index: &BPlusTreeIndex) -> BustubxResult<
}
// 层序遍历
let mut curr_queue = VecDeque::new();
curr_queue.push_back(index.root_page_id);
curr_queue.push_back(index.root_page_id.load(Ordering::SeqCst));

let mut level_index = 1;
loop {
Expand Down
2 changes: 1 addition & 1 deletion bustubx/src/execution/physical_plan/seq_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl PhysicalSeqScan {
impl VolcanoExecutor for PhysicalSeqScan {
fn init(&self, context: &mut ExecutionContext) -> BustubxResult<()> {
let table_heap = context.catalog.table_heap(&self.table)?;
*self.iterator.lock().unwrap() = Some(TableIterator::new(table_heap, (..)));
*self.iterator.lock().unwrap() = Some(TableIterator::new(table_heap, ..));
Ok(())
}

Expand Down
Loading

0 comments on commit fbfde65

Please sign in to comment.