diff --git a/Cargo.lock b/Cargo.lock index b4ef77b..1f00811 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -398,6 +398,7 @@ dependencies = [ "libc", "lmdb-rkv", "ptr_meta", + "rand", "rkyv", "rkyv_dyn", "rkyv_typename", diff --git a/Cargo.toml b/Cargo.toml index b11e449..6308631 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,6 +85,7 @@ walkdir = "2.3.2" futures-test = "0.3.16" tempfile = "3.2" bincode = "2.0.0-dev" +rand = "0.8" [patch.crates-io] bincode = { git = "https://github.com/dequbed/bincode.git", branch = "feature/in_place_buffer" } diff --git a/src/db/mod.rs b/src/db/mod.rs index 7f56833..96f0a66 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -53,6 +53,7 @@ pub use resources::{ ResourceDB, }; +#[derive(Debug)] pub enum DBError { LMDB(lmdb::Error), RKYV( as Fallible>::Error), @@ -88,5 +89,4 @@ impl> Adapter for AllocAdapter { fn from_db_err(e: lmdb::Error) -> Self::Error { e.into() } -} - +} \ No newline at end of file diff --git a/src/db/state.rs b/src/db/state.rs index 5071a4a..f876bc2 100644 --- a/src/db/state.rs +++ b/src/db/state.rs @@ -149,54 +149,38 @@ impl StateAccessor { } } -#[cfg(test_dis)] +#[cfg(test)] mod tests { use super::*; - use crate::db::tests::open_test_env; - use lmdb::{ - EnvironmentFlags as EF, - DatabaseFlags as DF, - WriteFlags as WF, - }; - - use rkyv::Infallible; - use rkyv::ser::serializers::AllocSerializer; - use rkyv::archived_root; - use rkyv::util::archived_value; + use crate::state::value::Vec3u8; + use crate::state::value::{OID_COLOUR, OID_POWERED, OID_INTENSITY}; + use std::ops::Deref; #[test] fn construct_state() { + let tmpdir = tempfile::tempdir().unwrap(); + let mut tmppath = tmpdir.path().to_owned(); + tmppath.push("db"); + let db = StateDB::init(tmppath).unwrap(); let b = State::build() - .add("Colour".to_string(), Vec3u8 { a: 1, b: 2, c: 3}) - .add("Powered".to_string(), Bool(true)) - .add("Intensity".to_string(), UInt32(4242)) + .add(OID_COLOUR.clone(), Box::new(Vec3u8 { a: 1, b: 2, c: 3})) + .add(OID_POWERED.clone(), Box::new(true)) + .add(OID_INTENSITY.clone(), Box::new(1023)) .finish(); - println!("({}) {:?}", b.hash(), b); - let mut serializer = AllocSerializer::<256>::default(); - let pos = serializer.serialize_value(&b).unwrap(); - let buf = serializer.into_serializer().into_inner(); + let c = State::build() + .add(OID_COLOUR.clone(), Box::new(Vec3u8 { a: 1, b: 2, c: 3})) + .add(OID_POWERED.clone(), Box::new(true)) + .add(OID_INTENSITY.clone(), Box::new(1023)) + .finish(); - println!("Encsize: {}", buf.len()); - - let archived_state = unsafe { - archived_value::(buf.as_ref(), pos) - }; - let s: State = archived_state.deserialize(&mut Infallible).unwrap(); - - println!("({}) {:?}", pos, s); - } - - #[test] - fn function_name_test() { - let te = open_text_env(); - let ildb = e.create_db(Some("input"), DF::empty()).expect("Failed to create db file"); - let oldb = e.create_db(Some("output"), DF::empty()).expect("Failed to create db file"); - - let idb = DB::new(e.env.clone(), ildb); - let odb = DB::new(e.env.clone(), oldb); - let db = StateDB::new(idb, odb); + let key = rand::random(); + db.update(key, &b, &c).unwrap(); + let d = db.get_input(key).unwrap().unwrap(); + let e = db.get_output(key).unwrap().unwrap(); + assert_eq!(&b, d.deref()); + assert_eq!(&c, e.deref()); } } diff --git a/src/state/mod.rs b/src/state/mod.rs index 0e4ea71..6a16b53 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -33,6 +33,7 @@ use serde::de::Error as _; #[derive(serde::Serialize, serde::Deserialize)] #[derive(Archive, Serialize, Deserialize)] +#[archive_attr(derive(Debug))] /// State object of a resource /// /// This object serves three functions: @@ -59,6 +60,12 @@ impl PartialEq for State { } } +impl PartialEq> for State { + fn eq(&self, other: &Archived) -> bool { + self.hash == other.hash + } +} + impl Eq for State {} impl fmt::Debug for State { @@ -117,6 +124,7 @@ pub struct Entry<'a> { } #[derive(Debug, Archive, Serialize, Deserialize)] +#[archive_attr(derive(Debug))] pub struct OwnedEntry { pub oid: ObjectIdentifier, pub val: Box, diff --git a/src/state/value.rs b/src/state/value.rs index b79b6be..18874ff 100644 --- a/src/state/value.rs +++ b/src/state/value.rs @@ -261,6 +261,7 @@ impl ArchiveUnsized for dyn SerializeValue { } } +#[derive(Debug)] pub struct ArchivedValueMetadata { type_oid: Archived, } @@ -465,36 +466,46 @@ lazy_static::lazy_static! { pub static ref OID_U8: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.2").unwrap() }; - static ref OID_U16: ObjectIdentifier = { + pub static ref OID_U16: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.3").unwrap() }; - static ref OID_U32: ObjectIdentifier = { + pub static ref OID_U32: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.4").unwrap() }; - static ref OID_U64: ObjectIdentifier = { + pub static ref OID_U64: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.5").unwrap() }; - static ref OID_U128: ObjectIdentifier = { + pub static ref OID_U128: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.6").unwrap() }; - static ref OID_I8: ObjectIdentifier = { + pub static ref OID_I8: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.7").unwrap() }; - static ref OID_I16: ObjectIdentifier = { + pub static ref OID_I16: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.8").unwrap() }; - static ref OID_I32: ObjectIdentifier = { + pub static ref OID_I32: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.9").unwrap() }; - static ref OID_I64: ObjectIdentifier = { + pub static ref OID_I64: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.10").unwrap() }; - static ref OID_I128: ObjectIdentifier = { + pub static ref OID_I128: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.11").unwrap() }; - static ref OID_VEC3U8: ObjectIdentifier = { + pub static ref OID_VEC3U8: ObjectIdentifier = { ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.1.13").unwrap() }; + + pub static ref OID_POWERED: ObjectIdentifier = { + ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.2.1").unwrap() + }; + pub static ref OID_INTENSITY: ObjectIdentifier = { + ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.2.2").unwrap() + }; + pub static ref OID_COLOUR: ObjectIdentifier = { + ObjectIdentifier::from_str("1.3.6.1.4.1.48398.612.2.3").unwrap() + }; } oidvalue!(OID_BOOL, bool); oidvalue!(OID_U8, u8);