qiaojialin commented on a change in pull request #793: [IOTDB-274] Refactor 
MManager
URL: https://github.com/apache/incubator-iotdb/pull/793#discussion_r380542837
 
 

 ##########
 File path: server/src/main/java/org/apache/iotdb/db/metadata/MTree.java
 ##########
 @@ -50,131 +56,111 @@
 public class MTree implements Serializable {
 
   private static final long serialVersionUID = -4200394435237291964L;
-  private static final String PATH_SEPARATOR = "\\.";
-  private static final String NO_CHILD_ERROR = "Node [%s] doesn't have child 
named: [%s]";
-  private static final String NOT_LEAF_NODE = "is NOT the leaf node";
   private MNode root;
-
-  MTree(String rootName) {
-    this.root = new MNode(rootName, null, false);
-  }
-
   /**
-   * function for adding timeseries.It should check whether seriesPath exists.
+   * dummy node is used for the default param of traverse methods
    */
-  void addTimeseriesPath(String timeseriesPath, TSDataType dataType, 
TSEncoding encoding,
-      CompressionType compressor, Map<String, String> props) throws 
PathException {
-    String[] nodeNames = MetaUtils.getNodeNames(timeseriesPath, 
PATH_SEPARATOR);
-    if (nodeNames.length <= 1 || !nodeNames[0].equals(root.getName())) {
-      throw new MTreePathException("Timeseries", timeseriesPath);
-    }
-    MNode cur = findLeafParent(nodeNames);
-    String levelPath = cur.getDataFileName();
+  private MNode dummyNode;
 
-    MNode leaf = new MNode(nodeNames[nodeNames.length - 1], cur, dataType, 
encoding, compressor);
-    if (props != null && !props.isEmpty()) {
-      leaf.getSchema().setProps(props);
-    }
-    leaf.setDataFileName(levelPath);
-    if (cur.isLeaf()) {
-      throw new MTreePathException(timeseriesPath, "can't be created",
-          String.format("node [%s] is left node", cur.getName()));
-    }
-    cur.addChild(nodeNames[nodeNames.length - 1], leaf);
+  MTree(String rootName) {
+    this.root = new InternalMNode(rootName, null);
+    this.dummyNode = new InternalMNode("dummy", null);
   }
 
   /**
-   * function for adding deviceId
-   */
-  MNode addDeviceId(String deviceId) throws PathException {
-    String[] nodeNames = MetaUtils.getNodeNames(deviceId, PATH_SEPARATOR);
+   * Add path
+   *
+   * @param path timeseries path
+   * @param dataType data type
+   * @param encoding encoding
+   * @param compressor compressor
+   * @param props props
+   */
+  void addPath(String path, TSDataType dataType, TSEncoding encoding,
+      CompressionType compressor, Map<String, String> props) throws 
MetadataException {
+    String[] nodeNames = MetaUtils.getNodeNames(path);
     if (nodeNames.length <= 1 || !nodeNames[0].equals(root.getName())) {
-      throw new MTreePathException("Timeseries", deviceId);
-    }
-    MNode cur = getRoot();
-    for (int i = 1; i < nodeNames.length; i++) {
-      if (!cur.hasChild(nodeNames[i])) {
-        cur.addChild(nodeNames[i], new MNode(nodeNames[i], cur, false));
-      }
-      cur = cur.getChild(nodeNames[i]);
+      throw new IllegalPathException(path);
     }
-    return cur;
-  }
-
-  private MNode findLeafParent(String[] nodeNames) throws PathException {
     MNode cur = root;
-    String levelPath = null;
+    String storageGroupName = null;
     int i = 1;
     while (i < nodeNames.length - 1) {
       String nodeName = nodeNames[i];
-      if (cur.isStorageGroup()) {
-        levelPath = cur.getDataFileName();
+      if (cur.isNodeType(MNodeType.STORAGE_GROUP_MNODE)) {
+        storageGroupName = cur.getStorageGroupName();
       }
-      if (!cur.hasChild(nodeName)) {
-        if (cur.isLeaf()) {
-          throw new MTreePathException(String.join(",", nodeNames), "can't be 
created",
-              String.format("node [%s] is left node", cur.getName()));
+      if (!cur.hasChildWithKey(nodeName)) {
+        if (cur.isNodeType(MNodeType.LEAF_MNODE)) {
+          throw new PathAlreadyExistException(cur.getFullPath());
         }
-        cur.addChild(nodeName, new MNode(nodeName, cur, false));
+        cur.addChild(nodeName, new InternalMNode(nodeName, cur));
       }
-      cur.setDataFileName(levelPath);
+      cur.setStorageGroupName(storageGroupName);
       cur = cur.getChild(nodeName);
-      if (levelPath == null) {
-        levelPath = cur.getDataFileName();
+      if (storageGroupName == null) {
+        storageGroupName = cur.getStorageGroupName();
       }
       i++;
     }
-    cur.setDataFileName(levelPath);
-    return cur;
+    cur.setStorageGroupName(storageGroupName);
+    MNode leaf = new LeafMNode(nodeNames[nodeNames.length - 1], cur, dataType, 
encoding,
+        compressor);
+    leaf.getSchema().setProps(props);
+    leaf.setStorageGroupName(cur.getStorageGroupName());
+    cur.addChild(nodeNames[nodeNames.length - 1], leaf);
   }
 
-
   /**
-   * function for checking whether the given path exists.
+   * Add path to MTree. This is available IF and ONLY IF creating schema 
automatically is enabled
    *
-   * @param path -seriesPath not necessarily the whole seriesPath (possibly a 
prefix of a sequence)
+   * @param path device id
    */
-  boolean isPathExist(String path) {
-    String[] nodeNames = nodeNames = MetaUtils.getNodeNames(path, 
PATH_SEPARATOR);
+  MNode addPath(String path) throws MetadataException {
 
 Review comment:
   I wonder why this method exists, could this be replaced by addPath(String 
path, TSDataType dataType, TSEncoding encoding, CompressionType compressor, 
Map<String, String> props)?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to