Fix delete bug

This commit is contained in:
Yuriy Dupyn 2024-02-03 21:34:50 +01:00
parent 3bf04ae2d6
commit a345bf99c6
4 changed files with 107 additions and 62 deletions

View file

@ -9,7 +9,7 @@ mod store_header;
use crate::entry::{Entry, EntryDetailed};
use crate::storage_engine::{Store, FilePosition};
use crate::cursor::{ReadCursor, WriteCursor};
use crate::cursor::{ReadCursor, WriteCursor, CursorWithStoreHeader, PrimitiveCursor};
type Data = u32;
@ -20,8 +20,8 @@ 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?);
// println!("THE STORE: {:?}", store);
// println!("THE BYTES: {:?}", store.read_all_bytes().await?);
Ok(store)
}
@ -29,8 +29,8 @@ async fn create_store() -> Result<Store<Data>> {
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?);
// println!("THE STORE: {:?}", store);
// println!("THE BYTES: {:?}", store.read_all_bytes().await?);
Ok(store)
}
@ -60,6 +60,22 @@ async fn read_entry(cursor: &mut ReadCursor<Data>, file_position: FilePosition)
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<()> {
@ -67,42 +83,46 @@ async fn main() -> Result<()> {
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 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?;
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())?;
// 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 entry_header = cursor.read_entry_header().await.map_err(|e| e.to_io_or_panic())?;
// println!("entry header = {:?}", entry_header);
let entry3: Entry<u32> = Entry::new(vec![50,50,50,50,50]);
append_entry(&mut cursor, &entry3).await?;
// 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 entry0: Entry<u32> = Entry::new(vec![99, 98, 97, 96, 95]);
// append_entry(&mut cursor, &entry0).await?;
let mut cursor = store.read_cursor().await.map_err(|e| e.to_io_or_panic())?;
// 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 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);
// 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");