215 lines
7.9 KiB
Rust
215 lines
7.9 KiB
Rust
mod storage_engine;
|
|
mod binary_coding;
|
|
mod error;
|
|
mod index;
|
|
mod cursor;
|
|
mod entry;
|
|
mod entry_header;
|
|
mod store_header;
|
|
|
|
use crate::entry::{Entry, EntryDetailed};
|
|
use crate::storage_engine::{Store, FilePosition};
|
|
use crate::cursor::{ReadCursor, WriteCursor, CursorWithStoreHeader, CursorWithWriteAccessToIndex};
|
|
|
|
type Data = u32;
|
|
|
|
const TABLE_PATH: &'static str = "test_table";
|
|
|
|
type Result<T> = std::result::Result<T, std::io::Error>;
|
|
|
|
async fn create_store() -> Result<Store<Data>> {
|
|
let store: Store<Data> = Store::new(TABLE_PATH, 5, 0).await.map_err(|e| e.to_io_or_panic())?;
|
|
println!("CREATED");
|
|
// println!("THE STORE: {:?}", store);
|
|
// println!("THE BYTES: {:?}", store.read_all_bytes().await?);
|
|
|
|
Ok(store)
|
|
}
|
|
|
|
async fn connect_store() -> Result<Store<Data>> {
|
|
let store: Store<Data> = Store::connect(TABLE_PATH).await.map_err(|e| e.to_io_or_panic())?;
|
|
println!("CONNECTED");
|
|
// println!("THE STORE: {:?}", store);
|
|
// println!("THE BYTES: {:?}", store.read_all_bytes().await?);
|
|
Ok(store)
|
|
}
|
|
|
|
async fn create_or_connect() -> Result<Store<Data>> {
|
|
let exists = storage_engine::store_exists(TABLE_PATH).await.map_err(|e| e.to_io_or_panic())?;
|
|
if exists {
|
|
connect_store().await
|
|
} else {
|
|
create_store().await
|
|
}
|
|
}
|
|
|
|
|
|
async fn append_entry(cursor: &mut WriteCursor<'_, Data>, entry: Entry<Data>) -> Result<FilePosition> {
|
|
println!("APPENDING");
|
|
println!("entry == {:?}", entry);
|
|
|
|
// let file_position: FilePosition = cursor.append_entry(&entry).await.map_err(|e| e.to_io_or_panic())?;
|
|
let file_position: FilePosition = cursor.insert_entry(entry).await.map_err(|e| e.to_io_or_panic())?;
|
|
println!("file_position == {:?}", file_position);
|
|
Ok(file_position)
|
|
}
|
|
|
|
async fn read_entry(cursor: &mut ReadCursor<'_, Data>, file_position: FilePosition) -> Result<Option<EntryDetailed<Data>>> {
|
|
println!("READING ENTRY at file_position={}", file_position);
|
|
let entry = cursor.read_entry_at(file_position).await.map_err(|e| e.to_io_or_panic())?;
|
|
println!("ENTRY: {:?}", entry);
|
|
Ok(entry)
|
|
}
|
|
|
|
async fn append_bunch_of_entries(store: &mut Store<Data>) -> Result<()> {
|
|
let mut cursor = store.write_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
let entry0: Entry<u32> = Entry::new(vec![1, 2, 3, 4, 5]);
|
|
append_entry(&mut cursor, entry0).await?;
|
|
|
|
let entry1: Entry<u32> = Entry::new(vec![200, 200, 5, 6, 7]);
|
|
append_entry(&mut cursor, entry1).await?;
|
|
|
|
// println!("{:?}", store.read_all_bytes().await?);
|
|
let entry2: Entry<u32> = Entry::new(vec![99, 98, 97, 96, 95]);
|
|
append_entry(&mut cursor, entry2).await?;
|
|
|
|
let entry3: Entry<u32> = Entry::new(vec![50,50,50,50,50]);
|
|
append_entry(&mut cursor, entry3).await?;
|
|
|
|
let entry4: Entry<u32> = Entry::new(vec![1,50,50,50,50]); // same 0-th column as entry0
|
|
append_entry(&mut cursor, entry4).await?;
|
|
Ok(())
|
|
}
|
|
|
|
async fn test_garbage_collection(store: &mut Store<Data>) -> Result<()> {
|
|
let mut cursor = store.write_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
// cursor.delete_entries_where_eq()
|
|
// 1. mark a bunch of entries as deleted
|
|
let column = 0;
|
|
let value = 1;
|
|
cursor.delete_entries_where_eq(column, &value, true).await.map_err(|e| e.to_io_or_panic())?;
|
|
// let value = 50;
|
|
// cursor.delete_entries_where_eq(column, &value).await.map_err(|e| e.to_io_or_panic())?;
|
|
|
|
// cursor.initiate_garbage_collection().await.map_err(|e| e.to_io_or_panic())?;
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<()> {
|
|
println!("STOOOOOOOOOOOORAAAAAAAAAAAGE");
|
|
|
|
let mut store: Store<Data> = create_or_connect().await?;
|
|
|
|
if store.header.total_count == 0 {
|
|
println!("INSERTING!");
|
|
append_bunch_of_entries(&mut store).await?;
|
|
}
|
|
|
|
{
|
|
// let mut cursor = store.write_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
|
|
// let entry: Entry<u32> = Entry::new(vec![60, 50, 40, 30, 20]);
|
|
// let file_position = append_entry(&mut cursor, &entry).await?;
|
|
// let file_position = 215;
|
|
// cursor.seek_to(file_position).await.map_err(|e| e.to_io_or_panic())?;
|
|
|
|
// let entry_header = cursor.read_entry_header().await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("entry header = {:?}", entry_header);
|
|
|
|
// println!("FILE POSITION == {}", file_position);
|
|
// cursor.mark_deleted_at(file_position).await.map_err(|e| e.to_io_or_panic())?;
|
|
// let entry_header = cursor.read_entry_header().await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("entry header after delete = {:?}", entry_header);
|
|
}
|
|
|
|
// println!("{:?}", store);
|
|
// println!("{:?}", store.read_all_bytes().await?);
|
|
{
|
|
let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
cursor.read_entries().await.map_err(|e| e.to_io_or_panic())?;
|
|
}
|
|
|
|
test_garbage_collection(&mut store).await?;
|
|
|
|
// {
|
|
// let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
// cursor.read_entries().await.map_err(|e| e.to_io_or_panic())?;
|
|
// }
|
|
|
|
|
|
// {
|
|
// let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
// let x = cursor.next().await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// let x = cursor.next().await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// let x = cursor.next().await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// let x = cursor.next().await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// let x = cursor.next().await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// }
|
|
|
|
// {
|
|
// let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
// let column = 2;
|
|
// let x = cursor.next_at_column(column).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// let x = cursor.next_at_column(column).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// let x = cursor.next_at_column(column).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// let x = cursor.next_at_column(column).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// let x = cursor.next_at_column(column).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// }
|
|
|
|
// {
|
|
// let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
// let column = 0;
|
|
// let value = 1;
|
|
// let entries = cursor.select_entries_where_eq(column, &value).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("ARE INDEXES WORKING???");
|
|
// println!("{:?}", entries);
|
|
// }
|
|
|
|
// {
|
|
// let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
// let column = 1;
|
|
// let value = 2;
|
|
// let entries = cursor.select_entries_where_eq(column, &value).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("ARE INDEXES WORKING???");
|
|
// println!("{:?}", entries);
|
|
// }
|
|
|
|
// {
|
|
// let column = 1;
|
|
// // println!("BUILDING AN INDEX");
|
|
// // store.attach_index(column).await.map_err(|e| e.to_io_or_panic())?;
|
|
// // println!("INDEX BUILT!");
|
|
|
|
// let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
// let value = 2;
|
|
// let entries = cursor.select_entries_where_eq(column, &value).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("ARE INDEXES WORKING???");
|
|
// println!("{:?}", entries);
|
|
// }
|
|
|
|
|
|
// {
|
|
// let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
|
|
// let column = 3;
|
|
// let t0 = 6;
|
|
// let x = cursor.find_first_eq_bruteforce(column, &t0).await.map_err(|e| e.to_io_or_panic())?;
|
|
// println!("{:?}", x);
|
|
// }
|
|
|
|
|
|
|
|
|
|
println!("DONE");
|
|
Ok(())
|
|
}
|