Skip to content

Commit

Permalink
feat: add catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
fansehep committed Feb 1, 2024
1 parent 8e63752 commit 193dafe
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/catalog/column.rs
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(),
}
}
}
97 changes: 97 additions & 0 deletions src/catalog/mod.rs
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!()
}
}
10 changes: 10 additions & 0 deletions src/catalog/schema.rs
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>;
28 changes: 28 additions & 0 deletions src/catalog/tableinfo.rs
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,
}
1 change: 1 addition & 0 deletions src/lib.rs
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;
18 changes: 18 additions & 0 deletions src/types/mod.rs
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),
}

0 comments on commit 193dafe

Please sign in to comment.