liming30 commented on code in PR #833: URL: https://github.com/apache/incubator-paimon/pull/833#discussion_r1169927826
########## paimon-core/src/main/java/org/apache/paimon/mergetree/compact/LoserTree.java: ########## @@ -0,0 +1,345 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.paimon.mergetree.compact; + +import org.apache.paimon.reader.RecordReader; +import org.apache.paimon.utils.ExceptionUtils; + +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +/** + * A variant of the loser tree. In the LSM-Tree architecture, there will be duplicate Keys in + * multiple {@link RecordReader}, and these Keys need to be merged. In the loser tree, we return in + * the order of the Keys, but because the returned objects may be reused in the {@link RecordReader} + * or the {@link MergeFunction}, for a single {@link RecordReader}, we cannot get the next Key + * immediately after returning a Key, and we need to wait until the same Key in all {@link + * RecordReader} is returned before proceeding to the next Key. + * + * <p>The process of building the loser tree is the same as a regular loser tree. The difference is + * that in the process of adjusting the tree, we need to record the index of the same key and the + * state of the winner/loser for subsequent quick adjustment of the position of the winner. + * + * <p>Detailed design can refer to + * https://docs.google.com/document/d/1OPyb9o9K226Fb4O-SzZpBYgwfsvNY7X0H5T3ivdu1ck/edit?usp=sharing + */ +public class LoserTree<T> implements Closeable { + private final int[] tree; + private final int size; + private final List<LeafIterator<T>> leaves; + private final Comparator<T> firstComparator; + private final Comparator<T> secondComparator; + + private boolean initialized; + + public LoserTree( + List<RecordReader<T>> nextBatchReaders, + Comparator<T> firstComparator, + Comparator<T> secondComparator) { + this.size = nextBatchReaders.size(); + this.leaves = new ArrayList<>(size); + this.tree = new int[size]; + this.firstComparator = + (e1, e2) -> e1 == null ? -1 : (e2 == null ? 1 : firstComparator.compare(e1, e2)); + this.secondComparator = + (e1, e2) -> e1 == null ? -1 : (e2 == null ? 1 : secondComparator.compare(e1, e2)); Review Comment: I have Added comments in code. -- 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: issues-unsubscr...@paimon.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org