use std::marker::PhantomData; pub use lmdb::{ Environment, DatabaseFlags, WriteFlags, EnvironmentFlags, Transaction, RoTransaction, RwTransaction, }; use rkyv::{Fallible, Serialize, ser::serializers::AllocSerializer, AlignedVec, Archived}; mod raw; pub use raw::RawDB; mod typed; // re-exports pub use typed::{ DB, TypedCursor, Adapter, OutputBuffer, }; mod hash; pub use hash::{ HashDB, }; mod fix; pub mod index; pub use fix::LMDBorrow; use lmdb::Error; use rkyv::Deserialize; use rkyv::ser::serializers::AlignedSerializer; use std::sync::Arc; use std::path::Path; use crate::users::db::{User, UserDB}; use std::collections::HashMap; use rkyv::Infallible; use crate::resources::state::{State, db::StateDB}; use std::iter::FromIterator; use std::ops::Deref; use crate::authentication::db::PassDB; use crate::resources::search::ResourcesHandle; use crate::utils::oid::{ArchivedObjectIdentifier, ObjectIdentifier}; use crate::resources::state::value::SerializeValue; #[derive(Debug)] pub enum DBError { LMDB(lmdb::Error), RKYV( as Fallible>::Error), } pub(crate) type Result = std::result::Result; impl From for DBError { fn from(e: lmdb::Error) -> Self { Self::LMDB(e) } } type Ser = AllocSerializer<1024>; #[derive(Clone)] pub struct AllocAdapter { phantom: PhantomData, } impl Fallible for AllocAdapter { type Error = DBError; } impl> Adapter for AllocAdapter { type Serializer = Ser; type Value = V; fn new_serializer() -> Self::Serializer { Self::Serializer::default() } fn from_ser_err(e: ::Error) -> Self::Error { DBError::RKYV(e) } fn from_db_err(e: lmdb::Error) -> Self::Error { e.into() } } #[derive(Copy, Clone, Debug)] pub struct AlignedAdapter { phantom: PhantomData, } impl Fallible for AlignedAdapter { type Error = lmdb::Error; } impl>> Adapter for AlignedAdapter { type Serializer = AlignedSerializer; type Value = V; fn new_serializer() -> Self::Serializer { Self::Serializer::default() } fn from_ser_err(_: ::Error) -> ::Error { unreachable!() } fn from_db_err(e: Error) -> ::Error { e } } #[derive(Debug, serde::Serialize)] pub struct Dump { users: HashMap, passwds: HashMap, states: HashMap, } impl Dump { pub fn new(userdb: UserDB, passdb: PassDB, resources: ResourcesHandle) -> Result { let users = HashMap::from_iter(userdb.get_all()?.into_iter()); let passwds = HashMap::from_iter(passdb.get_all()?.into_iter()); let mut states = HashMap::new(); for resource in resources.list_all().into_iter() { if let Some(output) = resource.get_raw_state() { let output: State = Deserialize::::deserialize(output.deref(), &mut Infallible).unwrap(); let old = states.insert(resource.get_id().to_string(), output); assert!(old.is_none()); } } Ok(Self { users, passwds, states }) } }