use async_trait::async_trait; use crate::binary_coding::encode; use bincode; use bincode::Encode; use crate::cursor_capabilities::primitive::CursorCanWrite; use crate::cursor_capabilities::traversal::CursorCanTraverse; use crate::segments::entry::Entry; use crate::segments::store_header::StoreHeader; use crate::store::{FilePosition, Result}; #[async_trait] pub trait CursorCanModifyEntries: CursorCanTraverse + CursorCanWrite { fn header_mut(&mut self) -> &mut StoreHeader; fn set_eof_file_position(&mut self, new_file_position: FilePosition); // ===Store Header Manipulation=== async fn increment_total_count(&mut self) -> Result<()> where T: Send, { self.seek_to_start().await?; self.seek_to(StoreHeader::TOTAL_COUNT_OFFSET as u64).await?; let new_count = self.header_mut().increment_total_count(); self.write_bytes(&encode::(&new_count)?).await?; Ok(()) } async fn increment_deleted_count(&mut self) -> Result<()> where T: Send, { self.seek_to_start().await?; self.seek_to(StoreHeader::DELETED_COUNT_OFFSET as u64) .await?; let new_count = self.header_mut().increment_deleted_count(); self.write_bytes(&encode::(&new_count)?).await?; Ok(()) } async fn set_header(&mut self, header: &StoreHeader) -> Result<()> where T: Send, { self.seek_to_start().await?; let encoded_header: Vec = header.encode()?; self.write_bytes(&encoded_header).await?; Ok(()) } // ===Append Entry=== // Moves cursor to the end. // Returns file position to the start of the new entry. async fn append_entry_no_indexing(&mut self, entry: &Entry) -> Result where T: Encode + Send + Sync, { self.increment_total_count().await?; let encoded_entry: Vec = entry.encode()?; let file_position = self.seek_to_end().await?; self.write_bytes(&encoded_entry).await?; let eof_file_position: FilePosition = self.current_file_position().await?; self.set_eof_file_position(eof_file_position); Ok(file_position) } }