SilverNarcissus commented on a change in pull request #713: [IOTDB-418] New 
series reader
URL: https://github.com/apache/incubator-iotdb/pull/713#discussion_r377997898
 
 

 ##########
 File path: 
server/src/main/java/org/apache/iotdb/db/query/reader/series/SeriesReader.java
 ##########
 @@ -0,0 +1,444 @@
+/*
+ * 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.reader.series;
+
+import org.apache.iotdb.db.engine.cache.DeviceMetaDataCache;
+import org.apache.iotdb.db.engine.modification.Modification;
+import org.apache.iotdb.db.engine.querycontext.QueryDataSource;
+import org.apache.iotdb.db.engine.querycontext.ReadOnlyMemChunk;
+import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.control.FileReaderManager;
+import org.apache.iotdb.db.query.reader.chunk.MemChunkLoader;
+import org.apache.iotdb.db.query.reader.chunk.MemChunkReader;
+import org.apache.iotdb.db.query.reader.universal.PriorityMergeReader;
+import org.apache.iotdb.db.utils.QueryUtils;
+import org.apache.iotdb.db.utils.TestOnly;
+import org.apache.iotdb.tsfile.file.metadata.ChunkMetaData;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.TsFileSequenceReader;
+import org.apache.iotdb.tsfile.read.common.BatchData;
+import org.apache.iotdb.tsfile.read.common.Chunk;
+import org.apache.iotdb.tsfile.read.common.Path;
+import org.apache.iotdb.tsfile.read.controller.ChunkLoaderImpl;
+import org.apache.iotdb.tsfile.read.controller.IChunkLoader;
+import org.apache.iotdb.tsfile.read.filter.basic.Filter;
+import org.apache.iotdb.tsfile.read.filter.basic.UnaryFilter;
+import org.apache.iotdb.tsfile.read.reader.IChunkReader;
+import org.apache.iotdb.tsfile.read.reader.IPageReader;
+import org.apache.iotdb.tsfile.read.reader.chunk.ChunkReader;
+
+import java.io.IOException;
+import java.util.*;
+
+public class SeriesReader {
+
+  private final Path seriesPath;
+  private final TSDataType dataType;
+  private final QueryContext context;
+  private final Filter timeFilter;
+  private final Filter valueFilter;
+
+  private final List<TsFileResource> seqFileResource;
+  private final PriorityQueue<TsFileResource> unseqFileResource;
+
+  private final List<ChunkMetaData> seqChunkMetadatas = new LinkedList<>();
+  private final PriorityQueue<ChunkMetaData> unseqChunkMetadatas =
+      new 
PriorityQueue<>(Comparator.comparingLong(ChunkMetaData::getStartTime));
+
+  private boolean hasCachedFirstChunkMetadata;
+  private ChunkMetaData firstChunkMetaData;
+
+  private PriorityQueue<VersionPair<IPageReader>> overlappedPageReaders =
+      new PriorityQueue<>(
+          Comparator.comparingLong(pageReader -> 
pageReader.data.getStatistics().getStartTime()));
+
+  private PriorityMergeReader mergeReader = new PriorityMergeReader();
+
+  private boolean hasCachedNextBatch;
+  private BatchData cachedBatchData;
+
+
+  public SeriesReader(Path seriesPath, TSDataType dataType, QueryContext 
context,
+      QueryDataSource dataSource, Filter timeFilter, Filter valueFilter) {
+    this.seriesPath = seriesPath;
+    this.dataType = dataType;
+    this.context = context;
+    this.seqFileResource = dataSource.getSeqResources();
+    this.unseqFileResource = 
sortUnSeqFileResources(dataSource.getUnseqResources());
+    this.timeFilter = timeFilter;
+    this.valueFilter = valueFilter;
+  }
+
+  @TestOnly
+  public SeriesReader(Path seriesPath, TSDataType dataType, QueryContext 
context,
+      List<TsFileResource> seqFileResource, List<TsFileResource> 
unseqFileResource,
+      Filter timeFilter, Filter valueFilter) {
+    this.seriesPath = seriesPath;
+    this.dataType = dataType;
+    this.context = context;
+    this.seqFileResource = seqFileResource;
+    this.unseqFileResource = sortUnSeqFileResources(unseqFileResource);
+    this.timeFilter = timeFilter;
+    this.valueFilter = valueFilter;
+  }
+
+
+  public boolean hasNextChunk() throws IOException {
+    if (hasCachedFirstChunkMetadata) {
+      return true;
+    }
+    // init first chunk metadata whose startTime is minimum
+    tryToInitFirstChunk();
+
+    return hasCachedFirstChunkMetadata;
+  }
+
+  /**
+   * Because seq data and unseq data intersect, the minimum startTime taken 
from two files at a time
+   * is used as the reference time to start reading data
+   */
+  private void tryToInitFirstChunk() throws IOException {
+    tryToFillChunkMetadatas();
+    hasCachedFirstChunkMetadata = true;
+    if (!seqChunkMetadatas.isEmpty() && unseqChunkMetadatas.isEmpty()) {
+      // only has seq
+      firstChunkMetaData = seqChunkMetadatas.remove(0);
+    } else if (seqChunkMetadatas.isEmpty() && !unseqChunkMetadatas.isEmpty()) {
+      // only has unseq
+      firstChunkMetaData = unseqChunkMetadatas.poll();
+    } else if (!seqChunkMetadatas.isEmpty()) {
+      // has seq and unseq
+      if (seqChunkMetadatas.get(0).getStartTime() <= 
unseqChunkMetadatas.peek().getStartTime()) {
+        firstChunkMetaData = seqChunkMetadatas.remove(0);
+      } else {
+        firstChunkMetaData = unseqChunkMetadatas.poll();
+      }
+    } else {
+      // no seq nor unseq
+      hasCachedFirstChunkMetadata = false;
+    }
+    tryToFillChunkMetadatas();
+  }
+
+  public boolean isChunkOverlapped() {
+    Statistics chunkStatistics = firstChunkMetaData.getStatistics();
+    return mergeReader.hasNextTimeValuePair()
+        || (!seqChunkMetadatas.isEmpty()
+        && chunkStatistics.getEndTime() >= 
seqChunkMetadatas.get(0).getStartTime())
+        || (!unseqChunkMetadatas.isEmpty()
+        && chunkStatistics.getEndTime() >= 
unseqChunkMetadatas.peek().getStartTime());
+  }
+
+  public Statistics currentChunkStatistics() {
+    return firstChunkMetaData.getStatistics();
+  }
+
+  public void skipCurrentChunk() {
+    hasCachedFirstChunkMetadata = false;
+    firstChunkMetaData = null;
+  }
+
+  public boolean hasNextPage() throws IOException {
+    if (!overlappedPageReaders.isEmpty()) {
+      return true;
+    }
+
+    fillOverlappedPageReaders();
+
+    return !overlappedPageReaders.isEmpty();
+  }
+
+  private void fillOverlappedPageReaders() throws IOException {
+    if (!hasCachedFirstChunkMetadata) {
+      return;
+    }
+    unpackOneChunkMetaData(firstChunkMetaData);
+    hasCachedFirstChunkMetadata = false;
+    firstChunkMetaData = null;
+  }
+
+  private void unpackOneChunkMetaData(ChunkMetaData chunkMetaData) throws 
IOException {
+    initChunkReader(chunkMetaData)
+        .getPageReaderList()
+        .forEach(
+            pageReader ->
+                overlappedPageReaders.add(
+                    new VersionPair(chunkMetaData.getVersion(), pageReader)));
+  }
+
+
+  protected BatchData nextPage() throws IOException {
+    BatchData pageData = Objects
+        .requireNonNull(overlappedPageReaders.poll().data, "No Batch data")
+        .getAllSatisfiedPageData();
+    // only need to consider valueFilter because timeFilter has been set into 
the page reader
+    if (valueFilter == null) {
+      return pageData;
+    }
+    BatchData batchData = new BatchData(pageData.getDataType());
+    while (pageData.hasCurrent()) {
+      if (valueFilter.satisfy(pageData.currentTime(), 
pageData.currentValue())) {
+        batchData.putAnObject(pageData.currentTime(), pageData.currentValue());
+      }
+      pageData.next();
+    }
+    return batchData;
+  }
+
+  protected boolean isPageOverlapped() {
+    Statistics pageStatistics = 
overlappedPageReaders.peek().data.getStatistics();
+    return mergeReader.hasNextTimeValuePair()
+        || (!seqChunkMetadatas.isEmpty()
+        && pageStatistics.getEndTime() >= 
seqChunkMetadatas.get(0).getStartTime())
+        || (!unseqChunkMetadatas.isEmpty()
+        && pageStatistics.getEndTime() >= 
unseqChunkMetadatas.peek().getStartTime());
+  }
+
+  public Statistics currentPageStatistics() throws IOException {
+    if (overlappedPageReaders.isEmpty() || overlappedPageReaders.peek().data 
== null) {
+      throw new IOException("No next page statistics.");
+    }
+    return overlappedPageReaders.peek().data.getStatistics();
+  }
+
+  public void skipCurrentPage() {
+    overlappedPageReaders.poll();
+  }
+
+  public boolean hasNextOverlappedPage() throws IOException {
 
 Review comment:
   OOM error may appear in this method. The reason is if many pages are over 
lapped, returned batch data will be very large. 

----------------------------------------------------------------
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

Reply via email to