select all

This commit is contained in:
Yuriy Dupyn 2024-02-05 18:41:40 +01:00
parent 84a880f9e6
commit c7166bd12e
8 changed files with 59 additions and 15 deletions

1
Cargo.lock generated
View file

@ -286,6 +286,7 @@ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
"bimap", "bimap",
"bincode",
"proto", "proto",
"serde", "serde",
"storage_engine", "storage_engine",

View file

@ -10,6 +10,7 @@ rust-version = "1.74"
anyhow = "1.0.79" anyhow = "1.0.79"
async-trait = "0.1.77" async-trait = "0.1.77"
bimap = { version = "0.6.3", features = ["serde"] } bimap = { version = "0.6.3", features = ["serde"] }
bincode = "2.0.0-rc.3"
serde = { version = "1.0.196", features = ["derive"] } serde = { version = "1.0.196", features = ["derive"] }
tokio = { version = "1.34.0", features = ["full"] } tokio = { version = "1.34.0", features = ["full"] }
thiserror = "1.0.50" thiserror = "1.0.50"

View file

@ -14,7 +14,9 @@ pub enum RuntimeError {
#[error("table {0} already indexes column {1}")] #[error("table {0} already indexes column {1}")]
AttemptToIndexAlreadyIndexedColumn(TableName, ColumnName), AttemptToIndexAlreadyIndexedColumn(TableName, ColumnName),
#[error("Storage Engine error for table {0}: {1}")] #[error("Storage Engine error for table {0}: {1}")]
StorageEngineError(TableName, storage_engine::error::Error) StorageEngineError(TableName, storage_engine::error::Error),
#[error("runtime anyhow error: {0}")]
AnyhowError(anyhow::Error)
} }
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -28,3 +30,10 @@ pub enum TypeConversionError {
#[error("unknown type with oid {oid} and size {size}")] #[error("unknown type with oid {oid} and size {size}")]
UnknownType { oid: PgOid, size: i16 }, UnknownType { oid: PgOid, size: i16 },
} }
impl From<anyhow::Error> for RuntimeError {
fn from(e: anyhow::Error) -> RuntimeError {
Self::AnyhowError(e)
}
}

View file

@ -5,6 +5,7 @@ use crate::type_system::Value;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::slice::SliceIndex; use std::slice::SliceIndex;
use storage_engine::segments::entry::EntryDetailed;
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Row(Vec<Value>); pub struct Row(Vec<Value>);
@ -39,6 +40,12 @@ impl FromIterator<Value> for Row {
} }
} }
impl From<EntryDetailed<Value>> for Row {
fn from(entry: EntryDetailed<Value>) -> Self {
Row(entry.data)
}
}
// To satisfy clippy. // To satisfy clippy.
impl Default for Row { impl Default for Row {
fn default() -> Self { fn default() -> Self {

View file

@ -5,11 +5,13 @@ use crate::schema::{Column, TableName, TablePosition, TableSchema};
use crate::type_system::Value; use crate::type_system::Value;
use crate::error::RuntimeError; use crate::error::RuntimeError;
use crate::response_writer::ResponseWriter; use crate::response_writer::ResponseWriter;
use crate::internals::row::Row;
use bimap::BiMap; use bimap::BiMap;
use storage_engine::store::Store; use storage_engine::store::Store;
use storage_engine::cursor::{ReadCursor, WriteCursor}; use storage_engine::cursor::{ReadCursor, WriteCursor};
use storage_engine::cursor_capabilities::traversal::CursorCanTraverse;
// ==============Interpreter================ // ==============Interpreter================
#[derive(Debug)] #[derive(Debug)]
@ -36,12 +38,12 @@ impl Default for State {
impl Table { impl Table {
async fn read(&self) -> DbResult<ReadCursor<Value>> { async fn read(&self) -> DbResult<ReadCursor<Value>> {
let cursor = self.store.read_cursor().await.map_err(|err| RuntimeError::StorageEngineError(self.schema.table_name().to_string(), err))?; let cursor = self.store.read_cursor().await.map_err(|e| RuntimeError::StorageEngineError(self.schema.table_name().to_string(), e))?;
Ok(cursor) Ok(cursor)
} }
async fn write(&mut self) -> DbResult<WriteCursor<Value>> { async fn write(&mut self) -> DbResult<WriteCursor<Value>> {
let cursor = self.store.write_cursor().await.map_err(|err| RuntimeError::StorageEngineError(self.schema.table_name().to_string(), err))?; let cursor = self.store.write_cursor().await.map_err(|e| RuntimeError::StorageEngineError(self.schema.table_name().to_string(), e))?;
Ok(cursor) Ok(cursor)
} }
} }
@ -73,6 +75,15 @@ impl State {
&mut self.tables[table_position] &mut self.tables[table_position]
} }
async fn select_all_rows<Writer: ResponseWriter>(table: &Table, mut cursor: ReadCursor<'_, Value>, response_writer: &mut Writer, column_selection: ColumnSelection) -> DbResult<()> {
while let Some(entry) = cursor.next_alive().await.map_err(|e| RuntimeError::StorageEngineError(table.schema.table_name().to_string(), e))? {
let row: Row = From::from(entry);
let restricted_row = row.restrict_columns(&column_selection);
response_writer.write_table_row(&restricted_row).await.map_err(|e| RuntimeError::AnyhowError(e))?;
}
Ok(())
}
pub async fn interpret<Writer: ResponseWriter>(&mut self, response_writer: &mut Writer, operation: Operation) -> DbResult<()> { pub async fn interpret<Writer: ResponseWriter>(&mut self, response_writer: &mut Writer, operation: Operation) -> DbResult<()> {
use Operation::*; use Operation::*;
@ -81,18 +92,16 @@ impl State {
let table: &Table = self.table_at(table_position); let table: &Table = self.table_at(table_position);
let cursor = table.read().await?; let cursor = table.read().await?;
let selected_rows = match maybe_condition { response_writer.write_table_header(&table.schema, &column_selection).await.map_err(|e| RuntimeError::AnyhowError(e))?;
match maybe_condition {
None => { None => {
// select all rows Self::select_all_rows(&table, cursor, response_writer, column_selection).await
todo!()
} }
Some(Condition::Eq(eq_column, value)) => { Some(Condition::Eq(eq_column, value)) => {
todo!() todo!()
} }
}; }
todo!()
} }
Insert(table_position, values) => { Insert(table_position, values) => {
let table: &mut Table = self.table_at_mut(table_position); let table: &mut Table = self.table_at_mut(table_position);

View file

@ -1,7 +1,9 @@
use crate::schema::Column; use crate::schema::{Column, TableSchema};
use crate::type_system::Value; use crate::type_system::Value;
use crate::operation::ColumnSelection;
use std::ops::Index; use std::ops::Index;
use std::slice::SliceIndex; use std::slice::SliceIndex;
use storage_engine::segments::entry::EntryDetailed;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RestrictedRow(Vec<(Column, Value)>); pub struct RestrictedRow(Vec<(Column, Value)>);

View file

@ -2,9 +2,12 @@ use crate::error::TypeConversionError;
use proto::message::primitive::pgoid::PgOid; use proto::message::primitive::pgoid::PgOid;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::cmp::Ordering; use std::cmp::Ordering;
// TODO: Private???
// use bincode::{Encode, Encoder, EncodeError, Decode, Decoder, DecodeError};
use bincode::{Encode, Decode};
// ==============Types================ // ==============Types================
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
pub enum DbType { pub enum DbType {
String, String,
Int, Int,
@ -15,7 +18,7 @@ pub enum DbType {
// ==============Values================ // ==============Values================
pub type Uuid = u64; pub type Uuid = u64;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode)]
#[serde(try_from = "String", into = "String")] #[serde(try_from = "String", into = "String")]
pub enum Value { pub enum Value {
Number(f64), Number(f64),
@ -81,6 +84,18 @@ impl Ord for IndexableValue {
} }
} }
// impl Encode for Value {
// fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
// todo!()
// }
// }
// impl Decode for Value {
// fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
// todo!()
// }
// }
impl DbType { impl DbType {
fn new_n_option(n: usize, inside: DbType) -> DbType { fn new_n_option(n: usize, inside: DbType) -> DbType {
if n == 0 { if n == 0 {

View file

@ -1,4 +1,4 @@
pub(crate) mod primitive; pub(crate) mod primitive;
pub(crate) mod traversal; pub mod traversal;
pub(crate) mod entry_modification; pub mod entry_modification;
pub(crate) mod index_access; pub mod index_access;