Make Table struct fields private
This commit is contained in:
parent
0996d0dbe1
commit
966c9bf284
2 changed files with 25 additions and 13 deletions
|
|
@ -9,10 +9,10 @@ use crate::type_system::{IndexableValue, Uuid, Value};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Table {
|
pub struct Table {
|
||||||
pub schema: TableSchema,
|
schema: TableSchema,
|
||||||
pub rows: Rows, // TODO: Consider wrapping this in a lock. Also consider if we need to have the
|
rows: Rows, // TODO: Consider wrapping this in a lock. Also consider if we need to have the
|
||||||
// same lock for both rows and indexes
|
// same lock for both rows and indexes
|
||||||
pub indexes: HashMap<ColumnPosition, ColumnIndex>,
|
indexes: HashMap<ColumnPosition, ColumnIndex>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Rows = BTreeMap<Uuid, Row>;
|
pub type Rows = BTreeMap<Uuid, Row>;
|
||||||
|
|
@ -26,6 +26,18 @@ impl Table {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn schema(&self) -> &TableSchema {
|
||||||
|
&self.schema
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rows(&self) -> &Rows {
|
||||||
|
&self.rows
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn indexes(&self) -> &HashMap<ColumnPosition, ColumnIndex> {
|
||||||
|
&self.indexes
|
||||||
|
}
|
||||||
|
|
||||||
// ======Selection======
|
// ======Selection======
|
||||||
fn get_row_by_id(&self, id: Uuid) -> Option<Row> {
|
fn get_row_by_id(&self, id: Uuid) -> Option<Row> {
|
||||||
self.rows.get(&id).cloned()
|
self.rows.get(&id).cloned()
|
||||||
|
|
|
||||||
|
|
@ -80,14 +80,14 @@ impl State {
|
||||||
let table: &Table = self.table_from_name(&table_name)?;
|
let table: &Table = self.table_from_name(&table_name)?;
|
||||||
|
|
||||||
let selected_column_positions: Vec<ColumnPosition> = table
|
let selected_column_positions: Vec<ColumnPosition> = table
|
||||||
.schema
|
.schema()
|
||||||
.column_positions_from_column_selection(&column_selection)?;
|
.column_positions_from_column_selection(&column_selection)?;
|
||||||
let selected_rows = match maybe_condition {
|
let selected_rows = match maybe_condition {
|
||||||
None => table.select_all_rows(&selected_column_positions),
|
None => table.select_all_rows(&selected_column_positions),
|
||||||
|
|
||||||
Some(Condition::Eq(eq_column_name, value)) => {
|
Some(Condition::Eq(eq_column_name, value)) => {
|
||||||
let eq_column_position = table
|
let eq_column_position = table
|
||||||
.schema
|
.schema()
|
||||||
.column_position_from_column_name(&eq_column_name)?;
|
.column_position_from_column_name(&eq_column_name)?;
|
||||||
table.select_rows_where_eq(
|
table.select_rows_where_eq(
|
||||||
&selected_column_positions,
|
&selected_column_positions,
|
||||||
|
|
@ -102,7 +102,7 @@ impl State {
|
||||||
Insert(table_name, values) => {
|
Insert(table_name, values) => {
|
||||||
let table: &mut Table = self.table_from_name_mut(&table_name)?;
|
let table: &mut Table = self.table_from_name_mut(&table_name)?;
|
||||||
|
|
||||||
let (id, row) = table.schema.row_from_insertion_values(values)?;
|
let (id, row) = table.schema().row_from_insertion_values(values)?;
|
||||||
table.insert_row_at(id, row)?;
|
table.insert_row_at(id, row)?;
|
||||||
Ok(Response::Inserted)
|
Ok(Response::Inserted)
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +113,7 @@ impl State {
|
||||||
None => table.delete_all_rows(),
|
None => table.delete_all_rows(),
|
||||||
Some(Condition::Eq(eq_column_name, value)) => {
|
Some(Condition::Eq(eq_column_name, value)) => {
|
||||||
let eq_column_position = table
|
let eq_column_position = table
|
||||||
.schema
|
.schema()
|
||||||
.column_position_from_column_name(&eq_column_name)?;
|
.column_position_from_column_name(&eq_column_name)?;
|
||||||
table.delete_rows_where_eq(eq_column_position, value)?
|
table.delete_rows_where_eq(eq_column_position, value)?
|
||||||
}
|
}
|
||||||
|
|
@ -130,7 +130,7 @@ impl State {
|
||||||
CreateIndex(table_name, column_name) => {
|
CreateIndex(table_name, column_name) => {
|
||||||
let table: &mut Table = self.table_from_name_mut(&table_name)?;
|
let table: &mut Table = self.table_from_name_mut(&table_name)?;
|
||||||
let column_position: ColumnPosition = table
|
let column_position: ColumnPosition = table
|
||||||
.schema
|
.schema()
|
||||||
.column_position_from_column_name(&column_name)?;
|
.column_position_from_column_name(&column_name)?;
|
||||||
|
|
||||||
table.attach_index(column_position)?;
|
table.attach_index(column_position)?;
|
||||||
|
|
@ -180,9 +180,9 @@ mod tests {
|
||||||
|
|
||||||
assert!(state.tables.len() == 1);
|
assert!(state.tables.len() == 1);
|
||||||
let table = &state.tables[0];
|
let table = &state.tables[0];
|
||||||
assert!(table.rows.len() == 0);
|
assert!(table.rows().len() == 0);
|
||||||
|
|
||||||
assert!(table.schema.table_name == users);
|
assert!(table.schema().table_name == users);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -497,12 +497,12 @@ mod tests {
|
||||||
|
|
||||||
assert!(state.tables.len() == 1);
|
assert!(state.tables.len() == 1);
|
||||||
let table = &state.tables[0];
|
let table = &state.tables[0];
|
||||||
assert!(table.rows.len() == 2);
|
assert!(table.rows().len() == 2);
|
||||||
|
|
||||||
let user: ColumnPosition = 1;
|
let user: ColumnPosition = 1;
|
||||||
assert!(table.indexes.contains_key(&user));
|
assert!(table.indexes().contains_key(&user));
|
||||||
|
|
||||||
let index = table.indexes.get(&user).unwrap();
|
let index = table.indexes().get(&user).unwrap();
|
||||||
|
|
||||||
let plato_id = 0;
|
let plato_id = 0;
|
||||||
let aristotle_id = 1;
|
let aristotle_id = 1;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue