Skip to content

Commit

Permalink
fix removal for exprs with regex prefix (#971)
Browse files Browse the repository at this point in the history
Before it would always remove from the prefix tree even
if there were other similar expressions with the same
prefix. After that those other expressions would never
match because the sub-index would no longer be in the
tree. Now it will only remove from the prefix tree if
the sub-index is empty.
  • Loading branch information
brharrington authored Apr 25, 2022
1 parent 3fbf20e commit abab84f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,19 @@ private boolean remove(List<Query.KeyQuery> queries, int i, T value) {
hasKeyIdx = null;
}
} else {
if (kq instanceof Query.Regex) {
Query.Regex re = (Query.Regex) kq;
otherChecksTree.remove(re.pattern().prefix(), kq);
} else {
otherChecksTree.remove(null, kq);
}
QueryIndex<T> idx = otherChecks.get(kq);
if (idx != null && idx.remove(queries, j, value)) {
result = true;
otherChecksCache.clear();
if (idx.isEmpty())
if (idx.isEmpty()) {
otherChecks.remove(kq);
if (kq instanceof Query.Regex) {
Query.Regex re = (Query.Regex) kq;
otherChecksTree.remove(re.pattern().prefix(), kq);
} else {
otherChecksTree.remove(null, kq);
}
}
}

// Not queries should match if the key is missing from the id, so they need to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -387,6 +388,26 @@ public void removalOfNotQueryUsingQuery() {
Assertions.assertTrue(idx.isEmpty());
}

@Test
public void removalPrefixRegexSubtree() {
Query q1 = Parser.parseQuery("name,test,:eq,a,foo,:re,:and,b,bar,:eq,:and");
Query q2 = Parser.parseQuery("name,test,:eq,a,foo,:re,:and");
QueryIndex<Query> idx = QueryIndex.<Query>newInstance(registry)
.add(q1, q1)
.add(q2, q2);

Id id = id("test", "a", "foo", "b", "bar");

Set<Query> expected = new HashSet<>();
expected.add(q1);
expected.add(q2);
Assertions.assertEquals(expected, new HashSet<>(idx.findMatches(id)));

idx.remove(q1, q1);
expected.remove(q1);
Assertions.assertEquals(expected, new HashSet<>(idx.findMatches(id)));
}

@Test
public void toStringMethod() {
QueryIndex<Query> idx = QueryIndex.newInstance(registry);
Expand Down

0 comments on commit abab84f

Please sign in to comment.