Print first n entries

This commit is contained in:
Yuriy Dupyn 2024-02-02 13:56:37 +01:00
parent eb034592fa
commit cad4ba8215
5 changed files with 481 additions and 316 deletions

View file

@ -2,47 +2,85 @@ use tokio::sync::{Mutex, RwLock};
use tokio::fs::{File, OpenOptions};
use tokio::io::{BufReader, BufWriter, AsyncReadExt, AsyncWriteExt, AsyncSeekExt, SeekFrom};
use tokio::fs;
use std::path::Path;
mod storage_engine;
mod binary_coding;
mod error;
use crate::storage_engine::*;
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).await.map_err(|e| e.to_io_or_panic())?;
println!("CREATED");
println!("{:?}", store.read_all_bytes().await?);
let entry0: Entry<u32> = Entry::new_deleted(vec![1, 2, 3, 4, 5]);
append_entry(&mut store, &entry0).await?;
let entry1: Entry<u32> = Entry::new_deleted(vec![200, 200, 5, 6, 7]);
append_entry(&mut store, &entry1).await?;
println!("{:?}", 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!("{:?}", 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(store: &mut Store<Data>, entry: &Entry<Data>) -> Result<Cursor>{
println!("APPENDING");
println!("entry == {:?}", entry);
let cursor: Cursor = store.append_entry(&entry).await.map_err(|e| e.to_io_or_panic())?;
println!("cursor == {:?}", cursor);
Ok(cursor)
}
async fn read_entry(store: &mut Store<Data>, cursor: Cursor) -> Result<EntryDetailed<Data>>{
println!("READING ENTRY at cursor={}", cursor);
let entry = store.read_entry_at(cursor).await.map_err(|e| e.to_io_or_panic())?;
println!("ENTRY: {:?}", entry);
Ok(entry)
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
async fn main() -> Result<()> {
println!("STOOOOOOOOOOOORAAAAAAAAAAAGE");
let blob_name = "blob10.minisql";
let mut store: Store<Data> = create_or_connect().await?;
// WARNING: Number of columns is 5?????
let mut store = Store::new(blob_name, 5).await.map_err(|e| e.to_io_or_panic())?;
// let store_bytes = store.get_all_bytes().await.map_err(|e| e.to_io_or_panic())?;
// println!("{:?}", store_bytes);
let mut buff: Vec<u8> = vec![0;1];
let x = store.file.read_exact(&mut buff[..]).await?;
println!("{:?}", buff);
// let entry0: Entry<u32> = Entry::new_deleted(vec![1, 2, 3, 4, 5]);
// let entry1: Entry<u32> = Entry::new_deleted(vec![200,200,5,6,7]);
// let cursor0 = store.append_entry(&entry0).await.map_err(|e| e.to_io_or_panic())?;
// // println!("cursor0 = {}", cursor0);
// let cursor1 = store.append_entry(&entry1).await.map_err(|e| e.to_io_or_panic())?;
// println!("cursor0 = {}, cursor1 = {}", cursor0, cursor1);
// let mut store = Store::connect(blob_name).await.map_err(|e| e.to_io_or_panic())?;
// let entry0 = read_entry(&mut store, 16).await?;
// let entry1 = read_entry(&mut store, 45).await?;
// println!("{:?}", store);
// println!("{:?}", store.read_all_bytes().await?);
// let entry0: Entry<u32> = Entry::new(vec![99, 98, 97, 96, 95]);
// append_entry(&mut store, &entry0).await?;
store.read_entries(4).await.map_err(|e| e.to_io_or_panic())?;
// let x = store.entry_at::<u32>(16).await.map_err(|e| e.to_io_or_panic())?;
// println!("{:?}", x);
// let store_bytes = store.get_all_bytes().await.map_err(|e| e.to_io_or_panic())?;
// println!("{:?}", store_bytes);
// let mut store = ColumnStore::connect("blob08.minisql").await.map_err(|e| e.to_io_or_panic())?;
// let entry2: StoreEntry<u32> = StoreEntry::new_deleted(vec![3, 2, 1]);
// let cursor2 = store.append_entry(&entry2).await.map_err(|e| e.to_io_or_panic())?;
// println!("cursor2 = {}", cursor2);