|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Alibaba Group Holding Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.alibaba.fluss.metadata; |
| 18 | + |
| 19 | +import com.alibaba.fluss.annotation.PublicEvolving; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import static com.alibaba.fluss.utils.Preconditions.checkArgument; |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents the resolved {@link PartitionSpec}, which the partition spec is re-arranged to the |
| 31 | + * correct order by comparing with a list of strictly ordered partition keys. |
| 32 | + * |
| 33 | + * @since 0.6 |
| 34 | + */ |
| 35 | +@PublicEvolving |
| 36 | +public class ResolvedPartitionSpec { |
| 37 | + public static final String PARTITION_SPEC_SEPARATOR = "$"; |
| 38 | + |
| 39 | + private final List<String> partitionKeys; |
| 40 | + private final List<String> partitionValues; |
| 41 | + private final String partitionName; |
| 42 | + |
| 43 | + private ResolvedPartitionSpec(List<String> partitionKeys, List<String> partitionValues) { |
| 44 | + checkArgument( |
| 45 | + partitionKeys.size() == partitionValues.size(), |
| 46 | + "The number of partition keys and partition values should be the same."); |
| 47 | + this.partitionKeys = partitionKeys; |
| 48 | + this.partitionValues = partitionValues; |
| 49 | + this.partitionName = generatePartitionName(); |
| 50 | + } |
| 51 | + |
| 52 | + public static ResolvedPartitionSpec fromPartitionSpec( |
| 53 | + List<String> partitionKeys, PartitionSpec partitionSpec) { |
| 54 | + return new ResolvedPartitionSpec( |
| 55 | + partitionKeys, getReorderedPartitionValues(partitionKeys, partitionSpec)); |
| 56 | + } |
| 57 | + |
| 58 | + public static ResolvedPartitionSpec fromPartitionValue( |
| 59 | + String partitionKey, String partitionValue) { |
| 60 | + return new ResolvedPartitionSpec( |
| 61 | + Collections.singletonList(partitionKey), Collections.singletonList(partitionValue)); |
| 62 | + } |
| 63 | + |
| 64 | + public static ResolvedPartitionSpec fromPartitionName( |
| 65 | + List<String> partitionKeys, String partitionName) { |
| 66 | + return new ResolvedPartitionSpec(partitionKeys, Arrays.asList(partitionName.split("\\$"))); |
| 67 | + } |
| 68 | + |
| 69 | + public List<String> getPartitionKeys() { |
| 70 | + return partitionKeys; |
| 71 | + } |
| 72 | + |
| 73 | + public List<String> getPartitionValues() { |
| 74 | + return partitionValues; |
| 75 | + } |
| 76 | + |
| 77 | + public String getPartitionName() { |
| 78 | + return partitionName; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Generate the partition name for a partition table of specify partition values. |
| 83 | + * |
| 84 | + * <p>The partition name is in the following format: |
| 85 | + * |
| 86 | + * <pre> |
| 87 | + * value1$value2$...$valueN |
| 88 | + * </pre> |
| 89 | + * |
| 90 | + * <p>For example, if the partition keys are [a, b, c], and the partition values are [1, 2, 3], |
| 91 | + * the partition name is "1$2$3". |
| 92 | + * |
| 93 | + * <p>Currently, we only support one partition key. So the partition name is in the following |
| 94 | + * format: |
| 95 | + * |
| 96 | + * <pre> |
| 97 | + * value |
| 98 | + * </pre> |
| 99 | + * |
| 100 | + * <p>For example, if the partition keys are [a], and the partition value is [1], the partition |
| 101 | + * name will be "1". |
| 102 | + */ |
| 103 | + public String generatePartitionName() { |
| 104 | + return String.join(PARTITION_SPEC_SEPARATOR, partitionValues); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns the qualified partition name for a partition spec (partition keys and partition |
| 109 | + * values). The qualified partition name is not used as the partition directory path, but is |
| 110 | + * used as a pretty display name of a partition. The qualified partition name is in the |
| 111 | + * following format: |
| 112 | + * |
| 113 | + * <pre> |
| 114 | + * key1=value1/key2=value2/.../keyN=valueN |
| 115 | + * </pre> |
| 116 | + */ |
| 117 | + public String getPartitionQualifiedName() { |
| 118 | + StringBuilder sb = new StringBuilder(); |
| 119 | + for (int i = 0; i < partitionKeys.size(); i++) { |
| 120 | + sb.append(partitionKeys.get(i)).append("=").append(partitionValues.get(i)); |
| 121 | + if (i != partitionKeys.size() - 1) { |
| 122 | + sb.append("/"); |
| 123 | + } |
| 124 | + } |
| 125 | + return sb.toString(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public String toString() { |
| 130 | + return "ResolvedPartitionSpec{" |
| 131 | + + "partitionKeys=" |
| 132 | + + partitionKeys |
| 133 | + + ", partitionValues=" |
| 134 | + + partitionValues |
| 135 | + + '}'; |
| 136 | + } |
| 137 | + |
| 138 | + private static List<String> getReorderedPartitionValues( |
| 139 | + List<String> partitionKeys, PartitionSpec partitionSpec) { |
| 140 | + Map<String, String> partitionSpecMap = partitionSpec.getPartitionSpec(); |
| 141 | + List<String> reOrderedPartitionValues = new ArrayList<>(partitionKeys.size()); |
| 142 | + partitionKeys.forEach( |
| 143 | + partitionKey -> reOrderedPartitionValues.add(partitionSpecMap.get(partitionKey))); |
| 144 | + return reOrderedPartitionValues; |
| 145 | + } |
| 146 | +} |
0 commit comments