Skip to content

Commit

Permalink
Adjust implementation to still return articulation points
Browse files Browse the repository at this point in the history
  • Loading branch information
cfournie committed Apr 27, 2023
1 parent 9883ce0 commit d66a7fd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.lucene.util.graph;

import static org.apache.lucene.util.automaton.Operations.DEFAULT_DETERMINIZE_WORK_LIMIT;
import static org.apache.lucene.util.automaton.Operations.MAX_RECURSION_LEVEL;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -46,7 +47,6 @@
*/
public final class GraphTokenStreamFiniteStrings {

private static final int MAX_RECURSION_DEPTH = 1024;
private AttributeSource[] tokens = new AttributeSource[4];
private final Automaton det;
private final Transition transition = new Transition();
Expand Down Expand Up @@ -270,9 +270,13 @@ private static void articulationPointsRecurse(
int numT = a.initTransition(state, t);
for (int i = 0; i < numT; i++) {
a.getNextTransition(t);
if (visited.get(t.dest) == false && d < MAX_RECURSION_DEPTH) {
if (visited.get(t.dest) == false) {
parent[t.dest] = state;
articulationPointsRecurse(a, t.dest, d + 1, depth, low, parent, visited, points);
if (d < MAX_RECURSION_LEVEL) {
articulationPointsRecurse(a, t.dest, d + 1, depth, low, parent, visited, points);
} else {
continue;
}
childCount++;
if (low[t.dest] >= depth[state]) {
isArticulation = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.lucene.tests.analysis.Token;
import org.apache.lucene.tests.util.LuceneTestCase;

import static org.apache.lucene.util.automaton.Operations.MAX_RECURSION_LEVEL;

/** {@link GraphTokenStreamFiniteStrings} tests. */
public class TestGraphTokenStreamFiniteStrings extends LuceneTestCase {

Expand Down Expand Up @@ -663,12 +665,11 @@ public void testMultipleSidePathsWithGaps() throws Exception {
}

public void testLongTokenStreamStackOverflowError() throws Exception {

ArrayList<Token> tokens =
new ArrayList<Token>() {
{
add(token("turbo", 1, 1));
add(token("fast", 0, 2));
add(token("charged", 1, 1));
add(token("fast", 1, 1));
add(token("wi", 1, 1));
add(token("wifi", 0, 2));
add(token("fi", 1, 1));
Expand All @@ -682,6 +683,33 @@ public void testLongTokenStreamStackOverflowError() throws Exception {

TokenStream ts = new CannedTokenStream(tokens.toArray(new Token[0]));
GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(ts);
graph.articulationPoints(); // This will cause a java.lang.StackOverflowError

Iterator<TokenStream> it = graph.getFiniteStrings();
assertTrue(it.hasNext());
it.next();
assertTrue(it.hasNext());
it.next();
assertFalse(it.hasNext());

int[] points = graph.articulationPoints(); // This will cause a java.lang.StackOverflowError
assertEquals(points[0], 1);
assertEquals(points[1], 3);
assertEquals(points.length, MAX_RECURSION_LEVEL - 2);

assertFalse(graph.hasSidePath(0));
it = graph.getFiniteStrings(0, 1);
assertTrue(it.hasNext());
assertTokenStream(it.next(), new String[] {"fast"}, new int[] {1});
assertFalse(it.hasNext());
Term[] terms = graph.getTerms("field", 0);
assertArrayEquals(terms, new Term[] {new Term("field", "fast")});

assertTrue(graph.hasSidePath(1));
it = graph.getFiniteStrings(1, 3);
assertTrue(it.hasNext());
assertTokenStream(it.next(), new String[] {"wi", "fi"}, new int[] {1, 1});
assertTrue(it.hasNext());
assertTokenStream(it.next(), new String[] {"wifi"}, new int[] {1});
assertFalse(it.hasNext());
}
}

0 comments on commit d66a7fd

Please sign in to comment.