From 7dc9ea78fab427202e6b613366af0b6eea79cbda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sun, 18 Aug 2019 18:57:41 +0200 Subject: [PATCH] feat: Make the automaton DFA construction lazy --- meilidb-core/src/query_builder.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/meilidb-core/src/query_builder.rs b/meilidb-core/src/query_builder.rs index b436f8604..2da52189b 100644 --- a/meilidb-core/src/query_builder.rs +++ b/meilidb-core/src/query_builder.rs @@ -29,17 +29,27 @@ struct Automaton { ngram: usize, query_len: usize, is_exact: bool, - dfa: DFA, + is_prefix: bool, + query: String, } 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 { Automaton { index, ngram, query_len: query.len(), is_exact: true, - dfa: build_dfa(query), + is_prefix: false, + query: query.to_string(), } } @@ -49,7 +59,8 @@ impl Automaton { ngram, query_len: query.len(), is_exact: true, - dfa: build_prefix_dfa(query), + is_prefix: true, + query: query.to_string(), } } @@ -59,7 +70,8 @@ impl Automaton { ngram, query_len: query.len(), is_exact: false, - dfa: build_dfa(query), + is_prefix: false, + query: query.to_string(), } } } @@ -344,7 +356,8 @@ where S: Store + Sync, .par_bridge() .try_for_each_with((sender, store, searchables), |data, automaton| { 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 mut stream = words.search(&dfa).into_stream();