Move index update from table logic to interpreter
This commit is contained in:
parent
e9d3df7a22
commit
d87c95f1e1
2 changed files with 23 additions and 29 deletions
|
|
@ -1,9 +1,5 @@
|
||||||
use std::collections::{BTreeMap, HashSet};
|
use std::collections::{BTreeMap, HashSet};
|
||||||
use crate::type_system::{UUID, Value, IndexableValue};
|
use crate::type_system::{UUID, IndexableValue};
|
||||||
use crate::internals::schema::{ColumnName, DbResult};
|
|
||||||
use crate::internals::table::Table;
|
|
||||||
use crate::internals::row::ColumnPosition;
|
|
||||||
use crate::error::Error;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ColumnIndex {
|
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 {
|
pub fn remove(&mut self, value: &IndexableValue, id_to_be_removed: UUID) -> bool {
|
||||||
match self.index.get_mut(value) {
|
match self.index.get_mut(value) {
|
||||||
Some(ids) => {
|
Some(ids) => {
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ impl State {
|
||||||
let column_position: ColumnPosition = table.schema.column_position_from_column_name(&column_name)?;
|
let column_position: ColumnPosition = table.schema.column_position_from_column_name(&column_name)?;
|
||||||
|
|
||||||
let mut index: ColumnIndex = ColumnIndex::new();
|
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);
|
table.attach_index(column_position, index);
|
||||||
Ok(Response::IndexCreated)
|
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
|
// TODO: Give a better name to something that you can respond to with rows
|
||||||
trait SqlResponseConsumer {
|
trait SqlResponseConsumer {
|
||||||
// TODO:
|
// TODO:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue