use of com.apple.foundationdb.record.query.expressions.Comparisons.Comparison in project fdb-record-layer by FoundationDB.
the class QueryToKeyMatcher method matches.
@Nonnull
private Match matches(@Nonnull AndComponent query, @Nonnull KeyExpression key, @Nonnull MatchingMode matchingMode, @Nullable FilterSatisfiedMask filterMask) {
final List<QueryComponent> listOfQueries = query.getChildren();
final Iterator<KeyExpression> keyChildIterator;
final int keyChildSize = key.getColumnSize();
if (key instanceof ThenKeyExpression) {
List<KeyExpression> children = ((ThenKeyExpression) key).getChildren();
keyChildIterator = children.iterator();
} else {
keyChildIterator = Iterators.singletonIterator(key);
}
// Keep a local mask so that if we can only partially fulfill queries we don't pollute the main
// one with our state
FilterSatisfiedMask localMask = FilterSatisfiedMask.of(query);
localMask.setExpression(key);
List<Comparison> comparisons = new ArrayList<>(keyChildSize);
while (keyChildIterator.hasNext()) {
KeyExpression exp = keyChildIterator.next();
// Look for a query segment that matches this expression
boolean found = false;
boolean foundInequality = false;
final Iterator<FilterSatisfiedMask> childMaskIterator = localMask.getChildren().iterator();
for (QueryComponent querySegment : listOfQueries) {
Match match = matches(querySegment, exp, matchingMode, childMaskIterator.next());
if (match.getType() != MatchType.NO_MATCH) {
found = true;
comparisons.addAll(match.getComparisons());
if (match.getType() == MatchType.INEQUALITY) {
foundInequality = true;
}
break;
}
}
if (!found) {
return Match.none();
}
// Only the last comparison in the list can be inequality.
if (localMask.allSatisfied() || foundInequality) {
break;
}
}
if (matchingMode.equals(MatchingMode.SATISFY_QUERY) && !localMask.allSatisfied() || matchingMode.equals(MatchingMode.COVER_KEY) && comparisons.size() < keyChildSize) {
// filters that have not yet been satisfied.
return Match.none();
}
if (filterMask != null) {
filterMask.mergeWith(localMask);
}
return new Match(comparisons);
}
use of com.apple.foundationdb.record.query.expressions.Comparisons.Comparison in project fdb-record-layer by FoundationDB.
the class Ordering method intersectEqualityBoundKeys.
/**
* Intersect the equality-bound keys of two orderings. This method is usually passed in as a method reference to
* {@link #combineEqualityBoundKeys(List, BinaryOperator)} as the binary operator.
* @param left multimap of equality-bound keys of the left ordering (and their bindings)
* @param right multimap of equality-bound keys of the right ordering (and their bindings)
* @return new combined multimap of equality-bound keys (and their bindings)
*/
@Nonnull
public static SetMultimap<KeyExpression, Comparison> intersectEqualityBoundKeys(@Nonnull SetMultimap<KeyExpression, Comparison> left, @Nonnull SetMultimap<KeyExpression, Comparison> right) {
final ImmutableSetMultimap.Builder<KeyExpression, Comparison> resultBuilder = ImmutableSetMultimap.builder();
for (final Map.Entry<KeyExpression, Collection<Comparison>> rightEntry : right.asMap().entrySet()) {
final KeyExpression rightKey = rightEntry.getKey();
if (left.containsKey(rightKey)) {
//
// Left side contains the same key. We can only retain this key in the result, however, if at least
// one actual comparison on right is in left as well.
//
final Collection<Comparison> rightComparisons = rightEntry.getValue();
final boolean anyMatchingComparison = rightComparisons.stream().anyMatch(rightComparison -> left.containsEntry(rightKey, rightComparison));
if (anyMatchingComparison) {
resultBuilder.putAll(rightKey, rightComparisons);
}
}
}
return resultBuilder.build();
}
Aggregations