|
| 1 | +/* |
| 2 | + * TranslationMap.java |
| 3 | + * |
| 4 | + * This source file is part of the FoundationDB open source project |
| 5 | + * |
| 6 | + * Copyright 2015-2024 Apple Inc. and the FoundationDB project authors |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.apple.foundationdb.record.query.plan.cascades.values.translation; |
| 22 | + |
| 23 | +import com.apple.foundationdb.record.query.plan.cascades.AliasMap; |
| 24 | +import com.apple.foundationdb.record.query.plan.cascades.CorrelationIdentifier; |
| 25 | +import com.apple.foundationdb.record.query.plan.cascades.typing.Type; |
| 26 | +import com.apple.foundationdb.record.query.plan.cascades.values.LeafValue; |
| 27 | +import com.apple.foundationdb.record.query.plan.cascades.values.QuantifiedObjectValue; |
| 28 | +import com.apple.foundationdb.record.query.plan.cascades.values.QuantifiedValue; |
| 29 | +import com.apple.foundationdb.record.query.plan.cascades.values.Value; |
| 30 | +import com.google.common.base.Preconditions; |
| 31 | +import com.google.common.base.Verify; |
| 32 | +import com.google.common.collect.ImmutableMap; |
| 33 | +import com.google.common.collect.ImmutableSet; |
| 34 | +import com.google.common.collect.Maps; |
| 35 | + |
| 36 | +import javax.annotation.Nonnull; |
| 37 | +import javax.annotation.Nullable; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.Optional; |
| 40 | +import java.util.Set; |
| 41 | + |
| 42 | +/** |
| 43 | + * Map used to specify translations. |
| 44 | + */ |
| 45 | +public class TranslationMap { |
| 46 | + @Nonnull |
| 47 | + private static final TranslationMap EMPTY = new TranslationMap(ImmutableMap.of()); |
| 48 | + |
| 49 | + @Nonnull |
| 50 | + private final Map<CorrelationIdentifier, TranslationTarget> aliasToTargetMap; |
| 51 | + |
| 52 | + private TranslationMap(@Nonnull final Map<CorrelationIdentifier, TranslationTarget> aliasToTargetMap) { |
| 53 | + this.aliasToTargetMap = ImmutableMap.copyOf(aliasToTargetMap); |
| 54 | + } |
| 55 | + |
| 56 | + @Nonnull |
| 57 | + public Optional<AliasMap> getAliasMapMaybe() { |
| 58 | + return Optional.empty(); |
| 59 | + } |
| 60 | + |
| 61 | + public boolean definesOnlyIdentities() { |
| 62 | + return getAliasMapMaybe().map(AliasMap::definesOnlyIdentities) |
| 63 | + .orElseGet(aliasToTargetMap::isEmpty); |
| 64 | + } |
| 65 | + |
| 66 | + public boolean containsSourceAlias(@Nullable CorrelationIdentifier sourceAlias) { |
| 67 | + return aliasToTargetMap.containsKey(sourceAlias); |
| 68 | + } |
| 69 | + |
| 70 | + @Nonnull |
| 71 | + public Value applyTranslationFunction(@Nonnull final CorrelationIdentifier sourceAlias, |
| 72 | + @Nonnull final LeafValue leafValue) { |
| 73 | + final var translationTarget = Preconditions.checkNotNull(aliasToTargetMap.get(sourceAlias)); |
| 74 | + return translationTarget.translate(sourceAlias, leafValue); |
| 75 | + } |
| 76 | + |
| 77 | + @Nonnull |
| 78 | + public Builder toBuilder() { |
| 79 | + return new Builder(aliasToTargetMap); |
| 80 | + } |
| 81 | + |
| 82 | + @Nonnull |
| 83 | + public static Builder builder() { |
| 84 | + return new Builder(); |
| 85 | + } |
| 86 | + |
| 87 | + @Nonnull |
| 88 | + public static TranslationMap empty() { |
| 89 | + return EMPTY; |
| 90 | + } |
| 91 | + |
| 92 | + @Nonnull |
| 93 | + public static TranslationMap rebaseWithAliasMap(@Nonnull final AliasMap aliasMap) { |
| 94 | + final var translationMapBuilder = ImmutableMap.<CorrelationIdentifier, TranslationTarget>builder(); |
| 95 | + for (final var entry : aliasMap.entrySet()) { |
| 96 | + translationMapBuilder.put(entry.getKey(), |
| 97 | + new TranslationTarget(((sourceAlias, leafValue) -> leafValue.rebaseLeaf(entry.getValue())))); |
| 98 | + } |
| 99 | + return new AliasMapBasedTranslationMap(translationMapBuilder.build(), aliasMap); |
| 100 | + } |
| 101 | + |
| 102 | + @Nonnull |
| 103 | + public static TranslationMap ofAliases(@Nonnull final CorrelationIdentifier source, |
| 104 | + @Nonnull final CorrelationIdentifier target) { |
| 105 | + return rebaseWithAliasMap(AliasMap.ofAliases(source, target)); |
| 106 | + } |
| 107 | + |
| 108 | + @Nonnull |
| 109 | + public static TranslationMap compose(@Nonnull final Iterable<TranslationMap> translationMaps) { |
| 110 | + final var builder = TranslationMap.builder(); |
| 111 | + for (final var translationMap : translationMaps) { |
| 112 | + builder.compose(translationMap); |
| 113 | + } |
| 114 | + return builder.build(); |
| 115 | + } |
| 116 | + |
| 117 | + private static class AliasMapBasedTranslationMap extends TranslationMap { |
| 118 | + @Nonnull |
| 119 | + private final AliasMap aliasMap; |
| 120 | + |
| 121 | + public AliasMapBasedTranslationMap(@Nonnull final Map<CorrelationIdentifier, TranslationTarget> aliasToTargetMap, |
| 122 | + @Nonnull final AliasMap aliasMap) { |
| 123 | + super(aliasToTargetMap); |
| 124 | + this.aliasMap = aliasMap; |
| 125 | + } |
| 126 | + |
| 127 | + @Nonnull |
| 128 | + @Override |
| 129 | + public Optional<AliasMap> getAliasMapMaybe() { |
| 130 | + return Optional.of(aliasMap); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private static class TranslationTarget { |
| 135 | + @Nonnull |
| 136 | + private final TranslationFunction translationFunction; |
| 137 | + |
| 138 | + public TranslationTarget(@Nonnull final TranslationFunction translationFunction) { |
| 139 | + this.translationFunction = translationFunction; |
| 140 | + } |
| 141 | + |
| 142 | + @Nonnull |
| 143 | + public Value translate(@Nonnull final CorrelationIdentifier sourceAlias, |
| 144 | + @Nonnull final LeafValue leafValue) { |
| 145 | + return translationFunction.apply(sourceAlias, leafValue); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Functional interface to specify the translation to take place when a {@link QuantifiedValue} is encountered. |
| 151 | + */ |
| 152 | + @FunctionalInterface |
| 153 | + public interface TranslationFunction { |
| 154 | + @Nonnull |
| 155 | + Value apply(@Nonnull CorrelationIdentifier sourceAlias, |
| 156 | + @Nonnull LeafValue leafValue); |
| 157 | + |
| 158 | + @Nonnull |
| 159 | + static TranslationFunction adjustValueType(@Nonnull final Value translationTargetValue) { |
| 160 | + final var translationTargetType = translationTargetValue.getResultType(); |
| 161 | + if (translationTargetValue instanceof QuantifiedObjectValue) { |
| 162 | + if (translationTargetType instanceof Type.Erasable && |
| 163 | + ((Type.Erasable)translationTargetType).isErased()) { |
| 164 | + return (source, quantifiedValue) -> QuantifiedObjectValue.of(((QuantifiedObjectValue)translationTargetValue).getAlias(), |
| 165 | + quantifiedValue.getResultType()); |
| 166 | + } |
| 167 | + } |
| 168 | + Verify.verify(!(translationTargetType instanceof Type.Erasable) || |
| 169 | + !((Type.Erasable)translationTargetType).isErased()); |
| 170 | + return (ignored, ignored2) -> translationTargetValue; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Builder class for a translation map. |
| 176 | + */ |
| 177 | + public static class Builder { |
| 178 | + @Nonnull |
| 179 | + private final Map<CorrelationIdentifier, TranslationTarget> aliasToTargetMap; |
| 180 | + |
| 181 | + public Builder() { |
| 182 | + this.aliasToTargetMap = Maps.newLinkedHashMap(); |
| 183 | + } |
| 184 | + |
| 185 | + private Builder(@Nonnull final Map<CorrelationIdentifier, TranslationTarget> aliasToTargetMap) { |
| 186 | + this.aliasToTargetMap = Maps.newLinkedHashMap(aliasToTargetMap); |
| 187 | + } |
| 188 | + |
| 189 | + @Nonnull |
| 190 | + public TranslationMap build() { |
| 191 | + if (aliasToTargetMap.isEmpty()) { |
| 192 | + return TranslationMap.empty(); |
| 193 | + } |
| 194 | + return new TranslationMap(aliasToTargetMap); |
| 195 | + } |
| 196 | + |
| 197 | + @Nonnull |
| 198 | + public Builder.When when(@Nonnull final CorrelationIdentifier sourceAlias) { |
| 199 | + return new When(sourceAlias); |
| 200 | + } |
| 201 | + |
| 202 | + @Nonnull |
| 203 | + public Builder.WhenAny whenAny(@Nonnull final Iterable<CorrelationIdentifier> sourceAliases) { |
| 204 | + return new WhenAny(sourceAliases); |
| 205 | + } |
| 206 | + |
| 207 | + @Nonnull |
| 208 | + public Builder compose(@Nonnull final TranslationMap other) { |
| 209 | + other.aliasToTargetMap |
| 210 | + .forEach((key, value) -> { |
| 211 | + Verify.verify(!aliasToTargetMap.containsKey(key)); |
| 212 | + aliasToTargetMap.put(key, value); |
| 213 | + }); |
| 214 | + return this; |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Class to provide fluent API, e.g. {@code .when(...).then(...)} |
| 219 | + */ |
| 220 | + public class When { |
| 221 | + @Nonnull |
| 222 | + private final CorrelationIdentifier sourceAlias; |
| 223 | + |
| 224 | + public When(@Nonnull final CorrelationIdentifier sourceAlias) { |
| 225 | + this.sourceAlias = sourceAlias; |
| 226 | + } |
| 227 | + |
| 228 | + @Nonnull |
| 229 | + public Builder then(@Nonnull final TranslationFunction translationFunction) { |
| 230 | + aliasToTargetMap.put(sourceAlias, new TranslationTarget(translationFunction)); |
| 231 | + return Builder.this; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Class to provide fluent API, e.g. {@code .when(...).then(...)} |
| 237 | + */ |
| 238 | + public class WhenAny { |
| 239 | + @Nonnull |
| 240 | + private final Set<CorrelationIdentifier> sourceAliases; |
| 241 | + |
| 242 | + public WhenAny(@Nonnull final Iterable<CorrelationIdentifier> sourceAliases) { |
| 243 | + this.sourceAliases = ImmutableSet.copyOf(sourceAliases); |
| 244 | + } |
| 245 | + |
| 246 | + @Nonnull |
| 247 | + public Builder then(@Nonnull TranslationFunction translationFunction) { |
| 248 | + for (final CorrelationIdentifier sourceAlias : sourceAliases) { |
| 249 | + aliasToTargetMap.put(sourceAlias, new TranslationTarget(translationFunction)); |
| 250 | + } |
| 251 | + return Builder.this; |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments