From 193dafec9cb30d7b77453c39db2b009332ab1e2e Mon Sep 17 00:00:00 2001 From: fansehep Date: Thu, 1 Feb 2024 11:05:09 +0800 Subject: [PATCH] feat: add catalog --- src/catalog/column.rs | 16 +++++++ src/catalog/mod.rs | 97 ++++++++++++++++++++++++++++++++++++++++ src/catalog/schema.rs | 10 +++++ src/catalog/tableinfo.rs | 28 ++++++++++++ src/lib.rs | 1 + src/types/mod.rs | 18 ++++++++ 6 files changed, 170 insertions(+) create mode 100644 src/catalog/column.rs create mode 100644 src/catalog/mod.rs create mode 100644 src/catalog/schema.rs create mode 100644 src/catalog/tableinfo.rs diff --git a/src/catalog/column.rs b/src/catalog/column.rs new file mode 100644 index 0000000..4481724 --- /dev/null +++ b/src/catalog/column.rs @@ -0,0 +1,16 @@ +use crate::types::LogicalType; + +#[derive(Debug, Clone, PartialEq)] +pub struct Column { + pub column_type: LogicalType, + pub name: String, +} + +impl Column { + pub fn new(logical_type: LogicalType, name: &str) -> Self { + Self { + column_type: logical_type, + name: name.to_owned(), + } + } +} diff --git a/src/catalog/mod.rs b/src/catalog/mod.rs new file mode 100644 index 0000000..e08adf2 --- /dev/null +++ b/src/catalog/mod.rs @@ -0,0 +1,97 @@ +use std::{ + collections::HashMap, + sync::{ + atomic::{self, AtomicI32}, + Arc, + }, +}; + +use log::error; + +use self::{ + schema::Schema, + tableinfo::{IndexInfo, TableInfo}, +}; +use crate::{ + error::{Error::Internal, Result}, + fmt_err, +}; +pub mod column; +pub mod schema; +pub mod tableinfo; + +pub type TableId = i32; +pub type TableInfoRef = Arc; +pub type IndexInfoRef = Arc; + +#[derive(Debug)] +pub struct CataLog { + id_table_map: HashMap>, + name_id_map: HashMap, + next_table_id: AtomicI32, + _id_index_map: HashMap, + index_names_map: HashMap>, +} + +impl Default for CataLog { + fn default() -> Self { + Self { + _id_index_map: HashMap::new(), + name_id_map: HashMap::new(), + next_table_id: 0.into(), + id_table_map: HashMap::new(), + index_names_map: HashMap::new(), + } + } +} + +impl CataLog { + pub fn create_table(&mut self, table_name: &String, schema: &Schema) -> Result { + // 表不能已经存在 + if self.name_id_map.contains_key(table_name) { + return Err(Internal(fmt_err!("{table_name} has been exist"))); + } + + let table_id = self.next_table_id.fetch_add(1, atomic::Ordering::Release); + let table_info = Arc::new(TableInfo::new(table_name, table_id, schema)); + + // 更新元数据信息 + self.id_table_map.insert(table_id, Arc::clone(&table_info)); + self.name_id_map.insert(table_name.clone(), table_id); + self.index_names_map + .insert(table_name.clone(), HashMap::new()); + + Ok(table_info) + } + + pub fn is_table_exist(&self, table_name: &str) -> bool { + self.name_id_map.get(table_name).is_some() + } + + pub fn table_by_name(&self, table_name: &str) -> Option { + match self.name_id_map.get(table_name) { + Some(id) => match self.id_table_map.get(id) { + Some(table) => Some(Arc::clone(table)), + None => { + error!("table_name: {table_name} {id} is not exist"); + None + } + }, + None => None, + } + } + + pub fn table_by_id(&self, table_id: TableId) -> Option { + self.id_table_map.get(&table_id).cloned() + } + + #[warn(clippy::too_many_arguments)] + pub fn create_index( + &mut self, + _index_name: &str, + _table_name: &str, + _shema: &Schema, + ) -> Result { + unimplemented!() + } +} diff --git a/src/catalog/schema.rs b/src/catalog/schema.rs new file mode 100644 index 0000000..6fe756e --- /dev/null +++ b/src/catalog/schema.rs @@ -0,0 +1,10 @@ +use std::sync::Arc; + +use super::column::Column; + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct Schema { + pub columns: Vec, +} + +pub type SchemaRef = Arc; diff --git a/src/catalog/tableinfo.rs b/src/catalog/tableinfo.rs new file mode 100644 index 0000000..0f0b299 --- /dev/null +++ b/src/catalog/tableinfo.rs @@ -0,0 +1,28 @@ +use super::schema::Schema; + +#[derive(Debug)] +pub struct TableInfo { + pub name: String, + pub id: i32, + pub schema: Schema, +} + +impl TableInfo { + pub fn new(table_name: &String, id: i32, schema: &Schema) -> Self { + Self { + name: table_name.to_owned(), + id, + schema: schema.clone(), + } + } +} + +#[derive(Debug, Clone)] +pub struct IndexInfo { + pub key_schema: Schema, + pub name: String, + pub id: i32, + pub table_name: String, + pub key_size: usize, + pub is_primary_key: bool, +} diff --git a/src/lib.rs b/src/lib.rs index 5369881..cf94281 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod catalog; pub mod error; pub mod parser; pub mod types; diff --git a/src/types/mod.rs b/src/types/mod.rs index fc45e21..d6fd30e 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1 +1,19 @@ pub mod value; + +#[derive(Debug, Clone, PartialEq)] +pub enum LogicalType { + Null, + Bool, + Int8, + Int16, + Int32, + Int64, + UInt8, + UInt16, + UInt32, + UInt64, + Float32, + Float64, + String, + VarChar(usize), +}