Introduce total_count
This commit is contained in:
parent
cad4ba8215
commit
2f23df1009
3 changed files with 61 additions and 8 deletions
|
|
@ -36,11 +36,17 @@ pub struct Store<T> {
|
|||
pub struct StoreHeader {
|
||||
number_of_columns: usize,
|
||||
deleted_count: usize,
|
||||
total_count: usize,
|
||||
}
|
||||
impl StoreHeader {
|
||||
const NUMBER_OF_COLUMNS_SIZE: usize = size_of::<usize>();
|
||||
const DELETED_COUNT_SIZE: usize = size_of::<usize>();
|
||||
const SIZE: usize = Self::NUMBER_OF_COLUMNS_SIZE + Self::DELETED_COUNT_SIZE;
|
||||
const TOTAL_COUNT_SIZE: usize = size_of::<usize>();
|
||||
const SIZE: usize = Self::NUMBER_OF_COLUMNS_SIZE + Self::DELETED_COUNT_SIZE + Self::TOTAL_COUNT_SIZE;
|
||||
|
||||
const NUMBER_OF_COLUMNS_OFFSET: usize = 0;
|
||||
const DELETED_COUNT_OFFSET: usize = Self::NUMBER_OF_COLUMNS_OFFSET + Self::NUMBER_OF_COLUMNS_SIZE;
|
||||
const TOTAL_COUNT_OFFSET: usize = Self::DELETED_COUNT_OFFSET + Self::DELETED_COUNT_SIZE;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -153,6 +159,7 @@ impl <T>Store<T> {
|
|||
let header = StoreHeader {
|
||||
number_of_columns,
|
||||
deleted_count: 0,
|
||||
total_count: 0,
|
||||
};
|
||||
let encoded_header: Vec<u8> = header.encode()?;
|
||||
|
||||
|
|
@ -192,17 +199,46 @@ impl <T>Store<T> {
|
|||
}
|
||||
|
||||
// ===Append Entry===
|
||||
async fn increment_total_count(&mut self) -> Result<()> {
|
||||
self.seek_to_start().await?;
|
||||
self.seek_to(StoreHeader::TOTAL_COUNT_OFFSET as u64).await?;
|
||||
let new_count = self.header.increment_total_count();
|
||||
self.write_bytes(&encode::<usize>(&new_count)?).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn increment_deleted_count(&mut self) -> Result<()> {
|
||||
self.seek_to_start().await?;
|
||||
self.seek_to(StoreHeader::DELETED_COUNT_OFFSET as u64).await?;
|
||||
let new_count = self.header.increment_deleted_count();
|
||||
self.write_bytes(&encode::<usize>(&new_count)?).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Moves cursor to the end.
|
||||
pub async fn append_entry(&mut self, entry: &Entry<T>) -> Result<Cursor>
|
||||
where T: Encode
|
||||
{
|
||||
self.increment_total_count().await?;
|
||||
|
||||
let encoded_entry: Vec<u8> = entry.encode()?;
|
||||
self.seek_to_end().await?;
|
||||
let cursor: Cursor = self.current_cursor().await?;
|
||||
self.write_bytes(&encoded_entry).await?;
|
||||
|
||||
Ok(cursor)
|
||||
}
|
||||
|
||||
// ===Deletion===
|
||||
pub async fn mark_deleted_at(&mut self, cursor: Cursor) -> Result<()> {
|
||||
self.increment_deleted_count().await?;
|
||||
|
||||
self.seek_to(cursor).await?;
|
||||
|
||||
// TODO: Now you need to mutate the entry itself
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
||||
// ===Lookup===
|
||||
// WARNING: The cursor has to be at the start of an entry. Otherwise garbage data will be
|
||||
|
|
@ -266,6 +302,7 @@ impl StoreHeader {
|
|||
// FORMAT: First Number of Columns, Then Deleted Count.
|
||||
let mut result = encode(&self.number_of_columns)?;
|
||||
result.append(&mut encode(&self.deleted_count)?);
|
||||
result.append(&mut encode(&self.total_count)?);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
|
|
@ -274,20 +311,35 @@ impl StoreHeader {
|
|||
}
|
||||
|
||||
async fn decode(result: &mut [u8]) -> Result<StoreHeader> {
|
||||
let offset = 0;
|
||||
let (number_of_columns, offset) =
|
||||
decode::<usize>(&result[offset..offset + Self::NUMBER_OF_COLUMNS_SIZE])
|
||||
let (number_of_columns, _) =
|
||||
decode::<usize>(&result[Self::NUMBER_OF_COLUMNS_OFFSET..Self::NUMBER_OF_COLUMNS_OFFSET + Self::NUMBER_OF_COLUMNS_SIZE])
|
||||
.map_err(|e| Error::DecodeError(DecodeErrorKind::StoreHeaderNumberOfColumns, e))?;
|
||||
let (deleted_count, _) =
|
||||
decode::<usize>(&result[offset..offset + Self::DELETED_COUNT_SIZE])
|
||||
decode::<usize>(&result[Self::DELETED_COUNT_OFFSET..Self::DELETED_COUNT_OFFSET + Self::DELETED_COUNT_SIZE])
|
||||
.map_err(|e| Error::DecodeError(DecodeErrorKind::StoreHeaderDeletedCount, e))?;
|
||||
let (total_count, _offset) =
|
||||
decode::<usize>(&result[Self::TOTAL_COUNT_OFFSET..Self::TOTAL_COUNT_OFFSET + Self::TOTAL_COUNT_SIZE])
|
||||
.map_err(|e| Error::DecodeError(DecodeErrorKind::StoreHeaderTotalCount, e))?;
|
||||
let header = StoreHeader {
|
||||
number_of_columns,
|
||||
deleted_count,
|
||||
total_count,
|
||||
};
|
||||
|
||||
Ok(header)
|
||||
}
|
||||
|
||||
// returns new count
|
||||
fn increment_total_count(&mut self) -> usize {
|
||||
self.total_count += 1;
|
||||
self.total_count
|
||||
}
|
||||
|
||||
// returns new count
|
||||
fn increment_deleted_count(&mut self) -> usize {
|
||||
self.deleted_count += 1;
|
||||
self.deleted_count
|
||||
}
|
||||
}
|
||||
|
||||
// ====Entry====
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue