diff --git a/storage_engine/src/index.rs b/storage_engine/src/index.rs index dde57d3..c28c91e 100644 --- a/storage_engine/src/index.rs +++ b/storage_engine/src/index.rs @@ -4,13 +4,14 @@ use tokio::fs::{File, OpenOptions, DirBuilder}; use std::path::Path; use std::collections::{BTreeMap}; +use async_trait::async_trait; use bincode; use bincode::{Decode, Encode}; use crate::binary_coding::{encode, decode, encode_sequence, decode_sequence}; use tokio::fs; -use crate::error::{Error, DecodeErrorKind}; +use crate::error::Error; use std::mem::size_of; @@ -19,6 +20,7 @@ type Result = std::result::Result; // Implements a persistant self-balancing Binary Search Tree. Nope. // We need fixed-size nodes. But we want to index Strings which are variable length. +#[derive(Debug)] pub struct Index { file: File, // None means index is asleep on disk. @@ -28,11 +30,28 @@ pub struct Index { value_type: PhantomData, } +#[derive(Debug)] pub struct IndexHeader { } +use crate::storage_engine::FilePosition; + +#[async_trait] +pub trait SomethingSupportingLeq { + async fn less_than_eq(&mut self, file_position0: FilePosition, file_position1: FilePosition) -> std::result::Result; +} + impl Index { - pub async fn new(file_name: &str) -> Result> { + // TODO: delete + // pub async fn new(file_name: &str, less_than_eq: &F) -> Result> + // where F: Fn(&mut Store, K, K) -> Fut, + // Store: SomethingSupportingLeq, + // Fut: Future>, + // { + // todo!() + // } + pub async fn new(file_name: &str) -> Result> + { todo!() } @@ -56,9 +75,11 @@ impl Index { todo!() } - pub async fn lookup(&mut self, k: K) -> Result> + pub async fn lookup(&mut self, store: &mut Store, k: K) -> Result> where K: Encode + Decode, + Store: SomethingSupportingLeq, { + let x = store.less_than_eq(123, 123).await?; todo!() } diff --git a/storage_engine/src/storage_engine.rs b/storage_engine/src/storage_engine.rs index 6075476..00054b1 100644 --- a/storage_engine/src/storage_engine.rs +++ b/storage_engine/src/storage_engine.rs @@ -3,13 +3,20 @@ use tokio::fs::{File, OpenOptions, DirBuilder}; use std::path::Path; use std::marker::PhantomData; +use async_trait::async_trait; + use bincode; use bincode::{Decode, Encode}; use crate::binary_coding::{encode, decode, encode_sequence, encode_sequence_with_sizes, decode_sequence}; use tokio::fs; +use crate::index::SomethingSupportingLeq; use crate::error::{Error, DecodeErrorKind}; +use crate::index::Index; + +use std::cell::RefCell; + use std::mem::size_of; type Result = std::result::Result; @@ -24,6 +31,7 @@ pub struct Store { file: File, // primary_index: Vec>>, // indexes: Vec>>>, + // primary_index: Index, header: StoreHeader, data_type: PhantomData, @@ -36,6 +44,11 @@ pub struct Store { // list } + + +type PositionOfValue = FilePosition; +type PositionOfRow = FilePosition; + // TODO: Basically a pointer to Store + its own file position // pub struct Cursor<'a, T> { // } @@ -113,6 +126,23 @@ pub async fn store_exists(table_folder: &str) -> Result { Ok(fs::metadata(table_folder).await.is_ok()) } +pub async fn less_than_eq(store: &mut Store, file_position0: FilePosition, file_position1: FilePosition) -> Result { + todo!() +} + +// pub trait SomethingSupportingLeq { +// async fn less_than_eq(&mut self, file_position0: FilePosition, file_position1: FilePosition) -> std::result::Result; +// } + +#[async_trait] +impl SomethingSupportingLeq for Store + where T: Send +{ + async fn less_than_eq(&mut self, file_position0: FilePosition, file_position1: FilePosition) -> std::result::Result { + Ok(true) + } +} + impl Store { const ROWS_FILE_NAME: &'static str = "rows"; @@ -168,6 +198,10 @@ impl Store { Ok(bytes) } + pub async fn less_than_eq(&mut self, file_position0: FilePosition, file_position1: FilePosition) -> Result { + todo!() + } + // ===Creation=== pub async fn new(table_folder: &str, number_of_columns: usize, primary_column: Column) -> Result { let path_to_table = Path::new(table_folder); @@ -190,8 +224,11 @@ impl Store { primary_column, }; let encoded_header: Vec = header.encode()?; + - // Index::new(format!("rows", primary_column.to_string())) + // let index: Index = Index::new( + // &format!("rows_{}", primary_column.to_string()), + // ).await?; let mut store = Self { table_folder: table_folder.to_string(), @@ -202,6 +239,7 @@ impl Store { }; store.write_bytes(&encoded_header).await?; store.eof_file_position = store.current_file_position().await?; + Ok(store) } @@ -231,7 +269,7 @@ impl Store { file, header, data_type: PhantomData::, - eof_file_position + eof_file_position, }; Ok(store) } @@ -298,6 +336,14 @@ impl Store { Ok(header) } + pub async fn search_for(&mut self, index: T) -> Result<()> + where T: Send + { + // let index = self.primary_index.borrow_mut(); + // let x = index.lookup(self, 123).await?; + todo!() + } + // Returns None when file_positoin == eof_file_position pub async fn read_entry_at(&mut self, file_position: FilePosition) -> Result>> where T: Decode