You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/10/17 15:48:28 UTC

[GitHub] [iotdb] jun0315 opened a new pull request #4172: [IOTDB-1689]Compatibility of Apache IoTDB with InfluxDB - Insert Data

jun0315 opened a new pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172


   see [IOTDB-1689](https://issues.apache.org/jira/browse/IOTDB-1806)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733505827



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/IoTDBInfluxDB.java
##########
@@ -173,40 +165,71 @@ public void write(
       final ConsistencyLevel consistency,
       final TimeUnit precision,
       final List<String> records) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+    throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
   }
 
   @Override
   public void write(final int udpPort, final String records) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+    throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
   }
 
   @Override
   public void write(final int udpPort, final List<String> records) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+    throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public QueryResult query(final Query query) {
+    throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public void createDatabase(final String name) {
+    ParameterUtils.checkNonEmptyString(name, "database name");
+    try {
+      session.setStorageGroup("root." + name);
+    } catch (IoTDBConnectionException e) {
+      throw new InfluxDBException(e.getMessage());
+    } catch (StatementExecutionException e) {
+      if (e.getStatusCode() != 300) {

Review comment:
       ```suggestion
         // e.getStatusCode() == 300 if the database is already existed.
         if (e.getStatusCode() != 300) {
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43720511/badge)](https://coveralls.io/builds/43720511)
   
   Coverage decreased (-0.5%) to 67.268% when pulling **7e47caad5dfd4c94c71e205edce03ceae044726e on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43724446/badge)](https://coveralls.io/builds/43724446)
   
   Coverage decreased (-0.6%) to 67.236% when pulling **0fa2145cdc600aa41dc5df6fb98d99bcaea03f69 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43665839/badge)](https://coveralls.io/builds/43665839)
   
   Coverage decreased (-0.5%) to 67.278% when pulling **52a1e6c0b61e67187a9c2901a712694d9d00a5eb on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43602713/badge)](https://coveralls.io/builds/43602713)
   
   Coverage decreased (-0.3%) to 67.462% when pulling **0c7d2987573eea8f0d1a0b26d6d2f6f50fa587d3 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls commented on pull request #4172: [IOTDB-1689]Compatibility of Apache IoTDB with InfluxDB - Insert Data

Posted by GitBox <gi...@apache.org>.
coveralls commented on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43557705/badge)](https://coveralls.io/builds/43557705)
   
   Coverage increased (+0.01%) to 67.805% when pulling **1b5dab897243e94768223e6669df87836ab093ca on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43668445/badge)](https://coveralls.io/builds/43668445)
   
   Coverage decreased (-0.6%) to 67.24% when pulling **c35c3314e4e4c80ababc30ae0d83bebe4f8ec780 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733294249



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/imp/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.influxdb.protocol.imp;
+
+import org.apache.iotdb.influxdb.IoTDBInfluxDBUtils;
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (!database.equals(this.databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (database != null && !database.equals(databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+    List<String> deviceIds = new ArrayList<>();
+    List<Long> times = new ArrayList<>();
+    List<List<String>> measurementsList = new ArrayList<>();
+    List<List<TSDataType>> typesList = new ArrayList<>();
+    List<List<Object>> valuesList = new ArrayList<>();
+    for (Point point : batchPoints.getPoints()) {
+      IoTDBRecord ioTDBRecord = generatePointRecord(point);
+      deviceIds.add(ioTDBRecord.getDeviceId());
+      times.add(ioTDBRecord.getTime());
+      measurementsList.add(ioTDBRecord.getMeasurements());
+      typesList.add(ioTDBRecord.getTypes());
+      valuesList.add(ioTDBRecord.getValues());
+    }
+    try {
+      session.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  /**
+   * when the database changes, update the database related information, that is, obtain the list
+   * and order of all tags corresponding to the database from iotdb
+   *
+   * @param database update database name
+   */
+  private void updateDatabase(String database) {
+    this.databaseCache.setDatabase(database);
+    try {
+      SessionDataSet result =
+          session.executeQueryStatement(
+              "select * from root.TAG_INFO where database_name="
+                  + String.format("\"%s\"", database));
+      Map<String, Map<String, Integer>> measurementTagOrder = new HashMap<>();
+      while (result.hasNext()) {
+        List<Field> fields = result.next().getFields();
+        String measurementName = fields.get(1).getStringValue();
+        Map<String, Integer> tagOrder;
+        if (measurementTagOrder.containsKey(measurementName)) {
+          tagOrder = measurementTagOrder.get(measurementName);
+        } else {
+          tagOrder = new HashMap<>();
+        }
+        tagOrder.put(fields.get(2).getStringValue(), fields.get(3).getIntV());
+        measurementTagOrder.put(measurementName, tagOrder);
+      }
+      this.databaseCache.setMeasurementTagOrder(measurementTagOrder);
+    } catch (StatementExecutionException e) {
+      // at first execution, tag_INFO table is not created, intercept the error
+      if (e.getStatusCode() != 411) {
+        throw new InfluxDBException(e.getMessage());
+      }
+    } catch (IoTDBConnectionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  private IoTDBRecord generatePointRecord(Point point) {
+    String measurement = null;
+    Map<String, String> tags = new HashMap<>();
+    Map<String, Object> fields = new HashMap<>();
+    Long time = null;
+    java.lang.reflect.Field[] reflectFields = point.getClass().getDeclaredFields();
+    // Get the property of point in influxdb by reflection
+    for (java.lang.reflect.Field reflectField : reflectFields) {
+      reflectField.setAccessible(true);
+      try {
+        if (reflectField.getType().getName().equalsIgnoreCase("java.util.Map")
+            && reflectField.getName().equalsIgnoreCase("fields")) {
+          fields = (Map<String, Object>) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.util.Map")
+            && reflectField.getName().equalsIgnoreCase("tags")) {
+          tags = (Map<String, String>) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.lang.String")
+            && reflectField.getName().equalsIgnoreCase("measurement")) {
+          measurement = (String) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.lang.Number")
+            && reflectField.getName().equalsIgnoreCase("time")) {
+          time = (Long) reflectField.get(point);
+        }
+        // set current time
+        if (time == null) {
+          time = System.currentTimeMillis();
+        }
+      } catch (IllegalAccessException e) {
+        throw new IllegalArgumentException(e.getMessage());
+      }
+    }
+    IoTDBInfluxDBUtils.checkNonEmptyString(measurement, "measurement name");
+
+    String path = generatePath(measurement, tags);
+
+    List<String> measurements = new ArrayList<>();
+    List<TSDataType> types = new ArrayList<>();
+    List<Object> values = new ArrayList<>();
+    for (Map.Entry<String, Object> entry : fields.entrySet()) {
+      measurements.add(entry.getKey());
+      Object value = entry.getValue();
+      types.add(IoTDBInfluxDBUtils.normalTypeToTSDataType(value));
+      values.add(value);
+    }
+    return new IoTDBRecord(path, time, measurements, types, values);
+  }
+
+  private String generatePath(String measurement, Map<String, String> tags) {
+    String database = this.databaseCache.getDatabase();
+    Map<String, Map<String, Integer>> measurementTagOrder =
+        this.databaseCache.getMeasurementTagOrder();
+    Map<String, Integer> tagOrders = measurementTagOrder.get(measurement);
+    if (tagOrders == null) {
+      tagOrders = new HashMap<>();
+    }
+    int measurementTagNum = tagOrders.size();
+    // The actual number of tags at the time of current insertion
+    Map<Integer, String> realTagOrders = new HashMap<>();
+    for (Map.Entry<String, String> entry : tags.entrySet()) {

Review comment:
       ```suggestion
       for (Map.Entry<String, String> tag : tags.entrySet()) {
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43724572/badge)](https://coveralls.io/builds/43724572)
   
   Coverage decreased (-0.5%) to 67.241% when pulling **26b3f5d360646d7e30c757699ff385507ecaf9c5 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43723591/badge)](https://coveralls.io/builds/43723591)
   
   Coverage decreased (-0.6%) to 67.237% when pulling **c683fa1a3b9091efc501510cea306a2af94f10bb on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43724305/badge)](https://coveralls.io/builds/43724305)
   
   Coverage decreased (-0.5%) to 67.242% when pulling **d9483d123f8891c4237724b39aa14e68b48dcf1d on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43724568/badge)](https://coveralls.io/builds/43724568)
   
   Coverage decreased (-0.5%) to 67.257% when pulling **26b3f5d360646d7e30c757699ff385507ecaf9c5 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733281613



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/example/InfluxDBExample.java
##########
@@ -55,6 +55,7 @@ private static void insertData() {
     builder.fields(fields);
     builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
     Point point = builder.build();
+    point.lineProtocol();

Review comment:
       ```suggestion
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733509795



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/impl/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.influxdb.protocol.impl;
+
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.influxdb.protocol.util.DataTypeUtils;
+import org.apache.iotdb.influxdb.protocol.util.ParameterUtils;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (databaseCache.getDatabaseTagOrders().get(database) == null) {

Review comment:
       ```suggestion
       if (database != null && databaseCache.getDatabaseTagOrders().get(database) == null) {
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733512833



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/impl/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.influxdb.protocol.impl;
+
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.influxdb.protocol.util.DataTypeUtils;
+import org.apache.iotdb.influxdb.protocol.util.ParameterUtils;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (databaseCache.getDatabaseTagOrders().get(database) == null) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (databaseCache.getDatabaseTagOrders().get(database) == null) {
+      updateDatabase(database);
+    }
+    List<String> deviceIds = new ArrayList<>();
+    List<Long> times = new ArrayList<>();
+    List<List<String>> measurementsList = new ArrayList<>();
+    List<List<TSDataType>> typesList = new ArrayList<>();
+    List<List<Object>> valuesList = new ArrayList<>();
+    for (Point point : batchPoints.getPoints()) {
+      IoTDBRecord iotdbRecord = generatePointRecord(point);
+      deviceIds.add(iotdbRecord.getDeviceId());
+      times.add(iotdbRecord.getTime());
+      measurementsList.add(iotdbRecord.getMeasurements());
+      typesList.add(iotdbRecord.getTypes());
+      valuesList.add(iotdbRecord.getValues());
+    }
+    try {
+      session.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  /**
+   * when the database changes, update the database related information, that is, obtain the list
+   * and order of all tags corresponding to the database from iotdb
+   *
+   * @param database update database name
+   */
+  private void updateDatabase(String database) {
+    try {
+      SessionDataSet result =
+          session.executeQueryStatement(
+              "select database_name,measurement_name,tag_name,tag_order from root.TAG_INFO where database_name="
+                  + String.format("\"%s\"", database));
+      Map<String, Map<String, Integer>> measurementTagOrders = new HashMap<>();
+      while (result.hasNext()) {
+        List<Field> fields = result.next().getFields();
+        String measurementName = fields.get(1).getStringValue();
+        Map<String, Integer> tagOrder;
+        if (measurementTagOrders.containsKey(measurementName)) {
+          tagOrder = measurementTagOrders.get(measurementName);
+        } else {
+          tagOrder = new HashMap<>();
+        }
+        tagOrder.put(fields.get(2).getStringValue(), fields.get(3).getIntV());
+        measurementTagOrders.put(measurementName, tagOrder);
+      }
+      this.databaseCache.updateDatabaseOrders(database, measurementTagOrders);
+      this.databaseCache.setCurrentDatabase(database);
+    } catch (StatementExecutionException e) {
+      // at first execution, tag_INFO table is not created, intercept the error

Review comment:
       ```suggestion
         // at first execution, TAG_INFO table is not created, intercept the error
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43666734/badge)](https://coveralls.io/builds/43666734)
   
   Coverage decreased (-0.6%) to 67.23% when pulling **83d3753d33d8dcb2af21dc946f906b878c443adf on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43722451/badge)](https://coveralls.io/builds/43722451)
   
   Coverage decreased (-0.5%) to 67.25% when pulling **13104df5278295868c8000a429e743938677f834 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1806] Compatibility of Apache IoTDB with InfluxDB - Data Point Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43724810/badge)](https://coveralls.io/builds/43724810)
   
   Coverage decreased (-0.5%) to 67.244% when pulling **378881da486cd3c9aea8579f430320ec191760f1 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733297235



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/imp/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.influxdb.protocol.imp;
+
+import org.apache.iotdb.influxdb.IoTDBInfluxDBUtils;
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (!database.equals(this.databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (database != null && !database.equals(databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+    List<String> deviceIds = new ArrayList<>();
+    List<Long> times = new ArrayList<>();
+    List<List<String>> measurementsList = new ArrayList<>();
+    List<List<TSDataType>> typesList = new ArrayList<>();
+    List<List<Object>> valuesList = new ArrayList<>();
+    for (Point point : batchPoints.getPoints()) {
+      IoTDBRecord ioTDBRecord = generatePointRecord(point);
+      deviceIds.add(ioTDBRecord.getDeviceId());
+      times.add(ioTDBRecord.getTime());
+      measurementsList.add(ioTDBRecord.getMeasurements());
+      typesList.add(ioTDBRecord.getTypes());
+      valuesList.add(ioTDBRecord.getValues());
+    }
+    try {
+      session.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  /**
+   * when the database changes, update the database related information, that is, obtain the list
+   * and order of all tags corresponding to the database from iotdb
+   *
+   * @param database update database name
+   */
+  private void updateDatabase(String database) {
+    this.databaseCache.setDatabase(database);
+    try {
+      SessionDataSet result =
+          session.executeQueryStatement(
+              "select * from root.TAG_INFO where database_name="
+                  + String.format("\"%s\"", database));
+      Map<String, Map<String, Integer>> measurementTagOrder = new HashMap<>();
+      while (result.hasNext()) {
+        List<Field> fields = result.next().getFields();
+        String measurementName = fields.get(1).getStringValue();
+        Map<String, Integer> tagOrder;
+        if (measurementTagOrder.containsKey(measurementName)) {
+          tagOrder = measurementTagOrder.get(measurementName);
+        } else {
+          tagOrder = new HashMap<>();
+        }
+        tagOrder.put(fields.get(2).getStringValue(), fields.get(3).getIntV());
+        measurementTagOrder.put(measurementName, tagOrder);
+      }
+      this.databaseCache.setMeasurementTagOrder(measurementTagOrder);
+    } catch (StatementExecutionException e) {
+      // at first execution, tag_INFO table is not created, intercept the error
+      if (e.getStatusCode() != 411) {
+        throw new InfluxDBException(e.getMessage());
+      }
+    } catch (IoTDBConnectionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  private IoTDBRecord generatePointRecord(Point point) {
+    String measurement = null;
+    Map<String, String> tags = new HashMap<>();
+    Map<String, Object> fields = new HashMap<>();
+    Long time = null;
+    java.lang.reflect.Field[] reflectFields = point.getClass().getDeclaredFields();
+    // Get the property of point in influxdb by reflection
+    for (java.lang.reflect.Field reflectField : reflectFields) {
+      reflectField.setAccessible(true);
+      try {
+        if (reflectField.getType().getName().equalsIgnoreCase("java.util.Map")
+            && reflectField.getName().equalsIgnoreCase("fields")) {
+          fields = (Map<String, Object>) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.util.Map")
+            && reflectField.getName().equalsIgnoreCase("tags")) {
+          tags = (Map<String, String>) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.lang.String")
+            && reflectField.getName().equalsIgnoreCase("measurement")) {
+          measurement = (String) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.lang.Number")
+            && reflectField.getName().equalsIgnoreCase("time")) {
+          time = (Long) reflectField.get(point);
+        }
+        // set current time
+        if (time == null) {
+          time = System.currentTimeMillis();
+        }
+      } catch (IllegalAccessException e) {
+        throw new IllegalArgumentException(e.getMessage());
+      }
+    }
+    IoTDBInfluxDBUtils.checkNonEmptyString(measurement, "measurement name");
+
+    String path = generatePath(measurement, tags);
+
+    List<String> measurements = new ArrayList<>();
+    List<TSDataType> types = new ArrayList<>();
+    List<Object> values = new ArrayList<>();
+    for (Map.Entry<String, Object> entry : fields.entrySet()) {
+      measurements.add(entry.getKey());
+      Object value = entry.getValue();
+      types.add(IoTDBInfluxDBUtils.normalTypeToTSDataType(value));
+      values.add(value);
+    }
+    return new IoTDBRecord(path, time, measurements, types, values);
+  }
+
+  private String generatePath(String measurement, Map<String, String> tags) {

Review comment:
       先数据库后内存




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43720422/badge)](https://coveralls.io/builds/43720422)
   
   Coverage decreased (-0.5%) to 67.243% when pulling **7e47caad5dfd4c94c71e205edce03ceae044726e on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733286925



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/IoTDBInfluxDB.java
##########
@@ -85,57 +88,50 @@ private void openSession() {
 
   @Override
   public void write(final Point point) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+    write(null, null, point);
   }
 
   @Override
-  public QueryResult query(final Query query) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+  public void write(final String database, final String retentionPolicy, final Point point) {
+    BatchPoints batchPoints =
+        BatchPoints.database(database).retentionPolicy(retentionPolicy).build();
+    batchPoints.point(point);
+    write(batchPoints);
   }
 
   @Override
-  public void createDatabase(final String name) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+  public void write(final int udpPort, final Point point) {
+    write(null, null, point);
   }
 
   @Override
-  public void deleteDatabase(final String name) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+  public void write(final BatchPoints batchPoints) {
+    influxDBService.writePoints(
+        batchPoints.getDatabase(),
+        batchPoints.getRetentionPolicy(),
+        TimeUtil.toTimePrecision(batchPoints.getPrecision()),
+        batchPoints.getConsistency().value(),
+        batchPoints);
   }
 
   @Override
-  public InfluxDB setDatabase(final String database) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+  public void writeWithRetry(final BatchPoints batchPoints) {
+    influxDBService.writePoints(
+        batchPoints.getDatabase(),
+        batchPoints.getRetentionPolicy(),
+        TimeUtil.toTimePrecision(batchPoints.getPrecision()),
+        batchPoints.getConsistency().value(),
+        batchPoints);

Review comment:
       ```suggestion
       throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689]Compatibility of Apache IoTDB with InfluxDB - Insert Data

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43557729/badge)](https://coveralls.io/builds/43557729)
   
   Coverage increased (+0.03%) to 67.816% when pulling **1b5dab897243e94768223e6669df87836ab093ca on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733281385



##########
File path: influxdb-protocol/pom.xml
##########
@@ -51,7 +51,7 @@
         <dependency>
             <groupId>org.influxdb</groupId>
             <artifactId>influxdb-java</artifactId>
-            <version>2.21</version>
+            <version>LATEST</version>

Review comment:
       ```suggestion
               <version>2.21</version>
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733290829



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/cache/DatabaseCache.java
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.influxdb.protocol.cache;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class DatabaseCache {
+  // Tag list and order under current measurement
+  private Map<String, Integer> tagOrders = new HashMap<>();
+
+  private Map<String, Map<String, Integer>> measurementTagOrder = new HashMap<>();
+
+  // Database currently selected by influxdb
+  private String database;
+
+  public DatabaseCache() {}

Review comment:
       LRU




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43667079/badge)](https://coveralls.io/builds/43667079)
   
   Coverage decreased (-0.6%) to 67.236% when pulling **83d3753d33d8dcb2af21dc946f906b878c443adf on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43602212/badge)](https://coveralls.io/builds/43602212)
   
   Coverage decreased (-0.3%) to 67.487% when pulling **0c7d2987573eea8f0d1a0b26d6d2f6f50fa587d3 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43722427/badge)](https://coveralls.io/builds/43722427)
   
   Coverage decreased (-0.5%) to 67.262% when pulling **13104df5278295868c8000a429e743938677f834 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43722376/badge)](https://coveralls.io/builds/43722376)
   
   Coverage decreased (-0.5%) to 67.262% when pulling **13104df5278295868c8000a429e743938677f834 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43665883/badge)](https://coveralls.io/builds/43665883)
   
   Coverage decreased (-0.6%) to 67.234% when pulling **52a1e6c0b61e67187a9c2901a712694d9d00a5eb on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43665933/badge)](https://coveralls.io/builds/43665933)
   
   Coverage decreased (-0.5%) to 67.254% when pulling **abaf245480100bdff50fc78a5f2614de3d75b1e8 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733291469



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/imp/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.influxdb.protocol.imp;
+
+import org.apache.iotdb.influxdb.IoTDBInfluxDBUtils;
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (!database.equals(this.databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (database != null && !database.equals(databaseCache.getDatabase())) {

Review comment:
       Why database != null




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733288630



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/imp/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.influxdb.protocol.imp;
+
+import org.apache.iotdb.influxdb.IoTDBInfluxDBUtils;
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (!database.equals(this.databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (database != null && !database.equals(databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+    List<String> deviceIds = new ArrayList<>();
+    List<Long> times = new ArrayList<>();
+    List<List<String>> measurementsList = new ArrayList<>();
+    List<List<TSDataType>> typesList = new ArrayList<>();
+    List<List<Object>> valuesList = new ArrayList<>();
+    for (Point point : batchPoints.getPoints()) {
+      IoTDBRecord ioTDBRecord = generatePointRecord(point);
+      deviceIds.add(ioTDBRecord.getDeviceId());
+      times.add(ioTDBRecord.getTime());
+      measurementsList.add(ioTDBRecord.getMeasurements());
+      typesList.add(ioTDBRecord.getTypes());
+      valuesList.add(ioTDBRecord.getValues());
+    }
+    try {
+      session.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  /**
+   * when the database changes, update the database related information, that is, obtain the list
+   * and order of all tags corresponding to the database from iotdb
+   *
+   * @param database update database name
+   */
+  private void updateDatabase(String database) {
+    this.databaseCache.setDatabase(database);
+    try {
+      SessionDataSet result =
+          session.executeQueryStatement(
+              "select * from root.TAG_INFO where database_name="
+                  + String.format("\"%s\"", database));
+      Map<String, Map<String, Integer>> measurementTagOrder = new HashMap<>();
+      while (result.hasNext()) {
+        List<Field> fields = result.next().getFields();
+        String measurementName = fields.get(1).getStringValue();
+        Map<String, Integer> tagOrder;
+        if (measurementTagOrder.containsKey(measurementName)) {
+          tagOrder = measurementTagOrder.get(measurementName);
+        } else {
+          tagOrder = new HashMap<>();
+        }
+        tagOrder.put(fields.get(2).getStringValue(), fields.get(3).getIntV());
+        measurementTagOrder.put(measurementName, tagOrder);
+      }
+      this.databaseCache.setMeasurementTagOrder(measurementTagOrder);

Review comment:
       ```suggestion
       try {
         SessionDataSet result =
             session.executeQueryStatement(
                 "select * from root.TAG_INFO where database_name="
                     + String.format("\"%s\"", database));
         Map<String, Map<String, Integer>> measurementTagOrder = new HashMap<>();
         while (result.hasNext()) {
           List<Field> fields = result.next().getFields();
           String measurementName = fields.get(1).getStringValue();
           Map<String, Integer> tagOrder;
           if (measurementTagOrder.containsKey(measurementName)) {
             tagOrder = measurementTagOrder.get(measurementName);
           } else {
             tagOrder = new HashMap<>();
           }
           tagOrder.put(fields.get(2).getStringValue(), fields.get(3).getIntV());
           measurementTagOrder.put(measurementName, tagOrder);
         }
         this.databaseCache.setMeasurementTagOrder(measurementTagOrder);
         this.databaseCache.setDatabase(database);
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r735120422



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/example/InfluxDBExample.java
##########
@@ -36,6 +36,7 @@
 
   public static void main(String[] args) throws Exception {
     influxDB = IoTDBInfluxDBFactory.connect("http://127.0.0.1:6667", "root", "root");
+    InfluxDB influxDB1 = IoTDBInfluxDBFactory.connect("http://127.0.0.1:6667", "root", "root");

Review comment:
       ```suggestion
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733286086



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/IoTDBInfluxDB.java
##########
@@ -173,40 +169,71 @@ public void write(
       final ConsistencyLevel consistency,
       final TimeUnit precision,
       final List<String> records) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+    throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
   }
 
   @Override
   public void write(final int udpPort, final String records) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+    throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
   }
 
   @Override
   public void write(final int udpPort, final List<String> records) {
-    throw new UnsupportedOperationException(METHOD_NOT_SUPPORTED);
+    throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public QueryResult query(final Query query) {
+    throw new UnsupportedOperationException(InfluxDBConstant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public void createDatabase(final String name) {
+    IoTDBInfluxDBUtils.checkNonEmptyString(name, "database name");
+    try {
+      session.setStorageGroup("root." + name);
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      if (!(e instanceof StatementExecutionException)

Review comment:
       catch ... catch ...




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733281763



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/constant/InfluxDBConstant.java
##########
@@ -0,0 +1,27 @@
+/*
+ * 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.influxdb.protocol.constant;
+
+public class InfluxDBConstant {
+
+  public static final String METHOD_NOT_SUPPORTED = "Method not supported.";
+
+  public static final String placeholder = "PH";

Review comment:
       ```suggestion
     public static final String PLACE_HOLDER = "PH";
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733284104



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/IoTDBInfluxDBUtils.java
##########
@@ -33,4 +37,26 @@ public static void checkNonEmptyString(String string, String name)
       throw new IllegalArgumentException("Expecting a non-empty string for " + name);
     }
   }
+
+  /**
+   * convert normal type to a type
+   *
+   * @param value need to convert value
+   * @return corresponding TSDataType
+   */
+  public static TSDataType normalTypeToTSDataType(Object value) {

Review comment:
       DataTypeUtils




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733284153



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/IoTDBInfluxDBUtils.java
##########
@@ -33,4 +37,26 @@ public static void checkNonEmptyString(String string, String name)
       throw new IllegalArgumentException("Expecting a non-empty string for " + name);

Review comment:
       ParameterUtils




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43723573/badge)](https://coveralls.io/builds/43723573)
   
   Coverage decreased (-0.5%) to 67.273% when pulling **c683fa1a3b9091efc501510cea306a2af94f10bb on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43724469/badge)](https://coveralls.io/builds/43724469)
   
   Coverage decreased (-0.5%) to 67.245% when pulling **26b3f5d360646d7e30c757699ff385507ecaf9c5 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43666167/badge)](https://coveralls.io/builds/43666167)
   
   Coverage decreased (-0.6%) to 67.239% when pulling **83d3753d33d8dcb2af21dc946f906b878c443adf on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43601030/badge)](https://coveralls.io/builds/43601030)
   
   Coverage decreased (-0.3%) to 67.461% when pulling **28880acd7a6c6675bd87233841521402588ec105 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43603006/badge)](https://coveralls.io/builds/43603006)
   
   Coverage decreased (-0.4%) to 67.436% when pulling **0c7d2987573eea8f0d1a0b26d6d2f6f50fa587d3 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733289174



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/imp/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.influxdb.protocol.imp;
+
+import org.apache.iotdb.influxdb.IoTDBInfluxDBUtils;
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (!database.equals(this.databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (database != null && !database.equals(databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+    List<String> deviceIds = new ArrayList<>();
+    List<Long> times = new ArrayList<>();
+    List<List<String>> measurementsList = new ArrayList<>();
+    List<List<TSDataType>> typesList = new ArrayList<>();
+    List<List<Object>> valuesList = new ArrayList<>();
+    for (Point point : batchPoints.getPoints()) {
+      IoTDBRecord ioTDBRecord = generatePointRecord(point);
+      deviceIds.add(ioTDBRecord.getDeviceId());
+      times.add(ioTDBRecord.getTime());
+      measurementsList.add(ioTDBRecord.getMeasurements());
+      typesList.add(ioTDBRecord.getTypes());
+      valuesList.add(ioTDBRecord.getValues());
+    }
+    try {
+      session.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  /**
+   * when the database changes, update the database related information, that is, obtain the list
+   * and order of all tags corresponding to the database from iotdb
+   *
+   * @param database update database name
+   */
+  private void updateDatabase(String database) {
+    this.databaseCache.setDatabase(database);
+    try {
+      SessionDataSet result =
+          session.executeQueryStatement(
+              "select * from root.TAG_INFO where database_name="

Review comment:
       ->      "select a, b, c from root.TAG_INFO where database_name="
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733291905



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/imp/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.influxdb.protocol.imp;
+
+import org.apache.iotdb.influxdb.IoTDBInfluxDBUtils;
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (!database.equals(this.databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (database != null && !database.equals(databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+    List<String> deviceIds = new ArrayList<>();
+    List<Long> times = new ArrayList<>();
+    List<List<String>> measurementsList = new ArrayList<>();
+    List<List<TSDataType>> typesList = new ArrayList<>();
+    List<List<Object>> valuesList = new ArrayList<>();
+    for (Point point : batchPoints.getPoints()) {
+      IoTDBRecord ioTDBRecord = generatePointRecord(point);

Review comment:
       ```suggestion
         IoTDBRecord iotdbRecord = generatePointRecord(point);
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733293846



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/imp/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.influxdb.protocol.imp;
+
+import org.apache.iotdb.influxdb.IoTDBInfluxDBUtils;
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (!database.equals(this.databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (database != null && !database.equals(databaseCache.getDatabase())) {
+      updateDatabase(database);
+    }
+    List<String> deviceIds = new ArrayList<>();
+    List<Long> times = new ArrayList<>();
+    List<List<String>> measurementsList = new ArrayList<>();
+    List<List<TSDataType>> typesList = new ArrayList<>();
+    List<List<Object>> valuesList = new ArrayList<>();
+    for (Point point : batchPoints.getPoints()) {
+      IoTDBRecord ioTDBRecord = generatePointRecord(point);
+      deviceIds.add(ioTDBRecord.getDeviceId());
+      times.add(ioTDBRecord.getTime());
+      measurementsList.add(ioTDBRecord.getMeasurements());
+      typesList.add(ioTDBRecord.getTypes());
+      valuesList.add(ioTDBRecord.getValues());
+    }
+    try {
+      session.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  /**
+   * when the database changes, update the database related information, that is, obtain the list
+   * and order of all tags corresponding to the database from iotdb
+   *
+   * @param database update database name
+   */
+  private void updateDatabase(String database) {
+    this.databaseCache.setDatabase(database);
+    try {
+      SessionDataSet result =
+          session.executeQueryStatement(
+              "select * from root.TAG_INFO where database_name="
+                  + String.format("\"%s\"", database));
+      Map<String, Map<String, Integer>> measurementTagOrder = new HashMap<>();
+      while (result.hasNext()) {
+        List<Field> fields = result.next().getFields();
+        String measurementName = fields.get(1).getStringValue();
+        Map<String, Integer> tagOrder;
+        if (measurementTagOrder.containsKey(measurementName)) {
+          tagOrder = measurementTagOrder.get(measurementName);
+        } else {
+          tagOrder = new HashMap<>();
+        }
+        tagOrder.put(fields.get(2).getStringValue(), fields.get(3).getIntV());
+        measurementTagOrder.put(measurementName, tagOrder);
+      }
+      this.databaseCache.setMeasurementTagOrder(measurementTagOrder);
+    } catch (StatementExecutionException e) {
+      // at first execution, tag_INFO table is not created, intercept the error
+      if (e.getStatusCode() != 411) {
+        throw new InfluxDBException(e.getMessage());
+      }
+    } catch (IoTDBConnectionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  private IoTDBRecord generatePointRecord(Point point) {
+    String measurement = null;
+    Map<String, String> tags = new HashMap<>();
+    Map<String, Object> fields = new HashMap<>();
+    Long time = null;
+    java.lang.reflect.Field[] reflectFields = point.getClass().getDeclaredFields();
+    // Get the property of point in influxdb by reflection
+    for (java.lang.reflect.Field reflectField : reflectFields) {
+      reflectField.setAccessible(true);
+      try {
+        if (reflectField.getType().getName().equalsIgnoreCase("java.util.Map")
+            && reflectField.getName().equalsIgnoreCase("fields")) {
+          fields = (Map<String, Object>) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.util.Map")
+            && reflectField.getName().equalsIgnoreCase("tags")) {
+          tags = (Map<String, String>) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.lang.String")
+            && reflectField.getName().equalsIgnoreCase("measurement")) {
+          measurement = (String) reflectField.get(point);
+        }
+        if (reflectField.getType().getName().equalsIgnoreCase("java.lang.Number")
+            && reflectField.getName().equalsIgnoreCase("time")) {
+          time = (Long) reflectField.get(point);
+        }
+        // set current time
+        if (time == null) {
+          time = System.currentTimeMillis();
+        }
+      } catch (IllegalAccessException e) {
+        throw new IllegalArgumentException(e.getMessage());
+      }
+    }
+    IoTDBInfluxDBUtils.checkNonEmptyString(measurement, "measurement name");
+
+    String path = generatePath(measurement, tags);
+
+    List<String> measurements = new ArrayList<>();
+    List<TSDataType> types = new ArrayList<>();
+    List<Object> values = new ArrayList<>();
+    for (Map.Entry<String, Object> entry : fields.entrySet()) {
+      measurements.add(entry.getKey());
+      Object value = entry.getValue();
+      types.add(IoTDBInfluxDBUtils.normalTypeToTSDataType(value));
+      values.add(value);
+    }
+    return new IoTDBRecord(path, time, measurements, types, values);
+  }
+
+  private String generatePath(String measurement, Map<String, String> tags) {
+    String database = this.databaseCache.getDatabase();
+    Map<String, Map<String, Integer>> measurementTagOrder =

Review comment:
       ```suggestion
       Map<String, Map<String, Integer>> measurementTagOrders =
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43724554/badge)](https://coveralls.io/builds/43724554)
   
   Coverage decreased (-0.5%) to 67.253% when pulling **26b3f5d360646d7e30c757699ff385507ecaf9c5 on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu merged pull request #4172: [IOTDB-1806] Compatibility of Apache IoTDB with InfluxDB - Data Point Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu merged pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] coveralls edited a comment on pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
coveralls edited a comment on pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#issuecomment-945154904


   
   [![Coverage Status](https://coveralls.io/builds/43724343/badge)](https://coveralls.io/builds/43724343)
   
   Coverage decreased (-0.5%) to 67.255% when pulling **0a9277fda3043cce377f429e502a91934c8033ce on jun0315:influxdb_insert_data** into **0c39ef6b250c53210577ff001ab4fefd1d93954f on apache:master**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #4172: [IOTDB-1689] Compatibility of Apache IoTDB with InfluxDB - Data Insertion

Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #4172:
URL: https://github.com/apache/iotdb/pull/4172#discussion_r733513157



##########
File path: influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/protocol/impl/IoTDBInfluxDBService.java
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.influxdb.protocol.impl;
+
+import org.apache.iotdb.influxdb.protocol.cache.DatabaseCache;
+import org.apache.iotdb.influxdb.protocol.constant.InfluxDBConstant;
+import org.apache.iotdb.influxdb.protocol.dto.IoTDBRecord;
+import org.apache.iotdb.influxdb.protocol.util.DataTypeUtils;
+import org.apache.iotdb.influxdb.protocol.util.ParameterUtils;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.SessionDataSet;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class IoTDBInfluxDBService {
+  private final Session session;
+  private DatabaseCache databaseCache;
+
+  public IoTDBInfluxDBService(Session session) {
+    this.session = session;
+    this.databaseCache = new DatabaseCache();
+  }
+
+  public void setDatabase(String database) {
+    if (databaseCache.getDatabaseTagOrders().get(database) == null) {
+      updateDatabase(database);
+    }
+  }
+
+  public void writePoints(
+      String database,
+      String retentionPolicy,
+      String precision,
+      String consistency,
+      BatchPoints batchPoints) {
+    if (databaseCache.getDatabaseTagOrders().get(database) == null) {
+      updateDatabase(database);
+    }
+    List<String> deviceIds = new ArrayList<>();
+    List<Long> times = new ArrayList<>();
+    List<List<String>> measurementsList = new ArrayList<>();
+    List<List<TSDataType>> typesList = new ArrayList<>();
+    List<List<Object>> valuesList = new ArrayList<>();
+    for (Point point : batchPoints.getPoints()) {
+      IoTDBRecord iotdbRecord = generatePointRecord(point);
+      deviceIds.add(iotdbRecord.getDeviceId());
+      times.add(iotdbRecord.getTime());
+      measurementsList.add(iotdbRecord.getMeasurements());
+      typesList.add(iotdbRecord.getTypes());
+      valuesList.add(iotdbRecord.getValues());
+    }
+    try {
+      session.insertRecords(deviceIds, times, measurementsList, typesList, valuesList);
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      throw new InfluxDBException(e.getMessage());
+    }
+  }
+
+  /**
+   * when the database changes, update the database related information, that is, obtain the list
+   * and order of all tags corresponding to the database from iotdb
+   *
+   * @param database update database name
+   */
+  private void updateDatabase(String database) {
+    try {
+      SessionDataSet result =
+          session.executeQueryStatement(
+              "select database_name,measurement_name,tag_name,tag_order from root.TAG_INFO where database_name="
+                  + String.format("\"%s\"", database));
+      Map<String, Map<String, Integer>> measurementTagOrders = new HashMap<>();
+      while (result.hasNext()) {
+        List<Field> fields = result.next().getFields();
+        String measurementName = fields.get(1).getStringValue();
+        Map<String, Integer> tagOrder;
+        if (measurementTagOrders.containsKey(measurementName)) {
+          tagOrder = measurementTagOrders.get(measurementName);
+        } else {
+          tagOrder = new HashMap<>();
+        }
+        tagOrder.put(fields.get(2).getStringValue(), fields.get(3).getIntV());
+        measurementTagOrders.put(measurementName, tagOrder);
+      }
+      this.databaseCache.updateDatabaseOrders(database, measurementTagOrders);
+      this.databaseCache.setCurrentDatabase(database);
+    } catch (StatementExecutionException e) {
+      // at first execution, tag_INFO table is not created, intercept the error
+      if (e.getStatusCode() != 411) {
+        throw new InfluxDBException(e.getMessage());
+      } else {
+        // when tag_INFO table is not created,we also should set database.

Review comment:
       ```suggestion
           // Retry: when TAG_INFO table is not created, we also should set the database.
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org