This is an automated email from the ASF dual-hosted git repository.
rong pushed a commit to branch rel/1.2
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/rel/1.2 by this push:
new 465f379895c Add sorting based on timestamps to the insertTable method
in the rest API v1/v2 (#10661)
465f379895c is described below
commit 465f379895c54ade2a200b07641b343d726be2cb
Author: CloudWise-Lukemiao
<[email protected]>
AuthorDate: Fri Jul 28 17:48:23 2023 +0800
Add sorting based on timestamps to the insertTable method in the rest API
v1/v2 (#10661)
Co-authored-by: Cloudwise_Luke <[email protected]>
---
.../rest/utils/InsertTabletSortDataUtils.java | 69 ++++++++++++++++++++++
.../protocol/rest/v1/impl/RestApiServiceImpl.java | 10 ++++
.../protocol/rest/v2/impl/RestApiServiceImpl.java | 10 ++++
3 files changed, 89 insertions(+)
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/utils/InsertTabletSortDataUtils.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/utils/InsertTabletSortDataUtils.java
new file mode 100644
index 00000000000..fb1db7d4ddf
--- /dev/null
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/utils/InsertTabletSortDataUtils.java
@@ -0,0 +1,69 @@
+/*
+ * 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());
+ }
+}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
index f48f0203b06..ab6096179b5 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
@@ -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,15 @@ 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);
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
index 91595a1f92d..f1e03ba86bc 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
@@ -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,15 @@ 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);