Fix indexes types

This commit is contained in:
Yuriy Dupyn 2024-02-04 18:13:05 +01:00
parent 89305b6126
commit 4c0f91ad33
3 changed files with 36 additions and 18 deletions

View file

@ -21,7 +21,7 @@ use crate::index::Index;
// ===Concrete Cursors=== // ===Concrete Cursors===
pub struct ReadCursor<'a, T> { pub struct ReadCursor<'a, T> {
header: StoreHeader, header: StoreHeader,
indexes: Vec<Option<&'a Index<T, FilePosition>>>, indexes: &'a [Option<Index<T, FilePosition>>],
file: File, file: File,
data_type: PhantomData<T>, data_type: PhantomData<T>,
@ -30,7 +30,7 @@ pub struct ReadCursor<'a, T> {
pub struct WriteCursor<'a, T> { pub struct WriteCursor<'a, T> {
header: &'a mut StoreHeader, header: &'a mut StoreHeader,
indexes: Vec<Option<&'a Index<T, FilePosition>>>, indexes: &'a mut [Option<Index<T, FilePosition>>],
file: File, file: File,
data_type: PhantomData<T>, data_type: PhantomData<T>,
@ -231,7 +231,7 @@ pub trait CursorWithStoreHeader<T>: PrimitiveCursor<T> {
#[async_trait] #[async_trait]
pub trait CursorWithAccessToIndex<T>: CursorWithStoreHeader<T> { pub trait CursorWithAccessToIndex<T>: CursorWithStoreHeader<T> {
fn indexes(&mut self) -> &[Option<&Index<T, FilePosition>>]; fn indexes(&mut self) -> &[Option<Index<T, FilePosition>>];
async fn find_in_index(&mut self, k: &T) -> Result<Option<FilePosition>> async fn find_in_index(&mut self, k: &T) -> Result<Option<FilePosition>>
where T: Encode + Decode + Ord + Send + Sync where T: Encode + Decode + Ord + Send + Sync
@ -277,13 +277,13 @@ impl <T>CursorWithStoreHeader<T> for WriteCursor<'_, T> {
// ===CursorWithAccessToIndex=== // ===CursorWithAccessToIndex===
impl <T>CursorWithAccessToIndex<T> for ReadCursor<'_, T> { impl <T>CursorWithAccessToIndex<T> for ReadCursor<'_, T> {
fn indexes(&mut self) -> &[Option<&Index<T, FilePosition>>] { fn indexes(&mut self) -> &[Option<Index<T, FilePosition>>] {
&self.indexes &self.indexes
} }
} }
impl <T>CursorWithAccessToIndex<T> for WriteCursor<'_, T> { impl <T>CursorWithAccessToIndex<T> for WriteCursor<'_, T> {
fn indexes(&mut self) -> &[Option<&Index<T, FilePosition>>] { fn indexes(&mut self) -> &[Option<Index<T, FilePosition>>] {
&self.indexes &self.indexes
} }
} }
@ -304,7 +304,7 @@ impl <'cursor, T> ReadCursor<'cursor, T> {
header: store.header.clone(), header: store.header.clone(),
file, file,
data_type: store.data_type, data_type: store.data_type,
indexes: todo!(), indexes: &store.indexes,
eof_file_position: 0, eof_file_position: 0,
}; };
@ -337,7 +337,7 @@ impl <'cursor, T> WriteCursor<'cursor, T>
header: &mut store.header, header: &mut store.header,
file, file,
data_type: store.data_type, data_type: store.data_type,
indexes: todo!(), indexes: &mut store.indexes,
eof_file_position: 0, eof_file_position: 0,
}; };

View file

@ -34,7 +34,7 @@ pub struct Store<T> {
// All // All
pub header: StoreHeader, pub header: StoreHeader,
pub data_type: PhantomData<T>, pub data_type: PhantomData<T>,
pub primary_index: Index<T, FilePosition>, pub indexes: Vec<Option<Index<T, FilePosition>>>,
} }
pub type PositionOfValue = FilePosition; pub type PositionOfValue = FilePosition;
@ -70,14 +70,18 @@ impl <T>Store<T> {
// We don't need the file right now. Only cursors will later open it. // We don't need the file right now. Only cursors will later open it.
Self::create_empty_rows_file(path_to_rows, &header).await?; Self::create_empty_rows_file(path_to_rows, &header).await?;
let primary_index: Index<T, FilePosition> = Index::new( // TODO: I need to construct indexes
&format!("rows_{}", primary_column.to_string()), // let primary_index: Index<T, FilePosition> = Index::new(
).await?; // &format!("rows_{}", primary_column.to_string()),
// ).await?;
// TODO
let indexes = vec![];
let store = Self { let store = Self {
header, header,
data_type: PhantomData::<T>, data_type: PhantomData::<T>,
primary_index, indexes,
}; };
Ok(store) Ok(store)
@ -118,31 +122,39 @@ impl <T>Store<T> {
let header = StoreHeader::decode(table_folder, &mut header_bytes).await?; let header = StoreHeader::decode(table_folder, &mut header_bytes).await?;
let primary_index: Index<T, FilePosition> = Index::connect( // let primary_index: Index<T, FilePosition> = Index::connect(
&format!("rows_{}", header.primary_column.to_string()), // &format!("rows_{}", header.primary_column.to_string()),
).await?; // ).await?;
// TODO
let indexes = vec![];
let store = Self { let store = Self {
header, header,
data_type: PhantomData::<T>, data_type: PhantomData::<T>,
primary_index indexes,
}; };
Ok(store) Ok(store)
} }
// ===Cursors=== // ===Cursors===
pub async fn read_cursor(&self) -> Result<ReadCursor<T>> pub async fn read_cursor(&self) -> Result<ReadCursor<T>>
where T: Send + Sync where T: Send + Sync
{ {
ReadCursor::new(self).await ReadCursor::new(self).await
} }
pub async fn write_cursor(&mut self) -> Result<WriteCursor<T>> pub async fn write_cursor(&mut self) -> Result<WriteCursor<T>>
where T: Send + Sync where T: Send + Sync
{ {
WriteCursor::new(self).await 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. // For debugging.
pub async fn read_all_bytes(&mut self) -> std::result::Result<Vec<u8>, std::io::Error> pub async fn read_all_bytes(&mut self) -> std::result::Result<Vec<u8>, std::io::Error>
where T: Send + Sync where T: Send + Sync

View file

@ -11,6 +11,8 @@ pub struct StoreHeader {
pub deleted_count: usize, pub deleted_count: usize,
pub total_count: usize, pub total_count: usize,
pub primary_column: Column, pub primary_column: Column,
// TODO
// pub indexed_columns: Vec<bool>,
} }
impl StoreHeader { impl StoreHeader {
@ -25,6 +27,10 @@ impl StoreHeader {
pub const TOTAL_COUNT_OFFSET: usize = Self::DELETED_COUNT_OFFSET + Self::DELETED_COUNT_SIZE; 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; pub const PRIMARY_COLUMN_OFFSET: usize = Self::TOTAL_COUNT_OFFSET + Self::TOTAL_COUNT_SIZE;
fn indexed_columns_size(&self) -> usize {
size_of::<bool>() * self.number_of_columns
}
pub fn encode(&self) -> Result<Vec<u8>> { pub fn encode(&self) -> Result<Vec<u8>> {
let mut result = encode(&self.number_of_columns)?; let mut result = encode(&self.number_of_columns)?;
result.append(&mut encode(&self.deleted_count)?); result.append(&mut encode(&self.deleted_count)?);