Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix check for matching with missing key #1009

Merged
merged 1 commit into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,6 +20,7 @@
import com.netflix.spectator.impl.Cache;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -175,8 +176,9 @@ private void add(List<Query.KeyQuery> queries, int i, T value) {
otherChecksCache.clear();

// Not queries should match if the key is missing from the id, so they need to
// be included in the other keys sub-tree as well
if (kq instanceof Query.InvertedKeyQuery) {
// be included in the other keys sub-tree as well. Check this by seeing if it will
// match an empty map as there could be a variety of inverted types.
if (kq.matches(Collections.emptyMap())) {
if (missingKeysIdx == null) {
missingKeysIdx = QueryIndex.empty(registry);
}
Expand Down Expand Up @@ -267,8 +269,9 @@ private boolean remove(List<Query.KeyQuery> queries, int i, T value) {
}

// Not queries should match if the key is missing from the id, so they need to
// be included in the other keys sub-tree as well
if (kq instanceof Query.InvertedKeyQuery && missingKeysIdx != null) {
// be included in the other keys sub-tree as well. Check this by seeing if it will
// match an empty map as there could be a variety of inverted types.
if (kq.matches(Collections.emptyMap()) && missingKeysIdx != null) {
result |= missingKeysIdx.remove(queries, j, value);
if (missingKeysIdx.isEmpty())
missingKeysIdx = null;
Expand Down
Expand Up @@ -372,6 +372,16 @@ public void multiNotInClause() {
Assertions.assertTrue(idx.findMatches(id("cpu", "id", "iowait")).isEmpty());
}

@Test
public void doubleNotsSameKey() {
Query q = Parser.parseQuery("a,1,:eq,b,2,:eq,:and,c,3,:eq,:not,:and,c,4,:eq,:not,:and");
QueryIndex<Query> idx = QueryIndex.<Query>newInstance(registry).add(q, q);
Assertions.assertFalse(idx.findMatches(id("cpu", "a", "1", "b", "2", "c", "5")).isEmpty());
Assertions.assertTrue(idx.findMatches(id("cpu", "a", "1", "b", "2", "c", "3")).isEmpty());
Assertions.assertTrue(idx.findMatches(id("cpu", "a", "1", "b", "2", "c", "4")).isEmpty());
Assertions.assertFalse(idx.findMatches(id("cpu", "a", "1", "b", "2")).isEmpty());
}

@Test
public void removalOfNotQuery() {
Query q = Parser.parseQuery("name,cpu,:eq,id,user,:eq,:not,:and");
Expand Down