From 1cf6afad9ab5ad12446cc828064a0ac141270fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 17 Oct 2019 12:31:46 +0200 Subject: [PATCH] Fix updates serialization to use serde_json instead of bincode --- meilidb-core/src/store/updates.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/meilidb-core/src/store/updates.rs b/meilidb-core/src/store/updates.rs index 14c839adb..7ec3b48aa 100644 --- a/meilidb-core/src/store/updates.rs +++ b/meilidb-core/src/store/updates.rs @@ -1,11 +1,31 @@ -use zlmdb::types::{OwnedType, Serde}; -use zlmdb::Result as ZResult; +use std::borrow::Cow; +use zlmdb::types::OwnedType; +use zlmdb::{Result as ZResult, BytesEncode, BytesDecode}; +use serde::{Serialize, Deserialize}; use crate::update::Update; use super::BEU64; +pub struct SerdeJson(std::marker::PhantomData); + +impl BytesEncode for SerdeJson where T: Serialize { + type EItem = T; + + fn bytes_encode(item: &Self::EItem) -> Option> { + serde_json::to_vec(item).map(Cow::Owned).ok() + } +} + +impl<'a, T: 'a> BytesDecode<'a> for SerdeJson where T: Deserialize<'a> + Clone { + type DItem = T; + + fn bytes_decode(bytes: &'a [u8]) -> Option { + serde_json::from_slice(bytes).ok() + } +} + #[derive(Copy, Clone)] pub struct Updates { - pub(crate) updates: zlmdb::Database, Serde>, + pub(crate) updates: zlmdb::Database, SerdeJson>, } impl Updates {