Skip to content

Commit

Permalink
refactor: tables
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Dec 16, 2023
1 parent 241f8c8 commit a9a3175
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/catalog/src/information_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,19 @@ macro_rules! setup_memory_table {
};
}

/// The `information_schema` tables info provider.
pub struct InformationSchemaProvider {
catalog_name: String,
catalog_manager: Weak<dyn CatalogManager>,
tables: Option<HashMap<String, TableRef>>,
tables: HashMap<String, TableRef>,
}

impl InformationSchemaProvider {
pub fn new(catalog_name: String, catalog_manager: Weak<dyn CatalogManager>) -> Self {
let mut provider = Self {
catalog_name,
catalog_manager,
tables: None,
tables: HashMap::new(),
};

provider.build_tables();
Expand All @@ -90,32 +91,33 @@ impl InformationSchemaProvider {

/// Returns table names in the order of table id.
pub fn table_names(&self) -> Vec<String> {
let mut tables = self.tables().into_values().collect::<Vec<_>>();
let mut tables = self.tables.values().clone().collect::<Vec<_>>();

tables.sort_by(|t1, t2| {
t1.table_info()
.table_id()
.partial_cmp(&t2.table_info().table_id())
.unwrap()
});

tables
.into_iter()
.map(|t| t.table_info().name.clone())
.collect()
}

/// Returns a map of [TableRef] in information schema.
pub fn tables(&self) -> HashMap<String, TableRef> {
// Safety: already built in `new`.
self.tables.clone().unwrap()
pub fn tables(&self) -> &HashMap<String, TableRef> {
assert!(!self.tables.is_empty());

&self.tables
}

/// Returns the [TableRef] by table name.
pub fn table(&self, name: &str) -> Option<TableRef> {
self.tables().get(name).cloned()
self.tables.get(name).cloned()
}

fn build_tables(&mut self) -> HashMap<String, TableRef> {
fn build_tables(&mut self) {
let mut tables = HashMap::new();
tables.insert(TABLES.to_string(), self.build_table(TABLES).unwrap());
tables.insert(COLUMNS.to_string(), self.build_table(COLUMNS).unwrap());
Expand All @@ -125,9 +127,7 @@ impl InformationSchemaProvider {
tables.insert((*name).to_string(), self.build_table(name).unwrap());
}

self.tables = Some(tables.clone());

tables
self.tables = tables;
}

fn build_table(&self, name: &str) -> Option<TableRef> {
Expand Down
2 changes: 1 addition & 1 deletion src/catalog/src/memory/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl MemoryCatalogManager {
catalog,
Arc::downgrade(self) as Weak<dyn CatalogManager>,
);
let information_schema = information_schema_provider.tables();
let information_schema = information_schema_provider.tables().clone();
let mut catalog = HashMap::new();
catalog.insert(INFORMATION_SCHEMA_NAME.to_string(), information_schema);
catalog
Expand Down

0 comments on commit a9a3175

Please sign in to comment.