This is an automated email from the ASF dual-hosted git repository. haonan pushed a commit to branch iotdb113511 in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 91e9bdd86e4505191fc4cbe3006967dc2f222fa6 Author: lvshuang <[email protected]> AuthorDate: Mon Feb 22 11:15:00 2021 +0800 cherry pick [IOTDB-1135] Fix count timeseries bug when the paths are nested (#2677) --- .../java/org/apache/iotdb/db/metadata/MTree.java | 75 +++++++++++----------- .../iotdb/db/metadata/MManagerBasicTest.java | 66 +++++++++++++++++++ 2 files changed, 105 insertions(+), 36 deletions(-) diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/MTree.java b/server/src/main/java/org/apache/iotdb/db/metadata/MTree.java index 561404c..e86c8a0 100644 --- a/server/src/main/java/org/apache/iotdb/db/metadata/MTree.java +++ b/server/src/main/java/org/apache/iotdb/db/metadata/MTree.java @@ -80,9 +80,7 @@ import org.apache.iotdb.tsfile.write.schema.MeasurementSchema; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * The hierarchical struct of the Metadata Tree is implemented in this class. - */ +/** The hierarchical struct of the Metadata Tree is implemented in this class. */ public class MTree implements Serializable { public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); @@ -806,7 +804,8 @@ public class MTree implements Serializable { } /** - * Get the count of timeseries under the given prefix path. + * Get the count of timeseries under the given prefix path. if prefixPath contains '*', then not + * throw PathNotExistException() * * @param prefixPath a prefix path or a full path, may contain '*'. */ @@ -815,7 +814,41 @@ public class MTree implements Serializable { if (nodes.length == 0 || !nodes[0].equals(root.getName())) { throw new IllegalPathException(prefixPath.getFullPath()); } - return getCount(root, nodes, 1); + try { + return getCount(root, nodes, 1, false); + } catch (PathNotExistException e) { + throw new PathNotExistException(prefixPath.getFullPath()); + } + } + + /** Traverse the MTree to get the count of timeseries. */ + private int getCount(MNode node, String[] nodes, int idx, boolean wildcard) + throws PathNotExistException { + if (idx < nodes.length) { + if (PATH_WILDCARD.equals(nodes[idx])) { + int sum = 0; + for (MNode child : node.getChildren().values()) { + sum += getCount(child, nodes, idx + 1, true); + } + return sum; + } else { + MNode child = node.getChild(nodes[idx]); + if (child == null) { + if (!wildcard) { + throw new PathNotExistException(node.getName() + NO_CHILDNODE_MSG + nodes[idx]); + } else { + return 0; + } + } + return getCount(child, nodes, idx + 1, wildcard); + } + } else { + int sum = node instanceof MeasurementMNode ? 1 : 0; + for (MNode child : node.getChildren().values()) { + sum += getCount(child, nodes, idx + 1, wildcard); + } + return sum; + } } /** @@ -867,37 +900,7 @@ public class MTree implements Serializable { return getCountInGivenLevel(node, level - (i - 1)); } - /** - * Traverse the MTree to get the count of timeseries. - */ - private int getCount(MNode node, String[] nodes, int idx) { - String nodeReg = MetaUtils.getNodeRegByIdx(idx, nodes); - if (!(PATH_WILDCARD).equals(nodeReg)) { - MNode next = node.getChild(nodeReg); - if (next != null) { - if (next instanceof MeasurementMNode) { - return 1; - } else { - return getCount(next, nodes, idx + 1); - } - } else { - return 0; - } - } else { - int cnt = 0; - for (MNode child : node.getChildren().values()) { - if (child instanceof MeasurementMNode) { - cnt++; - } - cnt += getCount(child, nodes, idx + 1); - } - return cnt; - } - } - - /** - * Traverse the MTree to get the count of devices. - */ + /** Traverse the MTree to get the count of devices. */ private int getDevicesCount(MNode node, String[] nodes, int idx) throws MetadataException { String nodeReg = MetaUtils.getNodeRegByIdx(idx, nodes); int cnt = 0; diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java index 0a0106a..9d1f63d 100644 --- a/server/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java +++ b/server/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java @@ -200,6 +200,72 @@ public class MManagerBasicTest { } @Test + public void testGetAllTimeseriesCount() { + MManager manager = IoTDB.metaManager; + + try { + manager.setStorageGroup(new PartialPath("root.laptop")); + manager.createTimeseries( + new PartialPath("root.laptop.d1"), + TSDataType.INT32, + TSEncoding.PLAIN, + CompressionType.GZIP, + null); + manager.createTimeseries( + new PartialPath("root.laptop.d1.s1"), + TSDataType.INT32, + TSEncoding.PLAIN, + CompressionType.GZIP, + null); + manager.createTimeseries( + new PartialPath("root.laptop.d1.s1.t1"), + TSDataType.INT32, + TSEncoding.PLAIN, + CompressionType.GZIP, + null); + manager.createTimeseries( + new PartialPath("root.laptop.d1.s2"), + TSDataType.INT32, + TSEncoding.PLAIN, + CompressionType.GZIP, + null); + manager.createTimeseries( + new PartialPath("root.laptop.d2.s1"), + TSDataType.INT32, + TSEncoding.PLAIN, + CompressionType.GZIP, + null); + manager.createTimeseries( + new PartialPath("root.laptop.d2.s2"), + TSDataType.INT32, + TSEncoding.PLAIN, + CompressionType.GZIP, + null); + + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root")), 6); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop")), 6); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop.*")), 6); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop.*.*")), 5); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop.*.*.t1")), 1); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop.*.s1")), 3); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop.d1")), 4); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop.d1.*")), 3); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop.d2.s1")), 1); + assertEquals(manager.getAllTimeseriesCount(new PartialPath("root.laptop.d2")), 2); + + try { + manager.getAllTimeseriesCount(new PartialPath("root.laptop.d3.s1")); + fail("Expected exception"); + } catch (MetadataException e) { + assertEquals("Path [root.laptop.d3.s1] does not exist", e.getMessage()); + } + } catch (MetadataException e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + @Test public void testSetStorageGroupAndExist() { MManager manager = IoTDB.metaManager;
