feat: Remove the PositiveBlobBuilder

This commit is contained in:
Clément Renault 2018-11-29 17:15:05 +01:00
parent ec5d17e8c2
commit 8b2a8da8fa
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
5 changed files with 15 additions and 57 deletions

View File

@ -2,7 +2,7 @@ mod ops;
pub mod positive; pub mod positive;
pub mod negative; 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::negative::{NegativeBlob, NegativeBlobBuilder};
pub use self::ops::OpBuilder; pub use self::ops::OpBuilder;

View File

@ -6,7 +6,7 @@ use itertools::{Itertools, Either};
use sdset::duo::DifferenceByKey; use sdset::duo::DifferenceByKey;
use sdset::{Set, SetOperation}; 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}; use crate::blob::{positive, negative};
fn blob_same_sign(a: &Blob, b: &Blob) -> bool { 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 stream = op_builder.union().into_stream();
let mut builder = RawPositiveBlobBuilder::memory(); let mut builder = PositiveBlobBuilder::memory();
while let Some((input, doc_indexes)) = stream.next() { while let Some((input, doc_indexes)) = stream.next() {
// FIXME empty doc_indexes must be handled by OpBuilder // FIXME empty doc_indexes must be handled by OpBuilder
if !doc_indexes.is_empty() { if !doc_indexes.is_empty() {
@ -81,7 +81,7 @@ impl OpBuilder {
let mut zipped = positives.into_iter().zip(negatives); let mut zipped = positives.into_iter().zip(negatives);
let mut buffer = Vec::new(); let mut buffer = Vec::new();
zipped.try_fold(PositiveBlob::default(), |base, (positive, negative)| { 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 doc_ids = Set::new_unchecked(negative.as_ref());
let op_builder = positive::OpBuilder::new().add(&base).add(&positive); let op_builder = positive::OpBuilder::new().add(&base).add(&positive);

View File

@ -6,7 +6,7 @@ use std::error::Error;
use fst::{map, Map, Streamer, IntoStreamer}; use fst::{map, Map, Streamer, IntoStreamer};
use crate::DocIndex; use crate::DocIndex;
use crate::data::{DocIndexes, RawDocIndexesBuilder, DocIndexesBuilder}; use crate::data::{DocIndexes, RawDocIndexesBuilder};
use serde::ser::{Serialize, Serializer, SerializeTuple}; use serde::ser::{Serialize, Serializer, SerializeTuple};
use serde::de::{self, Deserialize, Deserializer, SeqAccess, Visitor}; use serde::de::{self, Deserialize, Deserializer, SeqAccess, Visitor};
@ -133,15 +133,15 @@ impl<'de> Deserialize<'de> for PositiveBlob {
} }
} }
pub struct RawPositiveBlobBuilder<W, X> { pub struct PositiveBlobBuilder<W, X> {
map: fst::MapBuilder<W>, map: fst::MapBuilder<W>,
indexes: RawDocIndexesBuilder<X>, indexes: RawDocIndexesBuilder<X>,
value: u64, value: u64,
} }
impl RawPositiveBlobBuilder<Vec<u8>, Vec<u8>> { impl PositiveBlobBuilder<Vec<u8>, Vec<u8>> {
pub fn memory() -> Self { pub fn memory() -> Self {
RawPositiveBlobBuilder { PositiveBlobBuilder {
map: fst::MapBuilder::memory(), map: fst::MapBuilder::memory(),
indexes: RawDocIndexesBuilder::memory(), indexes: RawDocIndexesBuilder::memory(),
value: 0, value: 0,
@ -149,15 +149,18 @@ impl RawPositiveBlobBuilder<Vec<u8>, Vec<u8>> {
} }
} }
impl<W: Write, X: Write> RawPositiveBlobBuilder<W, X> { impl<W: Write, X: Write> PositiveBlobBuilder<W, X> {
pub fn new(map: W, indexes: X) -> Result<Self, Box<Error>> { pub fn new(map: W, indexes: X) -> Result<Self, Box<Error>> {
Ok(RawPositiveBlobBuilder { Ok(PositiveBlobBuilder {
map: fst::MapBuilder::new(map)?, map: fst::MapBuilder::new(map)?,
indexes: RawDocIndexesBuilder::new(indexes), indexes: RawDocIndexesBuilder::new(indexes),
value: 0, 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 ? // FIXME what if one write doesn't work but the other do ?
pub fn insert(&mut self, key: &[u8], doc_indexes: &[DocIndex]) -> Result<(), Box<Error>> { pub fn insert(&mut self, key: &[u8], doc_indexes: &[DocIndex]) -> Result<(), Box<Error>> {
self.map.insert(key, self.value)?; self.map.insert(key, self.value)?;
@ -176,48 +179,3 @@ impl<W: Write, X: Write> RawPositiveBlobBuilder<W, X> {
Ok((map, indexes)) Ok((map, indexes))
} }
} }
pub struct PositiveBlobBuilder<W, X> {
map: W,
indexes: DocIndexesBuilder<X>,
}
impl<W: Write, X: Write> PositiveBlobBuilder<W, X> {
pub fn new(map: W, indexes: X) -> Self {
Self { map, indexes: DocIndexesBuilder::new(indexes) }
}
pub fn insert<S: Into<String>>(&mut self, key: S, index: DocIndex) {
self.indexes.insert(key.into(), index)
}
pub fn finish(self) -> Result<(), Box<Error>> {
self.into_inner().map(drop)
}
pub fn into_inner(self) -> Result<(W, X), Box<Error>> {
// 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<u8>, Vec<u8>> {
pub fn memory() -> Self {
PositiveBlobBuilder::new(Vec::new(), Vec::new())
}
pub fn build(self) -> Result<PositiveBlob, Box<Error>> {
self.into_inner().and_then(|(m, i)| PositiveBlob::from_bytes(m, i))
}
}

View File

@ -1,5 +1,5 @@
mod blob; mod blob;
mod ops; mod ops;
pub use self::blob::{PositiveBlob, RawPositiveBlobBuilder, PositiveBlobBuilder}; pub use self::blob::{PositiveBlob, PositiveBlobBuilder};
pub use self::ops::OpBuilder; pub use self::ops::OpBuilder;

View File

@ -50,7 +50,7 @@ impl NegativeUpdateBuilder {
let doc_ids = DocIds::from_bytes(bytes)?; let doc_ids = DocIds::from_bytes(bytes)?;
let blob = Blob::Negative(NegativeBlob::from_raw(doc_ids)); let blob = Blob::Negative(NegativeBlob::from_raw(doc_ids));
let bytes = bincode::serialize(&blob)?; let bytes = bincode::serialize(&blob)?;
file_writer.merge(DATA_INDEX, &bytes); file_writer.merge(DATA_INDEX, &bytes)?;
// FIXME remove this ugly thing ! // FIXME remove this ugly thing !
// let Blob::Negative(negative_blob) = blob; // let Blob::Negative(negative_blob) = blob;