This is an automated email from the ASF dual-hosted git repository.
qiaojialin 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 0147ec4 [IOTDB-1482]Fix timeseries count and device count with
Template (#3544)
0147ec4 is described below
commit 0147ec40145581b7a07a8a431c2043db501a45e9
Author: zyk990424 <[email protected]>
AuthorDate: Tue Jul 13 14:55:53 2021 +0800
[IOTDB-1482]Fix timeseries count and device count with Template (#3544)
---
.../java/org/apache/iotdb/db/metadata/MTree.java | 17 ++-
.../iotdb/db/metadata/MManagerBasicTest.java | 124 +++++++++++++++++++++
2 files changed, 136 insertions(+), 5 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 efb6d2e..9dc7968 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
@@ -1017,6 +1017,10 @@ public class MTree implements Serializable {
} else {
MNode child = node.getChild(nodes[idx]);
if (child == null) {
+ if (node.isUseTemplate()
+ &&
node.getUpperTemplate().getSchemaMap().containsKey(nodes[idx])) {
+ return 1;
+ }
if (!wildcard) {
throw new PathNotExistException(node.getName() + NO_CHILDNODE_MSG
+ nodes[idx]);
} else {
@@ -1027,6 +1031,9 @@ public class MTree implements Serializable {
}
} else {
int sum = node instanceof MeasurementMNode ? 1 : 0;
+ if (node.isUseTemplate()) {
+ sum += node.getUpperTemplate().getSchemaMap().size();
+ }
for (MNode child : node.getChildren().values()) {
sum += getCount(child, nodes, idx + 1, wildcard);
}
@@ -1084,22 +1091,22 @@ public class MTree implements Serializable {
/** Traverse the MTree to get the count of devices. */
private int getDevicesCount(MNode node, String[] nodes, int idx) {
String nodeReg = MetaUtils.getNodeRegByIdx(idx, nodes);
- int cnt = 0;
+ boolean curIsDevice = node.isUseTemplate();
+ int cnt = curIsDevice ? 1 : 0;
if (!(PATH_WILDCARD).equals(nodeReg)) {
MNode next = node.getChild(nodeReg);
if (next != null) {
- if (next instanceof MeasurementMNode && idx >= nodes.length) {
+ if (next instanceof MeasurementMNode && idx >= nodes.length &&
!curIsDevice) {
cnt++;
} else {
cnt += getDevicesCount(node.getChild(nodeReg), nodes, idx + 1);
}
}
} else {
- boolean deviceAdded = false;
for (MNode child : node.getChildren().values()) {
- if (child instanceof MeasurementMNode && !deviceAdded && idx >=
nodes.length) {
+ if (child instanceof MeasurementMNode && !curIsDevice && idx >=
nodes.length) {
cnt++;
- deviceAdded = true;
+ curIsDevice = true;
}
cnt += getDevicesCount(child, nodes, idx + 1);
}
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 1022cf9..9203857 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
@@ -1266,6 +1266,130 @@ public class MManagerBasicTest {
}
@Test
+ public void testCountTimeseriesWithTemplate() {
+ List<List<String>> measurementList = new ArrayList<>();
+ measurementList.add(Collections.singletonList("s0"));
+ measurementList.add(Collections.singletonList("s1"));
+
+ List<List<TSDataType>> dataTypeList = new ArrayList<>();
+ dataTypeList.add(Collections.singletonList(TSDataType.INT32));
+ dataTypeList.add(Collections.singletonList(TSDataType.FLOAT));
+
+ List<List<TSEncoding>> encodingList = new ArrayList<>();
+ encodingList.add(Collections.singletonList(TSEncoding.RLE));
+ encodingList.add(Collections.singletonList(TSEncoding.RLE));
+
+ List<CompressionType> compressionTypes = new ArrayList<>();
+ for (int i = 0; i < 2; i++) {
+ compressionTypes.add(compressionType);
+ }
+
+ List<String> schemaNames = new ArrayList<>();
+ schemaNames.add("s0");
+ schemaNames.add("s1");
+
+ CreateTemplatePlan plan =
+ new CreateTemplatePlan(
+ "template1",
+ schemaNames,
+ measurementList,
+ dataTypeList,
+ encodingList,
+ compressionTypes);
+ MManager manager = IoTDB.metaManager;
+ try {
+ manager.createDeviceTemplate(plan);
+
+ // set device template
+ SetDeviceTemplatePlan setDeviceTemplatePlan =
+ new SetDeviceTemplatePlan("template1", "root.laptop.d1");
+ manager.setDeviceTemplate(setDeviceTemplatePlan);
+ manager.getDeviceNode(new
PartialPath("root.laptop.d1")).setUseTemplate(true);
+
+ manager.createTimeseries(
+ new PartialPath("root.computer.d1.s2"),
+ TSDataType.INT32,
+ TSEncoding.PLAIN,
+ CompressionType.GZIP,
+ null);
+
+ setDeviceTemplatePlan = new SetDeviceTemplatePlan("template1",
"root.computer");
+ manager.setDeviceTemplate(setDeviceTemplatePlan);
+ manager.getDeviceNode(new
PartialPath("root.computer.d1")).setUseTemplate(true);
+
+ Assert.assertEquals(2, manager.getAllTimeseriesCount(new
PartialPath("root.laptop.d1")));
+ Assert.assertEquals(1, manager.getAllTimeseriesCount(new
PartialPath("root.laptop.d1.s1")));
+ Assert.assertEquals(1, manager.getAllTimeseriesCount(new
PartialPath("root.computer.d1.s1")));
+ Assert.assertEquals(1, manager.getAllTimeseriesCount(new
PartialPath("root.computer.d1.s2")));
+ Assert.assertEquals(3, manager.getAllTimeseriesCount(new
PartialPath("root.computer.d1")));
+ Assert.assertEquals(3, manager.getAllTimeseriesCount(new
PartialPath("root.computer")));
+ Assert.assertEquals(5, manager.getAllTimeseriesCount(new
PartialPath("root")));
+
+ } catch (MetadataException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void testCountDeviceWithTemplate() {
+ List<List<String>> measurementList = new ArrayList<>();
+ measurementList.add(Collections.singletonList("s0"));
+ measurementList.add(Collections.singletonList("s1"));
+
+ List<List<TSDataType>> dataTypeList = new ArrayList<>();
+ dataTypeList.add(Collections.singletonList(TSDataType.INT32));
+ dataTypeList.add(Collections.singletonList(TSDataType.FLOAT));
+
+ List<List<TSEncoding>> encodingList = new ArrayList<>();
+ encodingList.add(Collections.singletonList(TSEncoding.RLE));
+ encodingList.add(Collections.singletonList(TSEncoding.RLE));
+
+ List<CompressionType> compressionTypes = new ArrayList<>();
+ for (int i = 0; i < 2; i++) {
+ compressionTypes.add(compressionType);
+ }
+
+ List<String> schemaNames = new ArrayList<>();
+ schemaNames.add("s0");
+ schemaNames.add("s1");
+
+ CreateTemplatePlan plan =
+ new CreateTemplatePlan(
+ "template1",
+ schemaNames,
+ measurementList,
+ dataTypeList,
+ encodingList,
+ compressionTypes);
+ MManager manager = IoTDB.metaManager;
+
+ try {
+ manager.createDeviceTemplate(plan);
+ // set device template
+ SetDeviceTemplatePlan setDeviceTemplatePlan =
+ new SetDeviceTemplatePlan("template1", "root.laptop.d1");
+ manager.setDeviceTemplate(setDeviceTemplatePlan);
+ manager.getDeviceNode(new
PartialPath("root.laptop.d1")).setUseTemplate(true);
+
+ manager.createTimeseries(
+ new PartialPath("root.laptop.d2.s1"),
+ TSDataType.INT32,
+ TSEncoding.PLAIN,
+ CompressionType.GZIP,
+ null);
+
+ Assert.assertEquals(1, manager.getDevicesNum(new
PartialPath("root.laptop.d1")));
+ Assert.assertEquals(1, manager.getDevicesNum(new
PartialPath("root.laptop.d2")));
+ Assert.assertEquals(2, manager.getDevicesNum(new
PartialPath("root.laptop")));
+
+ } catch (MetadataException e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
public void testTotalSeriesNumber() throws Exception {
MManager manager = IoTDB.metaManager;