Split cursor capabilities
This commit is contained in:
parent
f18fd3a796
commit
edfecfa8d6
7 changed files with 438 additions and 411 deletions
62
storage_engine/src/cursor_capabilities/primitive.rs
Normal file
62
storage_engine/src/cursor_capabilities/primitive.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
use tokio::io::{AsyncReadExt, AsyncWriteExt, AsyncSeekExt, SeekFrom};
|
||||
use tokio::fs::File;
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::storage_engine::{FilePosition, Result};
|
||||
|
||||
#[async_trait]
|
||||
pub(crate) trait CursorCanRead<T> {
|
||||
fn file(&mut self) -> &mut File;
|
||||
fn eof_file_position(&self) -> FilePosition;
|
||||
|
||||
async fn read_bytes(&mut self, bytes: &mut [u8]) -> Result<()> {
|
||||
self.file().read_exact(bytes).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_bytes(&mut self, count: usize) -> Result<Vec<u8>> {
|
||||
let mut result: Vec<u8> = Vec::with_capacity(count);
|
||||
self.read_bytes(&mut result).await?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn seek_to(&mut self, file_position: FilePosition) -> Result<FilePosition> {
|
||||
let file_position = self.file().seek(SeekFrom::Start(file_position)).await?;
|
||||
Ok(file_position)
|
||||
}
|
||||
|
||||
// Start of the file i.e. the Header, not the entries.
|
||||
async fn seek_to_start(&mut self) -> Result<FilePosition> {
|
||||
let file_position = self.file().seek(SeekFrom::Start(0)).await?;
|
||||
Ok(file_position)
|
||||
}
|
||||
|
||||
async fn seek_to_end(&mut self) -> Result<FilePosition> {
|
||||
let file_position = self.file().seek(SeekFrom::End(0)).await?;
|
||||
Ok(file_position)
|
||||
}
|
||||
|
||||
// Seeks from current position by offset and returns new file position
|
||||
async fn seek_by(&mut self, offset: i64) -> Result<FilePosition> {
|
||||
let file_position = self.file().seek(SeekFrom::Current(offset)).await?;
|
||||
Ok(file_position)
|
||||
}
|
||||
|
||||
async fn current_file_position(&mut self) -> Result<FilePosition> {
|
||||
let next_file_position: FilePosition = self.file().stream_position().await?;
|
||||
Ok(next_file_position)
|
||||
}
|
||||
|
||||
async fn is_at_eof(&mut self) -> Result<bool> {
|
||||
let current_file_position = self.current_file_position().await?;
|
||||
let eof_file_position = self.eof_file_position();
|
||||
Ok(current_file_position == eof_file_position)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub(crate) trait CursorCanWrite<T>: CursorCanRead<T> {
|
||||
async fn write_bytes(&mut self, bytes: &[u8]) -> Result<usize> {
|
||||
Ok(self.file().write(bytes).await?)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue