diff --git a/minisql/src/interpreter2.rs b/minisql/src/interpreter2.rs index f09c4e8..2dfb716 100644 --- a/minisql/src/interpreter2.rs +++ b/minisql/src/interpreter2.rs @@ -9,7 +9,8 @@ use crate::internals::row::Row; use bimap::BiMap; use std::path::{Path, PathBuf}; -use tokio::sync::RwLock; +use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; +use std::rc::Rc; use tokio::fs; use storage_engine::store::Store; @@ -21,24 +22,24 @@ use storage_engine::cursor_capabilities::index_access::CursorCanReadIndex; #[derive(Debug)] pub struct State { table_name_position_mapping: BiMap, + table_schemas: Vec>, tables: Tables, } - pub struct StateHandler { db_path: PathBuf, state: RwLock, } -pub type Tables = Vec; +pub type Tables = Vec>; #[derive(Debug)] pub struct Table { - schema: TableSchema, + schema: Rc, store: Store } -pub type DbSchema<'a> = Vec<(TableName, TablePosition, &'a TableSchema)>; +pub type DbSchema = Vec<(TableName, TablePosition, Rc)>; // To satisfy clippy. impl Default for State { fn default() -> Self { @@ -56,7 +57,7 @@ impl Table { let store: Store = Store::new(&path_to_table_folder, number_of_columns, primary_column).await.unwrap(); let table = Self { - schema: table_schema, + schema: Rc::new(table_schema), store, }; Ok(table) @@ -72,8 +73,8 @@ impl Table { Ok(cursor) } - pub fn schema(&self) -> &TableSchema { - &self.schema + pub fn schema(&self) -> Rc { + self.schema.clone() } pub fn table_name(&self) -> &TableName { @@ -85,6 +86,7 @@ impl State { pub fn new() -> Self { Self { table_name_position_mapping: BiMap::new(), + table_schemas: vec![], tables: vec![], } } @@ -92,18 +94,18 @@ impl State { pub fn db_schema(&self) -> DbSchema { let mut schema: DbSchema = Vec::new(); for (table_name, &table_position) in &self.table_name_position_mapping { - let table_schema: &TableSchema = self.tables[table_position].schema(); + let table_schema: Rc = self.table_schemas[table_position].clone(); schema.push((table_name.clone(), table_position, table_schema)); } schema } - fn table_at(&self, table_position: TablePosition) -> &Table { - &self.tables[table_position] + async fn table_at(&self, table_position: TablePosition) -> RwLockReadGuard
{ + self.tables[table_position].read().await } - fn table_at_mut(&mut self, table_position: TablePosition) -> &mut Table { - &mut self.tables[table_position] + async fn table_at_mut(&self, table_position: TablePosition) -> RwLockWriteGuard
{ + self.tables[table_position].write().await } async fn attach_table(&mut self, table: Table) { @@ -111,7 +113,8 @@ impl State { let new_table_position: TablePosition = self.tables.len(); self.table_name_position_mapping .insert(table.schema().table_name().clone(), new_table_position); - self.tables.push(table); + self.table_schemas.push(table.schema()); + self.tables.push(RwLock::new(table)); } async fn select_all_rows(table: &Table, mut cursor: ReadCursor<'_, Value>, response_writer: &mut Writer, column_selection: ColumnSelection) -> DbResult { @@ -138,41 +141,41 @@ impl State { Ok(count) } - pub async fn interpret(&mut self, db_path: &Path, response_writer: &mut Writer, operation: Operation) -> DbResult<()> { - use Operation::*; + // pub async fn interpret(&mut self, db_path: &Path, response_writer: &mut Writer, operation: Operation) -> DbResult<()> { + // use Operation::*; - match operation { - Select(table_position, column_selection, maybe_condition) => { - let table: &Table = self.table_at(table_position); - let cursor = table.read().await?; + // match operation { + // Select(table_position, column_selection, maybe_condition) => { + // let table: &Table = self.table_at(table_position); + // let cursor = table.read().await?; - response_writer.write_table_header(&table.schema, &column_selection).await.map_err(|e| RuntimeError::AnyhowError(e))?; - let count = match maybe_condition { - None => Self::select_all_rows(&table, cursor, response_writer, column_selection).await?, - Some(Condition::Eq(eq_column, value)) => Self::select_eq(&table, cursor, response_writer, column_selection, eq_column, value).await? - }; - response_writer.write_command_complete(CompleteStatus::Select(count)).await.map_err(|e| RuntimeError::AnyhowError(e)) - } - Insert(table_position, values) => { - let table: &mut Table = self.table_at_mut(table_position); - let cursor = table.write().await?; - todo!() - } - Delete(table_position, maybe_condition) => { - let table: &mut Table = self.table_at_mut(table_position); - let cursor = table.write().await?; - todo!() - } - CreateTable(table_schema) => { - let table = Table::new(table_schema, Path::new(db_path)).await?; - self.attach_table(table).await; - response_writer.write_command_complete(CompleteStatus::CreateTable).await.map_err(|e| RuntimeError::AnyhowError(e)) - } - CreateIndex(table_position, column) => { - todo!() - } - } - } + // response_writer.write_table_header(&table.schema, &column_selection).await.map_err(|e| RuntimeError::AnyhowError(e))?; + // let count = match maybe_condition { + // None => Self::select_all_rows(&table, cursor, response_writer, column_selection).await?, + // Some(Condition::Eq(eq_column, value)) => Self::select_eq(&table, cursor, response_writer, column_selection, eq_column, value).await? + // }; + // response_writer.write_command_complete(CompleteStatus::Select(count)).await.map_err(|e| RuntimeError::AnyhowError(e)) + // } + // Insert(table_position, values) => { + // let table: &mut Table = self.table_at_mut(table_position); + // let cursor = table.write().await?; + // todo!() + // } + // Delete(table_position, maybe_condition) => { + // let table: &mut Table = self.table_at_mut(table_position); + // let cursor = table.write().await?; + // todo!() + // } + // CreateTable(table_schema) => { + // let table = Table::new(table_schema, Path::new(db_path)).await?; + // self.attach_table(table).await; + // response_writer.write_command_complete(CompleteStatus::CreateTable).await.map_err(|e| RuntimeError::AnyhowError(e)) + // } + // CreateIndex(table_position, column) => { + // todo!() + // } + // } + // } } impl StateHandler { @@ -197,7 +200,7 @@ impl StateHandler { Select(table_position, column_selection, maybe_condition) => { let state = self.state.read().await; - let table: &Table = state.table_at(table_position); + let table = state.table_at(table_position).await; let cursor = table.read().await?; response_writer.write_table_header(&table.schema, &column_selection).await.map_err(|e| RuntimeError::AnyhowError(e))?;