[][src]Struct porigon::SearchStream

pub struct SearchStream<'s>(_);

FST stream on which various operations can be chained.

Methods

impl<'s> SearchStream<'s>[src]

pub fn rescore<F: 's + Fn(&[u8], u64, Score) -> Score>(self, func: F) -> Self[src]

Scores a stream, using the given closure.

Examples

Basic usage:

use fst::Streamer;
use porigon::Searchable;

let searchable = Searchable::build_from_iter(vec!(
    ("foo".as_bytes(), 0),
    ("foobar".as_bytes(), 1))
).unwrap();
let mut strm = searchable
    .starts_with("foo")
    .rescore(|key, _, _| key.len() as porigon::Score)
;
assert_eq!(strm.next(), Some(("foo".as_bytes(), 0, 3)));
assert_eq!(strm.next(), Some(("foobar".as_bytes(), 1, 6)));
assert_eq!(strm.next(), None);

You can also use this to build upon a previously set score:

use fst::Streamer;
use porigon::Searchable;

let searchable = Searchable::build_from_iter(vec!(
    ("foo".as_bytes(), 0),
    ("foobar".as_bytes(), 1))
).unwrap();
let mut strm = searchable
    .starts_with("foo")
    .rescore(|key, _, _| key.len() as porigon::Score)
    .rescore(|_, index, old_score| (old_score << 16) | index)
;
assert_eq!(strm.next(), Some(("foo".as_bytes(), 0, 3 << 16)));
assert_eq!(strm.next(), Some(("foobar".as_bytes(), 1, (6 << 16) | 1)));
assert_eq!(strm.next(), None);

pub fn filter<F: 's + Fn(&[u8], u64, Score) -> bool>(self, func: F) -> Self[src]

Filters a stream, using the given closure.

Example

use fst::Streamer;
use porigon::Searchable;

let searchable = Searchable::build_from_iter(vec!(
    ("foo".as_bytes(), 0),
    ("foobar".as_bytes(), 1))
).unwrap();
let mut strm = searchable
    .starts_with("foo")
    .filter(|key, _, _| key != "foobar".as_bytes())
;
assert_eq!(strm.next(), Some(("foo".as_bytes(), 0, 0)));
assert_eq!(strm.next(), None);

pub fn map<F: 's + Fn(&[u8], u64, Score) -> (&[u8], u64, Score)>(
    self,
    func: F
) -> Self
[src]

Maps over a stream, using the given closure.

This more of an advanced method, used for changing the stream's key or index. Most probably you want to use rescore() instead.

Example

use fst::Streamer;
use porigon::Searchable;

let mut items = vec!(
    ("this is a bar".as_bytes(), 15),
    ("is a bar".as_bytes(), (1 << 32) | 15),
    ("a bar".as_bytes(), (1 << 32) | 15),
    ("bar".as_bytes(), (1 << 32) | 15),
    ("barfoo".as_bytes(), 16)
);
items.sort_by_key(|(key, _)| *key);
let searchable = Searchable::build_from_iter(items).unwrap();
let mut strm = searchable
    .starts_with("bar")
    .map(|key, index, score| (key, index & !(1 << 32), score))
;
assert_eq!(strm.next(), Some(("bar".as_bytes(), 15, 0)));
assert_eq!(strm.next(), Some(("barfoo".as_bytes(), 16, 0)));
assert_eq!(strm.next(), None);

Trait Implementations

impl<'a, 's> Streamer<'a> for SearchStream<'s>[src]

type Item = (&'a [u8], u64, Score)

The type of the item emitted by this stream.

Auto Trait Implementations

impl<'s> !RefUnwindSafe for SearchStream<'s>

impl<'s> !Send for SearchStream<'s>

impl<'s> !Sync for SearchStream<'s>

impl<'s> Unpin for SearchStream<'s>

impl<'s> !UnwindSafe for SearchStream<'s>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<'a, S> IntoStreamer<'a> for S where
    S: Streamer<'a>, 
[src]

type Item = <S as Streamer<'a>>::Item

The type of the item emitted by the stream.

type Into = S

The type of the stream to be constructed.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.