Cleanup
This commit is contained in:
parent
2261fe39de
commit
3bf04ae2d6
7 changed files with 194 additions and 215 deletions
57
storage_engine/src/entry.rs
Normal file
57
storage_engine/src/entry.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use bincode::{Decode, Encode};
|
||||
|
||||
use crate::binary_coding::{encode, encode_sequence, encode_sequence_with_sizes, decode_sequence};
|
||||
use crate::storage_engine::Result;
|
||||
use crate::error::{Error, DecodeErrorKind};
|
||||
use crate::entry_header::{EntryHeader, EntryHeaderWithDataSize};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Entry<T> {
|
||||
header: EntryHeader,
|
||||
data: Vec<T>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EntryDetailed<T> {
|
||||
header: EntryHeaderWithDataSize,
|
||||
data: Vec<T>,
|
||||
}
|
||||
|
||||
impl EntryHeader {
|
||||
fn encode(self: &EntryHeader) -> Result<Vec<u8>> {
|
||||
let result: Vec<u8> = encode(&self.is_deleted)?;
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl <T>Entry<T> {
|
||||
pub fn new(data: Vec<T>) -> Self {
|
||||
Self { header: EntryHeader { is_deleted: false }, data }
|
||||
}
|
||||
|
||||
pub fn new_deleted(data: Vec<T>) -> Self {
|
||||
Self { header: EntryHeader { is_deleted: true}, data }
|
||||
}
|
||||
|
||||
// FORMAT: [EntryHeaderWithDataSize, ..sequence of data]
|
||||
pub fn encode(&self) -> Result<Vec<u8>>
|
||||
where T: Encode
|
||||
{
|
||||
let mut result: Vec<u8> = self.header.encode()?;
|
||||
|
||||
let (mut encoded_data, sizes) = encode_sequence_with_sizes(&self.data[..])?;
|
||||
result.append(&mut encode_sequence(&sizes)?); // sizes of data (fixed by number of columns)
|
||||
result.append(&mut encoded_data); // data variable size
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl <T>EntryDetailed<T> {
|
||||
pub fn decode(header: EntryHeaderWithDataSize, number_of_columns: usize, bytes: &[u8]) -> Result<Self>
|
||||
where T: Decode
|
||||
{
|
||||
let data = decode_sequence::<T>(number_of_columns, bytes)
|
||||
.map_err(|e| Error::DecodeError(DecodeErrorKind::EntryData, e))?;
|
||||
Ok(EntryDetailed { header, data })
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue