use std::collections::HashMap;
use crate::error::Result;
use crate::file_store::FileStore;
use crate::record::{Record, RecordId};
use crate::store::MemoryStore;
#[derive(Debug)]
pub(crate) enum Backend {
Memory(MemoryStore),
File(FileStore),
}
impl Backend {
pub(crate) fn upsert(&self, record: Record) -> Result<()> {
match self {
Self::Memory(store) => store.upsert(record),
Self::File(store) => store.upsert(record),
}
}
pub(crate) fn get(&self, id: RecordId) -> Result<Option<Record>> {
match self {
Self::Memory(store) => store.get(id),
Self::File(store) => store.get(id),
}
}
pub(crate) fn delete(&self, id: RecordId) -> Result<bool> {
match self {
Self::Memory(store) => store.delete(id),
Self::File(store) => store.delete(id),
}
}
pub(crate) fn len(&self) -> usize {
match self {
Self::Memory(store) => store.len(),
Self::File(store) => store.len(),
}
}
pub(crate) fn is_empty(&self) -> bool {
match self {
Self::Memory(store) => store.is_empty(),
Self::File(store) => store.is_empty(),
}
}
pub(crate) fn with_records<F, R>(&self, f: F) -> R
where
F: FnOnce(&HashMap<RecordId, Record>) -> R,
{
match self {
Self::Memory(store) => store.with_records(f),
Self::File(store) => store.with_records(f),
}
}
pub(crate) fn flush(&self) -> Result<()> {
match self {
Self::Memory(_) => Ok(()),
Self::File(store) => store.flush(),
}
}
pub(crate) fn close(self) -> Result<()> {
match self {
Self::Memory(_) => Ok(()),
Self::File(store) => store.compact(),
}
}
}