|
18 | 18 |
|
19 | 19 | package org.apache.paimon.utils; |
20 | 20 |
|
| 21 | +import org.apache.paimon.CoreOptions; |
| 22 | +import org.apache.paimon.Snapshot; |
| 23 | +import org.apache.paimon.data.BinaryRow; |
| 24 | +import org.apache.paimon.io.CompactIncrement; |
| 25 | +import org.apache.paimon.io.DataFileMeta; |
| 26 | +import org.apache.paimon.io.DataIncrement; |
21 | 27 | import org.apache.paimon.manifest.FileEntry; |
| 28 | +import org.apache.paimon.manifest.ManifestCommittable; |
22 | 29 | import org.apache.paimon.manifest.ManifestEntry; |
| 30 | +import org.apache.paimon.manifest.ManifestFile; |
| 31 | +import org.apache.paimon.manifest.ManifestList; |
| 32 | +import org.apache.paimon.operation.FileStoreCommit; |
| 33 | +import org.apache.paimon.table.FileStoreTable; |
| 34 | +import org.apache.paimon.table.sink.CommitMessageImpl; |
23 | 35 |
|
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.LinkedHashMap; |
24 | 39 | import java.util.List; |
25 | 40 | import java.util.Map; |
| 41 | +import java.util.Objects; |
| 42 | +import java.util.UUID; |
| 43 | +import java.util.function.Function; |
26 | 44 |
|
27 | | -/** Handler for branch merge data operations (manifest reading, committing). */ |
28 | | -public interface BranchMergeHandler { |
| 45 | +import static org.apache.paimon.utils.Preconditions.checkArgument; |
29 | 46 |
|
30 | | - /** Read all active data files from the given branch. */ |
31 | | - Map<FileEntry.Identifier, ManifestEntry> readBranchFiles(String branch); |
| 47 | +/** Branch merge handler backed by {@link FileStoreTable}. */ |
| 48 | +public class BranchMergeHandler { |
32 | 49 |
|
33 | | - /** Commit the given files to the target branch. */ |
34 | | - void commit(String targetBranch, List<ManifestEntry> filesToMerge); |
| 50 | + private final Function<String, FileStoreTable> branchTableFactory; |
| 51 | + |
| 52 | + public BranchMergeHandler(Function<String, FileStoreTable> branchTableFactory) { |
| 53 | + this.branchTableFactory = branchTableFactory; |
| 54 | + } |
| 55 | + |
| 56 | + public Map<FileEntry.Identifier, ManifestEntry> readBranchFiles(String branch) { |
| 57 | + FileStoreTable branchTable = branchTableFactory.apply(branch); |
| 58 | + Snapshot snapshot = branchTable.snapshotManager().latestSnapshot(); |
| 59 | + checkArgument( |
| 60 | + snapshot != null, |
| 61 | + "Cannot read branch '%s', because it does not have any snapshot.", |
| 62 | + branch); |
| 63 | + ManifestList manifestList = branchTable.store().manifestListFactory().create(); |
| 64 | + ManifestFile manifestFile = branchTable.store().manifestFileFactory().create(); |
| 65 | + Map<FileEntry.Identifier, ManifestEntry> files = new LinkedHashMap<>(); |
| 66 | + FileEntry.mergeEntries(manifestFile, manifestList.readDataManifests(snapshot), files, null); |
| 67 | + return files; |
| 68 | + } |
| 69 | + |
| 70 | + public void commit(String targetBranch, List<ManifestEntry> filesToMerge) { |
| 71 | + FileStoreTable branchTable = branchTableFactory.apply(targetBranch); |
| 72 | + boolean rowTrackingEnabled = |
| 73 | + new CoreOptions(branchTable.schema().options()).rowTrackingEnabled(); |
| 74 | + |
| 75 | + Map<MergeKey, List<DataFileMeta>> grouped = new LinkedHashMap<>(); |
| 76 | + for (ManifestEntry entry : filesToMerge) { |
| 77 | + DataFileMeta file = prepareFileForTargetCommit(entry.file(), rowTrackingEnabled); |
| 78 | + grouped.computeIfAbsent( |
| 79 | + new MergeKey( |
| 80 | + entry.partition().copy(), entry.bucket(), entry.totalBuckets()), |
| 81 | + k -> new ArrayList<>()) |
| 82 | + .add(file); |
| 83 | + } |
| 84 | + |
| 85 | + String commitUser = UUID.randomUUID().toString(); |
| 86 | + ManifestCommittable committable = new ManifestCommittable(0); |
| 87 | + for (Map.Entry<MergeKey, List<DataFileMeta>> e : grouped.entrySet()) { |
| 88 | + MergeKey key = e.getKey(); |
| 89 | + CommitMessageImpl message = |
| 90 | + new CommitMessageImpl( |
| 91 | + key.partition, |
| 92 | + key.bucket, |
| 93 | + key.totalBuckets, |
| 94 | + new DataIncrement( |
| 95 | + e.getValue(), Collections.emptyList(), Collections.emptyList()), |
| 96 | + CompactIncrement.emptyIncrement()); |
| 97 | + committable.addFileCommittable(message); |
| 98 | + } |
| 99 | + |
| 100 | + try (FileStoreCommit commit = branchTable.store().newCommit(commitUser, branchTable)) { |
| 101 | + commit.appendCommitCheckConflict(true).commit(committable, true); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private DataFileMeta prepareFileForTargetCommit(DataFileMeta file, boolean rowTrackingEnabled) { |
| 106 | + if (rowTrackingEnabled && file.firstRowId() != null) { |
| 107 | + // Source files already have row ids assigned in their branch. Clear them so the |
| 108 | + // target branch commit path assigns fresh, non-overlapping row ids. |
| 109 | + return file.newFirstRowId(null); |
| 110 | + } |
| 111 | + return file; |
| 112 | + } |
| 113 | + |
| 114 | + private static class MergeKey { |
| 115 | + final BinaryRow partition; |
| 116 | + final int bucket; |
| 117 | + final int totalBuckets; |
| 118 | + |
| 119 | + MergeKey(BinaryRow partition, int bucket, int totalBuckets) { |
| 120 | + this.partition = partition; |
| 121 | + this.bucket = bucket; |
| 122 | + this.totalBuckets = totalBuckets; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean equals(Object o) { |
| 127 | + if (this == o) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + if (!(o instanceof MergeKey)) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + MergeKey that = (MergeKey) o; |
| 134 | + return bucket == that.bucket |
| 135 | + && totalBuckets == that.totalBuckets |
| 136 | + && Objects.equals(partition, that.partition); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int hashCode() { |
| 141 | + return Objects.hash(partition, bucket, totalBuckets); |
| 142 | + } |
| 143 | + } |
35 | 144 | } |
0 commit comments