Use new cursors

This commit is contained in:
Yuriy Dupyn 2024-02-03 19:17:47 +01:00
parent a37c3a5e77
commit 2261fe39de
3 changed files with 81 additions and 283 deletions

View file

@ -11,6 +11,7 @@ mod index;
mod cursor;
use crate::storage_engine::*;
use crate::cursor::*;
type Data = u32;
@ -24,14 +25,6 @@ async fn create_store() -> Result<Store<Data>> {
println!("THE STORE: {:?}", store);
println!("THE BYTES: {:?}", store.read_all_bytes().await?);
let mut cursor = store.cursor(AccessMode::Write).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?);
Ok(store)
}
@ -53,7 +46,7 @@ async fn create_or_connect() -> Result<Store<Data>> {
}
async fn append_entry(cursor: &mut Cursor<Data>, entry: &Entry<Data>) -> Result<FilePosition>{
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())?;
@ -63,9 +56,10 @@ async fn append_entry(cursor: &mut Cursor<Data>, entry: &Entry<Data>) -> Result<
async fn read_entry(cursor: &mut Cursor<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)
// let entry = cursor.read_entry_at(file_position).await.map_err(|e| e.to_io_or_panic())?;
// println!("ENTRY: {:?}", entry);
// Ok(entry)
todo!()
}
@ -73,32 +67,44 @@ async fn read_entry(cursor: &mut Cursor<Data>, file_position: FilePosition) -> R
async fn main() -> Result<()> {
println!("STOOOOOOOOOOOORAAAAAAAAAAAGE");
let store: Store<Data> = create_or_connect().await?;
let mut store: Store<Data> = create_or_connect().await?;
{
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 entry0 = read_entry(&mut store, 16).await?;
// let entry1 = read_entry(&mut store, 45).await?;
// println!("{:?}", store);
// println!("{:?}", store.read_all_bytes().await?);
let mut cursor = store.cursor(AccessMode::Write).await.map_err(|e| e.to_io_or_panic())?;
let entry0: Entry<u32> = Entry::new(vec![99, 98, 97, 96, 95]);
append_entry(&mut cursor, &entry0).await?;
// let entry0: Entry<u32> = Entry::new(vec![99, 98, 97, 96, 95]);
// append_entry(&mut cursor, &entry0).await?;
// let entry1: Entry<u32> = Entry::new(vec![50,50,50,50,50]);
// let file_position = append_entry(&mut cursor, &entry1).await?;
// println!("CURRENT FILE_POSITION = {}", file_position);
let entry1: Entry<u32> = Entry::new(vec![50,50,50,50,50]);
let file_position = append_entry(&mut cursor, &entry1).await?;
println!("CURRENT FILE_POSITION = {}", file_position);
// Now file_position point to entry1.
// cursor.mark_deleted_at(file_position).await.map_err(|e| e.to_io_or_panic())?;
// cursor.seek_to(file_position).await.map_err(|e| e.to_io_or_panic())?;
cursor.read_entries().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);
println!("{:?}", store);
println!("{:?}", store.read_all_bytes().await?);
println!("DONE");