Skip to content

Commit

Permalink
fix AndNode NullPointer (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jialin Qiao committed Feb 27, 2020
1 parent 32c6fb0 commit 907176c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public boolean hasNextTimeColumn() throws IOException {
fillLeftCache();
fillRightCache();

if (!hasLeftValue() || !hasRightValue()) {
return false;
}

while (leftTimeColumn.hasCurrent() && rightTimeColumn.hasCurrent()) {
long leftValue = leftTimeColumn.currentTime();
long rightValue = rightTimeColumn.currentTime();
Expand Down Expand Up @@ -94,6 +98,14 @@ private void fillLeftCache() throws IOException {
}
}

private boolean hasLeftValue() {
return leftTimeColumn != null && leftTimeColumn.hasCurrent();
}

private boolean hasRightValue() {
return rightTimeColumn != null && rightTimeColumn.hasCurrent();
}

//no more data in cache and has more data in child
private boolean couldFillCache(TimeColumn timeSeries, Node child) throws IOException {
return (timeSeries == null || !timeSeries.hasCurrent()) && child.hasNextTimeColumn();
Expand All @@ -115,4 +127,4 @@ public TimeColumn nextTimeColumn() throws IOException {
public NodeType getType() {
return NodeType.AND;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ public boolean hasNextTimeColumn() throws IOException {
if (hasCached) {
return true;
}
if (reader.hasNextBatch()) {
hasCached = true;
while (reader.hasNextBatch()) {
cacheData = reader.nextBatch();
cachedTimeSeries = cacheData.getTimeColumn();
if (cacheData.hasCurrent()) {
hasCached = true;
cachedTimeSeries = cacheData.getTimeColumn();
break;
}
}
return hasCached;
}
Expand Down Expand Up @@ -82,4 +85,4 @@ public NodeType getType() {
return NodeType.LEAF;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/
package org.apache.iotdb.tsfile.read.query.timegenerator;

import org.apache.iotdb.tsfile.file.metadata.ChunkMetaData;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.read.common.BatchData;
import org.apache.iotdb.tsfile.read.common.TimeColumn;
import org.apache.iotdb.tsfile.read.query.timegenerator.node.AndNode;
import org.apache.iotdb.tsfile.read.query.timegenerator.node.LeafNode;
import org.apache.iotdb.tsfile.read.query.timegenerator.node.Node;
import org.apache.iotdb.tsfile.read.query.timegenerator.node.NodeType;
import org.apache.iotdb.tsfile.read.query.timegenerator.node.OrNode;
import org.apache.iotdb.tsfile.read.reader.FakedBatchReader;
import org.apache.iotdb.tsfile.read.reader.IBatchReader;
import org.apache.iotdb.tsfile.read.reader.series.AbstractFileSeriesReader;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -44,13 +43,17 @@ public void testType() {

@Test
public void testLeafNode() throws IOException {
int index = 0;
long[] timestamps = new long[]{1, 2, 3, 4, 5, 6, 7};
AbstractFileSeriesReader seriesReader = new FakedFileSeriesReader(timestamps);
Node leafNode = new LeafNode(seriesReader);
while (leafNode.hasNextTimeColumn()) {
Assert.assertEquals(timestamps[index++], leafNode.nextTimeColumn().currentTime());
IBatchReader batchReader = new FakedBatchReader(timestamps);
Node leafNode = new LeafNode(batchReader);

Assert.assertTrue(leafNode.hasNextTimeColumn());
TimeColumn timeColumn = leafNode.nextTimeColumn();
for (long timestamp : timestamps) {
Assert.assertEquals(timestamp, timeColumn.currentTime());
timeColumn.next();
}
Assert.assertFalse(leafNode.hasNextTimeColumn());
}

@Test
Expand All @@ -69,8 +72,8 @@ public void testOrNode() throws IOException {

private void testOr(long[] ret, long[] left, long[] right) throws IOException {
int index = 0;
Node orNode = new OrNode(new LeafNode(new FakedFileSeriesReader(left)),
new LeafNode(new FakedFileSeriesReader(right)));
Node orNode = new OrNode(new LeafNode(new FakedBatchReader(left)),
new LeafNode(new FakedBatchReader(right)));
while (orNode.hasNextTimeColumn()) {
TimeColumn timeSeries = orNode.nextTimeColumn();
while (timeSeries.hasCurrent()) {
Expand All @@ -93,8 +96,8 @@ public void testAndNode() throws IOException {

private void testAnd(long[] ret, long[] left, long[] right) throws IOException {
int index = 0;
Node andNode = new AndNode(new LeafNode(new FakedFileSeriesReader(left)),
new LeafNode(new FakedFileSeriesReader(right)));
Node andNode = new AndNode(new LeafNode(new FakedBatchReader(left)),
new LeafNode(new FakedBatchReader(right)));
while (andNode.hasNextTimeColumn()) {
TimeColumn timeSeries = andNode.nextTimeColumn();
while (timeSeries.hasCurrent()) {
Expand All @@ -106,44 +109,5 @@ private void testAnd(long[] ret, long[] left, long[] right) throws IOException {
Assert.assertEquals(ret.length, index);
}

private static class FakedFileSeriesReader extends AbstractFileSeriesReader {

BatchData data;
boolean hasCachedData;

public FakedFileSeriesReader(long[] timestamps) {
super(null, null, null);
data = new BatchData(TSDataType.INT32);
for (long time : timestamps) {
data.putInt(time, 1);
}
hasCachedData = true;
}

@Override
public boolean hasNextBatch() {
return hasCachedData;
}

@Override
public BatchData nextBatch() {
hasCachedData = false;
return data;
}

@Override
protected void initChunkReader(ChunkMetaData chunkMetaData) throws IOException {

}

@Override
protected boolean chunkSatisfied(ChunkMetaData chunkMetaData) {
return false;
}

@Override
public void close() {

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.tsfile.read.reader;

import java.io.IOException;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.read.common.BatchData;

public class FakedBatchReader implements IBatchReader {

private BatchData data;
private boolean hasCached = false;

public FakedBatchReader(long[] timestamps) {
data = new BatchData(TSDataType.INT32);
for (long time : timestamps) {
data.putInt(time, 1);
hasCached = true;
}
}

@Override
public boolean hasNextBatch() {
return hasCached;
}

@Override
public BatchData nextBatch() throws IOException {
if (data == null || !data.hasCurrent()) {
throw new IOException("no next batch");
}
hasCached = false;
return data;
}

@Override
public void close() {
}
}

0 comments on commit 907176c

Please sign in to comment.