Search in sources :

Example 31 with CorrelationIdentifier

use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.

the class ComputingMatcher method enumerate.

/**
 * Method to enumerate the permutations on this side against the permutation of the other side in order
 * to form matches (bijective mappings between the permutations). The match function is called for each pair of
 * elements (for a match attempt). If the match function returns a non-empty {@link Iterable} the pair is recorded
 * as a matching pair. We attempt to find a matching pair (one from this side; one from the other side) for each
 * element identified by {@link #getAliases()}. For each individual new such pair that is found,
 * the method {@link MatchAccumulator#accumulate} is called. Once a set of bindings is established for all aliases
 * in {@link #getAliases} this method then calls {@link MatchAccumulator#finish} to produce an iterable of type
 * {@code R}.
 * @param iterator an enumerating iterable for the permutations on this side
 * @param otherPermutation one permutation (that is not violating dependencies, constraints, etc.) of the other side
 * @return an {@link Iterator} of match results (of type {@code BoundMatch<R>})
 */
@SuppressWarnings("java:S135")
@Nonnull
public Iterator<BoundMatch<R>> enumerate(@Nonnull final EnumeratingIterator<CorrelationIdentifier> iterator, @Nonnull final List<CorrelationIdentifier> otherPermutation) {
    final Set<CorrelationIdentifier> aliases = getAliases();
    final AliasMap boundAliasesMap = getBoundAliasesMap();
    if (otherPermutation.isEmpty()) {
        return ImmutableList.of(BoundMatch.<R>withAliasMap(boundAliasesMap)).iterator();
    }
    final int size = otherPermutation.size();
    return new AbstractIterator<BoundMatch<R>>() {

        @Override
        protected BoundMatch<R> computeNext() {
            while (iterator.hasNext()) {
                final List<CorrelationIdentifier> permutation = iterator.next();
                final AliasMap.Builder aliasMapBuilder = AliasMap.builder(aliases.size());
                final MatchAccumulator<M, R> accumulatedMatchResult = matchAccumulatorSupplier.get();
                int i;
                for (i = 0; i < size; i++) {
                    final AliasMap aliasMap = aliasMapBuilder.build();
                    final CorrelationIdentifier alias = permutation.get(i);
                    final CorrelationIdentifier otherAlias = otherPermutation.get(i);
                    final Optional<AliasMap> locallyBoundMapOptional = mapDependenciesToOther(aliasMap, alias, otherAlias);
                    if (!locallyBoundMapOptional.isPresent()) {
                        break;
                    }
                    final AliasMap locallyBoundMap = locallyBoundMapOptional.get();
                    final T entity = Objects.requireNonNull(getAliasToElementMap().get(alias));
                    final T otherEntity = Objects.requireNonNull(getOtherAliasToElementMap().get(otherAlias));
                    final Iterable<M> matchResults = matchFunction.apply(entity, otherEntity, boundAliasesMap.combine(locallyBoundMap));
                    if (Iterables.isEmpty(matchResults)) {
                        break;
                    }
                    accumulatedMatchResult.accumulate(matchResults);
                    // We now amend the equivalences passed in by adding the already known bound aliases left
                    // of i and make them equivalent as well
                    aliasMapBuilder.put(alias, otherAlias);
                }
                final R result = accumulatedMatchResult.finish();
                if (i == size) {
                    iterator.skip(i - 1);
                    return BoundMatch.withAliasMapAndMatchResult(boundAliasesMap.combine(aliasMapBuilder.build()), result);
                } else {
                    iterator.skip(i);
                }
            }
            return endOfData();
        }
    };
}
Also used : AliasMap(com.apple.foundationdb.record.query.plan.temp.AliasMap) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) AbstractIterator(com.google.common.collect.AbstractIterator) Nonnull(javax.annotation.Nonnull)

Aggregations

CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)31 Set (java.util.Set)19 ImmutableSet (com.google.common.collect.ImmutableSet)18 Test (org.junit.jupiter.api.Test)17 List (java.util.List)15 ImmutableList (com.google.common.collect.ImmutableList)14 Nonnull (javax.annotation.Nonnull)11 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)10 API (com.apple.foundationdb.annotation.API)8 AliasMap (com.apple.foundationdb.record.query.plan.temp.AliasMap)8 Value (com.apple.foundationdb.record.query.predicates.Value)7 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)5 MatchInfo (com.apple.foundationdb.record.query.plan.temp.MatchInfo)5 PartialMatch (com.apple.foundationdb.record.query.plan.temp.PartialMatch)5 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)5 QueryPredicate (com.apple.foundationdb.record.query.predicates.QueryPredicate)5 Verify (com.google.common.base.Verify)5 Optional (java.util.Optional)5 RecordQueryFetchFromPartialRecordPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan)4 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)4