Alima777 commented on a change in pull request #1716:
URL: https://github.com/apache/iotdb/pull/1716#discussion_r508996169



##########
File path: server/src/main/java/org/apache/iotdb/db/http/router/Router.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.router;
+
+import com.google.gson.*;
+import com.librato.metrics.client.Json;

Review comment:
       remove this.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/exception/UnsupportedHttpMethod.java
##########
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.exception;
+
+public class UnsupportedHttpMethod extends Exception {

Review comment:
       Rename this as XXXException

##########
File path: 
server/src/main/java/org/apache/iotdb/db/http/handler/StorageGroupsHandlers.java
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.handler;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import io.netty.handler.codec.http.HttpMethod;
+import java.util.List;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.auth.AuthorityChecker;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.UnsupportedHttpMethod;
+import org.apache.iotdb.db.exception.metadata.IllegalPathException;
+import org.apache.iotdb.db.exception.metadata.StorageGroupNotSetException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.metadata.mnode.StorageGroupMNode;
+import org.apache.iotdb.db.qp.physical.sys.SetStorageGroupPlan;
+import org.apache.iotdb.db.service.IoTDB;
+
+public class StorageGroupsHandlers extends Handler {
+
+  public JsonElement handle(HttpMethod httpMethod, JsonElement json)
+      throws QueryProcessException, StorageEngineException
+      , StorageGroupNotSetException, AuthException
+      , IllegalPathException, UnsupportedHttpMethod {
+    checkLogin();
+    if (HttpMethod.GET.equals(httpMethod)) {
+      List<StorageGroupMNode> storageGroupMNodes = 
IoTDB.metaManager.getAllStorageGroupNodes();
+      JsonArray result = new JsonArray();
+      for (StorageGroupMNode storageGroupMNode : storageGroupMNodes) {
+        if (storageGroupMNode.getDataTTL() > 0 && 
storageGroupMNode.getDataTTL() < Long.MAX_VALUE) {
+          JsonObject jsonObject = new JsonObject();
+          jsonObject.addProperty(HttpConstant.STORAGE_GROUP, 
storageGroupMNode.getFullPath());
+          jsonObject.addProperty(HttpConstant.TTL, 
storageGroupMNode.getDataTTL());
+          result.add(jsonObject);
+        } else {
+          JsonObject jsonObject = new JsonObject();
+          jsonObject.addProperty(HttpConstant.STORAGE_GROUP, 
storageGroupMNode.getFullPath());
+          result.add(jsonObject);
+        }
+      }
+      return result;
+    } else if (HttpMethod.POST.equals(httpMethod)) {
+      JsonArray jsonArray = json.getAsJsonArray();

Review comment:
       For users, this design may be counter-intuitive. If I just want to set 
ONE storage group, I will use a JsonObject instead of JsonArray. But if I did 
so, it will throw an exception("not an JsonArray") here. So I think, we should 
**provide two interfaces for single and multiple process.**

##########
File path: 
server/src/main/java/org/apache/iotdb/db/http/handler/GetTimeSeriesHandler.java
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.http.handler;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.List;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.auth.AuthorityChecker;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.physical.sys.ShowTimeSeriesPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.control.QueryResourceManager;
+import 
org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet;
+import org.apache.thrift.TException;
+
+public class GetTimeSeriesHandler extends Handler {
+  public JsonElement handle(JsonArray json)
+      throws AuthException, MetadataException, TException, 
StorageEngineException,
+      QueryFilterOptimizationException, IOException, InterruptedException, 
SQLException, QueryProcessException {
+    checkLogin();

Review comment:
       Please check the http method is GET here.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/http/handler/GetTimeSeriesHandler.java
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.db.http.handler;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.List;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.auth.AuthorityChecker;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.physical.sys.ShowTimeSeriesPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.control.QueryResourceManager;
+import 
org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet;
+import org.apache.thrift.TException;
+
+public class GetTimeSeriesHandler extends Handler {
+  public JsonElement handle(JsonArray json)
+      throws AuthException, MetadataException, TException, 
StorageEngineException,
+      QueryFilterOptimizationException, IOException, InterruptedException, 
SQLException, QueryProcessException {
+    checkLogin();
+    JsonArray result = new JsonArray();
+    for(JsonElement object : json) {
+      String path = object.getAsString();
+      PartialPath partialPath = new PartialPath(path);
+      ShowTimeSeriesPlan plan = new ShowTimeSeriesPlan(partialPath);
+      plan.setHasLimit(true);
+      if(!AuthorityChecker.check(username, plan.getPaths(), 
plan.getOperatorType(), null)) {
+        throw new AuthException(String.format("%s can't be gotten by %s", 
path, username));
+      }
+      long queryID = QueryResourceManager.getInstance().assignQueryId(false);
+      QueryDataSet dataSet = executor.processQuery(plan, new 
QueryContext(queryID));
+      while(dataSet.hasNext()) {
+        JsonArray row = new JsonArray();
+        RowRecord rowRecord = dataSet.next();
+        List<Field> fields = rowRecord.getFields();
+        for(Field field : fields) {
+          if(field != null) {
+            row.add(field.getStringValue());
+          } else {
+            row.add(HttpConstant.NULL);
+          }
+        }
+        result.add(row);
+      }
+    }

Review comment:
       Please keep the json format for result. like
   {
       "timeseries": "root.sg.d1.s1",
       "alias": "temperature",
       ...
   }.
   It will be more usable. Otherwise I have to remember the column index, if i 
want to get one specific column.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/http/handler/InsertHandler.java
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.handler;
+
+import com.google.gson.*;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.metadata.IllegalPathException;
+import org.apache.iotdb.db.exception.metadata.StorageGroupNotSetException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.physical.crud.InsertRowPlan;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class InsertHandler extends Handler{
+  public JsonElement handle(JsonArray json)
+      throws IllegalPathException, QueryProcessException,
+      StorageEngineException, StorageGroupNotSetException, AuthException {
+    checkLogin();
+    for (JsonElement o : json) {
+      JsonObject object = o.getAsJsonObject();
+      String deviceID = object.get(HttpConstant.DEVICE_ID).getAsString();
+      JsonArray measurements = (JsonArray) 
object.get(HttpConstant.MEASUREMENTS);
+      long timestamps = object.get(HttpConstant.TIMESTAMP).getAsLong();
+      JsonArray values  = (JsonArray) object.get(HttpConstant.VALUES);

Review comment:
       Same as above.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/http/handler/UsersHandler.java
##########
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.handler;
+
+import java.util.List;
+import java.util.Map;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.auth.authorizer.BasicAuthorizer;
+import org.apache.iotdb.db.auth.authorizer.IAuthorizer;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+
+public class UsersHandler extends Handler{
+
+  public static boolean userLogin(Map<String, List<String>> p) throws 
AuthException {
+    List<String> usernameList = p.get(HttpConstant.USERNAME);
+    List<String> passwordList = p.get(HttpConstant.PASSWORD);
+    IAuthorizer authorizer = BasicAuthorizer.getInstance();
+    username = usernameList.get(0);
+    return authorizer.login(usernameList.get(0), passwordList.get(0));
+  }
+
+  public static boolean userLogout(Map<String, List<String>> p) throws 
AuthException {
+    if(username == null) {
+      throw new AuthException("you have already logout");
+    }
+    List<String> usernameList = p.get(HttpConstant.USERNAME);
+    if(!usernameList.get(0).equals(username)) {

Review comment:
       ```suggestion
       if (username == null) {
         throw new AuthException("you have already logout");
       }
       List<String> usernameList = p.get(HttpConstant.USERNAME);
       if (!usernameList.get(0).equals(username)) {
   ```

##########
File path: server/src/main/java/org/apache/iotdb/db/http/router/Router.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.router;
+
+import com.google.gson.*;
+import com.librato.metrics.client.Json;
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.QueryStringDecoder;
+import java.io.IOException;
+import java.sql.SQLException;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.UnsupportedHttpMethod;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.http.handler.DeleteStorageGroupsHandler;
+import org.apache.iotdb.db.http.handler.DeleteTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.GetTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.InsertHandler;
+import org.apache.iotdb.db.http.handler.QueryHandler;
+import org.apache.iotdb.db.http.handler.StorageGroupsHandlers;
+import org.apache.iotdb.db.http.handler.TimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.UsersHandler;
+import org.apache.iotdb.db.http.utils.URIUtils;
+import 
org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
+import org.apache.thrift.TException;
+
+/**
+ * Router that contains information about both route matching and return json.
+ */
+public class Router {
+
+  /**
+   * matching url and return JSON
+   * @param method http method
+   * @param uri uri will be matched
+   * @param json request JSON Object
+   * @return JSON object, may be a JSONArray or JSONObject
+   */
+  public JsonElement route(HttpMethod method, String uri, JsonElement json)
+      throws AuthException, MetadataException, QueryProcessException
+      , StorageEngineException, UnsupportedHttpMethod, SQLException, 
InterruptedException, QueryFilterOptimizationException, IOException, TException 
{
+    QueryStringDecoder decoder = new QueryStringDecoder(uri);
+    uri = URIUtils.removeParameter(uri);
+    switch (uri) {
+      case HttpConstant.ROUTING_STORAGE_GROUPS:
+        StorageGroupsHandlers storageGroupsHandlers = new 
StorageGroupsHandlers();
+        return storageGroupsHandlers.handle(method, json);
+      case HttpConstant.ROUTING_TIME_SERIES:
+        TimeSeriesHandler timeSeriesHandler = new TimeSeriesHandler();
+        return timeSeriesHandler.handle(method, json);
+      case HttpConstant.ROUTING_USER_LOGIN:
+        if (UsersHandler.userLogin(decoder.parameters())) {
+          JsonObject result = new JsonObject();
+          result.addProperty(HttpConstant.RESULT, 
HttpConstant.SUCCESSFUL_OPERATION);
+          return result;

Review comment:
       Successful JsonObject is used many places. Try to extract it to a method 
and use
   ` return getSuccessfulObject();`

##########
File path: server/src/main/java/org/apache/iotdb/db/http/handler/Handler.java
##########
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.handler;
+
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.qp.Planner;
+import org.apache.iotdb.db.qp.executor.IPlanExecutor;
+import org.apache.iotdb.db.qp.executor.PlanExecutor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+abstract class Handler {
+  protected static final Logger logger = 
LoggerFactory.getLogger(Handler.class);
+  protected static String username;
+  protected IPlanExecutor executor;
+  protected Planner processor;
+
+  Handler() {
+    try {
+      processor = new Planner();
+      executor = new PlanExecutor();
+    } catch (QueryProcessException e) {
+      logger.error(e.getMessage());
+    }
+  }
+
+  /**
+   * Check whether current user has logged in.
+   *
+   */
+  void checkLogin() throws AuthException{
+    if(username == null) {
+      throw new AuthException("didn't log in iotdb");
+    }
+  }
+}

Review comment:
       ```suggestion
     /**
      * Check whether current user has logged in.
      */
     void checkLogin() throws AuthException {
       if (username == null) {
         throw new AuthException("didn't log in iotdb");
       }
     }
   }
   ```

##########
File path: server/src/main/java/org/apache/iotdb/db/http/router/Router.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.router;
+
+import com.google.gson.*;
+import com.librato.metrics.client.Json;
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.QueryStringDecoder;
+import java.io.IOException;
+import java.sql.SQLException;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.UnsupportedHttpMethod;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.http.handler.DeleteStorageGroupsHandler;
+import org.apache.iotdb.db.http.handler.DeleteTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.GetTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.InsertHandler;
+import org.apache.iotdb.db.http.handler.QueryHandler;
+import org.apache.iotdb.db.http.handler.StorageGroupsHandlers;
+import org.apache.iotdb.db.http.handler.TimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.UsersHandler;
+import org.apache.iotdb.db.http.utils.URIUtils;
+import 
org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
+import org.apache.thrift.TException;
+
+/**
+ * Router that contains information about both route matching and return json.
+ */
+public class Router {

Review comment:
       `Router` may be ambiguous, how about `HttpRouter`? like `QueryRouter` in 
IoTDB

##########
File path: server/src/main/java/org/apache/iotdb/db/http/router/Router.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.router;
+
+import com.google.gson.*;
+import com.librato.metrics.client.Json;
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.QueryStringDecoder;
+import java.io.IOException;
+import java.sql.SQLException;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.UnsupportedHttpMethod;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.http.handler.DeleteStorageGroupsHandler;
+import org.apache.iotdb.db.http.handler.DeleteTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.GetTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.InsertHandler;
+import org.apache.iotdb.db.http.handler.QueryHandler;
+import org.apache.iotdb.db.http.handler.StorageGroupsHandlers;
+import org.apache.iotdb.db.http.handler.TimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.UsersHandler;
+import org.apache.iotdb.db.http.utils.URIUtils;
+import 
org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
+import org.apache.thrift.TException;
+
+/**
+ * Router that contains information about both route matching and return json.
+ */
+public class Router {
+
+  /**
+   * matching url and return JSON
+   * @param method http method
+   * @param uri uri will be matched
+   * @param json request JSON Object
+   * @return JSON object, may be a JSONArray or JSONObject
+   */
+  public JsonElement route(HttpMethod method, String uri, JsonElement json)
+      throws AuthException, MetadataException, QueryProcessException
+      , StorageEngineException, UnsupportedHttpMethod, SQLException, 
InterruptedException, QueryFilterOptimizationException, IOException, TException 
{
+    QueryStringDecoder decoder = new QueryStringDecoder(uri);
+    uri = URIUtils.removeParameter(uri);
+    switch (uri) {
+      case HttpConstant.ROUTING_STORAGE_GROUPS:
+        StorageGroupsHandlers storageGroupsHandlers = new 
StorageGroupsHandlers();
+        return storageGroupsHandlers.handle(method, json);
+      case HttpConstant.ROUTING_TIME_SERIES:
+        TimeSeriesHandler timeSeriesHandler = new TimeSeriesHandler();
+        return timeSeriesHandler.handle(method, json);
+      case HttpConstant.ROUTING_USER_LOGIN:
+        if (UsersHandler.userLogin(decoder.parameters())) {
+          JsonObject result = new JsonObject();
+          result.addProperty(HttpConstant.RESULT, 
HttpConstant.SUCCESSFUL_OPERATION);
+          return result;
+        } else {
+          throw new AuthException(String.format("%s can't log in", 
UsersHandler.getUsername()));
+        }
+      case HttpConstant.ROUTING_USER_LOGOUT:
+        if (UsersHandler.userLogout(decoder.parameters())) {
+          JsonObject result = new JsonObject();
+          result.addProperty(HttpConstant.RESULT, 
HttpConstant.SUCCESSFUL_OPERATION);
+          return result;
+        } else {
+          throw new AuthException(String.format("%s can't log out", 
UsersHandler.getUsername()));
+        }
+      case HttpConstant.ROUTING_QUERY:
+        QueryHandler queryHandler = new QueryHandler();
+        return queryHandler.handle(json.getAsJsonObject());
+      case HttpConstant.ROUTING_INSERT:
+        InsertHandler insertHandler = new InsertHandler();
+        return insertHandler.handle(json.getAsJsonArray());
+      case HttpConstant.ROUTING_STORAGE_GROUPS_DELETE:
+        DeleteStorageGroupsHandler deleteStorageGroupsHandler = new 
DeleteStorageGroupsHandler();
+        return deleteStorageGroupsHandler.handle(json.getAsJsonArray());
+      case HttpConstant.ROUTING_TIME_SERIES_DELETE:
+        DeleteTimeSeriesHandler deleteTimeSeriesHandler = new 
DeleteTimeSeriesHandler();
+        return deleteTimeSeriesHandler.handle(json.getAsJsonArray());
+      case HttpConstant.ROUTING_GET_TIME_SERIES:
+        GetTimeSeriesHandler getTimeSeriesHandler = new GetTimeSeriesHandler();
+        return getTimeSeriesHandler.handle(json.getAsJsonArray());

Review comment:
       Why `getTimeseries` and `createTimeseries` are in different URI and 
Handler, while `setStorageGroup` and `getStorageGroup` is in a single URI and 
StorageGroupHandler?
   Better seperate these two storageGroup Method to two URI and Handler.
   

##########
File path: server/src/main/java/org/apache/iotdb/db/http/utils/URIUtils.java
##########
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.utils;
+
+import org.apache.iotdb.db.http.constant.HttpConstant;
+
+public class URIUtils {
+  private URIUtils() {
+    throw new IllegalStateException("Utility class");
+  }
+  public static String removeParameter(String uri) {
+    int index = uri.indexOf(HttpConstant.QUESTION_MARK);
+    if(index < 0) {
+      if(uri.charAt(uri.length() - 1) == '/') {
+        return uri.substring(0, uri.length() - 1);
+      }
+    } else {
+      return uri.substring(0, index);
+    }
+    return uri;
+  }
+}

Review comment:
       Mind the code style (add blank line) here. And MANY places can be 
modified. You can use Control + Alt + L.

##########
File path: server/src/main/java/org/apache/iotdb/db/http/router/Router.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.router;
+
+import com.google.gson.*;
+import com.librato.metrics.client.Json;
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.QueryStringDecoder;
+import java.io.IOException;
+import java.sql.SQLException;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.UnsupportedHttpMethod;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.http.handler.DeleteStorageGroupsHandler;
+import org.apache.iotdb.db.http.handler.DeleteTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.GetTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.InsertHandler;
+import org.apache.iotdb.db.http.handler.QueryHandler;
+import org.apache.iotdb.db.http.handler.StorageGroupsHandlers;
+import org.apache.iotdb.db.http.handler.TimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.UsersHandler;
+import org.apache.iotdb.db.http.utils.URIUtils;
+import 
org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
+import org.apache.thrift.TException;
+
+/**
+ * Router that contains information about both route matching and return json.
+ */
+public class Router {
+
+  /**
+   * matching url and return JSON
+   * @param method http method
+   * @param uri uri will be matched
+   * @param json request JSON Object
+   * @return JSON object, may be a JSONArray or JSONObject
+   */
+  public JsonElement route(HttpMethod method, String uri, JsonElement json)
+      throws AuthException, MetadataException, QueryProcessException
+      , StorageEngineException, UnsupportedHttpMethod, SQLException, 
InterruptedException, QueryFilterOptimizationException, IOException, TException 
{
+    QueryStringDecoder decoder = new QueryStringDecoder(uri);
+    uri = URIUtils.removeParameter(uri);
+    switch (uri) {
+      case HttpConstant.ROUTING_STORAGE_GROUPS:
+        StorageGroupsHandlers storageGroupsHandlers = new 
StorageGroupsHandlers();
+        return storageGroupsHandlers.handle(method, json);
+      case HttpConstant.ROUTING_TIME_SERIES:
+        TimeSeriesHandler timeSeriesHandler = new TimeSeriesHandler();
+        return timeSeriesHandler.handle(method, json);
+      case HttpConstant.ROUTING_USER_LOGIN:
+        if (UsersHandler.userLogin(decoder.parameters())) {
+          JsonObject result = new JsonObject();
+          result.addProperty(HttpConstant.RESULT, 
HttpConstant.SUCCESSFUL_OPERATION);
+          return result;
+        } else {
+          throw new AuthException(String.format("%s can't log in", 
UsersHandler.getUsername()));
+        }
+      case HttpConstant.ROUTING_USER_LOGOUT:
+        if (UsersHandler.userLogout(decoder.parameters())) {
+          JsonObject result = new JsonObject();
+          result.addProperty(HttpConstant.RESULT, 
HttpConstant.SUCCESSFUL_OPERATION);
+          return result;
+        } else {
+          throw new AuthException(String.format("%s can't log out", 
UsersHandler.getUsername()));
+        }
+      case HttpConstant.ROUTING_QUERY:
+        QueryHandler queryHandler = new QueryHandler();
+        return queryHandler.handle(json.getAsJsonObject());
+      case HttpConstant.ROUTING_INSERT:
+        InsertHandler insertHandler = new InsertHandler();
+        return insertHandler.handle(json.getAsJsonArray());
+      case HttpConstant.ROUTING_STORAGE_GROUPS_DELETE:
+        DeleteStorageGroupsHandler deleteStorageGroupsHandler = new 
DeleteStorageGroupsHandler();
+        return deleteStorageGroupsHandler.handle(json.getAsJsonArray());
+      case HttpConstant.ROUTING_TIME_SERIES_DELETE:
+        DeleteTimeSeriesHandler deleteTimeSeriesHandler = new 
DeleteTimeSeriesHandler();
+        return deleteTimeSeriesHandler.handle(json.getAsJsonArray());

Review comment:
       Provide one more interface for JsonObject.

##########
File path: server/src/main/java/org/apache/iotdb/db/http/router/Router.java
##########
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.router;
+
+import com.google.gson.*;
+import com.librato.metrics.client.Json;
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.QueryStringDecoder;
+import java.io.IOException;
+import java.sql.SQLException;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.UnsupportedHttpMethod;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.http.handler.DeleteStorageGroupsHandler;
+import org.apache.iotdb.db.http.handler.DeleteTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.GetTimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.InsertHandler;
+import org.apache.iotdb.db.http.handler.QueryHandler;
+import org.apache.iotdb.db.http.handler.StorageGroupsHandlers;
+import org.apache.iotdb.db.http.handler.TimeSeriesHandler;
+import org.apache.iotdb.db.http.handler.UsersHandler;
+import org.apache.iotdb.db.http.utils.URIUtils;
+import 
org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
+import org.apache.thrift.TException;
+
+/**
+ * Router that contains information about both route matching and return json.
+ */
+public class Router {
+
+  /**
+   * matching url and return JSON
+   * @param method http method
+   * @param uri uri will be matched
+   * @param json request JSON Object
+   * @return JSON object, may be a JSONArray or JSONObject
+   */
+  public JsonElement route(HttpMethod method, String uri, JsonElement json)
+      throws AuthException, MetadataException, QueryProcessException
+      , StorageEngineException, UnsupportedHttpMethod, SQLException, 
InterruptedException, QueryFilterOptimizationException, IOException, TException 
{
+    QueryStringDecoder decoder = new QueryStringDecoder(uri);
+    uri = URIUtils.removeParameter(uri);
+    switch (uri) {
+      case HttpConstant.ROUTING_STORAGE_GROUPS:
+        StorageGroupsHandlers storageGroupsHandlers = new 
StorageGroupsHandlers();
+        return storageGroupsHandlers.handle(method, json);
+      case HttpConstant.ROUTING_TIME_SERIES:
+        TimeSeriesHandler timeSeriesHandler = new TimeSeriesHandler();
+        return timeSeriesHandler.handle(method, json);
+      case HttpConstant.ROUTING_USER_LOGIN:
+        if (UsersHandler.userLogin(decoder.parameters())) {
+          JsonObject result = new JsonObject();
+          result.addProperty(HttpConstant.RESULT, 
HttpConstant.SUCCESSFUL_OPERATION);
+          return result;
+        } else {
+          throw new AuthException(String.format("%s can't log in", 
UsersHandler.getUsername()));
+        }
+      case HttpConstant.ROUTING_USER_LOGOUT:
+        if (UsersHandler.userLogout(decoder.parameters())) {
+          JsonObject result = new JsonObject();
+          result.addProperty(HttpConstant.RESULT, 
HttpConstant.SUCCESSFUL_OPERATION);
+          return result;
+        } else {
+          throw new AuthException(String.format("%s can't log out", 
UsersHandler.getUsername()));
+        }
+      case HttpConstant.ROUTING_QUERY:
+        QueryHandler queryHandler = new QueryHandler();
+        return queryHandler.handle(json.getAsJsonObject());
+      case HttpConstant.ROUTING_INSERT:
+        InsertHandler insertHandler = new InsertHandler();
+        return insertHandler.handle(json.getAsJsonArray());
+      case HttpConstant.ROUTING_STORAGE_GROUPS_DELETE:
+        DeleteStorageGroupsHandler deleteStorageGroupsHandler = new 
DeleteStorageGroupsHandler();
+        return deleteStorageGroupsHandler.handle(json.getAsJsonArray());

Review comment:
       Same as the comment in StorageGroupHandler. Provide one more interface 
for JsonObject please.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/http/handler/TimeSeriesHandler.java
##########
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.handler;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import io.netty.handler.codec.http.HttpMethod;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.auth.AuthorityChecker;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.UnsupportedHttpMethod;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.physical.sys.CreateTimeSeriesPlan;
+import org.apache.iotdb.tsfile.common.conf.TSFileDescriptor;
+import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
+
+public class TimeSeriesHandler extends Handler {
+  public JsonElement handle(HttpMethod httpMethod, JsonElement json)
+      throws AuthException, MetadataException, QueryProcessException,
+      StorageEngineException, UnsupportedHttpMethod {
+    checkLogin();
+    if (HttpMethod.POST.equals(httpMethod)) {
+      JsonArray jsonArray = json.getAsJsonArray();

Review comment:
       Provice one more interface for JsonObject.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/http/handler/InsertHandler.java
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.handler;
+
+import com.google.gson.*;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.metadata.IllegalPathException;
+import org.apache.iotdb.db.exception.metadata.StorageGroupNotSetException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.physical.crud.InsertRowPlan;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class InsertHandler extends Handler{
+  public JsonElement handle(JsonArray json)
+      throws IllegalPathException, QueryProcessException,
+      StorageEngineException, StorageGroupNotSetException, AuthException {
+    checkLogin();
+    for (JsonElement o : json) {
+      JsonObject object = o.getAsJsonObject();
+      String deviceID = object.get(HttpConstant.DEVICE_ID).getAsString();
+      JsonArray measurements = (JsonArray) 
object.get(HttpConstant.MEASUREMENTS);

Review comment:
       Considering measurements as JsonObject here.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/http/handler/InsertHandler.java
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.http.handler;
+
+import com.google.gson.*;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.iotdb.db.auth.AuthException;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.metadata.IllegalPathException;
+import org.apache.iotdb.db.exception.metadata.StorageGroupNotSetException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.http.constant.HttpConstant;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.physical.crud.InsertRowPlan;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class InsertHandler extends Handler{
+  public JsonElement handle(JsonArray json)
+      throws IllegalPathException, QueryProcessException,
+      StorageEngineException, StorageGroupNotSetException, AuthException {
+    checkLogin();
+    for (JsonElement o : json) {
+      JsonObject object = o.getAsJsonObject();
+      String deviceID = object.get(HttpConstant.DEVICE_ID).getAsString();
+      JsonArray measurements = (JsonArray) 
object.get(HttpConstant.MEASUREMENTS);
+      long timestamps = object.get(HttpConstant.TIMESTAMP).getAsLong();
+      JsonArray values  = (JsonArray) object.get(HttpConstant.VALUES);
+      boolean isNeedInferType = 
object.get(HttpConstant.IS_NEED_INFER_TYPE).getAsBoolean();
+      if (!insertByRow(deviceID, timestamps, getListString(measurements), 
values, isNeedInferType)) {
+          throw new QueryProcessException(
+              String.format("%s can't be inserted successfully", deviceID));
+        }
+    }
+    JsonObject jsonObject = new JsonObject();
+    jsonObject.addProperty(HttpConstant.RESULT, 
HttpConstant.SUCCESSFUL_OPERATION);
+    return jsonObject;
+  }
+
+  private boolean insertByRow(String deviceId, long time, List<String> 
measurements,
+      JsonArray values, boolean isNeedInferType)
+      throws IllegalPathException, QueryProcessException, 
StorageEngineException, StorageGroupNotSetException {
+    InsertRowPlan plan = new InsertRowPlan();
+    plan.setDeviceId(new PartialPath(deviceId));
+    plan.setTime(time);
+    plan.setMeasurements(measurements.toArray(new String[0]));
+    plan.setDataTypes(new TSDataType[plan.getMeasurements().length]);
+    List<Object> valueList = new ArrayList<>();
+    if(isNeedInferType) {
+      plan.setNeedInferType(true);
+    } else {
+      TSDataType[] dataTypes = new TSDataType[measurements.size()];
+      for(int i = 0; i < measurements.size(); i++) {
+        JsonPrimitive value = values.get(i).getAsJsonPrimitive();
+        if(value.isNumber()) {
+          Number number = value.getAsNumber();
+          valueList.add(number.doubleValue());
+          dataTypes[i] = TSDataType.DOUBLE;
+        } else if(value.isString()) {
+          valueList.add(value.getAsString());
+          dataTypes[i] = TSDataType.TEXT;
+        } else if(value.isBoolean()) {
+          valueList.add(value.getAsBoolean());
+          dataTypes[i] = TSDataType.BOOLEAN;
+        } else {
+          throw new QueryProcessException("Unsupported json data type:" + 
dataTypes[i]);
+        }
+      }

Review comment:
       Why not get the dataType from mManager? User may input values with wrong 
data type, we can check it here instead of   in the process of executing 
insertPlan. 




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to