Further refine the interpreter

This commit is contained in:
Yuriy Dupyn 2023-10-30 22:43:27 +01:00
parent 972f849703
commit cb7b50109e

View file

@ -9,7 +9,6 @@ enum Operation {
Insert(TableName, InsertionValues), Insert(TableName, InsertionValues),
Delete(TableName, Option<Condition>), Delete(TableName, Option<Condition>),
// Update(...), // Update(...),
//
CreateTable(TableName, TableSchema), CreateTable(TableName, TableSchema),
CreateIndex(TableName, ColumnName), // TODO: Is this sufficient? CreateIndex(TableName, ColumnName), // TODO: Is this sufficient?
// DropTable(TableName), // DropTable(TableName),
@ -51,6 +50,7 @@ enum DbValue {
Int(u64), Int(u64),
Number(f64), Number(f64),
UUID(UUID), UUID(UUID),
// TODO: what bout null?
} }
// TODO: Can this be autogenerated from the values? // TODO: Can this be autogenerated from the values?
@ -82,7 +82,8 @@ type TablePosition = u32;
struct Table { struct Table {
schema: TableSchema, schema: TableSchema,
rows: Rows, rows: Rows, // TODO: Consider wrapping this in a lock. Also consider if we need to have the
// same lock for both rows and indexes
indexes: indexes:
HashMap<ColumnPosition, ColumnIndex> // TODO: Consider generalizing `ColumnPosition` to something that would also apply to a pair of `ColumnNames` etc HashMap<ColumnPosition, ColumnIndex> // TODO: Consider generalizing `ColumnPosition` to something that would also apply to a pair of `ColumnNames` etc
} }
@ -115,8 +116,13 @@ type ColumnPosition = u32;
type Row = Vec<DbValue>; type Row = Vec<DbValue>;
type Rows = type Rows =
// TODO: This should be some sort of an interface to a dictionary
// s.t. in the background it may modify stuff in memory or talk to the disk
BTreeMap<UUID, Row>; BTreeMap<UUID, Row>;
// interface
// insert(id, value)
// ==============Interpreter================ // ==============Interpreter================
struct State { struct State {
table_positions: HashMap<TableName, TablePosition>, table_positions: HashMap<TableName, TablePosition>,
@ -200,14 +206,44 @@ impl Table {
} }
fn select_where(&self, column_selection: ColumnSelection, maybe_condition: Option<Condition>, consumer: impl SqlConsumer) { fn select_where(&self, column_selection: ColumnSelection, maybe_condition: Option<Condition>, consumer: impl SqlConsumer) {
todo!() match maybe_condition {
None => {
// .iter() will give us an iterator over all the rows
// two choices
// 1. optimized version
// self.iter_with_columns(column_selection).for_each(|row| {
// consumer.send(row)
// });
// 2.
// self.iter()
// .map(|row| row.select_columns(column_selection))
// .for_each(|reduced_row| {
// consumer.send(row)
// });
todo!()
},
Some(Condition::Eq(column_name, value)) => {
// is column_name primary key? then it is easy
// self.get(id)
// is column_name indexed? Then get the index, and then it is not easy, because you
// may get a set of ids.
// what if it is not primary nor indexed? then you need to brute force your way
// through the whole table?
todo!()
}
}
} }
fn insert(&mut self, values: InsertionValues, consumer: impl SqlConsumer) { fn insert(&mut self, values: InsertionValues, consumer: impl SqlConsumer) {
// 1. You need to update indices
// 2. you simply insert the data
todo!() todo!()
} }
fn delete_where(&mut self, maybe_condition: Option<Condition>, consumer: impl SqlConsumer) { fn delete_where(&mut self, maybe_condition: Option<Condition>, consumer: impl SqlConsumer) {
// kinda similar to select with respect to the conditions
// update index
todo!() todo!()
} }
} }