Fix ser/de for `Coremap<Array, Array>`

next
Sayan Nandan 3 years ago
parent bd679f9b79
commit 8faf653d2e

@ -546,3 +546,26 @@ fn test_uninitialized() {
b.push(b'S');
assert_eq!(b.iter().count(), 1);
}
#[cfg(test)]
macro_rules! array_from_string {
($st:expr, $len:expr) => {{
let mut array: Array<u8, $len> = Array::new();
$st.chars().into_iter().for_each(|ch| array.push(ch as u8));
array
}};
}
#[test]
fn test_map_serialize() {
use crate::coredb::htable::Coremap;
let map = Coremap::new();
map.true_if_insert(
array_from_string!("hello", 5),
array_from_string!("sayan", 5),
);
let ret = map.serialize().unwrap();
let bc: Coremap<Array<u8, 5>, Array<u8, 5>> = Coremap::deserialize_array(ret).unwrap();
assert!(bc.len() == map.len());
assert!(bc.into_iter().all(|(k, _v)| { map.contains_key(&k) }));
}

@ -24,6 +24,7 @@
*
*/
use crate::coredb::array::Array;
use bytes::Bytes;
use libsky::TResult;
use serde::{Deserialize, Serialize};
@ -207,11 +208,6 @@ where
}
impl Coremap<Data, Data> {
/// Returns a `Coremap<Data, Data>` from the provided file (as a `Vec<u8>`)
pub fn deserialize(src: Vec<u8>) -> TResult<Self> {
let h: HashTable<Data, Data> = bincode::deserialize(&src)?;
Ok(Self { inner: h })
}
/// Returns atleast `count` number of keys from the hashtable
pub fn get_keys(&self, count: usize) -> Vec<Bytes> {
let mut v = Vec::with_capacity(count);
@ -221,6 +217,17 @@ impl Coremap<Data, Data> {
.for_each(|key| v.push(key));
v
}
/// Returns a `Coremap<Data, Data>` from the provided file (as a `Vec<u8>`)
pub fn deserialize(src: Vec<u8>) -> TResult<Self> {
let h: HashTable<Data, Data> = bincode::deserialize(&src)?;
Ok(Self { inner: h })
}
}
impl<const M: usize, const N: usize> Coremap<Array<u8, M>, Array<u8, N>> {
pub fn deserialize_array(bytes: Vec<u8>) -> TResult<Self> {
let h: HashTable<Array<u8, M>, Array<u8, N>> = bincode::deserialize(&bytes)?;
Ok(Self { inner: h })
}
}
impl<K: Eq + Hash, V> IntoIterator for Coremap<K, V> {
type Item = (K, V);
@ -371,7 +378,7 @@ fn test_de() {
Data::from_string("is writing open-source code".to_owned()),
);
let ser = x.serialize().unwrap();
let de = Coremap::deserialize(ser).unwrap();
let de = Coremap::<Data, Data>::deserialize(ser).unwrap();
assert!(de.contains_key(&Data::from("sayan")));
assert!(de.len() == x.len());
let hmap: Coremap<Data, Data> = Coremap::new();

Loading…
Cancel
Save