minisql/storage_engine/src/main.rs
2024-02-04 19:00:50 +01:00

157 lines
5.5 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, PrimitiveCursor};
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 mut 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 mut 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())?;
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)
todo!()
}
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?;
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())?;
}
{
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 = 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(())
}