Better API

This commit is contained in:
Paul Masurel
2017-05-18 23:33:15 +09:00
parent 2a08c247af
commit b3f62b8acc
5 changed files with 40 additions and 12 deletions

View File

@@ -5,6 +5,7 @@ use super::{FstMapStreamerBuilder, FstMapStreamer};
use directory::ReadOnlySource;
use common::BinarySerializable;
use std::marker::PhantomData;
use schema::{Field, Term};
fn convert_fst_error(e: fst::Error) -> io::Error {
@@ -104,22 +105,47 @@ impl<V> FstMap<V>
})
}
pub(crate) fn read_value(&self, offset: u64) -> V {
/// In the `FstMap`, the dictionary itself associated
/// each key `&[u8]` to a `u64` that is in fact the address
/// of the value object in a data array.
///
/// This method deserialize this object, and returns it.
pub(crate) fn read_value(&self, offset: u64) -> io::Result<V> {
let buffer = self.values_mmap.as_slice();
let mut cursor = &buffer[(offset as usize)..];
V::deserialize(&mut cursor).expect("Data in FST is corrupted")
V::deserialize(&mut cursor)
}
/// Returns, if present the value associated to a given key.
pub fn get<K: AsRef<[u8]>>(&self, key: K) -> Option<V> {
self.fst_index
.get(key)
.map(|offset| self.read_value(offset))
.map(|offset| {
self.read_value(offset)
.expect("The fst is corrupted. Failed to deserialize a value.")
})
}
/// Returns a stream of all the sorted terms.
pub fn stream(&self) -> FstMapStreamer<V> {
self.range().into_stream()
}
/// Returns a stream of all the sorted terms in the given field.
pub fn stream_field(&self, field: Field) -> FstMapStreamer<V> {
let start_term = Term::from_field_text(field, "");
let stop_term = Term::from_field_text(Field(field.0 + 1), "");
self.range()
.ge(start_term.as_slice())
.lt(stop_term.as_slice())
.into_stream()
}
/// Returns a range builder, to stream all of the terms
/// within an interval.
pub fn range(&self) -> FstMapStreamerBuilder<V> {
FstMapStreamerBuilder::new(self, self.fst_index.range())
}

View File

@@ -136,7 +136,7 @@ impl<'a> From<&'a [SegmentReader]> for FstMerger<'a, TermInfo>
fn from(segment_readers: &'a [SegmentReader]) -> FstMerger<'a, TermInfo> {
FstMerger::new(segment_readers
.iter()
.map(|reader| reader.term_infos().stream())
.map(|reader| reader.terms().stream())
.collect())
}
}

View File

@@ -99,6 +99,8 @@ impl<'a, V> FstMapStreamer<'a, V>
}
pub fn value(&self) -> V {
self.fst_map.read_value(self.offset)
self.fst_map
.read_value(self.offset)
.expect("Fst data is corrupted. Failed to deserialize a value.")
}
}