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===
pub struct ReadCursor<'a, T> {
header: StoreHeader,
indexes: Vec<Option<&'a Index<T, FilePosition>>>,
indexes: &'a [Option<Index<T, FilePosition>>],
file: File,
data_type: PhantomData<T>,
@ -30,7 +30,7 @@ pub struct ReadCursor<'a, T> {
pub struct WriteCursor<'a, T> {
header: &'a mut StoreHeader,
indexes: Vec<Option<&'a Index<T, FilePosition>>>,
indexes: &'a mut [Option<Index<T, FilePosition>>],
file: File,
data_type: PhantomData<T>,
@ -231,7 +231,7 @@ pub trait CursorWithStoreHeader<T>: PrimitiveCursor<T> {
#[async_trait]
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>>
where T: Encode + Decode + Ord + Send + Sync
@ -277,13 +277,13 @@ impl <T>CursorWithStoreHeader<T> for WriteCursor<'_, T> {
// ===CursorWithAccessToIndex===
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
}
}
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
}
}
@ -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,
};

View file

@ -34,7 +34,7 @@ pub struct Store<T> {
// All
pub header: StoreHeader,
pub data_type: PhantomData<T>,
pub primary_index: Index<T, FilePosition>,
pub indexes: Vec<Option<Index<T, 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.
Self::create_empty_rows_file(path_to_rows, &header).await?;
let primary_index: Index<T, FilePosition> = Index::new(
&format!("rows_{}", primary_column.to_string()),
).await?;
// TODO: I need to construct indexes
// let primary_index: Index<T, FilePosition> = Index::new(
// &format!("rows_{}", primary_column.to_string()),
// ).await?;
// TODO
let indexes = vec![];
let store = Self {
header,
data_type: PhantomData::<T>,
primary_index,
indexes,
};
Ok(store)
@ -118,14 +122,17 @@ impl <T>Store<T> {
let header = StoreHeader::decode(table_folder, &mut header_bytes).await?;
let primary_index: Index<T, FilePosition> = Index::connect(
&format!("rows_{}", header.primary_column.to_string()),
).await?;
// let primary_index: Index<T, FilePosition> = Index::connect(
// &format!("rows_{}", header.primary_column.to_string()),
// ).await?;
// TODO
let indexes = vec![];
let store = Self {
header,
data_type: PhantomData::<T>,
primary_index
indexes,
};
Ok(store)
}
@ -143,6 +150,11 @@ impl <T>Store<T> {
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<Vec<u8>, std::io::Error>
where T: Send + Sync

View file

@ -11,6 +11,8 @@ pub struct StoreHeader {
pub deleted_count: usize,
pub total_count: usize,
pub primary_column: Column,
// TODO
// pub indexed_columns: Vec<bool>,
}
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::<bool>() * self.number_of_columns
}
pub fn encode(&self) -> Result<Vec<u8>> {
let mut result = encode(&self.number_of_columns)?;
result.append(&mut encode(&self.deleted_count)?);