WilliamSong11 commented on a change in pull request #2732:
URL: https://github.com/apache/iotdb/pull/2732#discussion_r582504539
##########
File path: server/src/main/java/org/apache/iotdb/db/metadata/MTree.java
##########
@@ -1170,6 +1170,76 @@ private void findChildNodePathInNextLevel(
}
}
+ /**
+ * Get child node in the next level of the given path.
+ *
+ * <p>e.g., MTree has [root.sg1.d1.s1, root.sg1.d1.s2, root.sg1.d2.s1] given
path = root.sg1,
+ * return [d1, d2]
+ *
+ * <p>e.g., MTree has [root.sg1.d1.s1, root.sg1.d1.s2, root.sg1.d2.s1] given
path = root.sg1.d1
+ * return [s1, s2]
+ *
+ * @return All child nodes' seriesPath(s) of given seriesPath.
+ */
+ Set<String> getChildNodeInNextLevel(PartialPath path) throws
MetadataException {
+ String[] nodes = path.getNodes();
+ if (nodes.length == 0 || !nodes[0].equals(root.getName())) {
+ throw new IllegalPathException(path.getFullPath());
+ }
+ Set<String> childNodes = new TreeSet<>();
+ findChildNodeInNextLevel(root, nodes, 1, "", childNodes, nodes.length + 1);
+ return childNodes;
+ }
+
+ /**
+ * Traverse the MTree to match all child node path in next level
+ *
+ * @param node the current traversing node
+ * @param nodes split the prefix path with '.'
+ * @param idx the current index of array nodes
+ * @param parent store the node string having traversed
+ * @param res store all matched device names
+ * @param length expected length of path
+ */
+ @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity
warning
+ private void findChildNodeInNextLevel(
+ MNode node, String[] nodes, int idx, String parent, Set<String> res, int
length) {
+ if (node == null) {
+ return;
+ }
+ String nodeReg = MetaUtils.getNodeRegByIdx(idx, nodes);
+ if (!nodeReg.contains(PATH_WILDCARD)) {
+ if (idx == length) {
+ res.add(node.getName());
+ } else {
+ findChildNodeInNextLevel(
+ node.getChild(nodeReg),
+ nodes,
+ idx + 1,
+ parent + node.getName() + PATH_SEPARATOR,
+ res,
+ length);
+ }
+ } else {
+ if (node.getChildren().size() > 0) {
+ for (MNode child : node.getChildren().values()) {
+ if (!Pattern.matches(nodeReg.replace("*", ".*"), child.getName())) {
+ continue;
+ }
+ if (idx == length) {
+ res.add(node.getName());
+ } else {
+ findChildNodeInNextLevel(
+ child, nodes, idx + 1, parent + node.getName() +
PATH_SEPARATOR, res, length);
+ }
+ }
+ } else if (idx == length) {
+ String nodeName = node.getName();
+ res.add(nodeName);
+ }
Review comment:
Thank you for your reply
 I've run some local tests and the results seem to be in line with
expectations
  1、Local client testing
IoTDB> SET STORAGE GROUP TO root.gsxgt
SET STORAGE GROUP TO root.gsxgt
Msg: The statement is executed successfully.
IoTDB> show child nodes root.gsxgt
show child nodes root.gsxgt
+-----------+
|child nodes|
+-----------+
+-----------+
Empty set.
It costs 0.005s
IoTDB> CREATE TIMESERIES root.gsxgt.d1 WITH DATATYPE=BOOLEAN, ENCODING=PLAIN
CREATE TIMESERIES root.gsxgt.d1 WITH DATATYPE=BOOLEAN, ENCODING=PLAIN
Msg: The statement is executed successfully.
IoTDB> show child nodes root.gsxgt
show child nodes root.gsxgt
+-----------+
|child nodes|
+-----------+
| d1|
+-----------+
Total line number = 1
It costs 0.009s
IoTDB> show child nodes root.gsxgt.d1
show child nodes root.gsxgt.d1
+-----------+
|child nodes|
+-----------+
+-----------+
Empty set.
It costs 0.004s
IoTDB> CREATE TIMESERIES root.gsxgt.d1.s1 WITH DATATYPE=BOOLEAN,
ENCODING=PLAIN
CREATE TIMESERIES root.gsxgt.d1.s1 WITH DATATYPE=BOOLEAN, ENCODING=PLAIN
Msg: The statement is executed successfully.
IoTDB> show child nodes root.gsxgt.d1
show child nodes root.gsxgt.d1
+-----------+
|child nodes|
+-----------+
| s1|
+-----------+
Total line number = 1
It costs 0.005s
  2、org.apache.iotdb.db.metadata.MTreeTest --> Add Child Node as
an empty scene

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