Cleanup
This commit is contained in:
parent
2261fe39de
commit
3bf04ae2d6
7 changed files with 194 additions and 215 deletions
72
storage_engine/src/store_header.rs
Normal file
72
storage_engine/src/store_header.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
use crate::binary_coding::{encode, decode};
|
||||
use crate::storage_engine::{Result, Column};
|
||||
use crate::error::{Error, DecodeErrorKind};
|
||||
use std::mem::size_of;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StoreHeader {
|
||||
pub number_of_columns: usize,
|
||||
pub deleted_count: usize,
|
||||
pub total_count: usize,
|
||||
pub primary_column: Column,
|
||||
}
|
||||
|
||||
impl StoreHeader {
|
||||
pub const NUMBER_OF_COLUMNS_SIZE: usize = size_of::<usize>();
|
||||
pub const DELETED_COUNT_SIZE: usize = size_of::<usize>();
|
||||
pub const TOTAL_COUNT_SIZE: usize = size_of::<usize>();
|
||||
pub const PRIMARY_COLUMN_SIZE: usize = size_of::<Column>();
|
||||
pub const SIZE: usize = Self::NUMBER_OF_COLUMNS_SIZE + Self::DELETED_COUNT_SIZE + Self::TOTAL_COUNT_SIZE + Self::PRIMARY_COLUMN_SIZE;
|
||||
|
||||
pub const NUMBER_OF_COLUMNS_OFFSET: usize = 0;
|
||||
pub const DELETED_COUNT_OFFSET: usize = Self::NUMBER_OF_COLUMNS_OFFSET + Self::NUMBER_OF_COLUMNS_SIZE;
|
||||
pub const TOTAL_COUNT_OFFSET: usize = Self::DELETED_COUNT_OFFSET + Self::DELETED_COUNT_SIZE;
|
||||
pub const PRIMARY_COLUMN_OFFSET: usize = Self::TOTAL_COUNT_OFFSET + Self::TOTAL_COUNT_SIZE;
|
||||
|
||||
pub fn encode(&self) -> Result<Vec<u8>> {
|
||||
let mut result = encode(&self.number_of_columns)?;
|
||||
result.append(&mut encode(&self.deleted_count)?);
|
||||
result.append(&mut encode(&self.total_count)?);
|
||||
result.append(&mut encode(&self.primary_column)?);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn decode_buffer() -> [u8; StoreHeader::SIZE] {
|
||||
[0; StoreHeader::SIZE]
|
||||
}
|
||||
|
||||
pub async fn decode(result: &mut [u8]) -> Result<StoreHeader> {
|
||||
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[Self::DELETED_COUNT_OFFSET..Self::DELETED_COUNT_OFFSET + Self::DELETED_COUNT_SIZE])
|
||||
.map_err(|e| Error::DecodeError(DecodeErrorKind::StoreHeaderDeletedCount, e))?;
|
||||
let (total_count, _) =
|
||||
decode::<usize>(&result[Self::TOTAL_COUNT_OFFSET..Self::TOTAL_COUNT_OFFSET + Self::TOTAL_COUNT_SIZE])
|
||||
.map_err(|e| Error::DecodeError(DecodeErrorKind::StoreHeaderTotalCount, e))?;
|
||||
let (primary_column, _) =
|
||||
decode::<Column>(&result[Self::PRIMARY_COLUMN_OFFSET..Self::PRIMARY_COLUMN_OFFSET + Self::PRIMARY_COLUMN_SIZE])
|
||||
.map_err(|e| Error::DecodeError(DecodeErrorKind::StoreHeaderPrimaryColumn, e))?;
|
||||
let header = StoreHeader {
|
||||
number_of_columns,
|
||||
deleted_count,
|
||||
total_count,
|
||||
primary_column,
|
||||
};
|
||||
|
||||
Ok(header)
|
||||
}
|
||||
|
||||
// returns new count
|
||||
pub fn increment_total_count(&mut self) -> usize {
|
||||
self.total_count += 1;
|
||||
self.total_count
|
||||
}
|
||||
|
||||
// returns new count
|
||||
pub fn increment_deleted_count(&mut self) -> usize {
|
||||
self.deleted_count += 1;
|
||||
self.deleted_count
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue