Skip to content

Apache Lucene open-source search software

LUCENE-10010: don't determinize in CompiledAutomaton/RunAutomaton (#485)

Robert Muirb2e866b70366
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index aa5f7ee35e8..0362f6698cd 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -7,7 +7,10 @@ http://s.apache.org/luceneversions
API Changes
---------------------
-(No changes)
+
+* LUCENE-10010: AutomatonQuery, CompiledAutomaton, and RunAutomaton
+ classes no longer determinize NFAs. Instead it is the responsibility
+ of the caller to determinize. (Robert Muir)
New Features
---------------------
diff --git a/lucene/MIGRATE.md b/lucene/MIGRATE.md
index 2456127d30f..8081832b32e 100644
--- a/lucene/MIGRATE.md
+++ b/lucene/MIGRATE.md
@@ -17,6 +17,14 @@
# Apache Lucene Migration Guide
+## Migration from Lucene 9.x to Lucene 10.0
+
+### AutomatonQuery/CompiledAutomaton/RunAutomaton no longer determinize (LUCENE-10010)
+
+These classes no longer take a `determinizeWorkLimit` and no longer determinize
+behind the scenes. It is the responsibility of the caller to to call
+`Operations.determinize()` for DFA execution.
+
## Migration from Lucene 9.0 to Lucene 9.1
### Minor syntactical changes in StandardQueryParser (LUCENE-10223)
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/SimplePatternSplitTokenizer.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/SimplePatternSplitTokenizer.java
index bb0facc59e6..640ac92ed6a 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/SimplePatternSplitTokenizer.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/SimplePatternSplitTokenizer.java
@@ -88,7 +88,7 @@ public final class SimplePatternSplitTokenizer extends Tokenizer {
throw new IllegalArgumentException("please determinize the incoming automaton first");
}
- runDFA = new CharacterRunAutomaton(dfa, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+ runDFA = new CharacterRunAutomaton(dfa);
}
private void fillToken(int offsetStart) {
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/SimplePatternTokenizer.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/SimplePatternTokenizer.java
index 6c364400fc6..61812a21ece 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/SimplePatternTokenizer.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/pattern/SimplePatternTokenizer.java
@@ -98,7 +98,7 @@ public final class SimplePatternTokenizer extends Tokenizer {
throw new IllegalArgumentException("please determinize the incoming automaton first");
}
- runDFA = new CharacterRunAutomaton(dfa, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+ runDFA = new CharacterRunAutomaton(dfa);
}
@Override
diff --git a/lucene/core/src/java/org/apache/lucene/search/AutomatonQuery.java b/lucene/core/src/java/org/apache/lucene/search/AutomatonQuery.java
index ef30c0a4817..327960feb8e 100644
--- a/lucene/core/src/java/org/apache/lucene/search/AutomatonQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/AutomatonQuery.java
@@ -25,7 +25,6 @@ import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.CompiledAutomaton;
-import org.apache.lucene.util.automaton.Operations;
/**
* A {@link Query} that will match terms against a finite-state machine.
@@ -35,10 +34,9 @@ import org.apache.lucene.util.automaton.Operations;
* Alternatively, it can be created from a regular expression with {@link RegexpQuery} or from the
* standard Lucene wildcard syntax with {@link WildcardQuery}.
*
- * <p>When the query is executed, it will create an equivalent DFA of the finite-state machine, and
- * will enumerate the term dictionary in an intelligent way to reduce the number of comparisons. For
- * example: the regular expression of <code>[dl]og?</code> will make approximately four comparisons:
- * do, dog, lo, and log.
+ * <p>When the query is executed, it will will enumerate the term dictionary in an intelligent way
+ * to reduce the number of comparisons. For example: the regular expression of <code>[dl]og?</code>
+ * will make approximately four comparisons: do, dog, lo, and log.
*
* @lucene.experimental
*/
@@ -63,9 +61,10 @@ public class AutomatonQuery extends MultiTermQuery implements Accountable {
* @param term Term containing field and possibly some pattern structure. The term text is
* ignored.
* @param automaton Automaton to run, terms that are accepted are considered a match.
+ * @throws IllegalArgumentException if automaton is not determinized
*/
public AutomatonQuery(final Term term, Automaton automaton) {
- this(term, automaton, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+ this(term, automaton, false);
}
/**
@@ -74,34 +73,17 @@ public class AutomatonQuery extends MultiTermQuery implements Accountable {
* @param term Term containing field and possibly some pattern structure. The term text is
* ignored.
* @param automaton Automaton to run, terms that are accepted are considered a match.
- * @param determinizeWorkLimit maximum effort to spend determinizing the automaton. If the
- * automaton would need more than this much effort, TooComplexToDeterminizeException is
- * thrown. Higher numbers require more space but can process more complex automata.
- */
- public AutomatonQuery(final Term term, Automaton automaton, int determinizeWorkLimit) {
- this(term, automaton, determinizeWorkLimit, false);
- }
-
- /**
- * Create a new AutomatonQuery from an {@link Automaton}.
- *
- * @param term Term containing field and possibly some pattern structure. The term text is
- * ignored.
- * @param automaton Automaton to run, terms that are accepted are considered a match.
- * @param determinizeWorkLimit maximum effort to spend determinizing the automaton. If the
- * automaton will need more than this much effort, TooComplexToDeterminizeException is thrown.
- * Higher numbers require more space but can process more complex automata.
* @param isBinary if true, this automaton is already binary and will not go through the
* UTF32ToUTF8 conversion
+ * @throws IllegalArgumentException if automaton is not determinized
*/
- public AutomatonQuery(
- final Term term, Automaton automaton, int determinizeWorkLimit, boolean isBinary) {
+ public AutomatonQuery(final Term term, Automaton automaton, boolean isBinary) {
super(term.field());
this.term = term;
this.automaton = automaton;
this.automatonIsBinary = isBinary;
// TODO: we could take isFinite too, to save a bit of CPU in CompiledAutomaton ctor?:
- this.compiled = new CompiledAutomaton(automaton, null, true, determinizeWorkLimit, isBinary);
+ this.compiled = new CompiledAutomaton(automaton, null, true, isBinary);
this.ramBytesUsed =
BASE_RAM_BYTES + term.ramBytesUsed() + automaton.ramBytesUsed() + compiled.ramBytesUsed();
diff --git a/lucene/core/src/java/org/apache/lucene/search/PrefixQuery.java b/lucene/core/src/java/org/apache/lucene/search/PrefixQuery.java
index 9b13d824c7d..0ee553f563c 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PrefixQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PrefixQuery.java
@@ -30,9 +30,7 @@ public class PrefixQuery extends AutomatonQuery {
/** Constructs a query for terms starting with <code>prefix</code>. */
public PrefixQuery(Term prefix) {
- // It's OK to pass unlimited determinizeWorkLimit: the automaton is born small and
- // determinized:
- super(prefix, toAutomaton(prefix.bytes()), Integer.MAX_VALUE, true);
+ super(prefix, toAutomaton(prefix.bytes()), true);
}
/** Build an automaton accepting all terms with the specified prefix. */
diff --git a/lucene/core/src/java/org/apache/lucene/search/RegexpQuery.java b/lucene/core/src/java/org/apache/lucene/search/RegexpQuery.java
index 7e31058bf0b..350a1e2d820 100644
--- a/lucene/core/src/java/org/apache/lucene/search/RegexpQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/RegexpQuery.java
@@ -139,9 +139,10 @@ public class RegexpQuery extends AutomatonQuery {
int determinizeWorkLimit) {
super(
term,
- new RegExp(term.text(), syntax_flags, match_flags)
- .toAutomaton(provider, determinizeWorkLimit),
- determinizeWorkLimit);
+ Operations.determinize(
+ new RegExp(term.text(), syntax_flags, match_flags)
+ .toAutomaton(provider, determinizeWorkLimit),
+ determinizeWorkLimit));
}
/** Returns the regexp of this query wrapped in a Term. */
diff --git a/lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.java
index e2cde6ce465..b798baa640a 100644
--- a/lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/TermInSetQuery.java
@@ -140,13 +140,17 @@ public class TermInSetQuery extends Query implements Accountable {
}
}
+ // TODO: this is extremely slow. we should not be doing this.
private ByteRunAutomaton asByteRunAutomaton() {
TermIterator iterator = termData.iterator();
List<Automaton> automata = new ArrayList<>();
for (BytesRef term = iterator.next(); term != null; term = iterator.next()) {
automata.add(Automata.makeBinary(term));
}
- return new CompiledAutomaton(Operations.union(automata)).runAutomaton;
+ Automaton automaton =
+ Operations.determinize(
+ Operations.union(automata), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+ return new CompiledAutomaton(automaton).runAutomaton;
}
@Override
diff --git a/lucene/core/src/java/org/apache/lucene/search/TermRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/TermRangeQuery.java
index c446602448d..27d76ae1465 100644
--- a/lucene/core/src/java/org/apache/lucene/search/TermRangeQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/TermRangeQuery.java
@@ -63,7 +63,6 @@ public class TermRangeQuery extends AutomatonQuery {
super(
new Term(field, lowerTerm),
toAutomaton(lowerTerm, upperTerm, includeLower, includeUpper),
- Integer.MAX_VALUE,
true);
this.lowerTerm = lowerTerm;
this.upperTerm = upperTerm;
diff --git a/lucene/core/src/java/org/apache/lucene/search/WildcardQuery.java b/lucene/core/src/java/org/apache/lucene/search/WildcardQuery.java
index 19f312e0a79..27989ca6c12 100644
--- a/lucene/core/src/java/org/apache/lucene/search/WildcardQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/WildcardQuery.java
@@ -47,7 +47,7 @@ public class WildcardQuery extends AutomatonQuery {
/** Constructs a query for terms matching <code>term</code>. */
public WildcardQuery(Term term) {
- super(term, toAutomaton(term));
+ this(term, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
}
/**
@@ -59,7 +59,7 @@ public class WildcardQuery extends AutomatonQuery {
* otherwise know what to specify.
*/
public WildcardQuery(Term term, int determinizeWorkLimit) {
- super(term, toAutomaton(term), determinizeWorkLimit);
+ super(term, toAutomaton(term, determinizeWorkLimit));
}
/**
@@ -68,7 +68,7 @@ public class WildcardQuery extends AutomatonQuery {
* @lucene.internal
*/
@SuppressWarnings("fallthrough")
- public static Automaton toAutomaton(Term wildcardquery) {
+ public static Automaton toAutomaton(Term wildcardquery, int determinizeWorkLimit) {
List<Automaton> automata = new ArrayList<>();
String wildcardText = wildcardquery.text();
@@ -97,7 +97,7 @@ public class WildcardQuery extends AutomatonQuery {
i += length;
}
- return Operations.concatenate(automata);
+ return Operations.determinize(Operations.concatenate(automata), determinizeWorkLimit);
}
/** Returns the pattern term. */
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/ByteRunAutomaton.java b/lucene/core/src/java/org/apache/lucene/util/automaton/ByteRunAutomaton.java
index 1c43bbbae60..5f65445d4d8 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/ByteRunAutomaton.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/ByteRunAutomaton.java
@@ -19,14 +19,30 @@ package org.apache.lucene.util.automaton;
/** Automaton representation for matching UTF-8 byte[]. */
public class ByteRunAutomaton extends RunAutomaton {
- /** Converts incoming automaton to byte-based (UTF32ToUTF8) first */
+ /**
+ * Converts incoming automaton to byte-based (UTF32ToUTF8) first
+ *
+ * @throws IllegalArgumentException if the automaton is not deterministic
+ */
public ByteRunAutomaton(Automaton a) {
- this(a, false, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+ this(a, false);
}
- /** expert: if isBinary is true, the input is already byte-based */
- public ByteRunAutomaton(Automaton a, boolean isBinary, int determinizeWorkLimit) {
- super(isBinary ? a : new UTF32ToUTF8().convert(a), 256, determinizeWorkLimit);
+ /**
+ * expert: if isBinary is true, the input is already byte-based
+ *
+ * @throws IllegalArgumentException if the automaton is not deterministic
+ */
+ public ByteRunAutomaton(Automaton a, boolean isBinary) {
+ super(isBinary ? a : convert(a), 256);
+ }
+
+ static Automaton convert(Automaton a) {
+ if (!a.isDeterministic()) {
+ throw new IllegalArgumentException("Automaton must be deterministic");
+ }
+ // we checked the input is a DFA, according to mike this determinization is contained :)
+ return Operations.determinize(new UTF32ToUTF8().convert(a), Integer.MAX_VALUE);
}
/** Returns true if the given byte array is accepted by this automaton */
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/CharacterRunAutomaton.java b/lucene/core/src/java/org/apache/lucene/util/automaton/CharacterRunAutomaton.java
index f65ccd29ac7..92f98b1824a 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/CharacterRunAutomaton.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/CharacterRunAutomaton.java
@@ -18,22 +18,13 @@ package org.apache.lucene.util.automaton;
/** Automaton representation for matching char[]. */
public class CharacterRunAutomaton extends RunAutomaton {
- /** Construct with a default number of determinizeWorkLimit. */
- public CharacterRunAutomaton(Automaton a) {
- this(a, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
- }
-
/**
- * Constructor specifying determinizeWorkLimit.
+ * Construct from a DFA
*
- * @param a Automaton to match
- * @param determinizeWorkLimit maximum effort to spend determinizing the automataon. If more
- * effort is required then a TooComplexToDeterminizeException is thrown. Use {@link
- * Operations#DEFAULT_DETERMINIZE_WORK_LIMIT} as a decent default if you don't otherwise know
- * what to specify.
+ * @throws IllegalArgumentException if the automaton is not deterministic
*/
- public CharacterRunAutomaton(Automaton a, int determinizeWorkLimit) {
- super(a, Character.MAX_CODE_POINT + 1, determinizeWorkLimit);
+ public CharacterRunAutomaton(Automaton a) {
+ super(a, Character.MAX_CODE_POINT + 1);
}
/** Returns true if the given string is accepted by this automaton. */
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java b/lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java
index f2ec26d807a..1627bb66c7c 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/CompiledAutomaton.java
@@ -34,8 +34,8 @@ import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.UnicodeUtil;
/**
- * Immutable class holding compiled details for a given Automaton. The Automaton is deterministic,
- * must not have dead states but is not necessarily minimal.
+ * Immutable class holding compiled details for a given Automaton. The Automaton must be
+ * deterministic, must not have dead states but is not necessarily minimal.
*
* @lucene.experimental
*/
@@ -131,24 +131,27 @@ public class CompiledAutomaton implements Accountable {
* Create this. If finite is null, we use {@link Operations#isFinite} to determine whether it is
* finite. If simplify is true, we run possibly expensive operations to determine if the automaton
* is one the cases in {@link CompiledAutomaton.AUTOMATON_TYPE}.
+ *
+ * @throws IllegalArgumentException if the automaton is not deterministic
*/
public CompiledAutomaton(Automaton automaton, Boolean finite, boolean simplify) {
- this(automaton, finite, simplify, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT, false);
+ this(automaton, finite, simplify, false);
}
/**
* Create this. If finite is null, we use {@link Operations#isFinite} to determine whether it is
* finite. If simplify is true, we run possibly expensive operations to determine if the automaton
- * is one the cases in {@link CompiledAutomaton.AUTOMATON_TYPE}. If simplify requires
- * determinizing the automaton then at most determinizeWorkLimit effort will be spent. Any more
- * than that will cause a TooComplexToDeterminizeException.
+ * is one the cases in {@link CompiledAutomaton.AUTOMATON_TYPE}.
+ *
+ * @throws IllegalArgumentException if the automaton is not deterministic
*/
public CompiledAutomaton(
- Automaton automaton,
- Boolean finite,
- boolean simplify,
- int determinizeWorkLimit,
- boolean isBinary) {
+ Automaton automaton, Boolean finite, boolean simplify, boolean isBinary) {
+ // require DFA for input, as this class currently can't handle NFAs.
+ if (!automaton.isDeterministic()) {
+ throw new IllegalArgumentException("Automaton must be deterministic");
+ }
+
if (automaton.getNumStates() == 0) {
automaton = new Automaton();
automaton.createState();
@@ -193,8 +196,6 @@ public class CompiledAutomaton implements Accountable {
return;
}
- automaton = Operations.determinize(automaton, determinizeWorkLimit);
-
IntsRef singleton = Operations.getSingleton(automaton);
if (singleton != null) {
@@ -250,8 +251,9 @@ public class CompiledAutomaton implements Accountable {
}
}
- // This will determinize the binary automaton for us:
- runAutomaton = new ByteRunAutomaton(binary, true, determinizeWorkLimit);
+ // We already had a DFA (or threw exception), according to mike UTF32toUTF8 won't "blow up"
+ binary = Operations.determinize(binary, Integer.MAX_VALUE);
+ runAutomaton = new ByteRunAutomaton(binary, true);
this.automaton = runAutomaton.automaton;
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/RunAutomaton.java b/lucene/core/src/java/org/apache/lucene/util/automaton/RunAutomaton.java
index d4b2a2ec41e..dea87b9273d 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/RunAutomaton.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/RunAutomaton.java
@@ -56,20 +56,13 @@ public abstract class RunAutomaton implements Accountable {
* Constructs a new <code>RunAutomaton</code> from a deterministic <code>Automaton</code>.
*
* @param a an automaton
+ * @throws IllegalArgumentException if the automaton is not deterministic
*/
protected RunAutomaton(Automaton a, int alphabetSize) {
- this(a, alphabetSize, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
- }
-
- /**
- * Constructs a new <code>RunAutomaton</code> from a deterministic <code>Automaton</code>.
- *
- * @param a an automaton
- * @param determinizeWorkLimit maximum effort to spend while determinizing
- */
- protected RunAutomaton(Automaton a, int alphabetSize, int determinizeWorkLimit) {
this.alphabetSize = alphabetSize;
- a = Operations.determinize(a, determinizeWorkLimit);
+ if (!a.isDeterministic()) {
+ throw new IllegalArgumentException("Automaton must be deterministic");
+ }
this.automaton = a;
points = a.getStartPoints();
size = Math.max(1, a.getNumStates());
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum.java b/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum.java
index 5e26dda3cf6..d1cea7646cf 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum.java
@@ -280,7 +280,7 @@ public class TestTermsEnum extends LuceneTestCase {
a = Automata.makeStringUnion(sortedAcceptTerms);
}
- final CompiledAutomaton c = new CompiledAutomaton(a, true, false, 1000000, false);
+ final CompiledAutomaton c = new CompiledAutomaton(a, true, false, false);
final BytesRef[] acceptTermsArray = new BytesRef[acceptTerms.size()];
final Set<BytesRef> acceptTermsSet = new HashSet<>();
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum2.java b/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum2.java
index 97fc663298f..ee292f421d7 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum2.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum2.java
@@ -105,7 +105,7 @@ public class TestTermsEnum2 extends LuceneTestCase {
// AutomatonTestUtil.minimizeSimple(alternate);
// System.out.println("minimize done");
AutomatonQuery a1 = new AutomatonQuery(new Term("field", ""), automaton);
- AutomatonQuery a2 = new AutomatonQuery(new Term("field", ""), alternate, Integer.MAX_VALUE);
+ AutomatonQuery a2 = new AutomatonQuery(new Term("field", ""), alternate);
ScoreDoc[] origHits = searcher.search(a1, 25).scoreDocs;
ScoreDoc[] newHits = searcher.search(a2, 25).scoreDocs;
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestAutomatonQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestAutomatonQuery.java
index a4e84c31aff..71ed126cdd0 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestAutomatonQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestAutomatonQuery.java
@@ -113,7 +113,10 @@ public class TestAutomatonQuery extends LuceneTestCase {
assertAutomatonHits(1, Automata.makeChar('a'));
assertAutomatonHits(2, Automata.makeCharRange('a', 'b'));
assertAutomatonHits(2, Automata.makeDecimalInterval(1233, 2346, 0));
- assertAutomatonHits(1, Automata.makeDecimalInterval(0, 2000, 0));
+ assertAutomatonHits(
+ 1,
+ Operations.determinize(
+ Automata.makeDecimalInterval(0, 2000, 0), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT));
assertAutomatonHits(2, Operations.union(Automata.makeChar('a'), Automata.makeChar('b')));
assertAutomatonHits(0, Operations.intersection(Automata.makeChar('a'), Automata.makeChar('b')));
assertAutomatonHits(
@@ -124,12 +127,16 @@ public class TestAutomatonQuery extends LuceneTestCase {
DEFAULT_DETERMINIZE_WORK_LIMIT));
}
- /** Test that a nondeterministic automaton works correctly. (It should will be determinized) */
+ /** Test that a nondeterministic automaton fails, it should throw Exception */
public void testNFA() throws IOException {
// accept this or three, the union is an NFA (two transitions for 't' from
// initial state)
Automaton nfa = Operations.union(Automata.makeString("this"), Automata.makeString("three"));
- assertAutomatonHits(2, nfa);
+ expectThrows(
+ IllegalArgumentException.class,
+ () -> {
+ assertAutomatonHits(2, nfa);
+ });
}
public void testEquals() {
@@ -199,8 +206,9 @@ public class TestAutomatonQuery extends LuceneTestCase {
queries[i] =
new AutomatonQuery(
new Term("bogus", "bogus"),
- AutomatonTestUtil.randomAutomaton(random()),
- Integer.MAX_VALUE);
+ Operations.determinize(
+ AutomatonTestUtil.randomAutomaton(random()),
+ Operations.DEFAULT_DETERMINIZE_WORK_LIMIT));
}
final CountDownLatch startingGun = new CountDownLatch(1);
int numThreads = TestUtil.nextInt(random(), 2, 5);
@@ -236,6 +244,6 @@ public class TestAutomatonQuery extends LuceneTestCase {
terms.add(new BytesRef(TestUtil.randomUnicodeString(random())));
}
Collections.sort(terms);
- new AutomatonQuery(new Term("foo", "bar"), Automata.makeStringUnion(terms), Integer.MAX_VALUE);
+ new AutomatonQuery(new Term("foo", "bar"), Automata.makeStringUnion(terms));
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFuzzyQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestFuzzyQuery.java
index 7be05292bce..c1f3f05e8ed 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestFuzzyQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestFuzzyQuery.java
@@ -16,9 +16,6 @@
*/
package org.apache.lucene.search;
-import static org.hamcrest.CoreMatchers.containsString;
-
-import com.carrotsearch.randomizedtesting.RandomizedTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -48,7 +45,6 @@ import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil;
import org.apache.lucene.util.automaton.ByteRunAutomaton;
import org.apache.lucene.util.automaton.LevenshteinAutomata;
-import org.apache.lucene.util.automaton.Operations;
/** Tests {@link FuzzyQuery}. */
public class TestFuzzyQuery extends LuceneTestCase {
@@ -566,36 +562,6 @@ public class TestFuzzyQuery extends LuceneTestCase {
assertTrue(expected.getMessage().contains("maxExpansions must be positive"));
}
- private String randomRealisticMultiByteUnicode(int length) {
- while (true) {
- // There is 1 single-byte unicode block, and 194 multi-byte blocks
- String value = RandomizedTest.randomRealisticUnicodeOfCodepointLength(length);
- if (value.charAt(0) > Byte.MAX_VALUE) {
- return value;
- }
- }
- }
-
- public void testErrorMessage() {
- // 45 states per vector from Lev2TParametricDescription
- final int length = (Operations.DEFAULT_DETERMINIZE_WORK_LIMIT / 5) + 10;
- final String value = randomRealisticMultiByteUnicode(length);
-
- FuzzyTermsEnum.FuzzyTermsException expected =
- expectThrows(
- FuzzyTermsEnum.FuzzyTermsException.class,
- () -> {
- new FuzzyAutomatonBuilder(value, 2, 0, true).buildMaxEditAutomaton();
- });
- assertThat(expected.getMessage(), containsString(value));
-
- expected =
- expectThrows(
- FuzzyTermsEnum.FuzzyTermsException.class,
- () -> new FuzzyAutomatonBuilder(value, 2, 0, true).buildAutomatonSet());
- assertThat(expected.getMessage(), containsString(value));
- }
-
private void addDoc(String text, RandomIndexWriter writer) throws IOException {
Document doc = new Document();
doc.add(newTextField("field", text, Field.Store.YES));
diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java
index 0e3e06a1b78..da9d68f846a 100644
--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java
+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestCompiledAutomaton.java
@@ -36,7 +36,7 @@ public class TestCompiledAutomaton extends LuceneTestCase {
}
Collections.sort(terms);
final Automaton a = DaciukMihovAutomatonBuilder.build(terms);
- return new CompiledAutomaton(a, true, false, determinizeWorkLimit, false);
+ return new CompiledAutomaton(a, true, false, false);
}
private void testFloor(CompiledAutomaton c, String input, String expected) {
@@ -130,7 +130,7 @@ public class TestCompiledAutomaton extends LuceneTestCase {
a.addTransition(state, state, 0, 0xff);
a.finishState();
- CompiledAutomaton ca = new CompiledAutomaton(a, null, true, Integer.MAX_VALUE, true);
+ CompiledAutomaton ca = new CompiledAutomaton(a, null, true, true);
assertEquals(CompiledAutomaton.AUTOMATON_TYPE.ALL, ca.type);
}
@@ -142,7 +142,7 @@ public class TestCompiledAutomaton extends LuceneTestCase {
a.addTransition(state, state, 0, Character.MAX_CODE_POINT);
a.finishState();
- CompiledAutomaton ca = new CompiledAutomaton(a, null, true, Integer.MAX_VALUE, false);
+ CompiledAutomaton ca = new CompiledAutomaton(a, null, true, false);
assertEquals(CompiledAutomaton.AUTOMATON_TYPE.ALL, ca.type);
}
@@ -150,14 +150,14 @@ public class TestCompiledAutomaton extends LuceneTestCase {
public void testBinarySingleton() throws Exception {
// This is just ascii so we can pretend it's binary:
Automaton a = Automata.makeString("foobar");
- CompiledAutomaton ca = new CompiledAutomaton(a, null, true, Integer.MAX_VALUE, true);
+ CompiledAutomaton ca = new CompiledAutomaton(a, null, true, true);
assertEquals(CompiledAutomaton.AUTOMATON_TYPE.SINGLE, ca.type);
}
// LUCENE-6367
public void testUnicodeSingleton() throws Exception {
Automaton a = Automata.makeString(TestUtil.randomRealisticUnicodeString(random()));
- CompiledAutomaton ca = new CompiledAutomaton(a, null, true, Integer.MAX_VALUE, false);
+ CompiledAutomaton ca = new CompiledAutomaton(a, null, true, false);
assertEquals(CompiledAutomaton.AUTOMATON_TYPE.SINGLE, ca.type);
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestDeterminizeLexicon.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestDeterminizeLexicon.java
index 4e788dea73b..557c8ebbc61 100644
--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestDeterminizeLexicon.java
+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestDeterminizeLexicon.java
@@ -55,7 +55,7 @@ public class TestDeterminizeLexicon extends LuceneTestCase {
}
if (TEST_NIGHTLY) {
// TODO: very wasteful of RAM to do this without minimizing first.
- final ByteRunAutomaton lexByte = new ByteRunAutomaton(lex, false, 1000000);
+ final ByteRunAutomaton lexByte = new ByteRunAutomaton(lex, false);
for (String s : terms) {
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
assertTrue(lexByte.run(bytes, 0, bytes.length));
diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java
index 206ea38ecb7..ec191923502 100644
--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java
+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestUTF32ToUTF8.java
@@ -182,6 +182,7 @@ public class TestUTF32ToUTF8 extends LuceneTestCase {
RegExp re = new RegExp(".+\u0775");
String input = "\ufadc\ufffd\ub80b\uda5a\udc68\uf234\u0056\uda5b\udcc1\ufffd\ufffd\u0775";
Automaton automaton = re.toAutomaton();
+ automaton = Operations.determinize(automaton, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
CharacterRunAutomaton cra = new CharacterRunAutomaton(automaton);
ByteRunAutomaton bra = new ByteRunAutomaton(automaton);
@@ -196,6 +197,7 @@ public class TestUTF32ToUTF8 extends LuceneTestCase {
String input =
"\u5cfd\ufffd\ub2f7\u0033\ue304\u51d7\u3692\udb50\udfb3\u0576\udae2\udc62\u0053\u0449\u04d4";
Automaton automaton = re.toAutomaton();
+ automaton = Operations.determinize(automaton, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
CharacterRunAutomaton cra = new CharacterRunAutomaton(automaton);
ByteRunAutomaton bra = new ByteRunAutomaton(automaton);
@@ -208,8 +210,10 @@ public class TestUTF32ToUTF8 extends LuceneTestCase {
public void testRandomRegexes() throws Exception {
int num = atLeast(50);
for (int i = 0; i < num; i++) {
- assertAutomaton(
- new RegExp(AutomatonTestUtil.randomRegexp(random()), RegExp.NONE).toAutomaton());
+ Automaton automaton =
+ new RegExp(AutomatonTestUtil.randomRegexp(random()), RegExp.NONE).toAutomaton();
+ automaton = Operations.determinize(automaton, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+ assertAutomaton(automaton);
}
}
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java
index 51dfe48ad8b..080194aec88 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java
@@ -29,6 +29,7 @@ import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.automaton.CompiledAutomaton;
+import org.apache.lucene.util.automaton.Operations;
/**
* Factory functions for creating {@link IntervalsSource interval sources}.
@@ -185,7 +186,10 @@ public final class Intervals {
* @see WildcardQuery for glob format
*/
public static IntervalsSource wildcard(BytesRef wildcard, int maxExpansions) {
- CompiledAutomaton ca = new CompiledAutomaton(WildcardQuery.toAutomaton(new Term("", wildcard)));
+ CompiledAutomaton ca =
+ new CompiledAutomaton(
+ WildcardQuery.toAutomaton(
+ new Term("", wildcard), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT));
return new MultiTermIntervalsSource(ca, maxExpansions, wildcard.utf8ToString());
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/analysis/MockTokenFilter.java b/lucene/test-framework/src/java/org/apache/lucene/analysis/MockTokenFilter.java
index 3edad0696b7..e1c72070b29 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/analysis/MockTokenFilter.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/analysis/MockTokenFilter.java
@@ -42,41 +42,43 @@ public final class MockTokenFilter extends TokenFilter {
/** Set of common english stopwords */
public static final CharacterRunAutomaton ENGLISH_STOPSET =
new CharacterRunAutomaton(
- Operations.union(
- Arrays.asList(
- makeString("a"),
- makeString("an"),
- makeString("and"),
- makeString("are"),
- makeString("as"),
- makeString("at"),
- makeString("be"),
- makeString("but"),
- makeString("by"),
- makeString("for"),
- makeString("if"),
- makeString("in"),
- makeString("into"),
- makeString("is"),
- makeString("it"),
- makeString("no"),
- makeString("not"),
- makeString("of"),
- makeString("on"),
- makeString("or"),
- makeString("such"),
- makeString("that"),
- makeString("the"),
- makeString("their"),
- makeString("then"),
- makeString("there"),
- makeString("these"),
- makeString("they"),
- makeString("this"),
- makeString("to"),
- makeString("was"),
- makeString("will"),
- makeString("with"))));
+ Operations.determinize(
+ Operations.union(
+ Arrays.asList(
+ makeString("a"),
+ makeString("an"),
+ makeString("and"),
+ makeString("are"),
+ makeString("as"),
+ makeString("at"),
+ makeString("be"),
+ makeString("but"),
+ makeString("by"),
+ makeString("for"),
+ makeString("if"),
+ makeString("in"),
+ makeString("into"),
+ makeString("is"),
+ makeString("it"),
+ makeString("no"),
+ makeString("not"),
+ makeString("of"),
+ makeString("on"),
+ makeString("or"),
+ makeString("such"),
+ makeString("that"),
+ makeString("the"),
+ makeString("their"),
+ makeString("then"),
+ makeString("there"),
+ makeString("these"),
+ makeString("they"),
+ makeString("this"),
+ makeString("to"),
+ makeString("was"),
+ makeString("will"),
+ makeString("with"))),
+ Operations.DEFAULT_DETERMINIZE_WORK_LIMIT));
private final CharacterRunAutomaton filter;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/analysis/MockTokenizer.java b/lucene/test-framework/src/java/org/apache/lucene/analysis/MockTokenizer.java
index ed04a533673..01976bbb44b 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/analysis/MockTokenizer.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/analysis/MockTokenizer.java
@@ -24,6 +24,7 @@ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.util.AttributeFactory;
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
+import org.apache.lucene.util.automaton.Operations;
import org.apache.lucene.util.automaton.RegExp;
/**
@@ -42,16 +43,23 @@ import org.apache.lucene.util.automaton.RegExp;
public class MockTokenizer extends Tokenizer {
/** Acts Similar to WhitespaceTokenizer */
public static final CharacterRunAutomaton WHITESPACE =
- new CharacterRunAutomaton(new RegExp("[^ \t\r\n]+").toAutomaton());
+ new CharacterRunAutomaton(
+ Operations.determinize(
+ new RegExp("[^ \t\r\n]+").toAutomaton(), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT));
/**
* Acts Similar to KeywordTokenizer. TODO: Keyword returns an "empty" token for an empty reader...
*/
public static final CharacterRunAutomaton KEYWORD =
- new CharacterRunAutomaton(new RegExp(".*").toAutomaton());
+ new CharacterRunAutomaton(
+ Operations.determinize(
+ new RegExp(".*").toAutomaton(), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT));
/** Acts like LetterTokenizer. */
// the ugly regex below is incomplete Unicode 5.2 [:Letter:]
public static final CharacterRunAutomaton SIMPLE =
- new CharacterRunAutomaton(new RegExp("[A-Za-zªµºÀ-ÖØ-öø-ˁ一-鿌]+").toAutomaton());
+ new CharacterRunAutomaton(
+ Operations.determinize(
+ new RegExp("[A-Za-zªµºÀ-ÖØ-öø-ˁ一-鿌]+").toAutomaton(),
+ Operations.DEFAULT_DETERMINIZE_WORK_LIMIT));
private final CharacterRunAutomaton runAutomaton;
private final boolean lowerCase;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/RandomPostingsTester.java b/lucene/test-framework/src/java/org/apache/lucene/index/RandomPostingsTester.java
index e14ce273140..26db06401d7 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/index/RandomPostingsTester.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/index/RandomPostingsTester.java
@@ -60,6 +60,7 @@ import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.AutomatonTestUtil;
import org.apache.lucene.util.automaton.AutomatonTestUtil.RandomAcceptedStrings;
import org.apache.lucene.util.automaton.CompiledAutomaton;
+import org.apache.lucene.util.automaton.Operations;
/** Helper class extracted from BasePostingsFormatTestCase to exercise a postings format. */
public class RandomPostingsTester {
@@ -1578,7 +1579,8 @@ public class RandomPostingsTester {
for (String field : fields.keySet()) {
while (true) {
Automaton a = AutomatonTestUtil.randomAutomaton(random);
- CompiledAutomaton ca = new CompiledAutomaton(a, null, true, Integer.MAX_VALUE, false);
+ a = Operations.determinize(a, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+ CompiledAutomaton ca = new CompiledAutomaton(a, null, true, false);
if (ca.type != CompiledAutomaton.AUTOMATON_TYPE.NORMAL) {
// Keep retrying until we get an A that will really "use" the PF's intersect code:
continue;
diff --git a/lucene/test-framework/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java b/lucene/test-framework/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java
index 69a7829e876..a6d047084e6 100644
--- a/lucene/test-framework/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java
+++ b/lucene/test-framework/src/test/org/apache/lucene/analysis/TestMockAnalyzer.java
@@ -222,7 +222,7 @@ public class TestMockAnalyzer extends BaseTokenStreamTestCase {
int iters = TEST_NIGHTLY ? atLeast(30) : atLeast(1);
for (int i = 0; i < iters; i++) {
final CharacterRunAutomaton dfa =
- new CharacterRunAutomaton(AutomatonTestUtil.randomAutomaton(random()), Integer.MAX_VALUE);
+ new CharacterRunAutomaton(AutomatonTestUtil.randomAutomaton(random()));
final boolean lowercase = random().nextBoolean();
final int limit = TestUtil.nextInt(random(), 0, 500);
Analyzer a =