feat: Make the automaton DFA construction lazy

This commit is contained in:
Clément Renault 2019-08-18 18:57:41 +02:00
parent 0ee56314fb
commit 7dc9ea78fa
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE

View File

@ -29,17 +29,27 @@ struct Automaton {
ngram: usize, ngram: usize,
query_len: usize, query_len: usize,
is_exact: bool, is_exact: bool,
dfa: DFA, is_prefix: bool,
query: String,
} }
impl Automaton { impl Automaton {
fn dfa(&self) -> DFA {
if self.is_prefix {
build_prefix_dfa(&self.query)
} else {
build_dfa(&self.query)
}
}
fn exact(index: usize, ngram: usize, query: &str) -> Automaton { fn exact(index: usize, ngram: usize, query: &str) -> Automaton {
Automaton { Automaton {
index, index,
ngram, ngram,
query_len: query.len(), query_len: query.len(),
is_exact: true, is_exact: true,
dfa: build_dfa(query), is_prefix: false,
query: query.to_string(),
} }
} }
@ -49,7 +59,8 @@ impl Automaton {
ngram, ngram,
query_len: query.len(), query_len: query.len(),
is_exact: true, is_exact: true,
dfa: build_prefix_dfa(query), is_prefix: true,
query: query.to_string(),
} }
} }
@ -59,7 +70,8 @@ impl Automaton {
ngram, ngram,
query_len: query.len(), query_len: query.len(),
is_exact: false, is_exact: false,
dfa: build_dfa(query), is_prefix: false,
query: query.to_string(),
} }
} }
} }
@ -344,7 +356,8 @@ where S: Store + Sync,
.par_bridge() .par_bridge()
.try_for_each_with((sender, store, searchables), |data, automaton| { .try_for_each_with((sender, store, searchables), |data, automaton| {
let (sender, store, searchables) = data; let (sender, store, searchables) = data;
let Automaton { index, is_exact, query_len, dfa, .. } = automaton; let Automaton { index, is_exact, query_len, .. } = automaton;
let dfa = automaton.dfa();
let words = store.words().map_err(Error::StoreError)?; let words = store.words().map_err(Error::StoreError)?;
let mut stream = words.search(&dfa).into_stream(); let mut stream = words.search(&dfa).into_stream();