|
| 1 | +package com.scalar.db.transaction.consensuscommit; |
| 2 | + |
| 3 | +import com.scalar.db.api.Mutation; |
| 4 | +import com.scalar.db.api.StorageInfo; |
| 5 | +import com.scalar.db.common.StorageInfoProvider; |
| 6 | +import com.scalar.db.exception.storage.ExecutionException; |
| 7 | +import com.scalar.db.io.Key; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.LinkedHashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import javax.annotation.Nullable; |
| 16 | +import javax.annotation.concurrent.ThreadSafe; |
| 17 | + |
| 18 | +@ThreadSafe |
| 19 | +public class MutationsGrouper { |
| 20 | + |
| 21 | + private final StorageInfoProvider storageInfoProvider; |
| 22 | + |
| 23 | + public MutationsGrouper(StorageInfoProvider storageInfoProvider) { |
| 24 | + this.storageInfoProvider = storageInfoProvider; |
| 25 | + } |
| 26 | + |
| 27 | + public List<List<Mutation>> groupMutations(Collection<Mutation> mutations) |
| 28 | + throws ExecutionException { |
| 29 | + // MutationGroup mutations by their storage info and atomicity unit |
| 30 | + List<MutationGroup> groups = new ArrayList<>(); |
| 31 | + Map<MutationGroup, List<List<Mutation>>> groupToBatches = new LinkedHashMap<>(); |
| 32 | + |
| 33 | + for (Mutation mutation : mutations) { |
| 34 | + assert mutation.forNamespace().isPresent(); |
| 35 | + StorageInfo storageInfo = storageInfoProvider.getStorageInfo(mutation.forNamespace().get()); |
| 36 | + |
| 37 | + MutationGroup matchedGroup = null; |
| 38 | + for (MutationGroup group : groups) { |
| 39 | + if (group.isSameGroup(mutation, storageInfo)) { |
| 40 | + matchedGroup = group; |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + if (matchedGroup == null) { |
| 45 | + // If no matching group is found, create a new one |
| 46 | + matchedGroup = new MutationGroup(mutation, storageInfo); |
| 47 | + groups.add(matchedGroup); |
| 48 | + } |
| 49 | + |
| 50 | + List<List<Mutation>> batches = |
| 51 | + groupToBatches.computeIfAbsent(matchedGroup, g -> new ArrayList<>()); |
| 52 | + int maxCount = matchedGroup.storageInfo.getMaxAtomicMutationsCount(); |
| 53 | + |
| 54 | + if (batches.isEmpty() || batches.get(batches.size() - 1).size() >= maxCount) { |
| 55 | + // If the last batch is full or there are no batches yet, create a new batch |
| 56 | + batches.add(new ArrayList<>()); |
| 57 | + } |
| 58 | + |
| 59 | + batches.get(batches.size() - 1).add(mutation); |
| 60 | + } |
| 61 | + |
| 62 | + // Flatten the grouped mutations into a single list of batches |
| 63 | + return groupToBatches.values().stream().flatMap(List::stream).collect(Collectors.toList()); |
| 64 | + } |
| 65 | + |
| 66 | + private static class MutationGroup { |
| 67 | + public final StorageInfo storageInfo; |
| 68 | + @Nullable public final String namespace; |
| 69 | + @Nullable public final String table; |
| 70 | + @Nullable public final Key partitionKey; |
| 71 | + @Nullable public final Optional<Key> clusteringKey; |
| 72 | + |
| 73 | + public MutationGroup(Mutation mutation, StorageInfo storageInfo) { |
| 74 | + assert mutation.forNamespace().isPresent() && mutation.forTable().isPresent(); |
| 75 | + |
| 76 | + switch (storageInfo.getMutationAtomicityUnit()) { |
| 77 | + case RECORD: |
| 78 | + this.clusteringKey = mutation.getClusteringKey(); |
| 79 | + this.partitionKey = mutation.getPartitionKey(); |
| 80 | + this.table = mutation.forTable().get(); |
| 81 | + this.namespace = mutation.forNamespace().get(); |
| 82 | + this.storageInfo = storageInfo; |
| 83 | + break; |
| 84 | + case PARTITION: |
| 85 | + this.clusteringKey = null; |
| 86 | + this.partitionKey = mutation.getPartitionKey(); |
| 87 | + this.table = mutation.forTable().get(); |
| 88 | + this.namespace = mutation.forNamespace().get(); |
| 89 | + this.storageInfo = storageInfo; |
| 90 | + break; |
| 91 | + case TABLE: |
| 92 | + this.clusteringKey = null; |
| 93 | + this.partitionKey = null; |
| 94 | + this.table = mutation.forTable().get(); |
| 95 | + this.namespace = mutation.forNamespace().get(); |
| 96 | + this.storageInfo = storageInfo; |
| 97 | + break; |
| 98 | + case NAMESPACE: |
| 99 | + this.clusteringKey = null; |
| 100 | + this.partitionKey = null; |
| 101 | + this.table = null; |
| 102 | + this.namespace = mutation.forNamespace().get(); |
| 103 | + this.storageInfo = storageInfo; |
| 104 | + break; |
| 105 | + case STORAGE: |
| 106 | + this.clusteringKey = null; |
| 107 | + this.partitionKey = null; |
| 108 | + this.table = null; |
| 109 | + this.namespace = null; |
| 110 | + this.storageInfo = storageInfo; |
| 111 | + break; |
| 112 | + default: |
| 113 | + throw new AssertionError( |
| 114 | + "Unknown mutation atomicity unit: " + storageInfo.getMutationAtomicityUnit()); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + boolean isSameGroup(Mutation otherMutation, StorageInfo otherStorageInfo) { |
| 119 | + assert otherMutation.forNamespace().isPresent() && otherMutation.forTable().isPresent(); |
| 120 | + |
| 121 | + switch (storageInfo.getMutationAtomicityUnit()) { |
| 122 | + case RECORD: |
| 123 | + if (!otherMutation.getClusteringKey().equals(this.clusteringKey)) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + // Fall through |
| 127 | + case PARTITION: |
| 128 | + if (!otherMutation.getPartitionKey().equals(this.partitionKey)) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + // Fall through |
| 132 | + case TABLE: |
| 133 | + if (!otherMutation.forTable().get().equals(this.table)) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + // Fall through |
| 137 | + case NAMESPACE: |
| 138 | + if (!otherMutation.forNamespace().get().equals(this.namespace)) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + // Fall through |
| 142 | + case STORAGE: |
| 143 | + if (!otherStorageInfo.getStorageName().equals(this.storageInfo.getStorageName())) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + break; |
| 147 | + default: |
| 148 | + throw new AssertionError( |
| 149 | + "Unknown mutation atomicity unit: " + storageInfo.getMutationAtomicityUnit()); |
| 150 | + } |
| 151 | + |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments