Class BaseMatcher<T>
- java.lang.Object
-
- com.apple.foundationdb.record.query.plan.temp.matching.BaseMatcher<T>
-
- Type Parameters:
T
- the type representing the domain of matching
- Direct Known Subclasses:
ComputingMatcher
,FindingMatcher
public abstract class BaseMatcher<T> extends Object
Abstract base class for all matchers. Matching is an algorithm that matches two collections of things of typeT
resulting in a reversible mapping between the elements of the respective collections. By definition two empty collections of elements match with an empty bijective mapping. The bijective mapping that is produced by matching is always encoded in instances of the classAliasMap
. Matchers operate on a generic typeT
. However, the underlying mechanics always works on instances of classCorrelationIdentifier
. This separates the concept of equality from the actual objects that the client logic matches over. This class keeps maps to track object-to-alias and alias-to-object relationships. To clients (that is logic extending from this class), this class presents the elements that are the domain of matching to be of typeT
as long as the client can provided mappings between objects of typeT
andCorrelationIdentifier
s. Matching can be but may not be commutative. Instead of using left/right terminology it seemed better to talk about this/other as this and other are treated in a slightly asymmetric way. Clients still can implement matchers that impose that a subclass of this class returns the same matches with this and other reversed, however, that behavior is not encoded in this class per se.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BaseMatcher(AliasMap boundAliasesMap, Set<CorrelationIdentifier> aliases, Function<T,CorrelationIdentifier> elementToAliasFn, Map<CorrelationIdentifier,T> aliasToElementMap, com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier> dependsOnMap, Set<CorrelationIdentifier> otherAliases, Function<T,CorrelationIdentifier> otherElementToAliasFn, Map<CorrelationIdentifier,? extends T> otherAliasToElementMap, com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier> otherDependsOnMap)
Dumb constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected static <T> com.google.common.collect.ImmutableSet<CorrelationIdentifier>
computeAliases(Collection<? extends T> elements, Function<T,CorrelationIdentifier> elementToAliasFn)
Static helper to compute a set ofCorrelationIdentifier
s from a collection of elements of typeT
using the given element-to-alias function.protected static <T> com.google.common.collect.ImmutableMap<CorrelationIdentifier,T>
computeAliasToElementMap(Collection<? extends T> elements, Function<T,CorrelationIdentifier> elementToAliasFn)
Static helper to compute the alias-to-element map based on a collection of elements and on the element-to-alias function tht are both passed in.protected static <T> com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier>
computeDependsOnMap(Set<CorrelationIdentifier> aliases, Function<T,CorrelationIdentifier> elementToAliasFn, Map<CorrelationIdentifier,T> aliasToElementMap, Function<T,? extends Collection<T>> dependsOnFn)
Static helper to compute a dependency map fromCorrelationIdentifier
toCorrelationIdentifier
based on a set of aliases and mappings betweenCorrelationIdentifier
and typeT
.protected static <T> com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier>
computeDependsOnMapWithAliases(Set<CorrelationIdentifier> aliases, Map<CorrelationIdentifier,T> aliasToElementMap, Function<T,Set<CorrelationIdentifier>> dependsOnFn)
Static helper to compute a dependency map fromCorrelationIdentifier
toCorrelationIdentifier
based on a set of aliases and mappings betweenCorrelationIdentifier
and typeT
.Set<CorrelationIdentifier>
getAliases()
Map<CorrelationIdentifier,T>
getAliasToElementMap()
AliasMap
getBoundAliasesMap()
com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier>
getDependsOnMap()
Function<T,CorrelationIdentifier>
getElementToAliasFn()
Set<CorrelationIdentifier>
getOtherAliases()
Map<CorrelationIdentifier,? extends T>
getOtherAliasToElementMap()
com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier>
getOtherDependsOnMap()
Function<T,CorrelationIdentifier>
getOtherElementToAliasFn()
protected boolean
isIsomorphic(AliasMap aliasMap, Set<CorrelationIdentifier> set, Set<CorrelationIdentifier> otherSet)
Helper to determine whether thisset
is equals tootherSet
after translation using the givenAliasMap
.protected Optional<AliasMap>
mapDependenciesToOther(AliasMap aliasMap, CorrelationIdentifier alias, CorrelationIdentifier otherAlias)
Helper to map the dependencies of the given alias to the dependencies of the given other alias if such a mapping is sound, i.e.protected <R> Iterable<R>
match(EnumerationFunction<R> enumerationFunction)
Main method used for matching.protected abstract Iterable<List<CorrelationIdentifier>>
otherCombinations(List<CorrelationIdentifier> otherPermutation, int limitInclusive)
-
-
-
Constructor Detail
-
BaseMatcher
protected BaseMatcher(@Nonnull AliasMap boundAliasesMap, @Nonnull Set<CorrelationIdentifier> aliases, @Nonnull Function<T,CorrelationIdentifier> elementToAliasFn, @Nonnull Map<CorrelationIdentifier,T> aliasToElementMap, @Nonnull com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier> dependsOnMap, @Nonnull Set<CorrelationIdentifier> otherAliases, @Nonnull Function<T,CorrelationIdentifier> otherElementToAliasFn, @Nonnull Map<CorrelationIdentifier,? extends T> otherAliasToElementMap, @Nonnull com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier> otherDependsOnMap)
Dumb constructor.- Parameters:
boundAliasesMap
- a map with previously-bound aliases. Any match that is computed and passed back to the client is an amendment of this map, i.e., every resulting match'sAliasMap
always contains at least the bindings in this mapaliases
- a set of correlation identifiers representing the domain on this sideelementToAliasFn
- a function to map elements of typeT
on this side to correlation identifiersaliasToElementMap
- a map from correlation identifiers to elements of typeT
on this sidedependsOnMap
- a map from correlation identifiers to correlation identifiers describing the depends-on relationships between the elements (translated to aliases) on this sideotherAliases
- a set of correlation identifiers representing the domain on the other sideotherElementToAliasFn
- a function to map elements of typeT
on the other side to correlation identifiersotherAliasToElementMap
- a function to map elements of typeT
on the other side to correlation identifiers.otherDependsOnMap
- a map from correlation identifiers to correlation identifiers describing the depends-on relationships between the elements (translated to aliases) on the other side
-
-
Method Detail
-
getAliases
@Nonnull public Set<CorrelationIdentifier> getAliases()
-
getElementToAliasFn
@Nonnull public Function<T,CorrelationIdentifier> getElementToAliasFn()
-
getAliasToElementMap
@Nonnull public Map<CorrelationIdentifier,T> getAliasToElementMap()
-
getDependsOnMap
@Nonnull public com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier> getDependsOnMap()
-
getOtherAliases
@Nonnull public Set<CorrelationIdentifier> getOtherAliases()
-
getOtherElementToAliasFn
@Nonnull public Function<T,CorrelationIdentifier> getOtherElementToAliasFn()
-
getOtherAliasToElementMap
@Nonnull public Map<CorrelationIdentifier,? extends T> getOtherAliasToElementMap()
-
getOtherDependsOnMap
@Nonnull public com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier> getOtherDependsOnMap()
-
match
@Nonnull protected <R> Iterable<R> match(@Nonnull EnumerationFunction<R> enumerationFunction)
Main method used for matching. This method pairs up the two sides relying on topological sorting logic to create anEnumeratingIterable
that enumerates all alias permutations that are possible based on the depends-on constraints given to the matcher. The enumeration function that is also handed in can filter and skip the stream of permutations thereby pruning entire subtrees of possible matches. The enumeration function also defines the result of the matches if there are any, e.g. for a predicated matcher, theAliasMap
s themselves that map the elements from this to other are the match result themselves, however, a generic matcher may want to create objects specific to the matching use case. The typeR
(the resulting type of matching) is a generic type on the method (not on this class) which allows a matcher of a particular typeT
to provide matchers of different types of resultsR
.- Type Parameters:
R
- the result type of the enumeration function- Parameters:
enumerationFunction
- enumeration function. SeeEnumerationFunction
for further explanations.- Returns:
- an
Iterable
of typeR
-
otherCombinations
@Nonnull protected abstract Iterable<List<CorrelationIdentifier>> otherCombinations(List<CorrelationIdentifier> otherPermutation, int limitInclusive)
-
isIsomorphic
protected boolean isIsomorphic(@Nonnull AliasMap aliasMap, @Nonnull Set<CorrelationIdentifier> set, @Nonnull Set<CorrelationIdentifier> otherSet)
Helper to determine whether thisset
is equals tootherSet
after translation using the givenAliasMap
.- Parameters:
aliasMap
- alias map defining a translation from this to otherset
- this setotherSet
- other set- Returns:
- boolean equal to the result of
translate(set1, aliasMap).equals(otherSet)
-
mapDependenciesToOther
@Nonnull protected Optional<AliasMap> mapDependenciesToOther(@Nonnull AliasMap aliasMap, @Nonnull CorrelationIdentifier alias, @Nonnull CorrelationIdentifier otherAlias)
Helper to map the dependencies of the given alias to the dependencies of the given other alias if such a mapping is sound, i.e. the dependsOn functions for the given aliases sets are isomorphic under translation using the givenAliasMap
.- Parameters:
aliasMap
- alias map to define the translationalias
- alias on this sideotherAlias
- alias on the other side- Returns:
Optional.empty()
if the dependsOn functions for the given aliases sets is are isomorphic under translation usingaliasMap
,Optional.of(resultMap)
whereresultMap
is anAliasMap
only containing mappings from the dependsOn set ofalias
that are also contained ialiasMap
, otherwise.
-
computeAliases
@Nonnull protected static <T> com.google.common.collect.ImmutableSet<CorrelationIdentifier> computeAliases(@Nonnull Collection<? extends T> elements, @Nonnull Function<T,CorrelationIdentifier> elementToAliasFn)
Static helper to compute a set ofCorrelationIdentifier
s from a collection of elements of typeT
using the given element-to-alias function. Note that we allow a collection of elements to be passed in, we compute a set ofCorrelationIdentifier
s from it (i.e. duplicate aliases are not allowed).- Type Parameters:
T
- element type- Parameters:
elements
- a collection of elements of typeT
elementToAliasFn
- element to alias function- Returns:
- a set of aliases
-
computeAliasToElementMap
@Nonnull protected static <T> com.google.common.collect.ImmutableMap<CorrelationIdentifier,T> computeAliasToElementMap(@Nonnull Collection<? extends T> elements, @Nonnull Function<T,CorrelationIdentifier> elementToAliasFn)
Static helper to compute the alias-to-element map based on a collection of elements and on the element-to-alias function tht are both passed in.- Type Parameters:
T
- element type- Parameters:
elements
- collection of elements of typeT
elementToAliasFn
- element to alias function- Returns:
- a map from
CorrelationIdentifier
toT
representing the conceptual inverse of the element to alias function passed in
-
computeDependsOnMap
@Nonnull protected static <T> com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier> computeDependsOnMap(@Nonnull Set<CorrelationIdentifier> aliases, @Nonnull Function<T,CorrelationIdentifier> elementToAliasFn, @Nonnull Map<CorrelationIdentifier,T> aliasToElementMap, @Nonnull Function<T,? extends Collection<T>> dependsOnFn)
Static helper to compute a dependency map fromCorrelationIdentifier
toCorrelationIdentifier
based on a set of aliases and mappings betweenCorrelationIdentifier
and typeT
.- Type Parameters:
T
- element type- Parameters:
aliases
- a set of aliaseselementToAliasFn
- element to alias functionaliasToElementMap
- a map fromCorrelationIdentifier
toT
representing the conceptual inverse of the element to alias function passed independsOnFn
- function defining the dependOn relationships between an element and other elements (via aliases)- Returns:
- a multimap from
CorrelationIdentifier
toCorrelationIdentifier
where a containedkey, values
pair signifies thatkey
depends on each value invalues
-
computeDependsOnMapWithAliases
@Nonnull protected static <T> com.google.common.collect.ImmutableSetMultimap<CorrelationIdentifier,CorrelationIdentifier> computeDependsOnMapWithAliases(@Nonnull Set<CorrelationIdentifier> aliases, @Nonnull Map<CorrelationIdentifier,T> aliasToElementMap, @Nonnull Function<T,Set<CorrelationIdentifier>> dependsOnFn)
Static helper to compute a dependency map fromCorrelationIdentifier
toCorrelationIdentifier
based on a set of aliases and mappings betweenCorrelationIdentifier
and typeT
. This method optimizes for the case that the client uses dependsOn functions that already map toCorrelationIdentifier
as opposed to typeT
. See the matching logic inQuantifiers
for examples.- Type Parameters:
T
- element type- Parameters:
aliases
- a set of aliasesaliasToElementMap
- a map fromCorrelationIdentifier
toT
dependsOnFn
- function defining the dependOn relationships between an element and aliases (CorrelationIdentifier
s)- Returns:
- a multimap from
CorrelationIdentifier
toCorrelationIdentifier
where a containedkey, values
pair signifies thatkey
depends on each value invalues
-
-