tsreaper commented on code in PR #833:
URL: https://github.com/apache/incubator-paimon/pull/833#discussion_r1174812303


##########
paimon-core/src/main/java/org/apache/paimon/mergetree/compact/SortMergeReader.java:
##########
@@ -40,166 +38,70 @@
  */
 public class SortMergeReader<T> implements RecordReader<T> {
 
-    private final List<RecordReader<KeyValue>> nextBatchReaders;
-    private final Comparator<InternalRow> userKeyComparator;
     private final MergeFunctionWrapper<T> mergeFunctionWrapper;
-
-    private final PriorityQueue<Element> minHeap;
-    private final List<Element> polled;
+    private final LoserTree<KeyValue> loserTree;
 
     public SortMergeReader(
             List<RecordReader<KeyValue>> readers,
             Comparator<InternalRow> userKeyComparator,
             MergeFunctionWrapper<T> mergeFunctionWrapper) {
-        this.nextBatchReaders = new ArrayList<>(readers);
-        this.userKeyComparator = userKeyComparator;
         this.mergeFunctionWrapper = mergeFunctionWrapper;
-
-        this.minHeap =
-                new PriorityQueue<>(
-                        (e1, e2) -> {
-                            int result = 
userKeyComparator.compare(e1.kv.key(), e2.kv.key());
-                            if (result != 0) {
-                                return result;
-                            }
-                            return Long.compare(e1.kv.sequenceNumber(), 
e2.kv.sequenceNumber());
-                        });
-        this.polled = new ArrayList<>();
+        this.loserTree =
+                new LoserTree<>(
+                        readers,
+                        (e1, e2) -> userKeyComparator.compare(e2.key(), 
e1.key()),
+                        (e1, e2) -> Long.compare(e2.sequenceNumber(), 
e1.sequenceNumber()));
     }
 
     @Nullable
     @Override
     public RecordIterator<T> readBatch() throws IOException {
-        for (RecordReader<KeyValue> reader : nextBatchReaders) {
-            while (true) {
-                RecordIterator<KeyValue> iterator = reader.readBatch();
-                if (iterator == null) {
-                    // no more batches, permanently remove this reader
-                    reader.close();
-                    break;
-                }
-                KeyValue kv = iterator.next();
-                if (kv == null) {
-                    // empty iterator, clean up and try next batch
-                    iterator.releaseBatch();
-                } else {
-                    // found next kv
-                    minHeap.offer(new Element(kv, iterator, reader));
-                    break;
-                }
-            }
-        }
-        nextBatchReaders.clear();
-
-        return minHeap.isEmpty() ? null : new SortMergeIterator();
+        loserTree.initializeIfNeeded();
+        return loserTree.peekWinner() == null ? null : new SortMergeIterator();

Review Comment:
   Comparing with the old `readBatch` implementation, your new implementation 
will only produce 1 batch. Currently this does not affect much but it would be 
better to add comments.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to