use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.
the class TopologicalSortTest method testTopologicalSortImpossibleDependencies.
@Test
public void testTopologicalSortImpossibleDependencies() {
final CorrelationIdentifier a = CorrelationIdentifier.of("a");
final CorrelationIdentifier b = CorrelationIdentifier.of("b");
final CorrelationIdentifier c = CorrelationIdentifier.of("c");
final Map<CorrelationIdentifier, Set<CorrelationIdentifier>> dependencies = ImmutableMap.of(b, ImmutableSet.of(a), c, ImmutableSet.of(b), a, ImmutableSet.of(c));
final EnumeratingIterable<CorrelationIdentifier> topologicalPermutations = TopologicalSort.topologicalOrderPermutations(ImmutableSet.of(a, b, c), id -> dependencies.getOrDefault(id, ImmutableSet.of()));
final EnumeratingIterator<CorrelationIdentifier> iterator = topologicalPermutations.iterator();
assertFalse(iterator.hasNext());
}
use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.
the class FindingMatcher 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 predicate is called for each pair of
* elements (for a match attempt). If the match predicate returns {@code true} the pair is recorded
* as a matching pair. We attempt to find a matching pair (one element from this side; one from the
* other side) for each element identified by {@link #getAliases()}. For each individual new such pair that is found,
* we continue in the matching attempt. Once a set of bindings is established for all aliases
* in {@link #getAliases} this method then includes that {@link AliasMap} in the computable of {@link AliasMap}s that
* form the result and proceeds to consume further permutations from the iterator that is passed in in order to
* establish other matches between this and other.
* @param iterator an enumerating iterable for the permutations on this side
* @param otherOrdered one permutation (that is not violating dependencies, constraints, etc.) of the other side
* @return an {@link Iterator} of match results (of type {@link AliasMap})
*/
@SuppressWarnings("java:S135")
@Nonnull
public Iterator<AliasMap> enumerate(@Nonnull final EnumeratingIterator<CorrelationIdentifier> iterator, @Nonnull final List<CorrelationIdentifier> otherOrdered) {
final Set<CorrelationIdentifier> aliases = getAliases();
final AliasMap boundAliasesMap = getBoundAliasesMap();
if (otherOrdered.isEmpty()) {
return ImmutableList.of(boundAliasesMap).iterator();
}
int size = otherOrdered.size();
return new AbstractIterator<AliasMap>() {
@Override
protected AliasMap computeNext() {
while (iterator.hasNext()) {
final List<CorrelationIdentifier> ordered = iterator.next();
final AliasMap.Builder aliasMapBuilder = AliasMap.builder(aliases.size());
int i;
for (i = 0; i < size; i++) {
final AliasMap aliasMap = aliasMapBuilder.build();
final CorrelationIdentifier alias = ordered.get(i);
final CorrelationIdentifier otherAlias = otherOrdered.get(i);
final Optional<AliasMap> dependsOnMapOptional = mapDependenciesToOther(aliasMap, alias, otherAlias);
if (!dependsOnMapOptional.isPresent()) {
break;
}
final AliasMap dependsOnMap = dependsOnMapOptional.get();
final T entity = Objects.requireNonNull(getAliasToElementMap().get(alias));
final T otherEntity = Objects.requireNonNull(getOtherAliasToElementMap().get(otherAlias));
if (!matchPredicate.test(entity, otherEntity, boundAliasesMap.combine(dependsOnMap))) {
break;
}
// 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);
}
if (i == size) {
iterator.skip(i - 1);
return boundAliasesMap.derived(ordered.size()).zip(ordered, otherOrdered, i).build();
} else {
// we can skip all permutations where the i-th value is bound the way it currently is
iterator.skip(i);
}
}
return endOfData();
}
};
}
use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.
the class PartialOrderTest method testEligibleSetsSomeDependencies2.
@Test
void testEligibleSetsSomeDependencies2() {
final CorrelationIdentifier a = CorrelationIdentifier.of("a");
final CorrelationIdentifier b = CorrelationIdentifier.of("b");
final CorrelationIdentifier c = CorrelationIdentifier.of("c");
final CorrelationIdentifier d = CorrelationIdentifier.of("d");
final Map<CorrelationIdentifier, Set<CorrelationIdentifier>> dependencies = ImmutableMap.of(b, ImmutableSet.of(a), c, ImmutableSet.of(a), d, ImmutableSet.of(b, c));
final var partialOrder = PartialOrder.of(ImmutableSet.of(a, b, c, d), id -> dependencies.getOrDefault(id, ImmutableSet.of()));
var eligibleSet = partialOrder.eligibleSet();
assertEquals(ImmutableSet.of(a), eligibleSet.eligibleElements());
eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
assertEquals(ImmutableSet.of(b, c), eligibleSet.eligibleElements());
eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
assertEquals(ImmutableSet.of(d), eligibleSet.eligibleElements());
eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
assertTrue(eligibleSet.eligibleElements().isEmpty());
}
use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.
the class PartialOrderTest method testEligibleSetsFullDependencies.
@Test
void testEligibleSetsFullDependencies() {
final CorrelationIdentifier a = CorrelationIdentifier.of("a");
final CorrelationIdentifier b = CorrelationIdentifier.of("b");
final CorrelationIdentifier c = CorrelationIdentifier.of("c");
final Map<CorrelationIdentifier, Set<CorrelationIdentifier>> dependencies = ImmutableMap.of(b, ImmutableSet.of(a), c, ImmutableSet.of(b));
final var partialOrder = PartialOrder.of(ImmutableSet.of(a, b, c), id -> dependencies.getOrDefault(id, ImmutableSet.of()));
var eligibleSet = partialOrder.eligibleSet();
assertEquals(ImmutableSet.of(a), eligibleSet.eligibleElements());
eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
assertEquals(ImmutableSet.of(b), eligibleSet.eligibleElements());
eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
assertEquals(ImmutableSet.of(c), eligibleSet.eligibleElements());
eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
assertTrue(eligibleSet.eligibleElements().isEmpty());
}
use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.
the class PartialOrderTest method testEligibleSetsSomeDependencies.
@Test
void testEligibleSetsSomeDependencies() {
final CorrelationIdentifier a = CorrelationIdentifier.of("a");
final CorrelationIdentifier b = CorrelationIdentifier.of("b");
final CorrelationIdentifier c = CorrelationIdentifier.of("c");
final Map<CorrelationIdentifier, Set<CorrelationIdentifier>> dependencies = ImmutableMap.of(c, ImmutableSet.of(a));
final var partialOrder = PartialOrder.of(ImmutableSet.of(a, b, c), id -> dependencies.getOrDefault(id, ImmutableSet.of()));
var eligibleSet = partialOrder.eligibleSet();
assertEquals(ImmutableSet.of(a, b), eligibleSet.eligibleElements());
eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
assertEquals(ImmutableSet.of(c), eligibleSet.eligibleElements());
eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
assertTrue(eligibleSet.eligibleElements().isEmpty());
}
Aggregations