Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add catalog #52

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>>,
}
Comment on lines +28 to +34
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>>,
}
pub struct CataLog {
id2table: HashMap<i32, Arc<TableInfo>>,
name2id: HashMap<String, i32>,
next_table_id: AtomicI32,
id2index: HashMap<i32, IndexInfo>,
index2name: HashMap<String, HashMap<String, i32>>,
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样做的意义是?


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),
}
Loading