Thinking about indexes

This commit is contained in:
Yuriy Dupyn 2024-02-03 15:48:44 +01:00
parent 85ef52dfb4
commit dbd2ba9946
2 changed files with 72 additions and 5 deletions

View file

@ -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<T> = std::result::Result<T, Error>;
@ -24,6 +31,7 @@ pub struct Store<T> {
file: File,
// primary_index: Vec<Index<T, FilePosition>>>,
// indexes: Vec<Option<Index<T, HashSet<FilePosition>>>>,
// primary_index: Index<PositionOfValue, PositionOfRow>,
header: StoreHeader,
data_type: PhantomData<T>,
@ -36,6 +44,11 @@ pub struct Store<T> {
// 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<bool> {
Ok(fs::metadata(table_folder).await.is_ok())
}
pub async fn less_than_eq<T>(store: &mut Store<T>, file_position0: FilePosition, file_position1: FilePosition) -> Result<bool> {
todo!()
}
// pub trait SomethingSupportingLeq {
// async fn less_than_eq(&mut self, file_position0: FilePosition, file_position1: FilePosition) -> std::result::Result<bool, Error>;
// }
#[async_trait]
impl <T>SomethingSupportingLeq for Store<T>
where T: Send
{
async fn less_than_eq(&mut self, file_position0: FilePosition, file_position1: FilePosition) -> std::result::Result<bool, Error> {
Ok(true)
}
}
impl <T>Store<T> {
const ROWS_FILE_NAME: &'static str = "rows";
@ -168,6 +198,10 @@ impl <T>Store<T> {
Ok(bytes)
}
pub async fn less_than_eq(&mut self, file_position0: FilePosition, file_position1: FilePosition) -> Result<bool> {
todo!()
}
// ===Creation===
pub async fn new(table_folder: &str, number_of_columns: usize, primary_column: Column) -> Result<Self> {
let path_to_table = Path::new(table_folder);
@ -190,8 +224,11 @@ impl <T>Store<T> {
primary_column,
};
let encoded_header: Vec<u8> = header.encode()?;
// Index::new<T, FilePosition>(format!("rows", primary_column.to_string()))
// let index: Index<PositionOfValue, PositionOfRow> = Index::new(
// &format!("rows_{}", primary_column.to_string()),
// ).await?;
let mut store = Self {
table_folder: table_folder.to_string(),
@ -202,6 +239,7 @@ impl <T>Store<T> {
};
store.write_bytes(&encoded_header).await?;
store.eof_file_position = store.current_file_position().await?;
Ok(store)
}
@ -231,7 +269,7 @@ impl <T>Store<T> {
file,
header,
data_type: PhantomData::<T>,
eof_file_position
eof_file_position,
};
Ok(store)
}
@ -298,6 +336,14 @@ impl <T>Store<T> {
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<Option<EntryDetailed<T>>>
where T: Decode