fabaccess-bffh/bffhd/db/mod.rs

150 lines
3.3 KiB
Rust
Raw Normal View History

2021-12-06 21:53:42 +01:00
use std::marker::PhantomData;
2021-10-07 16:44:01 +02:00
pub use lmdb::{
Environment,
DatabaseFlags,
WriteFlags,
EnvironmentFlags,
Transaction,
RoTransaction,
RwTransaction,
};
2022-03-15 20:00:43 +01:00
use rkyv::{Fallible, Serialize, ser::serializers::AllocSerializer, AlignedVec};
2021-10-07 16:44:01 +02:00
mod raw;
2021-11-26 21:01:43 +01:00
pub use raw::RawDB;
2021-10-07 16:44:01 +02:00
mod typed;
// re-exports
pub use typed::{
DB,
2021-10-20 18:37:50 +02:00
TypedCursor,
2021-10-07 16:44:01 +02:00
Adapter,
OutputBuffer,
};
mod hash;
pub use hash::{
HashDB,
};
mod fix;
2022-03-10 20:52:34 +01:00
pub mod index;
2021-10-07 16:44:01 +02:00
pub use fix::LMDBorrow;
2021-10-19 11:16:24 +02:00
use lmdb::Error;
2021-10-20 20:56:47 +02:00
use rkyv::Deserialize;
2021-10-19 11:16:24 +02:00
use rkyv::ser::serializers::AlignedSerializer;
2022-03-15 20:00:43 +01:00
use crate::users::db::{User};
2021-10-20 18:37:50 +02:00
use std::collections::HashMap;
2022-03-15 16:27:41 +01:00
use std::fmt::{Display, Formatter};
2022-03-13 20:11:37 +01:00
use rkyv::Infallible;
2022-03-15 20:00:43 +01:00
use crate::resources::state::{State};
2021-10-20 18:37:50 +02:00
use std::iter::FromIterator;
2021-10-20 20:56:47 +02:00
use std::ops::Deref;
2022-03-13 20:11:37 +01:00
use crate::resources::search::ResourcesHandle;
2022-03-15 20:00:43 +01:00
2022-03-15 19:56:41 +01:00
use crate::Users;
2021-10-18 11:27:42 +02:00
#[derive(Debug)]
pub enum DBError {
LMDB(lmdb::Error),
RKYV(<AllocSerializer<1024> as Fallible>::Error),
}
2022-03-15 16:27:41 +01:00
impl Display for DBError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::LMDB(e) => write!(f, "LMDB error: {}", e),
Self::RKYV(e) => write!(f, "rkyv error: {}", e),
}
}
}
impl std::error::Error for DBError { }
2021-10-20 18:37:50 +02:00
pub(crate) type Result<T> = std::result::Result<T, DBError>;
impl From<lmdb::Error> for DBError {
fn from(e: lmdb::Error) -> Self {
Self::LMDB(e)
}
}
type Ser = AllocSerializer<1024>;
#[derive(Clone)]
2021-11-26 21:01:43 +01:00
pub struct AllocAdapter<V> {
phantom: PhantomData<V>,
}
impl<V> Fallible for AllocAdapter<V> {
type Error = DBError;
}
impl<V: Serialize<Ser>> Adapter for AllocAdapter<V> {
type Serializer = Ser;
type Value = V;
fn new_serializer() -> Self::Serializer {
Self::Serializer::default()
}
fn from_ser_err(e: <Self::Serializer as Fallible>::Error) -> Self::Error {
DBError::RKYV(e)
}
fn from_db_err(e: lmdb::Error) -> Self::Error {
e.into()
}
2021-10-19 11:16:24 +02:00
}
2021-10-28 01:10:35 +02:00
#[derive(Copy, Clone, Debug)]
2021-10-19 11:16:24 +02:00
pub struct AlignedAdapter<V> {
phantom: PhantomData<V>,
}
impl<V> Fallible for AlignedAdapter<V> {
type Error = lmdb::Error;
}
impl<V: Serialize<AlignedSerializer<AlignedVec>>> Adapter for AlignedAdapter<V> {
type Serializer = AlignedSerializer<AlignedVec>;
type Value = V;
fn new_serializer() -> Self::Serializer {
Self::Serializer::default()
}
fn from_ser_err(_: <Self::Serializer as Fallible>::Error) -> <Self as Fallible>::Error {
unreachable!()
}
fn from_db_err(e: Error) -> <Self as Fallible>::Error {
e
}
2021-10-20 18:37:50 +02:00
}
2021-10-20 20:56:47 +02:00
#[derive(Debug, serde::Serialize)]
2021-10-20 18:37:50 +02:00
pub struct Dump {
users: HashMap<String, User>,
2022-03-13 20:11:37 +01:00
states: HashMap<String, State>,
2021-10-20 18:37:50 +02:00
}
impl Dump {
2022-03-15 19:56:41 +01:00
pub fn new(userdb: Users, resources: ResourcesHandle) -> Result<Self> {
let users = HashMap::from_iter(userdb.into_inner().get_all()?.into_iter());
2021-10-20 20:56:47 +02:00
let mut states = HashMap::new();
2022-03-13 20:11:37 +01:00
for resource in resources.list_all().into_iter() {
if let Some(output) = resource.get_raw_state() {
let output: State = Deserialize::<State, _>::deserialize(output.deref(), &mut Infallible).unwrap();
let old = states.insert(resource.get_id().to_string(), output);
assert!(old.is_none());
}
2021-10-20 20:56:47 +02:00
}
2022-03-15 19:56:41 +01:00
Ok(Self { users, states })
2021-10-20 18:37:50 +02:00
}
2021-10-18 11:27:42 +02:00
}