This is an automated email from the ASF dual-hosted git repository.

rong pushed a commit to branch rel/1.1
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/rel/1.1 by this push:
     new 8cce727013c Add sorting based on timestamps to the insertTable method 
in the rest API v1/v2 (#10670)
8cce727013c is described below

commit 8cce727013c9a3c0c9f18c47653283eb05813876
Author: CloudWise-Lukemiao 
<[email protected]>
AuthorDate: Fri Jul 28 17:48:34 2023 +0800

    Add sorting based on timestamps to the insertTable method in the rest API 
v1/v2 (#10670)
    
    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/server/src/main/java/org/apache/iotdb/db/protocol/rest/utils/InsertTabletSortDataUtils.java
 
b/server/src/main/java/org/apache/iotdb/db/protocol/rest/utils/InsertTabletSortDataUtils.java
new file mode 100644
index 00000000000..5f8e37ce780
--- /dev/null
+++ 
b/server/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. index: [1,2,3,4,5], source:[1,0,3,2,4], 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/server/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
index 99139bcd6bf..1d07d835be3 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/protocol/rest/v1/impl/RestApiServiceImpl.java
@@ -31,6 +31,7 @@ import org.apache.iotdb.db.mpp.plan.parser.StatementGenerator;
 import org.apache.iotdb.db.mpp.plan.statement.Statement;
 import org.apache.iotdb.db.mpp.plan.statement.crud.InsertTabletStatement;
 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/server/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
index 2f1b3d1348c..2bbe929487b 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/protocol/rest/v2/impl/RestApiServiceImpl.java
@@ -31,6 +31,7 @@ import org.apache.iotdb.db.mpp.plan.parser.StatementGenerator;
 import org.apache.iotdb.db.mpp.plan.statement.Statement;
 import org.apache.iotdb.db.mpp.plan.statement.crud.InsertTabletStatement;
 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);
 

Reply via email to