1- use std:: collections:: { BTreeMap , HashMap } ;
1+ use std:: collections:: HashMap ;
22
33use itertools:: Itertools ;
44
55use crate :: catalog:: { CatalogError , ColumnCatalog } ;
6- use crate :: types:: { ColumnId , IdGenerator , TableId } ;
6+ use crate :: types:: { ColumnIdx , IdGenerator , TableIdx } ;
77#[ derive( Debug , Clone ) ]
88pub struct TableCatalog {
9- pub id : TableId ,
9+ pub id : Option < TableIdx > ,
1010 pub name : String ,
11+ generator : IdGenerator ,
1112 /// Mapping from column names to column ids
12- column_idxs : HashMap < String , ColumnId > ,
13- pub ( crate ) columns : BTreeMap < ColumnId , ColumnCatalog > ,
13+ column_idxs : HashMap < String , ColumnIdx > ,
14+ pub ( crate ) columns : Vec < ColumnCatalog > ,
1415}
1516
1617impl TableCatalog {
17- pub ( crate ) fn get_column_by_id ( & self , id : ColumnId ) -> Option < & ColumnCatalog > {
18- self . columns . get ( & id)
18+ pub ( crate ) fn get_column_by_id ( & self , id : ColumnIdx ) -> Option < & ColumnCatalog > {
19+ self . columns . get ( id)
1920 }
2021
21- pub ( crate ) fn get_column_id_by_name ( & self , name : & str ) -> Option < ColumnId > {
22+ pub ( crate ) fn get_column_id_by_name ( & self , name : & str ) -> Option < ColumnIdx > {
2223 self . column_idxs . get ( name) . cloned ( )
2324 }
2425
26+ pub ( crate ) fn get_column_by_name ( & self , name : & str ) -> Option < & ColumnCatalog > {
27+ let id = self . column_idxs . get ( name) ?;
28+ self . columns . get ( * id)
29+ }
30+
2531 pub ( crate ) fn contains_column ( & self , name : & str ) -> bool {
2632 self . column_idxs . contains_key ( name)
2733 }
2834
29- pub ( crate ) fn get_all_columns ( & self ) -> Vec < ( ColumnId , & ColumnCatalog ) > {
35+ pub ( crate ) fn get_all_columns ( & self ) -> Vec < ( ColumnIdx , & ColumnCatalog ) > {
3036 self . columns
3137 . iter ( )
32- . map ( | ( col_id , col ) | ( * col_id , col ) )
38+ . enumerate ( )
3339 . collect_vec ( )
3440 }
3541
3642 /// Add a column to the table catalog.
3743 pub ( crate ) fn add_column (
3844 & mut self ,
39- col_catalog : ColumnCatalog ,
40- ) -> Result < ColumnId , CatalogError > {
45+ mut col_catalog : ColumnCatalog ,
46+ ) -> Result < ColumnIdx , CatalogError > {
4147 if self . column_idxs . contains_key ( & col_catalog. name ) {
4248 return Err ( CatalogError :: Duplicated ( "column" , col_catalog. name . into ( ) ) ) ;
4349 }
4450
45- let col_id = col_catalog . id ;
51+ let col_id = self . generator . build ( ) ;
4652
53+ col_catalog. id = Some ( col_id) ;
4754 self . column_idxs . insert ( col_catalog. name . to_owned ( ) , col_id) ;
4855 self . columns . insert ( col_id, col_catalog) ;
4956
@@ -55,10 +62,11 @@ impl TableCatalog {
5562 columns : Vec < ColumnCatalog > ,
5663 ) -> Result < TableCatalog , CatalogError > {
5764 let mut table_catalog = TableCatalog {
58- id : IdGenerator :: build ( ) ,
65+ id : None ,
5966 name : table_name,
67+ generator : IdGenerator :: new ( ) ,
6068 column_idxs : HashMap :: new ( ) ,
61- columns : BTreeMap :: new ( ) ,
69+ columns : Vec :: new ( ) ,
6270 } ;
6371
6472 for col_catalog in columns. into_iter ( ) {
0 commit comments