From 4c0f91ad334f8676a7140aacf93966426e5b2848 Mon Sep 17 00:00:00 2001 From: Yuriy Dupyn <2153100+omedusyo@users.noreply.github.com> Date: Sun, 4 Feb 2024 18:13:05 +0100 Subject: [PATCH] Fix indexes types --- storage_engine/src/cursor.rs | 14 ++++++------ storage_engine/src/storage_engine.rs | 34 +++++++++++++++++++--------- storage_engine/src/store_header.rs | 6 +++++ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/storage_engine/src/cursor.rs b/storage_engine/src/cursor.rs index a0c1671..8fefaa6 100644 --- a/storage_engine/src/cursor.rs +++ b/storage_engine/src/cursor.rs @@ -21,7 +21,7 @@ use crate::index::Index; // ===Concrete Cursors=== pub struct ReadCursor<'a, T> { header: StoreHeader, - indexes: Vec>>, + indexes: &'a [Option>], file: File, data_type: PhantomData, @@ -30,7 +30,7 @@ pub struct ReadCursor<'a, T> { pub struct WriteCursor<'a, T> { header: &'a mut StoreHeader, - indexes: Vec>>, + indexes: &'a mut [Option>], file: File, data_type: PhantomData, @@ -231,7 +231,7 @@ pub trait CursorWithStoreHeader: PrimitiveCursor { #[async_trait] pub trait CursorWithAccessToIndex: CursorWithStoreHeader { - fn indexes(&mut self) -> &[Option<&Index>]; + fn indexes(&mut self) -> &[Option>]; async fn find_in_index(&mut self, k: &T) -> Result> where T: Encode + Decode + Ord + Send + Sync @@ -277,13 +277,13 @@ impl CursorWithStoreHeader for WriteCursor<'_, T> { // ===CursorWithAccessToIndex=== impl CursorWithAccessToIndex for ReadCursor<'_, T> { - fn indexes(&mut self) -> &[Option<&Index>] { + fn indexes(&mut self) -> &[Option>] { &self.indexes } } impl CursorWithAccessToIndex for WriteCursor<'_, T> { - fn indexes(&mut self) -> &[Option<&Index>] { + fn indexes(&mut self) -> &[Option>] { &self.indexes } } @@ -304,7 +304,7 @@ impl <'cursor, T> ReadCursor<'cursor, T> { header: store.header.clone(), file, data_type: store.data_type, - indexes: todo!(), + indexes: &store.indexes, eof_file_position: 0, }; @@ -337,7 +337,7 @@ impl <'cursor, T> WriteCursor<'cursor, T> header: &mut store.header, file, data_type: store.data_type, - indexes: todo!(), + indexes: &mut store.indexes, eof_file_position: 0, }; diff --git a/storage_engine/src/storage_engine.rs b/storage_engine/src/storage_engine.rs index 1ff53ad..a753a3d 100644 --- a/storage_engine/src/storage_engine.rs +++ b/storage_engine/src/storage_engine.rs @@ -34,7 +34,7 @@ pub struct Store { // All pub header: StoreHeader, pub data_type: PhantomData, - pub primary_index: Index, + pub indexes: Vec>>, } pub type PositionOfValue = FilePosition; @@ -70,14 +70,18 @@ impl Store { // We don't need the file right now. Only cursors will later open it. Self::create_empty_rows_file(path_to_rows, &header).await?; - let primary_index: Index = Index::new( - &format!("rows_{}", primary_column.to_string()), - ).await?; + // TODO: I need to construct indexes + // let primary_index: Index = Index::new( + // &format!("rows_{}", primary_column.to_string()), + // ).await?; + + // TODO + let indexes = vec![]; let store = Self { header, data_type: PhantomData::, - primary_index, + indexes, }; Ok(store) @@ -118,31 +122,39 @@ impl Store { let header = StoreHeader::decode(table_folder, &mut header_bytes).await?; - let primary_index: Index = Index::connect( - &format!("rows_{}", header.primary_column.to_string()), - ).await?; + // let primary_index: Index = Index::connect( + // &format!("rows_{}", header.primary_column.to_string()), + // ).await?; + + // TODO + let indexes = vec![]; let store = Self { header, data_type: PhantomData::, - primary_index + indexes, }; Ok(store) } // ===Cursors=== - pub async fn read_cursor(&self) -> Result> + pub async fn read_cursor(&self) -> Result> where T: Send + Sync { ReadCursor::new(self).await } - pub async fn write_cursor(&mut self) -> Result> + pub async fn write_cursor(&mut self) -> Result> where T: Send + Sync { WriteCursor::new(self).await } + pub async fn make_indexable(&mut self, column: Column) -> Result<()> { + // Creates an index from scratch at above column + todo!() + } + // For debugging. pub async fn read_all_bytes(&mut self) -> std::result::Result, std::io::Error> where T: Send + Sync diff --git a/storage_engine/src/store_header.rs b/storage_engine/src/store_header.rs index 73cbb55..fc571a4 100644 --- a/storage_engine/src/store_header.rs +++ b/storage_engine/src/store_header.rs @@ -11,6 +11,8 @@ pub struct StoreHeader { pub deleted_count: usize, pub total_count: usize, pub primary_column: Column, + // TODO + // pub indexed_columns: Vec, } impl StoreHeader { @@ -25,6 +27,10 @@ impl StoreHeader { pub const TOTAL_COUNT_OFFSET: usize = Self::DELETED_COUNT_OFFSET + Self::DELETED_COUNT_SIZE; pub const PRIMARY_COLUMN_OFFSET: usize = Self::TOTAL_COUNT_OFFSET + Self::TOTAL_COUNT_SIZE; + fn indexed_columns_size(&self) -> usize { + size_of::() * self.number_of_columns + } + pub fn encode(&self) -> Result> { let mut result = encode(&self.number_of_columns)?; result.append(&mut encode(&self.deleted_count)?);