Skip to content

Commit

Permalink
fix timegenerator cache bug (#905)
Browse files Browse the repository at this point in the history
* fix timegenerator cache bug
  • Loading branch information
Jialin Qiao committed Mar 13, 2020
1 parent 74f1eb9 commit 2f61528
Show file tree
Hide file tree
Showing 6 changed files with 527 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ public long next() throws IOException {

public Object getValue(Path path, long time) {
for (LeafNode leafNode : leafCache.get(path)) {
if (!leafNode.currentTimeIs(time)) {
continue;
Object value = leafNode.currentValue(time);
if (value != null) {
return value;
}
return leafNode.currentValue(time);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.iotdb.tsfile.read.query.timegenerator.node;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.apache.iotdb.tsfile.read.common.BatchData;
import org.apache.iotdb.tsfile.read.common.TimeColumn;
import org.apache.iotdb.tsfile.read.reader.IBatchReader;
Expand All @@ -27,7 +29,7 @@ public class LeafNode implements Node {

private IBatchReader reader;

private BatchData cacheData;
private List<BatchData> batchDataList = new LinkedList<>();
private TimeColumn cachedTimeSeries;
private boolean hasCached;

Expand All @@ -41,10 +43,11 @@ public boolean hasNextTimeColumn() throws IOException {
return true;
}
while (reader.hasNextBatch()) {
cacheData = reader.nextBatch();
if (cacheData.hasCurrent()) {
BatchData currentBatch = reader.nextBatch();
if (currentBatch.hasCurrent()) {
batchDataList.add(currentBatch);
hasCached = true;
cachedTimeSeries = cacheData.getTimeColumn();
cachedTimeSeries = currentBatch.getTimeColumn();
break;
}
}
Expand All @@ -60,24 +63,23 @@ public TimeColumn nextTimeColumn() throws IOException {
throw new IOException("no more data");
}

/**
* Check whether the current time equals the given time.
*
* @param time the given time
* @return True if the current time equals the given time. False if not.
*/
public boolean currentTimeIs(long time) {
if (!cacheData.hasCurrent()) {
return false;
}
return cacheData.currentTime() == time;
}

/**
* Function for getting the value at the given time.
*/
public Object currentValue(long time) {
return cacheData.getValueInTimestamp(time);
while (!batchDataList.isEmpty()) {
BatchData oldestBatch = batchDataList.get(0);
Object value = oldestBatch.getValueInTimestamp(time);
if (value != null) {
return value;
}
if (!oldestBatch.hasCurrent()) {
batchDataList.remove(0);
} else {
return null;
}
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private boolean checkMemorySizeAndMayFlushGroup() throws IOException {
* function just return false, the Override of IoTDB may return true.
* @throws IOException exception in IO
*/
private boolean flushAllChunkGroups() throws IOException {
public boolean flushAllChunkGroups() throws IOException {
if (recordCount > 0) {
for (Map.Entry<String, IChunkGroupWriter> entry: groupWriters.entrySet()) {
long pos = fileWriter.getPos();
Expand Down
180 changes: 180 additions & 0 deletions tsfile/src/test/java/org/apache/iotdb/tsfile/write/ReadWriteTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* 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.write;

import java.io.File;
import java.io.IOException;
import org.apache.iotdb.tsfile.constant.TestConstant;
import org.apache.iotdb.tsfile.exception.write.WriteProcessException;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
import org.apache.iotdb.tsfile.read.ReadOnlyTsFile;
import org.apache.iotdb.tsfile.read.TsFileSequenceReader;
import org.apache.iotdb.tsfile.read.common.Path;
import org.apache.iotdb.tsfile.read.expression.IExpression;
import org.apache.iotdb.tsfile.read.expression.QueryExpression;
import org.apache.iotdb.tsfile.read.expression.impl.BinaryExpression;
import org.apache.iotdb.tsfile.read.expression.impl.GlobalTimeExpression;
import org.apache.iotdb.tsfile.read.expression.impl.SingleSeriesExpression;
import org.apache.iotdb.tsfile.read.filter.TimeFilter;
import org.apache.iotdb.tsfile.read.filter.ValueFilter;
import org.apache.iotdb.tsfile.read.filter.basic.Filter;
import org.apache.iotdb.tsfile.read.filter.factory.FilterFactory;
import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet;
import org.apache.iotdb.tsfile.write.record.TSRecord;
import org.apache.iotdb.tsfile.write.record.datapoint.DataPoint;
import org.apache.iotdb.tsfile.write.record.datapoint.FloatDataPoint;
import org.apache.iotdb.tsfile.write.record.datapoint.IntDataPoint;
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import org.apache.iotdb.tsfile.write.schema.Schema;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ReadWriteTest {

private String tsfilePath = TestConstant.BASE_OUTPUT_PATH.concat("TestValueFilter.tsfile");

@Before
public void before() throws IOException, WriteProcessException {
writeFile(tsfilePath);
}

@After
public void after() {
File file = new File(tsfilePath);
if (file.exists()) {
file.delete();
}
}

@Test
public void testFilterAnd() throws IOException {
Filter timeFilter = FilterFactory.and(TimeFilter.gtEq(1L), TimeFilter.ltEq(8L));
IExpression timeExpression = new GlobalTimeExpression(timeFilter);

IExpression valueExpression = BinaryExpression
.and(new SingleSeriesExpression(new Path("d1.s1"), ValueFilter.gt(1.0f)),
new SingleSeriesExpression(new Path("d1.s2"), ValueFilter.lt(22)));

IExpression finalExpression = BinaryExpression.and(valueExpression, timeExpression);

QueryExpression queryExpression = QueryExpression.create().addSelectedPath(new Path("d1.s1"))
.addSelectedPath(new Path("d1.s2")).setExpression(finalExpression);

try (TsFileSequenceReader fileReader = new TsFileSequenceReader(tsfilePath)) {
ReadOnlyTsFile readOnlyTsFile = new ReadOnlyTsFile(fileReader);
QueryDataSet dataSet = readOnlyTsFile.query(queryExpression);
int i = 0;
String[] expected = new String[]{
"1\t1.2\t20",
"3\t1.4\t21",
"4\t1.2\t20",
"6\t7.2\t10",
"7\t6.2\t20"};
while (dataSet.hasNext()) {
Assert.assertEquals(expected[i], dataSet.next().toString());
i++;
}
Assert.assertEquals(5, i);
}
}



private void writeFile(String tsfilePath) throws IOException, WriteProcessException {
File f = new File(tsfilePath);
if (f.exists()) {
f.delete();
}

Schema schema = new Schema();
schema.registerMeasurement(new MeasurementSchema("s1", TSDataType.FLOAT, TSEncoding.RLE));
schema.registerMeasurement(new MeasurementSchema("s2", TSDataType.INT32, TSEncoding.TS_2DIFF));
schema.registerMeasurement(new MeasurementSchema("s3", TSDataType.INT32, TSEncoding.TS_2DIFF));

TsFileWriter tsFileWriter = new TsFileWriter(f, schema);

// construct TSRecord
TSRecord tsRecord = new TSRecord(1, "d1");
DataPoint dPoint1 = new FloatDataPoint("s1", 1.2f);
DataPoint dPoint2 = new IntDataPoint("s2", 20);
DataPoint dPoint3;
tsRecord.addTuple(dPoint1);
tsRecord.addTuple(dPoint2);

// write a TSRecord to TsFile
tsFileWriter.write(tsRecord);

tsRecord = new TSRecord(2, "d1");
dPoint2 = new IntDataPoint("s2", 20);
dPoint3 = new IntDataPoint("s3", 50);
tsRecord.addTuple(dPoint2);
tsRecord.addTuple(dPoint3);
tsFileWriter.write(tsRecord);

tsRecord = new TSRecord(3, "d1");
dPoint1 = new FloatDataPoint("s1", 1.4f);
dPoint2 = new IntDataPoint("s2", 21);
tsRecord.addTuple(dPoint1);
tsRecord.addTuple(dPoint2);
tsFileWriter.write(tsRecord);

tsRecord = new TSRecord(4, "d1");
dPoint1 = new FloatDataPoint("s1", 1.2f);
dPoint2 = new IntDataPoint("s2", 20);
dPoint3 = new IntDataPoint("s3", 51);
tsRecord.addTuple(dPoint1);
tsRecord.addTuple(dPoint2);
tsRecord.addTuple(dPoint3);
tsFileWriter.write(tsRecord);

tsRecord = new TSRecord(6, "d1");
dPoint1 = new FloatDataPoint("s1", 7.2f);
dPoint2 = new IntDataPoint("s2", 10);
dPoint3 = new IntDataPoint("s3", 11);
tsRecord.addTuple(dPoint1);
tsRecord.addTuple(dPoint2);
tsRecord.addTuple(dPoint3);
tsFileWriter.write(tsRecord);

tsRecord = new TSRecord(7, "d1");
dPoint1 = new FloatDataPoint("s1", 6.2f);
dPoint2 = new IntDataPoint("s2", 20);
dPoint3 = new IntDataPoint("s3", 21);
tsRecord.addTuple(dPoint1);
tsRecord.addTuple(dPoint2);
tsRecord.addTuple(dPoint3);
tsFileWriter.write(tsRecord);

tsRecord = new TSRecord(8, "d1");
dPoint1 = new FloatDataPoint("s1", 9.2f);
dPoint2 = new IntDataPoint("s2", 30);
dPoint3 = new IntDataPoint("s3", 31);
tsRecord.addTuple(dPoint1);
tsRecord.addTuple(dPoint2);
tsRecord.addTuple(dPoint3);
tsFileWriter.write(tsRecord);


// close TsFile
tsFileWriter.close();
}
}
Loading

0 comments on commit 2f61528

Please sign in to comment.