use serde::Deserialize;
use sled::{IVec, Tree};
use crate::database::{DatabaseError, deserialize_from_ivec};
pub type DbKey = [u8; 32];
pub(crate) fn sled_clear(tree: &Tree, key: &DbKey) -> sled::Result<()> {
tree.remove(key).map(|_| ())
}
pub(crate) fn sled_get_raw(tree: &Tree, key: &DbKey) -> sled::Result<Option<IVec>> {
tree.get(key)
}
pub(crate) fn sled_get_all_raw(tree: &Tree) -> sled::Result<Vec<IVec>> {
tree.iter().values().collect()
}
pub(crate) fn sled_get_batch_raw<I>(tree: &Tree, keys: I) -> Result<Vec<IVec>, DatabaseError>
where
I: IntoIterator<Item = DbKey>,
{
keys.into_iter()
.map(|id| tree.get(id)?.ok_or(DatabaseError::MissingEntry))
.collect::<Result<Vec<IVec>, DatabaseError>>()
}
pub(crate) fn sled_version_from_raw(raw: IVec) -> u32 {
#[derive(Deserialize)]
struct Version {
version: u32,
}
deserialize_from_ivec::<Version>(raw).version
}