shizy818 commented on code in PR #14265: URL: https://github.com/apache/iotdb/pull/14265#discussion_r1868775125
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/datastructure/MergeSortTvListIterator.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.iotdb.db.utils.datastructure; + +import org.apache.iotdb.db.utils.MathUtils; + +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.file.metadata.enums.TSEncoding; +import org.apache.tsfile.read.TimeValuePair; +import org.apache.tsfile.read.reader.IPointReader; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class MergeSortTvListIterator implements IPointReader { + private final List<TVList.TVListIterator> tvListIterators; + private final TSDataType tsDataType; + private TSEncoding encoding; + private int floatPrecision = -1; + + private TimeValuePair currentTvPair; + + public MergeSortTvListIterator(TSDataType tsDataType, List<TVList> tvLists) { + this.tsDataType = tsDataType; + tvListIterators = new ArrayList<>(); + for (TVList tvList : tvLists) { + tvListIterators.add(tvList.iterator()); + } + } + + public MergeSortTvListIterator( + TSDataType tsDataType, TSEncoding encoding, int floatPrecision, List<TVList> tvLists) { + this(tsDataType, tvLists); + this.encoding = encoding; + this.floatPrecision = floatPrecision; + } + + private int getSelectedTVListIndex() { + long time = Long.MAX_VALUE; + int selectedTVListIndex = -1; + for (int i = 0; i < tvListIterators.size(); i++) { + TVList.TVListIterator iterator = tvListIterators.get(i); + TimeValuePair currTvPair = null; + if (iterator.hasNext()) { + currTvPair = iterator.current(); + } + + // update minimum time and remember selected TVList + if (currTvPair != null && currTvPair.getTimestamp() <= time) { + time = currTvPair.getTimestamp(); + selectedTVListIndex = i; + } + } + return selectedTVListIndex; + } + + @Override + public boolean hasNextTimeValuePair() { + boolean hasNext = false; + int selectedTVListIndex = getSelectedTVListIndex(); + if (selectedTVListIndex >= 0) { + currentTvPair = tvListIterators.get(selectedTVListIndex).next(); + hasNext = true; + + // call next to skip identical timestamp in other iterators + for (int i = 0; i < tvListIterators.size(); i++) { + TimeValuePair tvPair = tvListIterators.get(i).current(); + if (tvPair != null && tvPair.getTimestamp() == currentTvPair.getTimestamp()) { + tvListIterators.get(i).next(); + } + } + } + return hasNext; + } + + @Override + public TimeValuePair nextTimeValuePair() { + return currentTimeValuePair(); + } + + @Override + public TimeValuePair currentTimeValuePair() { + if (encoding != null && floatPrecision != -1) { + if (tsDataType == TSDataType.FLOAT) { + float value = currentTvPair.getValue().getFloat(); + if (!Float.isNaN(value) + && (encoding == TSEncoding.RLE || encoding == TSEncoding.TS_2DIFF)) { + currentTvPair + .getValue() + .setFloat(MathUtils.roundWithGivenPrecision(value, floatPrecision)); + } + } else if (tsDataType == TSDataType.DOUBLE) { + double value = currentTvPair.getValue().getDouble(); + if (!Double.isNaN(value) + && (encoding == TSEncoding.RLE || encoding == TSEncoding.TS_2DIFF)) { + currentTvPair + .getValue() + .setDouble(MathUtils.roundWithGivenPrecision(value, floatPrecision)); + } + } + } + return currentTvPair; + } + + @Override + public long getUsedMemorySize() { + return 0; + } Review Comment: getUsedMemorySize for memory page reader is not used yet. -- 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]
