From 54889813cefe4f0978ecc87aea2c47551edbb3c5 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Wed, 30 Jun 2021 10:43:12 +0200 Subject: [PATCH] Implement some debug functions on the ExternalDocumentsIds struct --- milli/src/external_documents_ids.rs | 34 ++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/milli/src/external_documents_ids.rs b/milli/src/external_documents_ids.rs index 3dec638da..419105bd5 100644 --- a/milli/src/external_documents_ids.rs +++ b/milli/src/external_documents_ids.rs @@ -1,8 +1,12 @@ use std::borrow::Cow; +use std::collections::HashMap; use std::convert::TryInto; +use std::{fmt, str}; use fst::{IntoStreamer, Streamer}; +const DELETED_ID: u64 = u64::MAX; + pub struct ExternalDocumentsIds<'a> { pub(crate) hard: fst::Map>, pub(crate) soft: fst::Map>, @@ -32,7 +36,7 @@ impl<'a> ExternalDocumentsIds<'a> { let external_id = external_id.as_ref(); match self.soft.get(external_id).or_else(|| self.hard.get(external_id)) { // u64 MAX means deleted in the soft fst map - Some(id) if id != u64::MAX => Some(id.try_into().unwrap()), + Some(id) if id != DELETED_ID => Some(id.try_into().unwrap()), _otherwise => None, } } @@ -47,7 +51,7 @@ impl<'a> ExternalDocumentsIds<'a> { if docids.iter().any(|v| v.index == 1) { // If the `other` set returns a value here it means // that it must be marked as deleted. - new_soft_builder.insert(external_id, u64::MAX)?; + new_soft_builder.insert(external_id, DELETED_ID)?; } else { new_soft_builder.insert(external_id, docids[0].value)?; } @@ -77,6 +81,24 @@ impl<'a> ExternalDocumentsIds<'a> { self.merge_soft_into_hard() } + /// An helper function to debug this type, returns an `HashMap` of both, + /// soft and hard fst maps, combined. + pub fn to_hash_map(&self) -> HashMap { + let mut map = HashMap::new(); + + let union_op = self.hard.op().add(&self.soft).r#union(); + let mut iter = union_op.into_stream(); + while let Some((external_id, marked_docids)) = iter.next() { + let id = marked_docids.last().unwrap().value; + if id != DELETED_ID { + let external_id = str::from_utf8(external_id).unwrap(); + map.insert(external_id.to_owned(), id.try_into().unwrap()); + } + } + + map + } + fn merge_soft_into_hard(&mut self) -> fst::Result<()> { if self.soft.len() >= self.hard.len() / 2 { let union_op = self.hard.op().add(&self.soft).r#union(); @@ -85,7 +107,7 @@ impl<'a> ExternalDocumentsIds<'a> { let mut new_hard_builder = fst::MapBuilder::memory(); while let Some((external_id, docids)) = iter.next() { if docids.len() == 2 { - if docids[1].value != u64::MAX { + if docids[1].value != DELETED_ID { new_hard_builder.insert(external_id, docids[1].value)?; } } else { @@ -103,6 +125,12 @@ impl<'a> ExternalDocumentsIds<'a> { } } +impl fmt::Debug for ExternalDocumentsIds<'_> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("ExternalDocumentsIds").field(&self.to_hash_map()).finish() + } +} + impl Default for ExternalDocumentsIds<'static> { fn default() -> Self { ExternalDocumentsIds {