diff --git a/src/blob/mod.rs b/src/blob/mod.rs index daafcf1c8..76f28fef2 100644 --- a/src/blob/mod.rs +++ b/src/blob/mod.rs @@ -2,7 +2,7 @@ mod ops; pub mod positive; pub mod negative; -pub use self::positive::{PositiveBlob, RawPositiveBlobBuilder, PositiveBlobBuilder}; +pub use self::positive::{PositiveBlob, PositiveBlobBuilder}; pub use self::negative::{NegativeBlob, NegativeBlobBuilder}; pub use self::ops::OpBuilder; diff --git a/src/blob/ops.rs b/src/blob/ops.rs index 90f91547f..0a9da3abf 100644 --- a/src/blob/ops.rs +++ b/src/blob/ops.rs @@ -6,7 +6,7 @@ use itertools::{Itertools, Either}; use sdset::duo::DifferenceByKey; use sdset::{Set, SetOperation}; -use crate::blob::{Blob, Sign, PositiveBlob, RawPositiveBlobBuilder, NegativeBlob}; +use crate::blob::{Blob, Sign, PositiveBlob, PositiveBlobBuilder, NegativeBlob}; use crate::blob::{positive, negative}; fn blob_same_sign(a: &Blob, b: &Blob) -> bool { @@ -56,7 +56,7 @@ impl OpBuilder { } let mut stream = op_builder.union().into_stream(); - let mut builder = RawPositiveBlobBuilder::memory(); + let mut builder = PositiveBlobBuilder::memory(); while let Some((input, doc_indexes)) = stream.next() { // FIXME empty doc_indexes must be handled by OpBuilder if !doc_indexes.is_empty() { @@ -81,7 +81,7 @@ impl OpBuilder { let mut zipped = positives.into_iter().zip(negatives); let mut buffer = Vec::new(); zipped.try_fold(PositiveBlob::default(), |base, (positive, negative)| { - let mut builder = RawPositiveBlobBuilder::memory(); + let mut builder = PositiveBlobBuilder::memory(); let doc_ids = Set::new_unchecked(negative.as_ref()); let op_builder = positive::OpBuilder::new().add(&base).add(&positive); diff --git a/src/blob/positive/blob.rs b/src/blob/positive/blob.rs index 7cbcf47ab..ee143dbef 100644 --- a/src/blob/positive/blob.rs +++ b/src/blob/positive/blob.rs @@ -6,7 +6,7 @@ use std::error::Error; use fst::{map, Map, Streamer, IntoStreamer}; use crate::DocIndex; -use crate::data::{DocIndexes, RawDocIndexesBuilder, DocIndexesBuilder}; +use crate::data::{DocIndexes, RawDocIndexesBuilder}; use serde::ser::{Serialize, Serializer, SerializeTuple}; use serde::de::{self, Deserialize, Deserializer, SeqAccess, Visitor}; @@ -133,15 +133,15 @@ impl<'de> Deserialize<'de> for PositiveBlob { } } -pub struct RawPositiveBlobBuilder { +pub struct PositiveBlobBuilder { map: fst::MapBuilder, indexes: RawDocIndexesBuilder, value: u64, } -impl RawPositiveBlobBuilder, Vec> { +impl PositiveBlobBuilder, Vec> { pub fn memory() -> Self { - RawPositiveBlobBuilder { + PositiveBlobBuilder { map: fst::MapBuilder::memory(), indexes: RawDocIndexesBuilder::memory(), value: 0, @@ -149,15 +149,18 @@ impl RawPositiveBlobBuilder, Vec> { } } -impl RawPositiveBlobBuilder { +impl PositiveBlobBuilder { pub fn new(map: W, indexes: X) -> Result> { - Ok(RawPositiveBlobBuilder { + Ok(PositiveBlobBuilder { map: fst::MapBuilder::new(map)?, indexes: RawDocIndexesBuilder::new(indexes), value: 0, }) } + /// If a key is inserted that is less than or equal to any previous key added, + /// then an error is returned. Similarly, if there was a problem writing + /// to the underlying writer, an error is returned. // FIXME what if one write doesn't work but the other do ? pub fn insert(&mut self, key: &[u8], doc_indexes: &[DocIndex]) -> Result<(), Box> { self.map.insert(key, self.value)?; @@ -176,48 +179,3 @@ impl RawPositiveBlobBuilder { Ok((map, indexes)) } } - -pub struct PositiveBlobBuilder { - map: W, - indexes: DocIndexesBuilder, -} - -impl PositiveBlobBuilder { - pub fn new(map: W, indexes: X) -> Self { - Self { map, indexes: DocIndexesBuilder::new(indexes) } - } - - pub fn insert>(&mut self, key: S, index: DocIndex) { - self.indexes.insert(key.into(), index) - } - - pub fn finish(self) -> Result<(), Box> { - self.into_inner().map(drop) - } - - pub fn into_inner(self) -> Result<(W, X), Box> { - // FIXME insert a magic number that indicates if the endianess - // of the input is the same as the machine that is reading it. - - let map = { - let mut keys_builder = fst::MapBuilder::new(self.map)?; - let keys = self.indexes.keys().map(|(s, v)| (s, *v)); - keys_builder.extend_iter(keys)?; - keys_builder.into_inner()? - }; - - let indexes = self.indexes.into_inner()?; - - Ok((map, indexes)) - } -} - -impl PositiveBlobBuilder, Vec> { - pub fn memory() -> Self { - PositiveBlobBuilder::new(Vec::new(), Vec::new()) - } - - pub fn build(self) -> Result> { - self.into_inner().and_then(|(m, i)| PositiveBlob::from_bytes(m, i)) - } -} diff --git a/src/blob/positive/mod.rs b/src/blob/positive/mod.rs index be895b80b..d8e4e164e 100644 --- a/src/blob/positive/mod.rs +++ b/src/blob/positive/mod.rs @@ -1,5 +1,5 @@ mod blob; mod ops; -pub use self::blob::{PositiveBlob, RawPositiveBlobBuilder, PositiveBlobBuilder}; +pub use self::blob::{PositiveBlob, PositiveBlobBuilder}; pub use self::ops::OpBuilder; diff --git a/src/index/update/negative_update.rs b/src/index/update/negative_update.rs index 6c82919b5..342fde98c 100644 --- a/src/index/update/negative_update.rs +++ b/src/index/update/negative_update.rs @@ -50,7 +50,7 @@ impl NegativeUpdateBuilder { let doc_ids = DocIds::from_bytes(bytes)?; let blob = Blob::Negative(NegativeBlob::from_raw(doc_ids)); let bytes = bincode::serialize(&blob)?; - file_writer.merge(DATA_INDEX, &bytes); + file_writer.merge(DATA_INDEX, &bytes)?; // FIXME remove this ugly thing ! // let Blob::Negative(negative_blob) = blob;