jt2594838 commented on a change in pull request #738: [IOTDB-396] New query clause: disable align URL: https://github.com/apache/incubator-iotdb/pull/738#discussion_r366280738
########## File path: server/src/main/java/org/apache/iotdb/db/query/dataset/NonAlignEngineDataSet.java ########## @@ -0,0 +1,354 @@ +/* + * 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.query.dataset; + +import org.apache.iotdb.db.query.pool.QueryTaskPoolManager; +import org.apache.iotdb.db.query.reader.ManagedSeriesReader; +import org.apache.iotdb.db.tools.watermark.WatermarkEncoder; +import org.apache.iotdb.service.rpc.thrift.TSQueryNonAlignDataSet; +import org.apache.iotdb.tsfile.exception.write.UnSupportedDataTypeException; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.read.common.BatchData; +import org.apache.iotdb.tsfile.read.common.Path; +import org.apache.iotdb.tsfile.read.common.RowRecord; +import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet; +import org.apache.iotdb.tsfile.utils.Pair; +import org.apache.iotdb.tsfile.utils.PublicBAOS; +import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.atomic.AtomicIntegerArray; + +public class NonAlignEngineDataSet extends QueryDataSet { + + private static class ReadTask implements Runnable { + + private final ManagedSeriesReader reader; + private BlockingQueue<Pair<ByteBuffer, ByteBuffer>> blockingQueue; + private WatermarkEncoder encoder; + NonAlignEngineDataSet dataSet; + private int index; + + + public ReadTask(ManagedSeriesReader reader, + BlockingQueue<Pair<ByteBuffer, ByteBuffer>> blockingQueue, + WatermarkEncoder encoder, NonAlignEngineDataSet dataSet, int index) { + this.reader = reader; + this.blockingQueue = blockingQueue; + this.encoder = encoder; + this.dataSet = dataSet; + this.index = index; + } + + @Override + public void run() { + PublicBAOS timeBAOS = new PublicBAOS(); + PublicBAOS valueBAOS = new PublicBAOS(); + try { + synchronized (reader) { + // if the task is submitted, there must be free space in the queue + // so here we don't need to check whether the queue has free space + // the reader has next batch + if ((dataSet.cachedBatchData[index] != null && dataSet.cachedBatchData[index].hasCurrent()) + || reader.hasNextBatch()) { + BatchData batchData; + if (dataSet.cachedBatchData[index] != null && dataSet.cachedBatchData[index].hasCurrent()) + batchData = dataSet.cachedBatchData[index]; + else + batchData = reader.nextBatch(); + + int rowCount = 0; + while (rowCount < dataSet.fetchSize) { + + if ((dataSet.limit > 0 && dataSet.alreadyReturnedRowNumArray.get(index) >= dataSet.limit)) { + break; + } + + if (batchData != null && batchData.hasCurrent()) { + if (dataSet.offsetArray.get(index) == 0) { + long time = batchData.currentTime(); + ReadWriteIOUtils.write(time, timeBAOS); + TSDataType type = batchData.getDataType(); + switch (type) { + case INT32: + int intValue = batchData.getInt(); + if (encoder != null && encoder.needEncode(time)) { + intValue = encoder.encodeInt(intValue, time); + } + ReadWriteIOUtils.write(intValue, valueBAOS); + break; + case INT64: + long longValue = batchData.getLong(); + if (encoder != null && encoder.needEncode(time)) { + longValue = encoder.encodeLong(longValue, time); + } + ReadWriteIOUtils.write(longValue, valueBAOS); + break; + case FLOAT: + float floatValue = batchData.getFloat(); + if (encoder != null && encoder.needEncode(time)) { + floatValue = encoder.encodeFloat(floatValue, time); + } + ReadWriteIOUtils.write(floatValue, valueBAOS); + break; + case DOUBLE: + double doubleValue = batchData.getDouble(); + if (encoder != null && encoder.needEncode(time)) { + doubleValue = encoder.encodeDouble(doubleValue, time); + } + ReadWriteIOUtils.write(doubleValue, valueBAOS); + break; + case BOOLEAN: + ReadWriteIOUtils.write(batchData.getBoolean(), + valueBAOS); + break; + case TEXT: + ReadWriteIOUtils + .write(batchData.getBinary(), + valueBAOS); + break; + default: + throw new UnSupportedDataTypeException( + String.format("Data type %s is not supported.", type)); + } + } + batchData.next(); + } + else { + if (reader.hasNextBatch()) { + batchData = reader.nextBatch(); + dataSet.cachedBatchData[index] = batchData; + continue; + } + else + break; + } + if (dataSet.offsetArray.get(index) == 0) { + rowCount++; + if (dataSet.limit > 0) { + dataSet.alreadyReturnedRowNumArray.incrementAndGet(index); + } + } else { + dataSet.offsetArray.decrementAndGet(index); + } + } + if (rowCount == 0) { + blockingQueue.put(new Pair(null, null)); + // set the hasRemaining field in reader to false + // tell the Consumer not to submit another task for this reader any more + reader.setHasRemaining(false); + // remove itself from the QueryTaskPoolManager + reader.setManagedByQueryManager(false); + return; + } + + ByteBuffer timeBuffer = ByteBuffer.allocate(timeBAOS.size()); + timeBuffer.put(timeBAOS.getBuf(), 0, timeBAOS.size()); + timeBuffer.flip(); + ByteBuffer valueBuffer = ByteBuffer.allocate(valueBAOS.size()); + valueBuffer.put(valueBAOS.getBuf(), 0, valueBAOS.size()); + valueBuffer.flip(); Review comment: You may just wrap the byte[] in the BAOS with `ByteBuffer.wrap()` as it does not seem you will reuse it. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services