未验证 提交 76d66ed5 编写于 作者: C CloudWise-Lukemiao 提交者: GitHub

Add sorting based on timestamps to the insertTable method in the rest API v1/v2 (#10660)

Co-authored-by: NCloudwise_Luke <282583553@qq.com>
上级 7b02fea4
/*
* 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.protocol.rest.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class InsertTabletSortDataUtils {
public static boolean checkSorted(List<Long> times) {
for (int i = 1; i < times.size(); i++) {
if (times.get(i) < times.get(i - 1)) {
return false;
}
}
return true;
}
public static int[] sortTimeStampList(List<Long> list) {
int n = list.size();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = list.get(i);
}
Arrays.sort(arr);
int[] indices = new int[n];
for (int i = 0; i < n; i++) {
indices[i] = list.indexOf(arr[i]);
}
return indices;
}
public static <T> List<List<T>> sortList(List<List<T>> values, int[] index, int num) {
List<List<T>> sortedValues = new ArrayList<>();
for (int i = 0; i < num; i++) {
sortedValues.add(sortValueList(values.get(i), index));
}
return sortedValues;
}
/**
* Sort the input source list.
*
* <p>e.g. source:[1,0,3,2,4], index: [1,2,3,4,5], return : [2,1,4,3,5]
*
* @param source Input list
* @param index return order
* @param <T> Input type
* @return ordered list
*/
private static <T> List<T> sortValueList(List<T> source, int[] index) {
return Arrays.stream(index).mapToObj(source::get).collect(Collectors.toList());
}
}
......@@ -21,6 +21,7 @@ import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.conf.rest.IoTDBRestServiceDescriptor;
import org.apache.iotdb.db.protocol.rest.handler.AuthorizationHandler;
import org.apache.iotdb.db.protocol.rest.utils.InsertTabletSortDataUtils;
import org.apache.iotdb.db.protocol.rest.v1.RestApiService;
import org.apache.iotdb.db.protocol.rest.v1.handler.ExceptionHandler;
import org.apache.iotdb.db.protocol.rest.v1.handler.ExecuteStatementHandler;
......@@ -191,6 +192,16 @@ public class RestApiServiceImpl extends RestApiService {
try {
RequestValidationHandler.validateInsertTabletRequest(insertTabletRequest);
if (!InsertTabletSortDataUtils.checkSorted(insertTabletRequest.getTimestamps())) {
int[] index =
InsertTabletSortDataUtils.sortTimeStampList(insertTabletRequest.getTimestamps());
insertTabletRequest.getTimestamps().sort(Long::compareTo);
insertTabletRequest.setValues(
InsertTabletSortDataUtils.sortList(
insertTabletRequest.getValues(), index, insertTabletRequest.getDataTypes().size()));
}
InsertTabletStatement insertTabletStatement =
StatementConstructionHandler.constructInsertTabletStatement(insertTabletRequest);
......
......@@ -21,6 +21,7 @@ import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.conf.rest.IoTDBRestServiceDescriptor;
import org.apache.iotdb.db.protocol.rest.handler.AuthorizationHandler;
import org.apache.iotdb.db.protocol.rest.utils.InsertTabletSortDataUtils;
import org.apache.iotdb.db.protocol.rest.v2.RestApiService;
import org.apache.iotdb.db.protocol.rest.v2.handler.ExceptionHandler;
import org.apache.iotdb.db.protocol.rest.v2.handler.ExecuteStatementHandler;
......@@ -191,6 +192,16 @@ public class RestApiServiceImpl extends RestApiService {
try {
RequestValidationHandler.validateInsertTabletRequest(insertTabletRequest);
if (!InsertTabletSortDataUtils.checkSorted(insertTabletRequest.getTimestamps())) {
int[] index =
InsertTabletSortDataUtils.sortTimeStampList(insertTabletRequest.getTimestamps());
insertTabletRequest.getTimestamps().sort(Long::compareTo);
insertTabletRequest.setValues(
InsertTabletSortDataUtils.sortList(
insertTabletRequest.getValues(), index, insertTabletRequest.getDataTypes().size()));
}
InsertTabletStatement insertTabletStatement =
StatementConstructionHandler.constructInsertTabletStatement(insertTabletRequest);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册