diff --git a/storage_engine/src/cursor.rs b/storage_engine/src/cursor.rs index cf01190..8e0efe0 100644 --- a/storage_engine/src/cursor.rs +++ b/storage_engine/src/cursor.rs @@ -2,7 +2,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt, AsyncSeekExt, SeekFrom}; use tokio::fs::{File, OpenOptions}; use std::path::Path; use std::marker::PhantomData; -use crate::error::{Error, DecodeErrorKind}; +use std::collections::{BTreeMap, HashSet}; use async_trait::async_trait; @@ -10,6 +10,7 @@ use bincode; use bincode::{Decode, Encode}; use crate::binary_coding::{encode, decode}; +use crate::error::{Error, DecodeErrorKind}; use crate::entry::{Entry, EntryDetailed}; use crate::entry_header::{EntryHeaderWithDataSize, EntryHeader}; use crate::store_header::StoreHeader; @@ -23,7 +24,6 @@ pub struct ReadCursor<'a, T> { header: StoreHeader, indexes: &'a [Option>], file: File, - data_type: PhantomData, eof_file_position: FilePosition, } @@ -32,7 +32,6 @@ pub struct WriteCursor<'a, T> { header: &'a mut StoreHeader, indexes: &'a mut [Option>], file: File, - data_type: PhantomData, eof_file_position: FilePosition, } @@ -390,7 +389,6 @@ impl <'cursor, T> ReadCursor<'cursor, T> { let mut cursor = Self { header: store.header.clone(), file, - data_type: store.data_type, indexes: &store.indexes, eof_file_position: 0, @@ -425,7 +423,6 @@ impl <'cursor, T> WriteCursor<'cursor, T> let mut cursor = Self { header: &mut store.header, file, - data_type: store.data_type, indexes: &mut store.indexes, eof_file_position: 0, @@ -451,7 +448,6 @@ impl <'cursor, T> WriteCursor<'cursor, T> let mut cursor = Self { header, file, - data_type: PhantomData::, indexes, eof_file_position: 0, @@ -476,7 +472,7 @@ impl <'cursor, T> WriteCursor<'cursor, T> // ===Deletion=== pub async fn mark_deleted_at(&mut self, file_position: FilePosition) -> Result<()> - where T: Send + where T: Send + Decode + Encode { self.seek_to(file_position).await?; let mut entry_header = self.read_entry_header().await?; @@ -495,7 +491,7 @@ impl <'cursor, T> WriteCursor<'cursor, T> } async fn find_first_eq_bruteforce_and_delete(&mut self, column: Column, t0: &T) -> Result>> - where T: Decode + PartialEq + Send + Sync + where T: Decode + Encode + PartialEq + Send + Sync { let maybe_entry = self.find_first_eq_bruteforce(column, t0).await?; if let Some(entry) = maybe_entry { @@ -508,7 +504,7 @@ impl <'cursor, T> WriteCursor<'cursor, T> // ===Garbage Collection=== async fn attempt_garbage_collection_if_necessary(&mut self) -> Result<()> - where T: Send + where T: Send + Decode + Encode { // TODO: What should be the policy? Counting size of garbage? Counting how many entries are // garbage? @@ -519,11 +515,18 @@ impl <'cursor, T> WriteCursor<'cursor, T> } async fn initiate_garbage_collection(&mut self) -> Result - where T: Send + where T: Send + Decode + Encode { - // We'll dump all alive entries into a new file. let mut cursor_to_intermediate = self.spawn_cursor_to_intermediate_file().await?; + let in_memory_index: BTreeMap> = BTreeMap::new(); + + // We'll dump all alive entries into a new file. + while let Some(live_entry) = self.next_alive().await? { + let file_position = cursor_to_intermediate.append_entry(&live_entry.forget()).await?; + // TODO: Start indexing all of the indexable columns from scratch. + } + // In it there will be only the alive rows. // Afterwards we swap the files, and delete the garbage. todo!() diff --git a/storage_engine/src/entry.rs b/storage_engine/src/entry.rs index c628979..b292051 100644 --- a/storage_engine/src/entry.rs +++ b/storage_engine/src/entry.rs @@ -48,4 +48,11 @@ impl EntryDetailed { .map_err(|e| Error::DecodeError(DecodeErrorKind::EntryData, e))?; Ok(EntryDetailed { header, file_position, data }) } + + pub fn forget(self) -> Entry { + Entry { + header: self.header.into(), + data: self.data, + } + } } diff --git a/storage_engine/src/storage_engine.rs b/storage_engine/src/storage_engine.rs index e2df22e..6113c88 100644 --- a/storage_engine/src/storage_engine.rs +++ b/storage_engine/src/storage_engine.rs @@ -20,7 +20,6 @@ pub type FilePosition = u64; #[derive(Debug)] pub struct Store { pub header: StoreHeader, - pub data_type: PhantomData, pub indexes: StoreIndexes, } @@ -68,7 +67,6 @@ impl Store { let store = Self { header, - data_type: PhantomData::, indexes, }; @@ -171,7 +169,6 @@ impl Store { let store = Self { header, - data_type: PhantomData::, indexes }; Ok(store)