-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TableInfo>; | ||
pub type IndexInfoRef = Arc<IndexInfo>; | ||
|
||
#[derive(Debug)] | ||
pub struct CataLog { | ||
id_table_map: HashMap<i32, Arc<TableInfo>>, | ||
name_id_map: HashMap<String, i32>, | ||
next_table_id: AtomicI32, | ||
_id_index_map: HashMap<i32, IndexInfo>, | ||
index_names_map: HashMap<String, HashMap<String, i32>>, | ||
} | ||
|
||
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<TableInfoRef> { | ||
// 表不能已经存在 | ||
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<TableInfoRef> { | ||
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<TableInfoRef> { | ||
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<IndexInfoRef> { | ||
unimplemented!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::sync::Arc; | ||
|
||
use super::column::Column; | ||
|
||
#[derive(Debug, Clone, PartialEq, Default)] | ||
pub struct Schema { | ||
pub columns: Vec<Column>, | ||
} | ||
|
||
pub type SchemaRef = Arc<Schema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod catalog; | ||
pub mod error; | ||
pub mod parser; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
} |