|
8 | 8 | import org.apache.lucene.util.automaton.Automata; |
9 | 9 | import org.apache.lucene.util.automaton.Automaton; |
10 | 10 | import org.apache.lucene.util.automaton.CharacterRunAutomaton; |
| 11 | +import org.apache.lucene.util.automaton.MinimizationOperations; |
| 12 | +import org.apache.lucene.util.automaton.Operations; |
11 | 13 | import org.apache.lucene.util.automaton.RegExp; |
12 | 14 | import org.elasticsearch.common.cache.Cache; |
13 | 15 | import org.elasticsearch.common.cache.CacheBuilder; |
|
19 | 21 | import java.util.ArrayList; |
20 | 22 | import java.util.Arrays; |
21 | 23 | import java.util.Collection; |
| 24 | +import java.util.HashSet; |
22 | 25 | import java.util.List; |
| 26 | +import java.util.Set; |
23 | 27 | import java.util.concurrent.ExecutionException; |
| 28 | +import java.util.function.Function; |
24 | 29 | import java.util.function.Predicate; |
25 | 30 |
|
26 | | -import static org.apache.lucene.util.automaton.MinimizationOperations.minimize; |
27 | 31 | import static org.apache.lucene.util.automaton.Operations.DEFAULT_MAX_DETERMINIZED_STATES; |
28 | 32 | import static org.apache.lucene.util.automaton.Operations.concatenate; |
29 | 33 | import static org.apache.lucene.util.automaton.Operations.intersection; |
@@ -84,10 +88,82 @@ public static Automaton patterns(Collection<String> patterns) { |
84 | 88 | } |
85 | 89 |
|
86 | 90 | private static Automaton buildAutomaton(Collection<String> patterns) { |
87 | | - List<Automaton> automata = new ArrayList<>(patterns.size()); |
88 | | - for (String pattern : patterns) { |
89 | | - final Automaton patternAutomaton = pattern(pattern); |
90 | | - automata.add(patternAutomaton); |
| 91 | + if (patterns.size() == 1) { |
| 92 | + return minimize(pattern(patterns.iterator().next())); |
| 93 | + } |
| 94 | + |
| 95 | + final Function<Collection<String>, Automaton> build = strings -> { |
| 96 | + List<Automaton> automata = new ArrayList<>(strings.size()); |
| 97 | + for (String pattern : strings) { |
| 98 | + final Automaton patternAutomaton = pattern(pattern); |
| 99 | + automata.add(patternAutomaton); |
| 100 | + } |
| 101 | + return unionAndMinimize(automata); |
| 102 | + }; |
| 103 | + |
| 104 | + // We originally just compiled each automaton separately and then unioned them all. |
| 105 | + // However, that approach can be quite slow, and very memory intensive. |
| 106 | + // It is far more efficient if |
| 107 | + // 1. we strip leading/trailing "*" |
| 108 | + // 2. union the automaton produced from the remaining text |
| 109 | + // 3. append/prepend MatchAnyString automatons as appropriate |
| 110 | + // That is: |
| 111 | + // - `MATCH_ALL + (bullseye|daredevil) + MATCH_ALL` |
| 112 | + // can be determinized more efficiently than |
| 113 | + // - `(MATCH_ALL + bullseye + MATCH_ALL)|(MATCH_ALL + daredevil + MATCH_ALL)` |
| 114 | + |
| 115 | + final Set<String> prefix = new HashSet<>(); |
| 116 | + final Set<String> infix = new HashSet<>(); |
| 117 | + final Set<String> suffix = new HashSet<>(); |
| 118 | + final Set<String> misc = new HashSet<>(); |
| 119 | + |
| 120 | + for (String p : patterns) { |
| 121 | + if (p.length() <= 1) { |
| 122 | + // Single character strings (like "x" or "*"), or stray empty strings |
| 123 | + misc.add(p); |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + final char first = p.charAt(0); |
| 128 | + final char last = p.charAt(p.length() - 1); |
| 129 | + if (first == '/') { |
| 130 | + // regex ("/something/") |
| 131 | + misc.add(p); |
| 132 | + } else if (first == '*') { |
| 133 | + if (last == '*') { |
| 134 | + // *something* |
| 135 | + infix.add(p.substring(1, p.length() - 1)); |
| 136 | + } else { |
| 137 | + // *something |
| 138 | + suffix.add(p.substring(1)); |
| 139 | + } |
| 140 | + } else if (last == '*' && p.indexOf('*') != p.length() - 1) { |
| 141 | + // some*thing* |
| 142 | + // For simple prefix patterns ("something*") it's more efficient to do a single pass |
| 143 | + // Lucene can efficiently determinize automata that share a trailing MATCH_ANY accept state, |
| 144 | + // If we were to handle them here, we would run 2 minimize operations (one for the union of strings, |
| 145 | + // then another after concatenating MATCH_ANY), which is substantially slower. |
| 146 | + // However, that's not true if the string has an embedded '*' in it - in that case it is more efficient to determinize |
| 147 | + // the set of prefixes (with the embedded MATCH_ANY) and then concatenate another MATCH_ANY and minimize. |
| 148 | + prefix.add(p.substring(0, p.length() - 1)); |
| 149 | + } else { |
| 150 | + // something* / some*thing / some?thing / etc |
| 151 | + misc.add(p); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + final List<Automaton> automata = new ArrayList<>(); |
| 156 | + if (prefix.isEmpty() == false) { |
| 157 | + automata.add(Operations.concatenate(build.apply(prefix), Automata.makeAnyString())); |
| 158 | + } |
| 159 | + if (suffix.isEmpty() == false) { |
| 160 | + automata.add(Operations.concatenate(Automata.makeAnyString(), build.apply(suffix))); |
| 161 | + } |
| 162 | + if (infix.isEmpty() == false) { |
| 163 | + automata.add(Operations.concatenate(Arrays.asList(Automata.makeAnyString(), build.apply(infix), Automata.makeAnyString()))); |
| 164 | + } |
| 165 | + if (misc.isEmpty() == false) { |
| 166 | + automata.add(build.apply(misc)); |
91 | 167 | } |
92 | 168 | return unionAndMinimize(automata); |
93 | 169 | } |
@@ -172,18 +248,22 @@ static Automaton wildcard(String text) { |
172 | 248 | } |
173 | 249 |
|
174 | 250 | public static Automaton unionAndMinimize(Collection<Automaton> automata) { |
175 | | - Automaton res = union(automata); |
176 | | - return minimize(res, maxDeterminizedStates); |
| 251 | + Automaton res = automata.size() == 1 ? automata.iterator().next() : union(automata); |
| 252 | + return minimize(res); |
177 | 253 | } |
178 | 254 |
|
179 | 255 | public static Automaton minusAndMinimize(Automaton a1, Automaton a2) { |
180 | 256 | Automaton res = minus(a1, a2, maxDeterminizedStates); |
181 | | - return minimize(res, maxDeterminizedStates); |
| 257 | + return minimize(res); |
182 | 258 | } |
183 | 259 |
|
184 | 260 | public static Automaton intersectAndMinimize(Automaton a1, Automaton a2) { |
185 | 261 | Automaton res = intersection(a1, a2); |
186 | | - return minimize(res, maxDeterminizedStates); |
| 262 | + return minimize(res); |
| 263 | + } |
| 264 | + |
| 265 | + private static Automaton minimize(Automaton automaton) { |
| 266 | + return MinimizationOperations.minimize(automaton, maxDeterminizedStates); |
187 | 267 | } |
188 | 268 |
|
189 | 269 | public static Predicate<String> predicate(String... patterns) { |
|
0 commit comments