2020
2121import org .apache .paimon .Snapshot ;
2222import org .apache .paimon .Snapshot .CommitKind ;
23+ import org .apache .paimon .annotation .VisibleForTesting ;
2324import org .apache .paimon .data .BinaryRow ;
2425import org .apache .paimon .data .InternalRow ;
2526import org .apache .paimon .index .DeletionVectorMeta ;
@@ -82,8 +83,8 @@ public class ConflictDetection {
8283 private final boolean dataEvolutionEnabled ;
8384 private final IndexFileHandler indexFileHandler ;
8485 private final SnapshotManager snapshotManager ;
86+ private final CommitScanner commitScanner ;
8587
86- private @ Nullable CommitScanner scanner ;
8788 private @ Nullable PartitionExpire partitionExpire ;
8889 private @ Nullable Long rowIdCheckFromSnapshot = null ;
8990
@@ -97,7 +98,8 @@ public ConflictDetection(
9798 boolean deletionVectorsEnabled ,
9899 boolean dataEvolutionEnabled ,
99100 IndexFileHandler indexFileHandler ,
100- SnapshotManager snapshotManager ) {
101+ SnapshotManager snapshotManager ,
102+ CommitScanner commitScanner ) {
101103 this .tableName = tableName ;
102104 this .commitUser = commitUser ;
103105 this .partitionType = partitionType ;
@@ -108,16 +110,13 @@ public ConflictDetection(
108110 this .dataEvolutionEnabled = dataEvolutionEnabled ;
109111 this .indexFileHandler = indexFileHandler ;
110112 this .snapshotManager = snapshotManager ;
113+ this .commitScanner = commitScanner ;
111114 }
112115
113116 public void setRowIdCheckFromSnapshot (@ Nullable Long rowIdCheckFromSnapshot ) {
114117 this .rowIdCheckFromSnapshot = rowIdCheckFromSnapshot ;
115118 }
116119
117- public void setCommitScanner (CommitScanner scanner ) {
118- this .scanner = scanner ;
119- }
120-
121120 @ Nullable
122121 public Comparator <InternalRow > keyComparator () {
123122 return keyComparator ;
@@ -378,7 +377,8 @@ private Optional<RuntimeException> checkConflictForPartitionExpire(
378377 return Optional .empty ();
379378 }
380379
381- private Optional <RuntimeException > checkForRowIdFromSnapshot (
380+ @ VisibleForTesting
381+ Optional <RuntimeException > checkForRowIdFromSnapshot (
382382 Snapshot latestSnapshot ,
383383 List <SimpleFileEntry > deltaEntries ,
384384 List <IndexManifestEntry > deltaIndexEntries ) {
@@ -401,14 +401,18 @@ private Optional<RuntimeException> checkForRowIdFromSnapshot(
401401 }
402402
403403 // check history row id ranges
404- long checkNextRowId = snapshotManager .snapshot (rowIdCheckFromSnapshot ).nextRowId ();
404+ Long checkNextRowId = snapshotManager .snapshot (rowIdCheckFromSnapshot ).nextRowId ();
405+ checkState (
406+ checkNextRowId != null ,
407+ "Next row id cannot be null for snapshot %s." ,
408+ rowIdCheckFromSnapshot );
405409 for (long i = rowIdCheckFromSnapshot + 1 ; i <= latestSnapshot .id (); i ++) {
406410 Snapshot snapshot = snapshotManager .snapshot (i );
407411 if (snapshot .commitKind () == CommitKind .COMPACT ) {
408412 continue ;
409413 }
410414 List <ManifestEntry > changes =
411- scanner .readIncrementalEntries (snapshot , changedPartitions );
415+ commitScanner .readIncrementalEntries (snapshot , changedPartitions );
412416 for (ManifestEntry entry : changes ) {
413417 DataFileMeta file = entry .file ();
414418 long firstRowId = file .nonNullFirstRowId ();
@@ -429,7 +433,8 @@ private Optional<RuntimeException> checkForRowIdFromSnapshot(
429433 return Optional .empty ();
430434 }
431435
432- private Optional <RuntimeException > checkRowIdRangeConflicts (
436+ @ VisibleForTesting
437+ Optional <RuntimeException > checkRowIdRangeConflicts (
433438 CommitKind commitKind , Collection <SimpleFileEntry > mergedEntries ) {
434439 if (!dataEvolutionEnabled ) {
435440 return Optional .empty ();
@@ -694,4 +699,9 @@ public int hashCode() {
694699 return Objects .hash (partition , bucket , level );
695700 }
696701 }
702+
703+ /** Factory to create {@link ConflictDetection}. */
704+ public interface Factory {
705+ ConflictDetection create (CommitScanner scanner );
706+ }
697707}
0 commit comments