fix composite tuple serialization

This commit is contained in:
TudbuT 2023-06-24 18:45:45 +02:00
parent 6905778cd7
commit cdd2b30eb0
3 changed files with 57 additions and 5 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "microdb"
version = "0.3.3"
version = "0.3.4"
edition = "2021"
description = "A very small in-program database with cache, disk storage, etc."
license = "MIT"

View file

@ -42,23 +42,23 @@ macro_rules! impl_tuple {
let ($(ident!(v $tvarn),)*) = self;
let mut i = 0_u64;
$(
ComObj::to_db(ident!(v $tvarn), path.sub_path(i), db)?;
db.set_com(path.sub_path(i), ident!(v $tvarn))?;
i += 1;
)*
db.set_raw(path, i)
db.set_raw_hard(path, i)
}
fn remove<P: Path>(path: P, db: &MicroDB) -> Result<(), io::Error> {
<Vec<()> as ComObj>::remove::<P>(path, db) // tuples have identical layout to vecs
}
#[allow(unused_assignments, unused_variables, unused_mut)]
#[allow(unused_variables, unused_mut)]
fn from_db<P: Path>(path: P, db: &MicroDB) -> Result<Option<Self>, io::Error> {
let mut i = 0_u64;
let tup = (
$(
{
let value = extract!(<$tvarn as ComObj>::from_db(path.sub_path(i), db));
let value = extract!(db.get_com::<$tvarn, _>(path.sub_path(i)));
i += 1;
value
},
@ -166,4 +166,42 @@ mod tests {
fs::remove_file("tuples.test10.dmdb").unwrap();
fs::remove_file("tuples.test10.mmdb").unwrap();
}
#[test]
fn test_10_com() {
let db = MicroDB::create("tuples.test10c.dmdb", "tuples.test10c.mmdb", 100, 100).unwrap();
db.set_com(
"10tuple",
(
1u8,
"hii".to_owned(),
3u8,
4u32,
5u128,
"6".to_owned(),
"7".to_owned(),
8u8,
9i32,
10i128,
),
)
.unwrap();
assert_eq!(
db.get_com("10tuple").unwrap(),
Some((
1u8,
"hii".to_owned(),
3u8,
4u32,
5u128,
"6".to_owned(),
"7".to_owned(),
8u8,
9i32,
10i128
))
);
db.shutdown().unwrap();
fs::remove_file("tuples.test10c.dmdb").unwrap();
fs::remove_file("tuples.test10c.mmdb").unwrap();
}
}

View file

@ -98,6 +98,20 @@ impl MicroDB {
self.storage.set(&path, object.to_db())
}
/// Sets an item in the database at the path.
/// Here, the item is saved in a single blob at the path.
///
/// # Safety
///
/// This function will not clean up the substructure if there was a composite item here.
/// It may create database junk until the next time that substructure is cleaned by some
/// other function. Use this only if you know that the types of the previous inhabitant
/// and the new one are the same and that the types aren't dynamic (like [`Vec<T>`] is),
/// or if you WANT to keep sub-structure (if you're implementing a serializer for example).
pub fn set_raw_hard<T: RawObj, P: Path>(&self, path: P, object: T) -> Result<(), io::Error> {
self.storage.set(&path.to_db_path(), object.to_db())
}
/// Sets an item in the database at the path.
/// Here, the item is a composite item, so multiple blobs on sub-paths
/// may be created.