use bincode::{Decode, Encode}; use crate::binary_coding::{encode_sequence, encode_sequence_with_sizes, decode_sequence}; use crate::storage_engine::{Result, FilePosition}; use crate::error::{Error, DecodeErrorKind}; use crate::segments::entry_header::{EntryHeader, EntryHeaderWithDataSize}; #[derive(Debug)] pub struct Entry { pub header: EntryHeader, pub data: Vec, } #[derive(Debug)] pub struct EntryDetailed { pub header: EntryHeaderWithDataSize, pub file_position: FilePosition, pub data: Vec, } impl Entry { pub fn new(data: Vec) -> Self { Self { header: EntryHeader { is_deleted: false }, data } } pub fn new_deleted(data: Vec) -> Self { Self { header: EntryHeader { is_deleted: true}, data } } // FORMAT: [EntryHeaderWithDataSize, ..sequence of data] pub fn encode(&self) -> Result> where T: Encode { let mut result: Vec = 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 EntryDetailed { pub fn decode(header: EntryHeaderWithDataSize, file_position: FilePosition, number_of_columns: usize, bytes: &[u8]) -> Result where T: Decode { let data = decode_sequence::(number_of_columns, bytes) .map_err(|e| Error::DecodeError(DecodeErrorKind::EntryData, e))?; Ok(EntryDetailed { header, file_position, data }) } pub fn forget(&self) -> Entry where T: Clone { Entry { header: self.header.clone().into(), data: self.data.clone(), } } }