Sketch out indexes in Store
This commit is contained in:
parent
dae012daa7
commit
89305b6126
4 changed files with 82 additions and 32 deletions
|
|
@ -3,15 +3,12 @@ use tokio::fs::{File, OpenOptions, DirBuilder};
|
|||
use tokio::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::marker::PhantomData;
|
||||
use async_trait::async_trait;
|
||||
use bincode::{Decode, Encode};
|
||||
|
||||
use crate::index::SomethingSupportingLeq;
|
||||
use crate::error::Error;
|
||||
use crate::cursor::{ReadCursor, WriteCursor, CursorWithStoreHeader};
|
||||
use crate::store_header::StoreHeader;
|
||||
|
||||
// TODO
|
||||
// use crate::index::Index;
|
||||
use crate::index::Index;
|
||||
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
|
@ -37,6 +34,7 @@ pub struct Store<T> {
|
|||
// All
|
||||
pub header: StoreHeader,
|
||||
pub data_type: PhantomData<T>,
|
||||
pub primary_index: Index<T, FilePosition>,
|
||||
}
|
||||
|
||||
pub type PositionOfValue = FilePosition;
|
||||
|
|
@ -48,16 +46,14 @@ 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 const ROWS_FILE_NAME: &'static str = "rows";
|
||||
pub const GARBAGE_COLLECTION_INTERMEDIATE_ROWS_FILE_NAME: &'static str = "rows_intermediate";
|
||||
|
||||
impl <T>Store<T> {
|
||||
// ===Creation===
|
||||
pub async fn new(table_folder: &str, number_of_columns: usize, primary_column: Column) -> Result<Self> {
|
||||
pub async fn new(table_folder: &str, number_of_columns: usize, primary_column: Column) -> Result<Self>
|
||||
where T: Encode + Decode + Ord
|
||||
{
|
||||
let path_to_table = Path::new(table_folder);
|
||||
let path_to_rows = path_to_table.join(ROWS_FILE_NAME);
|
||||
DirBuilder::new()
|
||||
|
|
@ -74,14 +70,14 @@ 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?;
|
||||
|
||||
// TODO: indexes
|
||||
// let index: Index<PositionOfValue, PositionOfRow> = Index::new(
|
||||
// &format!("rows_{}", primary_column.to_string()),
|
||||
// ).await?;
|
||||
let primary_index: Index<T, FilePosition> = Index::new(
|
||||
&format!("rows_{}", primary_column.to_string()),
|
||||
).await?;
|
||||
|
||||
let store = Self {
|
||||
header,
|
||||
data_type: PhantomData::<T>,
|
||||
primary_index,
|
||||
};
|
||||
|
||||
Ok(store)
|
||||
|
|
@ -103,7 +99,7 @@ impl <T>Store<T> {
|
|||
}
|
||||
|
||||
pub async fn connect(table_folder: &str) -> Result<Self>
|
||||
where T: std::fmt::Debug
|
||||
where T: std::fmt::Debug + Encode + Decode + Ord
|
||||
{
|
||||
let path_to_table = Path::new(table_folder);
|
||||
let path_to_rows = path_to_table.join(ROWS_FILE_NAME);
|
||||
|
|
@ -121,29 +117,35 @@ impl <T>Store<T> {
|
|||
file.read_exact(&mut header_bytes).await?;
|
||||
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 store = Self {
|
||||
header,
|
||||
data_type: PhantomData::<T>,
|
||||
primary_index
|
||||
};
|
||||
Ok(store)
|
||||
}
|
||||
|
||||
// ===Cursors===
|
||||
pub async fn read_cursor(&self) -> Result<ReadCursor<T>>
|
||||
where T: Send
|
||||
where T: Send + Sync
|
||||
{
|
||||
ReadCursor::new(self).await
|
||||
}
|
||||
|
||||
pub async fn write_cursor(&mut self) -> Result<WriteCursor<T>>
|
||||
where T: Send
|
||||
where T: Send + Sync
|
||||
{
|
||||
WriteCursor::new(self).await
|
||||
}
|
||||
|
||||
// For debugging.
|
||||
pub async fn read_all_bytes(&mut self) -> std::result::Result<Vec<u8>, std::io::Error>
|
||||
where T: Send
|
||||
where T: Send + Sync
|
||||
{
|
||||
let mut cursor = self.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
||||
let bytes = cursor.read_all_bytes().await?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue