use std::fs::File;
use std::io::Write;
pub const SEGMENT_SEPARATOR: u8 = 0xAC;
pub trait Writer {
fn file(&self) -> File;
fn file_index(&self) -> usize;
fn persist(&mut self, key: &str, value: bson::Bson) -> Result<String, String> {
let temp_doc = bson::doc! {
"_k": key,
"_v": value,
};
let mut file = self.file();
let offset = file.metadata().unwrap().len();
let mut bytes = bson::to_vec(&temp_doc).unwrap();
bytes.push(SEGMENT_SEPARATOR);
file.write_all(&bytes).unwrap();
Ok(format!("{}_{}", self.file_index(), offset))
}
}