You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by hx...@apache.org on 2021/03/15 10:10:54 UTC

[iotdb] branch master updated: [ISSUE-2827] add an overrided addChild() method in MNode. (#2828)

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

hxd pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 5540272  [ISSUE-2827] add an overrided addChild() method in MNode. (#2828)
5540272 is described below

commit 5540272827188976828059c86c9d9f2efaf679a2
Author: Al Wei <10...@qq.com>
AuthorDate: Mon Mar 15 05:10:34 2021 -0500

    [ISSUE-2827] add an overrided addChild() method in MNode. (#2828)
    
    Co-authored-by: weizihan0110 <wz...@163.com>
---
 .../org/apache/iotdb/db/metadata/mnode/MNode.java  | 31 ++++++++++++++++++++++
 .../apache/iotdb/db/metadata/mnode/MNodeTest.java  | 27 +++++++++++++++++++
 2 files changed, 58 insertions(+)

diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNode.java b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNode.java
index 23f201c..e5f03ae 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNode.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/mnode/MNode.java
@@ -99,9 +99,40 @@ public class MNode implements Serializable {
       }
     }
 
+    child.parent = this;
     children.putIfAbsent(name, child);
   }
 
+  /**
+   * Add a child to the current mnode.
+   *
+   * <p>This method will not take the child's name as one of the inputs and will also make this
+   * Mnode be child node's parent. All is to reduce the probability of mistaken by users and be more
+   * convenient for users to use. And the return of this method is used to conveniently construct a
+   * chain of time series for users.
+   *
+   * @param child child's node
+   * @return return the MNode already added
+   */
+  MNode addChild(MNode child) {
+    /* use cpu time to exchange memory
+     * measurementNode's children should be null to save memory
+     * add child method will only be called when writing MTree, which is not a frequent operation
+     */
+    if (children == null) {
+      // double check, children is volatile
+      synchronized (this) {
+        if (children == null) {
+          children = new ConcurrentHashMap<>();
+        }
+      }
+    }
+
+    child.parent = this;
+    children.putIfAbsent(child.getName(), child);
+    return child;
+  }
+
   /** delete a child */
   public void deleteChild(String name) {
     if (children != null) {
diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/mnode/MNodeTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/mnode/MNodeTest.java
index 1a58bda..46494c9 100644
--- a/server/src/test/java/org/apache/iotdb/db/metadata/mnode/MNodeTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/metadata/mnode/MNodeTest.java
@@ -68,4 +68,31 @@ public class MNodeTest {
     assertEquals("root.c.b", multiFullPaths.get(0));
     assertEquals("root.c.b", rootNode.getChild("c").getChild("aliasOfb").getFullPath());
   }
+
+  @Test
+  public void testAddChild() {
+    MNode rootNode = new MNode(null, "root");
+
+    MNode speedNode =
+        rootNode
+            .addChild(new MNode(null, "sg1"))
+            .addChild(new MNode(null, "a"))
+            .addChild(new MNode(null, "b"))
+            .addChild(new MNode(null, "c"))
+            .addChild(new MNode(null, "d"))
+            .addChild(new MNode(null, "device"))
+            .addChild(new MNode(null, "speed"));
+    assertEquals("root.sg1.a.b.c.d.device.speed", speedNode.getFullPath());
+
+    MNode temperatureNode =
+        rootNode
+            .getChild("sg1")
+            .addChild(new MNode(null, "aa"))
+            .addChild(new MNode(null, "bb"))
+            .addChild(new MNode(null, "cc"))
+            .addChild(new MNode(null, "dd"))
+            .addChild(new MNode(null, "device11"))
+            .addChild(new MNode(null, "temperature"));
+    assertEquals("root.sg1.aa.bb.cc.dd.device11.temperature", temperatureNode.getFullPath());
+  }
 }