1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use fst::{self, Streamer};

/// `SymbolQuery` specifies the preficate for filtering symbols by name.
///
/// All matching is case-insensitive. Filtering by prefix or by subsequence
/// is supported, subsequence being a good default choice.
///
/// As the number of results might be huge, consider the `limit` hint,
/// which serves as *approximate* limit on the number of results returned.
///
/// To implement async streaming/pagination, use `greater_than` together with
/// `limit`.
#[derive(Debug)]
pub struct SymbolQuery {
    query_string: String,
    mode: Mode,
    limit: usize,
    greater_than: String,
}

#[derive(Debug, Clone, Copy)]
enum Mode {
    Prefix,
    Subsequence,
}

impl SymbolQuery {
    fn new(query_string: String, mode: Mode) -> SymbolQuery {
        SymbolQuery { query_string, mode, limit: usize::max_value(), greater_than: String::new() }
    }

    pub fn subsequence(query_string: &str) -> SymbolQuery {
        SymbolQuery::new(query_string.to_lowercase(), Mode::Subsequence)
    }

    pub fn prefix(query_string: &str) -> SymbolQuery {
        SymbolQuery::new(query_string.to_lowercase(), Mode::Prefix)
    }

    pub fn limit(self, limit: usize) -> SymbolQuery {
        SymbolQuery { limit, ..self }
    }

    pub fn greater_than(self, greater_than: &str) -> SymbolQuery {
        SymbolQuery { greater_than: greater_than.to_lowercase(), ..self }
    }

    pub(crate) fn build_stream<'a, I>(&'a self, fsts: I) -> fst::map::Union<'a>
    where
        I: Iterator<Item = &'a fst::Map<Vec<u8>>>,
    {
        let mut stream = fst::map::OpBuilder::new();
        let automaton = QueryAutomaton { query: &self.query_string, mode: self.mode };
        for fst in fsts {
            stream = stream.add(fst.search(automaton).gt(&self.greater_than));
        }
        stream.union()
    }

    pub(crate) fn search_stream<F, T>(&self, mut stream: fst::map::Union<'_>, f: F) -> Vec<T>
    where
        F: Fn(&mut Vec<T>, &fst::map::IndexedValue),
    {
        let mut res = Vec::new();
        while let Some((_, entries)) = stream.next() {
            for e in entries {
                f(&mut res, e);
            }
            if res.len() >= self.limit {
                break;
            }
        }
        res
    }
}

/// See http://docs.rs/fst for how we implement query processing.
///
/// In a nutshell, both the query and the set of available symbols
/// are encoded as two finite state machines. Then, the intersection
/// of state machines is built, which gives all the symbols matching
/// the query.
///
/// The `fst::Automaton` impl below implements a state machine for
/// the query, where the state is the number of bytes already matched.
#[derive(Clone, Copy)]
struct QueryAutomaton<'a> {
    query: &'a str,
    mode: Mode,
}

const NO_MATCH: usize = !0;

impl<'a> fst::Automaton for QueryAutomaton<'a> {
    type State = usize;

    fn start(&self) -> usize {
        0
    }

    fn is_match(&self, &state: &usize) -> bool {
        state == self.query.len()
    }

    fn accept(&self, &state: &usize, byte: u8) -> usize {
        if state == NO_MATCH {
            return state;
        }
        if state == self.query.len() {
            return state;
        }
        if byte == self.query.as_bytes()[state] {
            return state + 1;
        }
        match self.mode {
            Mode::Prefix => NO_MATCH,
            Mode::Subsequence => state,
        }
    }

    fn can_match(&self, &state: &usize) -> bool {
        state != NO_MATCH
    }

    fn will_always_match(&self, &state: &usize) -> bool {
        state == self.query.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::iter;

    const STARS: &[&str] = &[
        "agena", "agreetor", "algerib", "anektor", "antares", "arcturus", "canopus", "capella",
        "duendin", "golubin", "lalandry", "spica", "vega",
    ];

    fn check(q: SymbolQuery, expected: &[&str]) {
        let map =
            fst::Map::from_iter(STARS.iter().enumerate().map(|(i, &s)| (s, i as u64))).unwrap();
        let stream = q.build_stream(iter::once(&map));
        let actual = q.search_stream(stream, |acc, iv| acc.push(STARS[iv.value as usize]));
        assert_eq!(expected, actual.as_slice());
    }

    #[test]
    fn test_automaton() {
        check(SymbolQuery::prefix("an"), &["anektor", "antares"]);

        check(
            SymbolQuery::subsequence("an"),
            &["agena", "anektor", "antares", "canopus", "lalandry"],
        );

        check(SymbolQuery::subsequence("an").limit(2), &["agena", "anektor"]);
        check(
            SymbolQuery::subsequence("an").limit(2).greater_than("anektor"),
            &["antares", "canopus"],
        );
        check(SymbolQuery::subsequence("an").limit(2).greater_than("canopus"), &["lalandry"]);
    }
}