It's evening

This commit is contained in:
Nadja Reitzenstein 2021-10-20 20:56:47 +02:00
parent 9e244aab7e
commit 937d271b20
8 changed files with 123 additions and 21 deletions

View File

@ -4,6 +4,7 @@ use std::{
};
use crate::db::Transaction;
use std::fmt::{Debug, Formatter};
/// Memory Fixpoint for a value in the DB
///
@ -40,4 +41,18 @@ impl<'env, T, V> Deref for LMDBorrow<T, V>
// valid pointer so this is safe.
unsafe { self.ptr.as_ref() }
}
}
impl<'env, T, V: Debug> Debug for LMDBorrow<T, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.deref())
}
}
impl<'env, T, V: serde::Serialize> serde::Serialize for LMDBorrow<T, V> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::Serializer
{
self.deref().serialize(serializer)
}
}

View File

@ -14,7 +14,7 @@ pub use lmdb::{
RwTransaction,
};
use rkyv::{Fallible, Serialize, ser::serializers::AllocSerializer, AlignedVec};
use rkyv::{Fallible, Serialize, ser::serializers::AllocSerializer, AlignedVec, Archived};
mod raw;
use raw::RawDB;
@ -60,14 +60,17 @@ pub use user::{
};
use lmdb::Error;
use rkyv::Deserialize;
use rkyv::ser::serializers::AlignedSerializer;
use std::sync::Arc;
use std::path::Path;
use crate::db::user::User;
use crate::db::resources::Resource;
use std::collections::HashMap;
use crate::state::State;
use crate::state::{State, OwnedEntry};
use std::iter::FromIterator;
use std::ops::Deref;
use crate::oid::{ArchivedObjectIdentifier, ObjectIdentifier};
use crate::state::value::SerializeValue;
#[derive(Debug)]
pub enum DBError {
@ -169,21 +172,62 @@ impl Databases {
}
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[derive(Debug, serde::Serialize)]
pub struct Dump {
users: HashMap<String, User>,
passwds: HashMap<String, String>,
resources: HashMap<String, Resource>,
states: HashMap<String, (State, State)>,
}
impl Dump {
pub fn new(dbs: &Databases) -> Result<Self> {
let users = HashMap::from_iter(dbs.userdb.get_all()?.into_iter());
let passwds = HashMap::new();
let resources = HashMap::new();
let states = HashMap::new();
let passwds = HashMap::from_iter(dbs.passdb.get_all()?.into_iter());
let mut states = HashMap::new();
for (name, id) in dbs.resourcedb.get_all()?.into_iter() {
let input = dbs.statedb.get_input(id)?.map(|input| {
let input: &Archived<State> = input.deref();
let hash: u64 = input.hash;
let inner: Vec<OwnedEntry> = input.inner.iter().map(|entry| {
Ok(Self { users, passwds, resources, states })
let oid: &ArchivedObjectIdentifier = &entry.oid;
let bytes: &[u8] = oid.deref();
let mut vec = Vec::with_capacity(bytes.len());
vec.copy_from_slice(bytes);
let oid = ObjectIdentifier::new_unchecked(vec.into_boxed_slice());
let val: Box<dyn SerializeValue> = entry.val
.deserialize(&mut rkyv::Infallible).unwrap();
OwnedEntry { oid, val }
}).collect();
State { hash, inner }
}).unwrap_or(State::build().finish());
let output = dbs.statedb.get_output(id)?.map(|output| {
let output: &Archived<State> = output.deref();
let hash: u64 = output.hash;
let inner: Vec<OwnedEntry> = output.inner.iter().map(|entry| {
let oid: &ArchivedObjectIdentifier = &entry.oid;
let bytes: &[u8] = oid.deref();
let mut vec = Vec::with_capacity(bytes.len());
vec.copy_from_slice(bytes);
let oid = ObjectIdentifier::new_unchecked(vec.into_boxed_slice());
let val: Box<dyn SerializeValue> = entry.val
.deserialize(&mut rkyv::Infallible).unwrap();
OwnedEntry { oid, val }
}).collect();
State { hash, inner }
}).unwrap_or(State::build().finish());
let old = states.insert(name, (input, output));
assert!(old.is_none());
}
Ok(Self { users, passwds, states })
}
}

View File

@ -55,4 +55,19 @@ impl PassDB {
txn.commit()?;
Ok(())
}
pub fn get_all(&self) -> Result<Vec<(String, String)>> {
let txn = self.env.begin_ro_txn()?;
let mut cursor = self.db.open_ro_cursor(&txn)?;
let iter = cursor.iter_start();
let mut out = Vec::new();
for pass in iter {
let (uid, pass) = pass?;
let uid = unsafe { std::str::from_utf8_unchecked(uid).to_string() };
let pass = unsafe { pass.as_str().to_string() };
out.push((uid, pass));
}
Ok(out)
}
}

View File

@ -1,7 +1,6 @@
use rkyv::{Archive, Serialize, Deserialize};
use super::{
AllocAdapter,
DB,
};
use crate::db::AlignedAdapter;
@ -23,16 +22,16 @@ pub struct Resource {
#[derive(Clone)]
pub struct ResourceDB {
env: Arc<Environment>,
db: DB<AllocAdapter<Resource>>,
//db: DB<AllocAdapter<Resource>>,
id_index: DB<AlignedAdapter<u64>>,
}
impl ResourceDB {
pub unsafe fn new(env: Arc<Environment>, db: RawDB, id_index: RawDB) -> Self {
let db = DB::new_unchecked(db);
//let db = DB::new_unchecked(db);
let id_index = DB::new_unchecked(id_index);
Self { env, db, id_index }
Self { env, /*db,*/ id_index }
}
pub unsafe fn open(env: Arc<Environment>) -> Result<Self> {
@ -55,4 +54,19 @@ impl ResourceDB {
})?;
Ok(id)
}
pub fn get_all(&self) -> Result<Vec<(String, u64)>> {
let txn = self.env.begin_ro_txn()?;
let mut cursor = self.id_index.open_ro_cursor(&txn)?;
let iter = cursor.iter_start();
let mut out = Vec::new();
for id in iter {
let (name, id) = id?;
let name = unsafe { std::str::from_utf8_unchecked(name).to_string() };
out.push((name, *id));
}
Ok(out)
}
}

View File

@ -3,7 +3,7 @@ use std::{
path::Path,
};
use rkyv::Archived;
use rkyv::{Archived};
use super::{
DB,

View File

@ -485,6 +485,23 @@ mod serde_support {
}
}
}
impl ser::Serialize for ArchivedObjectIdentifier {
fn serialize<S>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
if serializer.is_human_readable() {
let encoded: String = convert_to_string(self.deref())
.expect("Failed to convert valid OID to String");
serializer.serialize_str(&encoded)
} else {
serializer.serialize_bytes(self.deref())
}
}
}
}
#[cfg(test)]

View File

@ -22,7 +22,7 @@ use rkyv::{
pub mod value;
use value::{SerializeValue, RegisteredImpl};
use crate::state::value::{TypeOid, DynVal, DynOwnedVal};
use crate::state::value::{TypeOid, DynVal, DynOwnedVal, };
use crate::oid::ObjectIdentifier;
use serde::ser::SerializeMap;
use std::ops::Deref;
@ -41,7 +41,7 @@ use serde::de::Error as _;
/// 2. it is serializable and storable in the database
/// 3. it is sendable and forwarded to all Actors and Notifys
pub struct State {
hash: u64,
pub hash: u64,
pub inner: Vec<OwnedEntry>,
}
@ -79,7 +79,6 @@ impl fmt::Debug for State {
}
}
pub struct StateBuilder {
hasher: DefaultHasher,
inner: Vec<OwnedEntry>

View File

@ -8,9 +8,7 @@ use std::{
str::FromStr,
};
use rkyv::{Archive, Archived, Serialize, Deserialize, out_field, Fallible, DeserializeUnsized,
ArchivePointee, ArchiveUnsized, ArchivedMetadata, SerializeUnsized,
};
use rkyv::{Archive, Archived, Serialize, Deserialize, out_field, Fallible, DeserializeUnsized, ArchivePointee, ArchiveUnsized, ArchivedMetadata, SerializeUnsized, };
use rkyv_dyn::{DynSerializer, DynError, DynDeserializer};
use rkyv_typename::TypeName;
use ptr_meta::{DynMetadata, Pointee};
@ -263,7 +261,7 @@ impl ArchiveUnsized for dyn SerializeValue {
#[derive(Debug)]
pub struct ArchivedValueMetadata {
type_oid: Archived<ObjectIdentifier>,
pub type_oid: Archived<ObjectIdentifier>,
}
impl ArchivedValueMetadata {