67 lines
2.2 KiB
Rust
67 lines
2.2 KiB
Rust
use async_trait::async_trait;
|
|
|
|
use bincode;
|
|
use bincode::Encode;
|
|
use crate::binary_coding::encode;
|
|
|
|
use crate::segments::entry::Entry;
|
|
use crate::segments::store_header::StoreHeader;
|
|
use crate::store::{FilePosition, Result};
|
|
use crate::cursor_capabilities::primitive::{CursorCanRead, CursorCanWrite};
|
|
use crate::cursor_capabilities::traversal::CursorCanTraverse;
|
|
|
|
#[async_trait]
|
|
pub trait CursorCanModifyEntries<T>: CursorCanTraverse<T> + CursorCanWrite<T> {
|
|
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::<usize>(&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::<usize>(&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<u8> = 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<T>) -> Result<FilePosition>
|
|
where T: Encode + Send + Sync
|
|
{
|
|
self.increment_total_count().await?;
|
|
|
|
let encoded_entry: Vec<u8> = 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)
|
|
}
|
|
}
|