diff --git a/minisql/src/internals/column_index.rs b/minisql/src/internals/column_index.rs index c9184c3..f02e8ce 100644 --- a/minisql/src/internals/column_index.rs +++ b/minisql/src/internals/column_index.rs @@ -1,9 +1,5 @@ use std::collections::{BTreeMap, HashSet}; -use crate::type_system::{UUID, Value, IndexableValue}; -use crate::internals::schema::{ColumnName, DbResult}; -use crate::internals::table::Table; -use crate::internals::row::ColumnPosition; -use crate::error::Error; +use crate::type_system::{UUID, IndexableValue}; #[derive(Debug)] pub struct ColumnIndex { @@ -34,29 +30,6 @@ impl ColumnIndex { } } - - // TODO: IS THIS THE RIGHT PLACE? - // Should be used in the case when an indexed is created after the table has existed for a - // while. In such a case you need to build the index from the already existing rows. - pub fn update_from_table(&mut self, table: &Table, column_position: ColumnPosition) -> DbResult<()> { - for (id, row) in &table.rows { - let value = match row.get(column_position) { - Some(Value::Indexable(value)) => { - value.clone() - }, - Some(_) => { - let column_name: ColumnName = table.schema.column_name_from_column_position(column_position)?; - return Err(Error::AttemptToIndexNonIndexableColumn(table.schema.table_name.to_string(), column_name)) - }, - None => { - return Err(Error::ColumnPositionDoesNotExist(table.schema.table_name.to_string(), column_position)) - } - }; - self.add(value, *id) - } - Ok(()) - } - pub fn remove(&mut self, value: &IndexableValue, id_to_be_removed: UUID) -> bool { match self.index.get_mut(value) { Some(ids) => { diff --git a/minisql/src/interpreter.rs b/minisql/src/interpreter.rs index 4e4ce35..34d6b14 100644 --- a/minisql/src/interpreter.rs +++ b/minisql/src/interpreter.rs @@ -120,7 +120,7 @@ impl State { let column_position: ColumnPosition = table.schema.column_position_from_column_name(&column_name)?; let mut index: ColumnIndex = ColumnIndex::new(); - let _ = index.update_from_table(&table, column_position)?; + let _ = update_index_from_table(&mut index, &table, column_position)?; table.attach_index(column_position, index); Ok(Response::IndexCreated) @@ -129,6 +129,27 @@ impl State { } } +// Should be used in the case when an indexed is created after the table has existed for a +// while. In such a case you need to build the index from the already existing rows. +fn update_index_from_table(column_index: &mut ColumnIndex, table: &Table, column_position: ColumnPosition) -> DbResult<()> { + for (id, row) in &table.rows { + let value = match row.get(column_position) { + Some(Value::Indexable(value)) => { + value.clone() + }, + Some(_) => { + let column_name: ColumnName = table.schema.column_name_from_column_position(column_position)?; + return Err(Error::AttemptToIndexNonIndexableColumn(table.schema.table_name.to_string(), column_name)) + }, + None => { + return Err(Error::ColumnPositionDoesNotExist(table.schema.table_name.to_string(), column_position)) + } + }; + column_index.add(value, *id) + } + Ok(()) +} + // TODO: Give a better name to something that you can respond to with rows trait SqlResponseConsumer { // TODO: