Renaming
This commit is contained in:
parent
167028a530
commit
84a880f9e6
7 changed files with 133 additions and 77 deletions
|
|
@ -3,10 +3,13 @@ use crate::restricted_row::RestrictedRow;
|
||||||
use crate::result::DbResult;
|
use crate::result::DbResult;
|
||||||
use crate::schema::{Column, TableName, TablePosition, TableSchema};
|
use crate::schema::{Column, TableName, TablePosition, TableSchema};
|
||||||
use crate::type_system::Value;
|
use crate::type_system::Value;
|
||||||
use bimap::BiMap;
|
use crate::error::RuntimeError;
|
||||||
use serde::{Deserialize, Serialize};
|
use crate::response_writer::ResponseWriter;
|
||||||
|
|
||||||
use storage_engine::store::{Store};
|
use bimap::BiMap;
|
||||||
|
|
||||||
|
use storage_engine::store::Store;
|
||||||
|
use storage_engine::cursor::{ReadCursor, WriteCursor};
|
||||||
|
|
||||||
// ==============Interpreter================
|
// ==============Interpreter================
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -31,6 +34,18 @@ impl Default for State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Table {
|
||||||
|
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))?;
|
||||||
|
Ok(cursor)
|
||||||
|
}
|
||||||
|
|
||||||
|
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))?;
|
||||||
|
Ok(cursor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
@ -50,17 +65,43 @@ impl State {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn interpret(&mut self, operation: Operation) -> DbResult<()> {
|
fn table_at(&self, table_position: TablePosition) -> &Table {
|
||||||
|
&self.tables[table_position]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn table_at_mut(&mut self, table_position: TablePosition) -> &mut Table {
|
||||||
|
&mut self.tables[table_position]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn interpret<Writer: ResponseWriter>(&mut self, response_writer: &mut Writer, operation: Operation) -> DbResult<()> {
|
||||||
use Operation::*;
|
use Operation::*;
|
||||||
|
|
||||||
match operation {
|
match operation {
|
||||||
Select(table_position, column_selection, maybe_condition) => {
|
Select(table_position, column_selection, maybe_condition) => {
|
||||||
|
let table: &Table = self.table_at(table_position);
|
||||||
|
let cursor = table.read().await?;
|
||||||
|
|
||||||
|
let selected_rows = match maybe_condition {
|
||||||
|
None => {
|
||||||
|
// select all rows
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
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 cursor = table.write().await?;
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
Delete(table_position, maybe_condition) => {
|
Delete(table_position, maybe_condition) => {
|
||||||
|
let table: &mut Table = self.table_at_mut(table_position);
|
||||||
|
let cursor = table.write().await?;
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
CreateTable(table_schema) => {
|
CreateTable(table_schema) => {
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,8 @@ use crate::segments::store_header::StoreHeader;
|
||||||
use crate::store::{Store, FilePosition, Column, Result, ROWS_FILE_NAME, GARBAGE_COLLECTION_INTERMEDIATE_ROWS_FILE_NAME};
|
use crate::store::{Store, FilePosition, Column, Result, ROWS_FILE_NAME, GARBAGE_COLLECTION_INTERMEDIATE_ROWS_FILE_NAME};
|
||||||
use crate::index::Index;
|
use crate::index::Index;
|
||||||
use crate::cursor_capabilities::primitive::{CursorCanRead, CursorCanWrite};
|
use crate::cursor_capabilities::primitive::{CursorCanRead, CursorCanWrite};
|
||||||
use crate::cursor_capabilities::header_access::{CursorCanReadHeader, CursorCanWriteHeader};
|
use crate::cursor_capabilities::traversal::CursorCanTraverse;
|
||||||
|
use crate::cursor_capabilities::entry_modification::CursorCanModifyEntries;
|
||||||
use crate::cursor_capabilities::index_access::{CursorCanWriteToIndex, CursorCanReadIndex};
|
use crate::cursor_capabilities::index_access::{CursorCanWriteToIndex, CursorCanReadIndex};
|
||||||
|
|
||||||
const GARBAGE_COLLECTION_TRIGGER: usize = 100;
|
const GARBAGE_COLLECTION_TRIGGER: usize = 100;
|
||||||
|
|
@ -82,24 +83,24 @@ impl <T>CursorCanWrite<T> for AppendOnlyCursor<T> {}
|
||||||
|
|
||||||
|
|
||||||
// ===capability to access header===
|
// ===capability to access header===
|
||||||
impl <T>CursorCanReadHeader<T> for ReadCursor<'_, T> {
|
impl <T>CursorCanTraverse<T> for ReadCursor<'_, T> {
|
||||||
fn header(&self) -> &StoreHeader { &self.header }
|
fn header(&self) -> &StoreHeader { &self.header }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>CursorCanReadHeader<T> for WriteCursor<'_, T> {
|
impl <T>CursorCanTraverse<T> for WriteCursor<'_, T> {
|
||||||
fn header(&self) -> &StoreHeader { &self.header }
|
fn header(&self) -> &StoreHeader { &self.header }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>CursorCanReadHeader<T> for AppendOnlyCursor<T> {
|
impl <T>CursorCanTraverse<T> for AppendOnlyCursor<T> {
|
||||||
fn header(&self) -> &StoreHeader { &self.header }
|
fn header(&self) -> &StoreHeader { &self.header }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>CursorCanWriteHeader<T> for WriteCursor<'_, T> {
|
impl <T>CursorCanModifyEntries<T> for WriteCursor<'_, T> {
|
||||||
fn header_mut(&mut self) -> &mut StoreHeader { self.header }
|
fn header_mut(&mut self) -> &mut StoreHeader { self.header }
|
||||||
fn set_eof_file_position(&mut self, new_file_position: FilePosition) { self.eof_file_position = new_file_position }
|
fn set_eof_file_position(&mut self, new_file_position: FilePosition) { self.eof_file_position = new_file_position }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T>CursorCanWriteHeader<T> for AppendOnlyCursor<T> {
|
impl <T>CursorCanModifyEntries<T> for AppendOnlyCursor<T> {
|
||||||
fn header_mut(&mut self) -> &mut StoreHeader { &mut self.header }
|
fn header_mut(&mut self) -> &mut StoreHeader { &mut self.header }
|
||||||
fn set_eof_file_position(&mut self, new_file_position: FilePosition) { self.eof_file_position = new_file_position }
|
fn set_eof_file_position(&mut self, new_file_position: FilePosition) { self.eof_file_position = new_file_position }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
67
storage_engine/src/cursor_capabilities/entry_modification.rs
Normal file
67
storage_engine/src/cursor_capabilities/entry_modification.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
use bincode;
|
||||||
|
use bincode::Encode;
|
||||||
|
use crate::binary_coding::encode;
|
||||||
|
|
||||||
|
use crate::segments::entry::Entry;
|
||||||
|
use crate::segments::store_header::StoreHeader;
|
||||||
|
use crate::store::{FilePosition, Result};
|
||||||
|
use crate::cursor_capabilities::primitive::{CursorCanRead, CursorCanWrite};
|
||||||
|
use crate::cursor_capabilities::traversal::CursorCanTraverse;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait CursorCanModifyEntries<T>: CursorCanTraverse<T> + CursorCanWrite<T> {
|
||||||
|
fn header_mut(&mut self) -> &mut StoreHeader;
|
||||||
|
fn set_eof_file_position(&mut self, new_file_position: FilePosition);
|
||||||
|
|
||||||
|
// ===Store Header Manipulation===
|
||||||
|
async fn increment_total_count(&mut self) -> Result<()>
|
||||||
|
where T: Send
|
||||||
|
{
|
||||||
|
self.seek_to_start().await?;
|
||||||
|
self.seek_to(StoreHeader::TOTAL_COUNT_OFFSET as u64).await?;
|
||||||
|
let new_count = self.header_mut().increment_total_count();
|
||||||
|
self.write_bytes(&encode::<usize>(&new_count)?).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn increment_deleted_count(&mut self) -> Result<()>
|
||||||
|
where T: Send
|
||||||
|
{
|
||||||
|
self.seek_to_start().await?;
|
||||||
|
self.seek_to(StoreHeader::DELETED_COUNT_OFFSET as u64).await?;
|
||||||
|
let new_count = self.header_mut().increment_deleted_count();
|
||||||
|
self.write_bytes(&encode::<usize>(&new_count)?).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn set_header(&mut self, header: &StoreHeader) -> Result<()>
|
||||||
|
where T: Send
|
||||||
|
{
|
||||||
|
self.seek_to_start().await?;
|
||||||
|
let encoded_header: Vec<u8> = header.encode()?;
|
||||||
|
self.write_bytes(&encoded_header).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===Append Entry===
|
||||||
|
|
||||||
|
// Moves cursor to the end.
|
||||||
|
// Returns file position to the start of the new entry.
|
||||||
|
async fn append_entry_no_indexing(&mut self, entry: &Entry<T>) -> Result<FilePosition>
|
||||||
|
where T: Encode + Send + Sync
|
||||||
|
{
|
||||||
|
self.increment_total_count().await?;
|
||||||
|
|
||||||
|
let encoded_entry: Vec<u8> = entry.encode()?;
|
||||||
|
let file_position = self.seek_to_end().await?;
|
||||||
|
self.write_bytes(&encoded_entry).await?;
|
||||||
|
|
||||||
|
let eof_file_position: FilePosition = self.current_file_position().await?;
|
||||||
|
self.set_eof_file_position(eof_file_position);
|
||||||
|
|
||||||
|
Ok(file_position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,10 +9,11 @@ use crate::error::Error;
|
||||||
use crate::segments::entry::{Entry, EntryDetailed};
|
use crate::segments::entry::{Entry, EntryDetailed};
|
||||||
use crate::store::{FilePosition, Column, Result};
|
use crate::store::{FilePosition, Column, Result};
|
||||||
use crate::index::Index;
|
use crate::index::Index;
|
||||||
use crate::cursor_capabilities::header_access::{CursorCanReadHeader, CursorCanWriteHeader};
|
use crate::cursor_capabilities::traversal::CursorCanTraverse;
|
||||||
|
use crate::cursor_capabilities::entry_modification::CursorCanModifyEntries;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait CursorCanReadIndex<T>: CursorCanReadHeader<T> {
|
pub trait CursorCanReadIndex<T>: CursorCanTraverse<T> {
|
||||||
fn indexes(&mut self) -> &[Option<Index<T, FilePosition>>];
|
fn indexes(&mut self) -> &[Option<Index<T, FilePosition>>];
|
||||||
|
|
||||||
async fn index_lookup(&mut self, column: Column, value: &T) -> Result<Vec<EntryDetailed<T>>>
|
async fn index_lookup(&mut self, column: Column, value: &T) -> Result<Vec<EntryDetailed<T>>>
|
||||||
|
|
@ -52,7 +53,7 @@ pub trait CursorCanReadIndex<T>: CursorCanReadHeader<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait CursorCanWriteToIndex<T>: CursorCanReadIndex<T> + CursorCanWriteHeader<T> {
|
pub trait CursorCanWriteToIndex<T>: CursorCanReadIndex<T> + CursorCanModifyEntries<T> {
|
||||||
fn indexes_mut(&mut self) -> &mut [Option<Index<T, FilePosition>>];
|
fn indexes_mut(&mut self) -> &mut [Option<Index<T, FilePosition>>];
|
||||||
|
|
||||||
// Assumes that the column is indexable.
|
// Assumes that the column is indexable.
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
pub(crate) mod primitive;
|
pub(crate) mod primitive;
|
||||||
pub(crate) mod header_access;
|
pub(crate) mod traversal;
|
||||||
|
pub(crate) mod entry_modification;
|
||||||
pub(crate) mod index_access;
|
pub(crate) mod index_access;
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,19 @@ use tokio::io::AsyncReadExt;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
use bincode;
|
use bincode;
|
||||||
use bincode::{Decode, Encode};
|
use bincode::Decode;
|
||||||
use crate::binary_coding::{encode, decode};
|
use crate::binary_coding::decode;
|
||||||
|
|
||||||
use crate::error::{Error, DecodeErrorKind};
|
use crate::error::{Error, DecodeErrorKind};
|
||||||
use crate::segments::entry::{Entry, EntryDetailed};
|
use crate::segments::entry::EntryDetailed;
|
||||||
use crate::segments::entry_header::EntryHeaderWithDataSize;
|
use crate::segments::entry_header::EntryHeaderWithDataSize;
|
||||||
use crate::segments::store_header::StoreHeader;
|
use crate::segments::store_header::StoreHeader;
|
||||||
use crate::store::{FilePosition, Column, Result};
|
use crate::store::{FilePosition, Column, Result};
|
||||||
use crate::cursor_capabilities::primitive::{CursorCanRead, CursorCanWrite};
|
use crate::cursor_capabilities::primitive::CursorCanRead;
|
||||||
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait CursorCanReadHeader<T>: CursorCanRead<T> {
|
pub trait CursorCanTraverse<T>: CursorCanRead<T> {
|
||||||
fn header(&self) -> &StoreHeader;
|
fn header(&self) -> &StoreHeader;
|
||||||
|
|
||||||
async fn seek_to_start_of_data(&mut self) -> Result<FilePosition> {
|
async fn seek_to_start_of_data(&mut self) -> Result<FilePosition> {
|
||||||
|
|
@ -172,59 +173,3 @@ pub trait CursorCanReadHeader<T>: CursorCanRead<T> {
|
||||||
Ok(bytes)
|
Ok(bytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
pub trait CursorCanWriteHeader<T>: CursorCanReadHeader<T> + CursorCanWrite<T> {
|
|
||||||
fn header_mut(&mut self) -> &mut StoreHeader;
|
|
||||||
fn set_eof_file_position(&mut self, new_file_position: FilePosition);
|
|
||||||
|
|
||||||
// ===Store Header Manipulation===
|
|
||||||
async fn increment_total_count(&mut self) -> Result<()>
|
|
||||||
where T: Send
|
|
||||||
{
|
|
||||||
self.seek_to_start().await?;
|
|
||||||
self.seek_to(StoreHeader::TOTAL_COUNT_OFFSET as u64).await?;
|
|
||||||
let new_count = self.header_mut().increment_total_count();
|
|
||||||
self.write_bytes(&encode::<usize>(&new_count)?).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn increment_deleted_count(&mut self) -> Result<()>
|
|
||||||
where T: Send
|
|
||||||
{
|
|
||||||
self.seek_to_start().await?;
|
|
||||||
self.seek_to(StoreHeader::DELETED_COUNT_OFFSET as u64).await?;
|
|
||||||
let new_count = self.header_mut().increment_deleted_count();
|
|
||||||
self.write_bytes(&encode::<usize>(&new_count)?).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn set_header(&mut self, header: &StoreHeader) -> Result<()>
|
|
||||||
where T: Send
|
|
||||||
{
|
|
||||||
self.seek_to_start().await?;
|
|
||||||
let encoded_header: Vec<u8> = header.encode()?;
|
|
||||||
self.write_bytes(&encoded_header).await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===Append Entry===
|
|
||||||
|
|
||||||
// Moves cursor to the end.
|
|
||||||
// Returns file position to the start of the new entry.
|
|
||||||
async fn append_entry_no_indexing(&mut self, entry: &Entry<T>) -> Result<FilePosition>
|
|
||||||
where T: Encode + Send + Sync
|
|
||||||
{
|
|
||||||
self.increment_total_count().await?;
|
|
||||||
|
|
||||||
let encoded_entry: Vec<u8> = entry.encode()?;
|
|
||||||
let file_position = self.seek_to_end().await?;
|
|
||||||
self.write_bytes(&encoded_entry).await?;
|
|
||||||
|
|
||||||
let eof_file_position: FilePosition = self.current_file_position().await?;
|
|
||||||
self.set_eof_file_position(eof_file_position);
|
|
||||||
|
|
||||||
Ok(file_position)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -6,7 +6,7 @@ use bincode::{Decode, Encode};
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::cursor::{ReadCursor, WriteCursor};
|
use crate::cursor::{ReadCursor, WriteCursor};
|
||||||
use crate::cursor_capabilities::header_access::CursorCanReadHeader;
|
use crate::cursor_capabilities::traversal::CursorCanTraverse;
|
||||||
use crate::segments::store_header::StoreHeader;
|
use crate::segments::store_header::StoreHeader;
|
||||||
use crate::index::Index;
|
use crate::index::Index;
|
||||||
|
|
||||||
|
|
@ -208,8 +208,8 @@ impl <T>Store<T> {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::segments::entry::Entry;
|
use crate::segments::entry::Entry;
|
||||||
use crate::cursor_capabilities::header_access::CursorCanReadHeader;
|
|
||||||
use crate::cursor_capabilities::index_access::{CursorCanWriteToIndex, CursorCanReadIndex};
|
use crate::cursor_capabilities::index_access::{CursorCanWriteToIndex, CursorCanReadIndex};
|
||||||
|
use crate::cursor_capabilities::traversal::CursorCanTraverse;
|
||||||
|
|
||||||
impl <T>Drop for Store<T> {
|
impl <T>Drop for Store<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue