diff --git a/meilidb-data/Cargo.toml b/meilidb-data/Cargo.toml index d73e04da1..1491cba31 100644 --- a/meilidb-data/Cargo.toml +++ b/meilidb-data/Cargo.toml @@ -25,6 +25,11 @@ zerocopy = "0.2.8" git = "https://github.com/3Hren/msgpack-rust.git" rev = "40b3d48" +[dependencies.rmpv] +git = "https://github.com/3Hren/msgpack-rust.git" +rev = "40b3d48" +features = ["with-serde"] + [dependencies.fst] git = "https://github.com/Kerollmops/fst.git" branch = "arc-byte-slice" diff --git a/meilidb-data/src/database/error.rs b/meilidb-data/src/database/error.rs index 16a160d93..1b5b11b02 100644 --- a/meilidb-data/src/database/error.rs +++ b/meilidb-data/src/database/error.rs @@ -9,6 +9,8 @@ pub enum Error { MissingDocumentId, SledError(sled::Error), FstError(fst::Error), + RmpDecodeError(rmp_serde::decode::Error), + RmpEncodeError(rmp_serde::encode::Error), BincodeError(bincode::Error), SerializerError(SerializerError), } @@ -25,6 +27,18 @@ impl From for Error { } } +impl From for Error { + fn from(error: rmp_serde::decode::Error) -> Error { + Error::RmpDecodeError(error) + } +} + +impl From for Error { + fn from(error: rmp_serde::encode::Error) -> Error { + Error::RmpEncodeError(error) + } +} + impl From for Error { fn from(error: bincode::Error) -> Error { Error::BincodeError(error) @@ -47,6 +61,8 @@ impl fmt::Display for Error { MissingDocumentId => write!(f, "document id is missing"), SledError(e) => write!(f, "Sled error; {}", e), FstError(e) => write!(f, "fst error; {}", e), + RmpDecodeError(e) => write!(f, "rmp decode error; {}", e), + RmpEncodeError(e) => write!(f, "rmp encode error; {}", e), BincodeError(e) => write!(f, "bincode error; {}", e), SerializerError(e) => write!(f, "serializer error; {}", e), } diff --git a/meilidb-data/src/database/index/mod.rs b/meilidb-data/src/database/index/mod.rs index c9d97724c..4d35bf690 100644 --- a/meilidb-data/src/database/index/mod.rs +++ b/meilidb-data/src/database/index/mod.rs @@ -44,15 +44,15 @@ fn event_is_set(event: &sled::Event) -> bool { #[derive(Deserialize)] enum UpdateOwned { - DocumentsAddition(Vec), + DocumentsAddition(Vec), DocumentsDeletion(Vec), SynonymsAddition(BTreeMap>), SynonymsDeletion(BTreeMap>>), } #[derive(Serialize)] -enum Update { - DocumentsAddition(Vec), +enum Update { + DocumentsAddition(Vec), DocumentsDeletion(Vec), SynonymsAddition(BTreeMap>), SynonymsDeletion(BTreeMap>>), @@ -72,7 +72,7 @@ fn spawn_update_system(index: Index) -> thread::JoinHandle<()> { // this is an emulation of the try block (#31436) let result: Result<(), Error> = (|| { - match bincode::deserialize(&update)? { + match rmp_serde::from_read_ref(&update)? { UpdateOwned::DocumentsAddition(documents) => { let ranked_map = index.cache.load().ranked_map.clone(); apply_documents_addition(&index, ranked_map, documents)?; @@ -293,8 +293,15 @@ impl Index { pub(crate) fn push_documents_addition(&self, addition: Vec) -> Result where D: serde::Serialize { - let addition = Update::DocumentsAddition(addition); - let update = bincode::serialize(&addition)?; + let mut values = Vec::with_capacity(addition.len()); + for add in addition { + let vec = rmp_serde::to_vec_named(&add)?; + let add = rmp_serde::from_read(&vec[..])?; + values.push(add); + } + + let addition = Update::DocumentsAddition(values); + let update = rmp_serde::to_vec_named(&addition)?; self.raw_push_update(update) } @@ -303,8 +310,8 @@ impl Index { deletion: Vec, ) -> Result { - let deletion = Update::<()>::DocumentsDeletion(deletion); - let update = bincode::serialize(&deletion)?; + let deletion = Update::DocumentsDeletion(deletion); + let update = rmp_serde::to_vec_named(&deletion)?; self.raw_push_update(update) } @@ -313,8 +320,8 @@ impl Index { addition: BTreeMap>, ) -> Result { - let addition = Update::<()>::SynonymsAddition(addition); - let update = bincode::serialize(&addition)?; + let addition = Update::SynonymsAddition(addition); + let update = rmp_serde::to_vec_named(&addition)?; self.raw_push_update(update) } @@ -323,8 +330,8 @@ impl Index { deletion: BTreeMap>>, ) -> Result { - let deletion = Update::<()>::SynonymsDeletion(deletion); - let update = bincode::serialize(&deletion)?; + let deletion = Update::SynonymsDeletion(deletion); + let update = rmp_serde::to_vec_named(&deletion)?; self.raw_push_update(update) } diff --git a/meilidb-data/src/database/update/documents_addition.rs b/meilidb-data/src/database/update/documents_addition.rs index 7c8ff99db..2e4e94736 100644 --- a/meilidb-data/src/database/update/documents_addition.rs +++ b/meilidb-data/src/database/update/documents_addition.rs @@ -35,7 +35,7 @@ impl<'a, D> DocumentsAddition<'a, D> { pub fn apply_documents_addition( index: &Index, mut ranked_map: RankedMap, - addition: Vec, + addition: Vec, ) -> Result<(), Error> { let mut document_ids = HashSet::new();