Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2669,8 +2669,7 @@ public TSStatus insertTablets(TSInsertTabletsReq req) {
if (!SESSION_MANAGER.checkLogin(clientSession)) {
return getNotLoggedInStatus();
}
req.setMeasurementsList(
PathUtils.checkIsLegalSingleMeasurementListsAndUpdate(req.getMeasurementsList()));
PathUtils.checkIsLegalSingleMeasurementListsAndUpdateInPlace(req.getMeasurementsList());

// Step 1: transfer from TSInsertTabletsReq to Statement
InsertMultiTabletsStatement statement = StatementGenerator.createStatement(req);
Expand Down Expand Up @@ -2736,8 +2735,7 @@ public TSStatus insertTablet(TSInsertTabletReq req) {

// check whether measurement is legal according to syntax convention (only for tree model)
if (!req.isWriteToTable()) {
req.setMeasurements(
PathUtils.checkIsLegalSingleMeasurementsAndUpdate(req.getMeasurements()));
PathUtils.checkIsLegalSingleMeasurementsAndUpdateInPlace(req.getMeasurements());
}

// Step 1: transfer from TSInsertTabletReq to Statement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,7 @@ public static InsertTabletStatement createStatement(TSInsertTabletReq insertTabl
? new PartialPath(insertTabletReq.getPrefixPath(), false)
: DEVICE_PATH_CACHE.getPartialPath(insertTabletReq.getPrefixPath()));
insertStatement.setMeasurements(insertTabletReq.getMeasurements().toArray(new String[0]));
TSDataType[] dataTypes = new TSDataType[insertTabletReq.types.size()];
for (int i = 0; i < insertTabletReq.types.size(); i++) {
dataTypes[i] = TSDataType.deserialize((byte) insertTabletReq.types.get(i).intValue());
}
TSDataType[] dataTypes = deserializeDataTypes(insertTabletReq.types);
insertStatement.setDataTypes(dataTypes);

TabletDecoder tabletDecoder =
Expand Down Expand Up @@ -397,32 +394,29 @@ public static InsertMultiTabletsStatement createStatement(TSInsertTabletsReq req
final long startTime = System.nanoTime();
// construct insert statement
InsertMultiTabletsStatement insertStatement = new InsertMultiTabletsStatement();
List<InsertTabletStatement> insertTabletStatementList = new ArrayList<>();
for (int i = 0; i < req.prefixPaths.size(); i++) {
int tabletCount = req.prefixPaths.size();
List<InsertTabletStatement> insertTabletStatementList = new ArrayList<>(tabletCount);
for (int i = 0; i < tabletCount; i++) {
List<String> measurements = req.measurementsList.get(i);
TSDataType[] dataTypes = deserializeDataTypes(req.typesList.get(i));
int rowCount = req.sizeList.get(i);
InsertTabletStatement insertTabletStatement = new InsertTabletStatement();
insertTabletStatement.setDevicePath(DEVICE_PATH_CACHE.getPartialPath(req.prefixPaths.get(i)));
insertTabletStatement.setMeasurements(req.measurementsList.get(i).toArray(new String[0]));
insertTabletStatement.setMeasurements(measurements.toArray(new String[0]));
long[] timestamps =
QueryDataSetUtils.readTimesFromBuffer(req.timestampsList.get(i), req.sizeList.get(i));
QueryDataSetUtils.readTimesFromBuffer(req.timestampsList.get(i), rowCount);
if (timestamps.length != 0) {
TimestampPrecisionUtils.checkTimestampPrecision(timestamps[timestamps.length - 1]);
}
insertTabletStatement.setTimes(timestamps);
insertTabletStatement.setColumns(
QueryDataSetUtils.readTabletValuesFromBuffer(
req.valuesList.get(i),
req.typesList.get(i),
req.measurementsList.get(i).size(),
req.sizeList.get(i)));
req.valuesList.get(i), dataTypes, measurements.size(), rowCount));
insertTabletStatement.setBitMaps(
QueryDataSetUtils.readBitMapsFromBuffer(
req.valuesList.get(i), req.measurementsList.get(i).size(), req.sizeList.get(i))
req.valuesList.get(i), measurements.size(), rowCount)
.orElse(null));
insertTabletStatement.setRowCount(req.sizeList.get(i));
TSDataType[] dataTypes = new TSDataType[req.typesList.get(i).size()];
for (int j = 0; j < dataTypes.length; j++) {
dataTypes[j] = TSDataType.deserialize((byte) req.typesList.get(i).get(j).intValue());
}
insertTabletStatement.setRowCount(rowCount);
insertTabletStatement.setDataTypes(dataTypes);
insertTabletStatement.setAligned(req.isAligned);
// skip empty tablet
Expand All @@ -436,6 +430,14 @@ public static InsertMultiTabletsStatement createStatement(TSInsertTabletsReq req
return insertStatement;
}

private static TSDataType[] deserializeDataTypes(List<Integer> serializedDataTypes) {
TSDataType[] dataTypes = new TSDataType[serializedDataTypes.size()];
for (int i = 0; i < dataTypes.length; i++) {
dataTypes[i] = TSDataType.deserialize((byte) serializedDataTypes.get(i).intValue());
}
return dataTypes;
}

public static InsertRowsStatement createStatement(TSInsertRecordsReq req)
throws IllegalPathException, QueryProcessException {
final long startTime = System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class TabletDecoder {
private final List<TSEncoding> columnEncodings;
private final IUnCompressor unCompressor;
private final int rowSize;
private final boolean allValueColumnsPlain;

/**
* @param compressionType the overall compression
Expand All @@ -60,6 +61,7 @@ public TabletDecoder(
this.columnEncodings = columnEncodings;
this.unCompressor = IUnCompressor.getUnCompressor(compressionType);
this.rowSize = rowSize;
this.allValueColumnsPlain = allValueColumnsPlain(dataTypes, columnEncodings);
}

public long[] decodeTime(ByteBuffer buffer) {
Expand Down Expand Up @@ -113,6 +115,10 @@ private long[] decodeColumnForTimeStamp(ByteBuffer buffer, int rowCount) {
TSDataType dataType = TSDataType.INT64;
TSEncoding encodingType = columnEncodings.get(0);

if (encodingType == TSEncoding.PLAIN) {
return QueryDataSetUtils.readTimesFromBuffer(buffer, rowCount);
}

Decoder decoder = Decoder.getDecoderByType(encodingType, dataType);
long[] result = new long[rowCount];
for (int i = 0; i < rowCount; i++) {
Expand All @@ -129,16 +135,40 @@ public Pair<Object[], ByteBuffer> decodeValues(ByteBuffer buffer) {
RPCServiceThriftHandlerMetrics.getInstance().recordCompressionSizeTimer(compressedSize);

long startDecodeTime = System.nanoTime();
Object[] columns = new Object[dataTypes.length];
for (int i = 0; i < dataTypes.length; i++) {
columns[i] = decodeColumn(uncompressed, i);
Object[] columns;
if (allValueColumnsPlain) {
columns =
QueryDataSetUtils.readTabletValuesFromBuffer(
uncompressed, dataTypes, dataTypes.length, rowSize);
} else {
columns = new Object[dataTypes.length];
for (int i = 0; i < dataTypes.length; i++) {
columns[i] = decodeColumn(uncompressed, i);
}
}

RPCServiceThriftHandlerMetrics.getInstance()
.recordDecodeLatencyTimer(System.nanoTime() - startDecodeTime);
return new Pair<>(columns, uncompressed);
}

private static boolean allValueColumnsPlain(
TSDataType[] dataTypes, List<TSEncoding> columnEncodings) {
for (int i = 0; i < dataTypes.length; i++) {
if (columnEncodings.get(i + 1) != TSEncoding.PLAIN || !supportsPlainFastPath(dataTypes[i])) {
return false;
}
}
return true;
}

private static boolean supportsPlainFastPath(TSDataType dataType) {
return switch (dataType) {
case BOOLEAN, DATE, INT32, TIMESTAMP, INT64, FLOAT, DOUBLE, STRING, BLOB, TEXT -> true;
case VECTOR, UNKNOWN, OBJECT -> false;
};
}

private Object decodeColumn(ByteBuffer uncompressed, int columnIndex) {
TSDataType dataType = dataTypes[columnIndex];
TSEncoding encoding = columnEncodings.get(columnIndex + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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;

import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.file.metadata.enums.CompressionType;
import org.apache.tsfile.file.metadata.enums.TSEncoding;
import org.apache.tsfile.utils.Binary;
import org.apache.tsfile.utils.Pair;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Collections;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class TabletDecoderTest {

@Test
public void testDecodeUncompressedPlainTablet() {
TSDataType[] dataTypes = {
TSDataType.BOOLEAN,
TSDataType.INT32,
TSDataType.INT64,
TSDataType.FLOAT,
TSDataType.DOUBLE,
TSDataType.TEXT
};
TabletDecoder decoder =
new TabletDecoder(
CompressionType.UNCOMPRESSED,
dataTypes,
Collections.nCopies(dataTypes.length + 1, TSEncoding.PLAIN),
2);

ByteBuffer timeBuffer = ByteBuffer.allocate(2 * Long.BYTES);
timeBuffer.putLong(1L).putLong(2L).flip();
assertArrayEquals(new long[] {1L, 2L}, decoder.decodeTime(timeBuffer));
assertFalse(timeBuffer.hasRemaining());

ByteBuffer valueBuffer = ByteBuffer.allocate(128);
valueBuffer.put((byte) 1).put((byte) 0);
valueBuffer.putInt(3).putInt(4);
valueBuffer.putLong(5L).putLong(6L);
valueBuffer.putFloat(7.0F).putFloat(8.0F);
valueBuffer.putDouble(9.0D).putDouble(10.0D);
valueBuffer.putInt(1).put((byte) 'a').putInt(1).put((byte) 'b');
valueBuffer.flip();

Pair<Object[], ByteBuffer> result = decoder.decodeValues(valueBuffer);

assertArrayEquals(new boolean[] {true, false}, (boolean[]) result.left[0]);
assertArrayEquals(new int[] {3, 4}, (int[]) result.left[1]);
assertArrayEquals(new long[] {5L, 6L}, (long[]) result.left[2]);
assertArrayEquals(new float[] {7.0F, 8.0F}, (float[]) result.left[3], 0.0F);
assertArrayEquals(new double[] {9.0D, 10.0D}, (double[]) result.left[4], 0.0D);
assertArrayEquals(
new Binary[] {new Binary(new byte[] {'a'}), new Binary(new byte[] {'b'})},
(Binary[]) result.left[5]);
assertEquals(valueBuffer, result.right);
assertFalse(result.right.hasRemaining());
}
}
Loading
Loading