From 28a007449703f8cdf66e32da84e53632cbab75ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 25 Jun 2019 11:21:12 +0200 Subject: [PATCH] feat: Expose a function to compute the DocumentId from an Hashable value --- meilidb-data/src/lib.rs | 1 + meilidb-data/src/serde/extract_document_id.rs | 13 +++++++------ meilidb-data/src/serde/mod.rs | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/meilidb-data/src/lib.rs b/meilidb-data/src/lib.rs index cb8a7dc9f..4d9719370 100644 --- a/meilidb-data/src/lib.rs +++ b/meilidb-data/src/lib.rs @@ -9,3 +9,4 @@ pub use rocksdb; pub use self::database::{Database, Index, CustomSettings}; pub use self::number::Number; pub use self::ranked_map::RankedMap; +pub use self::serde::compute_document_id; diff --git a/meilidb-data/src/serde/extract_document_id.rs b/meilidb-data/src/serde/extract_document_id.rs index 990094104..3ec2c200b 100644 --- a/meilidb-data/src/serde/extract_document_id.rs +++ b/meilidb-data/src/serde/extract_document_id.rs @@ -16,10 +16,11 @@ where D: serde::Serialize, document.serialize(serializer) } -fn calculate_hash(t: &T) -> u64 { +pub fn compute_document_id(t: &T) -> DocumentId { let mut s = SipHasher::new(); t.hash(&mut s); - s.finish() + let hash = s.finish(); + DocumentId(hash) } struct ExtractDocumentId<'a> { @@ -214,8 +215,8 @@ impl<'a> ser::SerializeMap for ExtractDocumentIdMapSerializer<'a> { if self.identifier == key { // TODO is it possible to have multiple ids? let id = bincode::serialize(value).unwrap(); - let hash = calculate_hash(&id); - self.document_id = Some(DocumentId(hash)); + let document_id = compute_document_id(&id); + self.document_id = Some(document_id); } Ok(()) @@ -245,8 +246,8 @@ impl<'a> ser::SerializeStruct for ExtractDocumentIdStructSerializer<'a> { if self.identifier == key { // TODO can it be possible to have multiple ids? let id = bincode::serialize(value).unwrap(); - let hash = calculate_hash(&id); - self.document_id = Some(DocumentId(hash)); + let document_id = compute_document_id(&id); + self.document_id = Some(document_id); } Ok(()) diff --git a/meilidb-data/src/serde/mod.rs b/meilidb-data/src/serde/mod.rs index b536c63cc..9ffa619ed 100644 --- a/meilidb-data/src/serde/mod.rs +++ b/meilidb-data/src/serde/mod.rs @@ -16,7 +16,7 @@ mod indexer; mod serializer; pub use self::deserializer::Deserializer; -pub use self::extract_document_id::extract_document_id; +pub use self::extract_document_id::{extract_document_id, compute_document_id}; pub use self::convert_to_string::ConvertToString; pub use self::convert_to_number::ConvertToNumber; pub use self::indexer::Indexer;