feat: Expose a function to compute the DocumentId from an Hashable value

This commit is contained in:
Clément Renault 2019-06-25 11:21:12 +02:00
parent 71c039db09
commit 28a0074497
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
3 changed files with 9 additions and 7 deletions

View File

@ -9,3 +9,4 @@ pub use rocksdb;
pub use self::database::{Database, Index, CustomSettings}; pub use self::database::{Database, Index, CustomSettings};
pub use self::number::Number; pub use self::number::Number;
pub use self::ranked_map::RankedMap; pub use self::ranked_map::RankedMap;
pub use self::serde::compute_document_id;

View File

@ -16,10 +16,11 @@ where D: serde::Serialize,
document.serialize(serializer) document.serialize(serializer)
} }
fn calculate_hash<T: Hash>(t: &T) -> u64 { pub fn compute_document_id<T: Hash>(t: &T) -> DocumentId {
let mut s = SipHasher::new(); let mut s = SipHasher::new();
t.hash(&mut s); t.hash(&mut s);
s.finish() let hash = s.finish();
DocumentId(hash)
} }
struct ExtractDocumentId<'a> { struct ExtractDocumentId<'a> {
@ -214,8 +215,8 @@ impl<'a> ser::SerializeMap for ExtractDocumentIdMapSerializer<'a> {
if self.identifier == key { if self.identifier == key {
// TODO is it possible to have multiple ids? // TODO is it possible to have multiple ids?
let id = bincode::serialize(value).unwrap(); let id = bincode::serialize(value).unwrap();
let hash = calculate_hash(&id); let document_id = compute_document_id(&id);
self.document_id = Some(DocumentId(hash)); self.document_id = Some(document_id);
} }
Ok(()) Ok(())
@ -245,8 +246,8 @@ impl<'a> ser::SerializeStruct for ExtractDocumentIdStructSerializer<'a> {
if self.identifier == key { if self.identifier == key {
// TODO can it be possible to have multiple ids? // TODO can it be possible to have multiple ids?
let id = bincode::serialize(value).unwrap(); let id = bincode::serialize(value).unwrap();
let hash = calculate_hash(&id); let document_id = compute_document_id(&id);
self.document_id = Some(DocumentId(hash)); self.document_id = Some(document_id);
} }
Ok(()) Ok(())

View File

@ -16,7 +16,7 @@ mod indexer;
mod serializer; mod serializer;
pub use self::deserializer::Deserializer; 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_string::ConvertToString;
pub use self::convert_to_number::ConvertToNumber; pub use self::convert_to_number::ConvertToNumber;
pub use self::indexer::Indexer; pub use self::indexer::Indexer;