MeiliSearch/src/lib.rs

70 lines
1.8 KiB
Rust
Raw Normal View History

#[macro_use] extern crate serde_derive;
2018-04-22 17:34:41 +02:00
extern crate bincode;
2018-04-22 15:54:34 +02:00
extern crate fst;
extern crate serde;
2018-04-22 15:54:34 +02:00
mod fst_map;
use std::ops::{Range, Deref, DerefMut};
use std::io::{Write, BufReader};
2018-04-22 17:34:41 +02:00
use std::fs::File;
use std::path::Path;
use std::str::from_utf8_unchecked;
use fst::Automaton;
2018-04-22 15:54:34 +02:00
pub use self::fst_map::{FstMap, FstMapBuilder};
use self::fst_map::Values;
2018-04-22 17:34:41 +02:00
pub struct StreamBuilder<'a, T: 'a, A: Automaton> {
inner: fst::map::StreamBuilder<'a, A>,
values: &'a Values<T>,
}
impl<'a, T, A: Automaton> Deref for StreamBuilder<'a, T, A> {
type Target = fst::map::StreamBuilder<'a, A>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'a, T, A: Automaton> DerefMut for StreamBuilder<'a, T, A> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<'a, T: 'a, A: Automaton> fst::IntoStreamer<'a> for StreamBuilder<'a, T, A> {
type Item = (&'a str, &'a [T]);
type Into = Stream<'a, T, A>;
fn into_stream(self) -> Self::Into {
Stream {
inner: self.inner.into_stream(),
values: self.values,
}
}
2018-04-22 15:54:34 +02:00
}
pub struct Stream<'a, T: 'a, A: Automaton = fst::automaton::AlwaysMatch> {
2018-04-22 17:34:41 +02:00
inner: fst::map::Stream<'a, A>,
values: &'a Values<T>,
2018-04-22 17:34:41 +02:00
}
impl<'a, 'm, T: 'a, A: Automaton> fst::Streamer<'a> for Stream<'m, T, A> {
type Item = (&'a str, &'a [T]);
2018-04-22 17:34:41 +02:00
fn next(&'a mut self) -> Option<Self::Item> {
// Here we can't just `map` because of some borrow rules
match self.inner.next() {
Some((key, i)) => {
let key = unsafe { from_utf8_unchecked(key) };
let values = unsafe { self.values.get_unchecked(i as usize) };
Some((key, values))
2018-04-22 17:34:41 +02:00
},
None => None,
}
}
}