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

zyk 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 7e3bea1349 [IOTDB-5361] Refactor device and timeseries count based on 
SchemaReader (#8755)
7e3bea1349 is described below

commit 7e3bea134975c7641706697a1dfe9e2430a1e730
Author: Marcos_Zyk <[email protected]>
AuthorDate: Thu Jan 5 12:28:01 2023 +0800

    [IOTDB-5361] Refactor device and timeseries count based on SchemaReader 
(#8755)
    
    [IOTDB-5361] Refactor device and timeseries count based on SchemaReader 
(#8755)
---
 .../iotdb/db/it/schema/IoTDBMetadataFetchIT.java   |  7 --
 .../schemaregion/rocksdb/RSchemaRegion.java        |  9 ---
 .../metadata/tagSchemaRegion/TagSchemaRegion.java  | 26 ------
 .../iotdb/db/metadata/mtree/IMTreeBelowSG.java     | 38 ---------
 .../db/metadata/mtree/MTreeBelowSGCachedImpl.java  | 53 ------------
 .../db/metadata/mtree/MTreeBelowSGMemoryImpl.java  | 53 ------------
 .../mtree/traverser/counter/EntityCounter.java     | 46 -----------
 .../counter/MNodeAboveSGLevelCounter.java          | 60 --------------
 .../mtree/traverser/counter/MNodeLevelCounter.java | 83 -------------------
 .../traverser/counter/MeasurementCounter.java      | 73 -----------------
 .../impl/read/SchemaRegionReadPlanFactory.java     |  4 +
 .../db/metadata/schemaregion/ISchemaRegion.java    | 23 ------
 .../schemaregion/SchemaRegionMemoryImpl.java       | 29 -------
 .../schemaregion/SchemaRegionSchemaFileImpl.java   | 29 -------
 .../operator/schema/DevicesCountOperator.java      | 82 ++++---------------
 ...CountOperator.java => SchemaCountOperator.java} | 84 +++++++++----------
 .../operator/schema/TimeSeriesCountOperator.java   | 93 +++++-----------------
 .../iotdb/db/metadata/mtree/MTreeBelowSGTest.java  | 57 -------------
 .../schemaRegion/SchemaRegionBasicTest.java        | 65 ++++++++-------
 .../schemaRegion/SchemaRegionTestUtil.java         | 38 +++++++++
 .../mpp/execution/operator/OperatorMemoryTest.java |  8 +-
 .../operator/schema/SchemaCountOperatorTest.java   | 79 ++++++++++++++++--
 22 files changed, 231 insertions(+), 808 deletions(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBMetadataFetchIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBMetadataFetchIT.java
index 530bfad679..a5fadb78f0 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBMetadataFetchIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBMetadataFetchIT.java
@@ -26,7 +26,6 @@ import org.apache.iotdb.itbase.category.LocalStandaloneIT;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
@@ -355,9 +354,6 @@ public class IoTDBMetadataFetchIT {
   }
 
   @Test
-  @Ignore(
-      value =
-          "Old IoTDB service not support 'count timeseries by tag',Waiting for 
the old IoTDB to be removed.")
   public void showCountTimeSeriesWithTag() throws SQLException {
     try (Connection connection = EnvFactory.getEnv().getConnection();
         Statement statement = connection.createStatement()) {
@@ -497,9 +493,6 @@ public class IoTDBMetadataFetchIT {
   }
 
   @Test
-  @Ignore(
-      value =
-          "Old IoTDB service not support 'count timeseries by tag',Waiting for 
the old IoTDB to be removed.")
   public void showCountTimeSeriesGroupByWithTag() throws SQLException {
     try (Connection connection = EnvFactory.getEnv().getConnection();
         Statement statement = connection.createStatement()) {
diff --git 
a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaRegion.java
 
b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaRegion.java
index 7c6e4e2bf2..e145067685 100644
--- 
a/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaRegion.java
+++ 
b/schema-engine-rocksdb/src/main/java/org/apache/iotdb/db/metadata/schemaregion/rocksdb/RSchemaRegion.java
@@ -804,21 +804,12 @@ public class RSchemaRegion implements ISchemaRegion {
     return index;
   }
 
-  @Override
   public long getAllTimeseriesCount(
       PartialPath pathPattern, Map<Integer, Template> templateMap, boolean 
isPrefixMatch)
       throws MetadataException {
     throw new UnsupportedOperationException();
   }
 
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, boolean isPrefixMatch, String key, String 
value, boolean isContains)
-      throws MetadataException {
-    return getMatchedMeasurementPathWithTags(pathPattern.getNodes()).size();
-  }
-
-  @Override
   public long getDevicesNum(PartialPath pathPattern, boolean isPrefixMatch)
       throws MetadataException {
     return getCountByNodeType(new Character[] {NODE_TYPE_ENTITY}, 
pathPattern.getNodes());
diff --git 
a/schema-engine-tag/src/main/java/org/apache/iotdb/db/metadata/tagSchemaRegion/TagSchemaRegion.java
 
b/schema-engine-tag/src/main/java/org/apache/iotdb/db/metadata/tagSchemaRegion/TagSchemaRegion.java
index 2690ba6758..9955502d6f 100644
--- 
a/schema-engine-tag/src/main/java/org/apache/iotdb/db/metadata/tagSchemaRegion/TagSchemaRegion.java
+++ 
b/schema-engine-tag/src/main/java/org/apache/iotdb/db/metadata/tagSchemaRegion/TagSchemaRegion.java
@@ -335,20 +335,6 @@ public class TagSchemaRegion implements ISchemaRegion {
     throw new UnsupportedOperationException("deleteTimeseriesInBlackList");
   }
 
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, Map<Integer, Template> templateMap, boolean 
isPrefixMatch)
-      throws MetadataException {
-    throw new UnsupportedOperationException("getAllTimeseriesCount");
-  }
-
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, boolean isPrefixMatch, String key, String 
value, boolean isContains)
-      throws MetadataException {
-    throw new UnsupportedOperationException("getAllTimeseriesCount");
-  }
-
   @Override
   public Map<PartialPath, Long> getMeasurementCountGroupByLevel(
       PartialPath pathPattern, int level, boolean isPrefixMatch) throws 
MetadataException {
@@ -367,18 +353,6 @@ public class TagSchemaRegion implements ISchemaRegion {
     throw new UnsupportedOperationException("getMeasurementCountGroupByLevel");
   }
 
-  @Override
-  public long getDevicesNum(PartialPath pathPattern, boolean isPrefixMatch)
-      throws MetadataException {
-    synchronized (idTableWithDeviceIDList) {
-      if (pathPattern.getFullPath().length() <= storageGroupFullPath.length()) 
{
-        return idTableWithDeviceIDList.size();
-      } else {
-        return getDeviceIDsFromInvertedIndex(pathPattern).size();
-      }
-    }
-  }
-
   @Override
   public List<PartialPath> getNodesListInGivenLevel(
       PartialPath pathPattern, int nodeLevel, boolean isPrefixMatch) throws 
MetadataException {
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/IMTreeBelowSG.java 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/IMTreeBelowSG.java
index d909d22106..143a1451ac 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/IMTreeBelowSG.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/IMTreeBelowSG.java
@@ -196,44 +196,6 @@ public interface IMTreeBelowSG {
   List<PartialPath> getNodesListInGivenLevel(
       PartialPath pathPattern, int nodeLevel, boolean isPrefixMatch) throws 
MetadataException;
 
-  /**
-   * Get the count of timeseries matching the given path.
-   *
-   * @param pathPattern a path pattern or a full path, may contain wildcard
-   * @param isPrefixMatch if true, the path pattern is used to match prefix 
path
-   */
-  long getAllTimeseriesCount(PartialPath pathPattern, boolean isPrefixMatch)
-      throws MetadataException;
-
-  /**
-   * Get the count of timeseries matching the given path.
-   *
-   * @param pathPattern a path pattern or a full path, may contain wildcard
-   * @param templateMap <TemplateId, Template>
-   * @param isPrefixMatch if true, the path pattern is used to match prefix 
path
-   */
-  long getAllTimeseriesCount(
-      PartialPath pathPattern, Map<Integer, Template> templateMap, boolean 
isPrefixMatch)
-      throws MetadataException;
-
-  /**
-   * Get the count of timeseries matching the given path by tag.
-   *
-   * @param pathPattern a path pattern or a full path, may contain wildcard
-   */
-  long getAllTimeseriesCount(
-      PartialPath pathPattern, boolean isPrefixMatch, List<String> timeseries, 
boolean hasTag)
-      throws MetadataException;
-
-  /**
-   * Get the count of devices matching the given path. If using prefix match, 
the path pattern is
-   * used to match prefix path. All timeseries start with the matched prefix 
path will be counted.
-   *
-   * @param pathPattern a path pattern or a full path, may contain wildcard
-   * @param isPrefixMatch if true, the path pattern is used to match prefix 
path
-   */
-  long getDevicesNum(PartialPath pathPattern, boolean isPrefixMatch) throws 
MetadataException;
-
   Map<PartialPath, Long> getMeasurementCountGroupByLevel(
       PartialPath pathPattern, int level, boolean isPrefixMatch) throws 
MetadataException;
 
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java
index d51714712a..5d4dce01cf 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java
@@ -46,8 +46,6 @@ import 
org.apache.iotdb.db.metadata.mtree.traverser.collector.EntityCollector;
 import org.apache.iotdb.db.metadata.mtree.traverser.collector.MNodeCollector;
 import 
org.apache.iotdb.db.metadata.mtree.traverser.collector.MeasurementCollector;
 import org.apache.iotdb.db.metadata.mtree.traverser.counter.CounterTraverser;
-import org.apache.iotdb.db.metadata.mtree.traverser.counter.EntityCounter;
-import org.apache.iotdb.db.metadata.mtree.traverser.counter.MeasurementCounter;
 import 
org.apache.iotdb.db.metadata.mtree.traverser.counter.MeasurementGroupByLevelCounter;
 import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowDevicesPlan;
 import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowTimeSeriesPlan;
@@ -841,57 +839,6 @@ public class MTreeBelowSGCachedImpl implements 
IMTreeBelowSG {
   // endregion
 
   // region Interfaces and Implementation for metadata count
-  /**
-   * Get the count of timeseries matching the given path.
-   *
-   * @param pathPattern a path pattern or a full path, may contain wildcard
-   */
-  @Override
-  public long getAllTimeseriesCount(PartialPath pathPattern, boolean 
isPrefixMatch)
-      throws MetadataException {
-    CounterTraverser counter = new MeasurementCounter(storageGroupMNode, 
pathPattern, store);
-    counter.setPrefixMatch(isPrefixMatch);
-    counter.traverse();
-    return counter.getCount();
-  }
-
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, Map<Integer, Template> templateMap, boolean 
isPrefixMatch)
-      throws MetadataException {
-    CounterTraverser counter = new MeasurementCounter(storageGroupMNode, 
pathPattern, store);
-    counter.setPrefixMatch(isPrefixMatch);
-    counter.setTemplateMap(templateMap);
-    counter.traverse();
-    return counter.getCount();
-  }
-
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, boolean isPrefixMatch, List<String> timeseries, 
boolean hasTag)
-      throws MetadataException {
-    CounterTraverser counter =
-        new MeasurementCounter(storageGroupMNode, pathPattern, store, 
timeseries, hasTag);
-    counter.setPrefixMatch(isPrefixMatch);
-    counter.traverse();
-    return counter.getCount();
-  }
-
-  /**
-   * Get the count of devices matching the given path. If using prefix match, 
the path pattern is
-   * used to match prefix path. All timeseries start with the matched prefix 
path will be counted.
-   *
-   * @param pathPattern a path pattern or a full path, may contain wildcard
-   * @param isPrefixMatch if true, the path pattern is used to match prefix 
path
-   */
-  @Override
-  public long getDevicesNum(PartialPath pathPattern, boolean isPrefixMatch)
-      throws MetadataException {
-    CounterTraverser counter = new EntityCounter(storageGroupMNode, 
pathPattern, store);
-    counter.setPrefixMatch(isPrefixMatch);
-    counter.traverse();
-    return counter.getCount();
-  }
 
   @Override
   public Map<PartialPath, Long> getMeasurementCountGroupByLevel(
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java
index ca66d3c53d..23d3d94597 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java
@@ -46,8 +46,6 @@ import 
org.apache.iotdb.db.metadata.mtree.traverser.collector.EntityCollector;
 import org.apache.iotdb.db.metadata.mtree.traverser.collector.MNodeCollector;
 import 
org.apache.iotdb.db.metadata.mtree.traverser.collector.MeasurementCollector;
 import org.apache.iotdb.db.metadata.mtree.traverser.counter.CounterTraverser;
-import org.apache.iotdb.db.metadata.mtree.traverser.counter.EntityCounter;
-import org.apache.iotdb.db.metadata.mtree.traverser.counter.MeasurementCounter;
 import 
org.apache.iotdb.db.metadata.mtree.traverser.counter.MeasurementGroupByLevelCounter;
 import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowDevicesPlan;
 import org.apache.iotdb.db.metadata.plan.schemaregion.read.IShowTimeSeriesPlan;
@@ -785,57 +783,6 @@ public class MTreeBelowSGMemoryImpl implements 
IMTreeBelowSG {
   // endregion
 
   // region Interfaces and Implementation for metadata count
-  /**
-   * Get the count of timeseries matching the given path.
-   *
-   * @param pathPattern a path pattern or a full path, may contain wildcard
-   */
-  @Override
-  public long getAllTimeseriesCount(PartialPath pathPattern, boolean 
isPrefixMatch)
-      throws MetadataException {
-    CounterTraverser counter = new MeasurementCounter(storageGroupMNode, 
pathPattern, store);
-    counter.setPrefixMatch(isPrefixMatch);
-    counter.traverse();
-    return counter.getCount();
-  }
-
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, Map<Integer, Template> templateMap, boolean 
isPrefixMatch)
-      throws MetadataException {
-    CounterTraverser counter = new MeasurementCounter(storageGroupMNode, 
pathPattern, store);
-    counter.setPrefixMatch(isPrefixMatch);
-    counter.setTemplateMap(templateMap);
-    counter.traverse();
-    return counter.getCount();
-  }
-
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, boolean isPrefixMatch, List<String> timeseries, 
boolean hasTag)
-      throws MetadataException {
-    CounterTraverser counter =
-        new MeasurementCounter(storageGroupMNode, pathPattern, store, 
timeseries, hasTag);
-    counter.setPrefixMatch(isPrefixMatch);
-    counter.traverse();
-    return counter.getCount();
-  }
-
-  /**
-   * Get the count of devices matching the given path. If using prefix match, 
the path pattern is
-   * used to match prefix path. All timeseries start with the matched prefix 
path will be counted.
-   *
-   * @param pathPattern a path pattern or a full path, may contain wildcard
-   * @param isPrefixMatch if true, the path pattern is used to match prefix 
path
-   */
-  @Override
-  public long getDevicesNum(PartialPath pathPattern, boolean isPrefixMatch)
-      throws MetadataException {
-    CounterTraverser counter = new EntityCounter(storageGroupMNode, 
pathPattern, store);
-    counter.setPrefixMatch(isPrefixMatch);
-    counter.traverse();
-    return counter.getCount();
-  }
 
   @Override
   public Map<PartialPath, Long> getMeasurementCountGroupByLevel(
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/EntityCounter.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/EntityCounter.java
deleted file mode 100644
index 60f703c83d..0000000000
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/EntityCounter.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.iotdb.db.metadata.mtree.traverser.counter;
-
-import org.apache.iotdb.commons.exception.MetadataException;
-import org.apache.iotdb.commons.path.PartialPath;
-import org.apache.iotdb.db.metadata.mnode.IMNode;
-import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
-
-// This class implements the entity count function.
-public class EntityCounter extends CounterTraverser {
-
-  public EntityCounter(IMNode startNode, PartialPath path, IMTreeStore store)
-      throws MetadataException {
-    super(startNode, path, store);
-  }
-
-  @Override
-  protected boolean processInternalMatchedMNode(IMNode node, int idx, int 
level) {
-    return false;
-  }
-
-  @Override
-  protected boolean processFullMatchedMNode(IMNode node, int idx, int level) {
-    if (node.isEntity()) {
-      count++;
-    }
-    return false;
-  }
-}
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MNodeAboveSGLevelCounter.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MNodeAboveSGLevelCounter.java
deleted file mode 100644
index d45358b2b1..0000000000
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MNodeAboveSGLevelCounter.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.iotdb.db.metadata.mtree.traverser.counter;
-
-import org.apache.iotdb.commons.exception.MetadataException;
-import org.apache.iotdb.commons.path.PartialPath;
-import org.apache.iotdb.db.metadata.mnode.IMNode;
-import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
-
-import java.util.HashSet;
-import java.util.Set;
-
-public class MNodeAboveSGLevelCounter extends MNodeLevelCounter {
-
-  protected Set<PartialPath> involvedStorageGroupMNodes = new HashSet<>();
-
-  public MNodeAboveSGLevelCounter(
-      IMNode startNode, PartialPath path, IMTreeStore store, int targetLevel)
-      throws MetadataException {
-    super(startNode, path, store, targetLevel);
-  }
-
-  @Override
-  protected boolean processInternalMatchedMNode(IMNode node, int idx, int 
level) {
-    if (node.isStorageGroup()) {
-      involvedStorageGroupMNodes.add(node.getPartialPath());
-      return true;
-    }
-    return super.processInternalMatchedMNode(node, idx, level);
-  }
-
-  @Override
-  protected boolean processFullMatchedMNode(IMNode node, int idx, int level) {
-    if (node.isStorageGroup()) {
-      involvedStorageGroupMNodes.add(node.getPartialPath());
-      return true;
-    }
-    return super.processFullMatchedMNode(node, idx, level);
-  }
-
-  public Set<PartialPath> getInvolvedStorageGroupMNodes() {
-    return involvedStorageGroupMNodes;
-  }
-}
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MNodeLevelCounter.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MNodeLevelCounter.java
deleted file mode 100644
index 2dd5680035..0000000000
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MNodeLevelCounter.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.iotdb.db.metadata.mtree.traverser.counter;
-
-import org.apache.iotdb.commons.exception.MetadataException;
-import org.apache.iotdb.commons.path.PartialPath;
-import org.apache.iotdb.db.metadata.mnode.IMNode;
-import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
-
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-
-//
-
-/**
- * This Traverser implements node count function. On finding a path matching 
the given pattern, if
- * the path is longer than the specified level, MNodeLevelCounter finds the 
node of the specified
- * level on the path and counts it. The same node will not be counted more 
than once.
- */
-public class MNodeLevelCounter extends CounterTraverser {
-
-  // level query option
-  protected int targetLevel;
-
-  private Set<IMNode> processedNodes = new HashSet<>();
-
-  public MNodeLevelCounter(IMNode startNode, PartialPath path, IMTreeStore 
store, int targetLevel)
-      throws MetadataException {
-    super(startNode, path, store);
-    this.targetLevel = targetLevel;
-  }
-
-  @Override
-  protected boolean processInternalMatchedMNode(IMNode node, int idx, int 
level) {
-    return false;
-  }
-
-  @Override
-  protected boolean processFullMatchedMNode(IMNode node, int idx, int level) {
-    return processLevelMatchedMNode(node, level);
-  }
-
-  private boolean processLevelMatchedMNode(IMNode node, int level) {
-    // move the cursor the given level when matched
-    if (level < targetLevel) {
-      return false;
-    }
-    // record processed node so they will not be processed twice
-    IMNode levelMatchedAncestor = getLevelMatchedAncestor(node, level);
-    if (!processedNodes.contains(levelMatchedAncestor)) {
-      processedNodes.add(levelMatchedAncestor);
-      count++;
-    }
-    return true;
-  }
-
-  private IMNode getLevelMatchedAncestor(IMNode node, int level) {
-    Iterator<IMNode> iterator = traverseContext.iterator();
-    while (level > targetLevel && iterator.hasNext()) {
-      node = iterator.next();
-      level--;
-    }
-
-    return node;
-  }
-}
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MeasurementCounter.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MeasurementCounter.java
deleted file mode 100644
index 6fe05844ec..0000000000
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/MeasurementCounter.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.iotdb.db.metadata.mtree.traverser.counter;
-
-import org.apache.iotdb.commons.exception.MetadataException;
-import org.apache.iotdb.commons.path.PartialPath;
-import org.apache.iotdb.db.metadata.mnode.IMNode;
-import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
-
-import java.util.ArrayList;
-import java.util.List;
-
-// This method implements the measurement count function.
-// One MultiMeasurement will only be count once.
-public class MeasurementCounter extends CounterTraverser {
-
-  private List<String> timeseries = new ArrayList<>();
-  private boolean hasTag = false;
-
-  public MeasurementCounter(IMNode startNode, PartialPath path, IMTreeStore 
store)
-      throws MetadataException {
-    super(startNode, path, store);
-    shouldTraverseTemplate = true;
-  }
-
-  public MeasurementCounter(
-      IMNode startNode,
-      PartialPath path,
-      IMTreeStore store,
-      List<String> timeseries,
-      boolean hasTag)
-      throws MetadataException {
-    super(startNode, path, store);
-    shouldTraverseTemplate = true;
-    this.timeseries = timeseries;
-    this.hasTag = hasTag;
-  }
-
-  @Override
-  protected boolean processInternalMatchedMNode(IMNode node, int idx, int 
level) {
-    return false;
-  }
-
-  @Override
-  protected boolean processFullMatchedMNode(IMNode node, int idx, int level) {
-    if (!node.isMeasurement()) {
-      return false;
-    }
-    if (hasTag) {
-      if (!timeseries.contains(node.getFullPath())) {
-        return true;
-      }
-    }
-    count++;
-    return true;
-  }
-}
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/plan/schemaregion/impl/read/SchemaRegionReadPlanFactory.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/plan/schemaregion/impl/read/SchemaRegionReadPlanFactory.java
index 05b3527571..4209d26215 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/plan/schemaregion/impl/read/SchemaRegionReadPlanFactory.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/metadata/plan/schemaregion/impl/read/SchemaRegionReadPlanFactory.java
@@ -35,6 +35,10 @@ public class SchemaRegionReadPlanFactory {
     return new ShowDevicesPlanImpl(path, 0, 0, false, -1);
   }
 
+  public static IShowDevicesPlan getShowDevicesPlan(PartialPath path, boolean 
isPrefixMatch) {
+    return new ShowDevicesPlanImpl(path, 0, 0, isPrefixMatch, -1);
+  }
+
   public static IShowDevicesPlan getShowDevicesPlan(
       PartialPath path, int limit, int offset, boolean isPrefixMatch) {
     return new ShowDevicesPlanImpl(path, limit, offset, isPrefixMatch, -1);
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/ISchemaRegion.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/ISchemaRegion.java
index d9a653b02a..e32dbc1390 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/ISchemaRegion.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/ISchemaRegion.java
@@ -176,19 +176,6 @@ public interface ISchemaRegion {
 
   // region Interfaces for metadata count
 
-  /**
-   * To calculate the count of timeseries matching given path. The path could 
be a pattern of a full
-   * path, may contain wildcard. If using prefix match, the path pattern is 
used to match prefix
-   * path. All timeseries start with the matched prefix path will be counted.
-   */
-  long getAllTimeseriesCount(
-      PartialPath pathPattern, Map<Integer, Template> templateMap, boolean 
isPrefixMatch)
-      throws MetadataException;
-
-  long getAllTimeseriesCount(
-      PartialPath pathPattern, boolean isPrefixMatch, String key, String 
value, boolean isContains)
-      throws MetadataException;
-
   /**
    * The measurements will be grouped by the node in given level and then 
counted for each group. If
    * no measurements found, but the path is contained in the group, then this 
path will also be
@@ -210,16 +197,6 @@ public interface ISchemaRegion {
       String value,
       boolean isContains)
       throws MetadataException;
-
-  /**
-   * To calculate the count of devices for given path pattern. If using prefix 
match, the path
-   * pattern is used to match prefix path. All timeseries start with the 
matched prefix path will be
-   * counted.
-   *
-   * @param pathPattern
-   * @param isPrefixMatch
-   */
-  long getDevicesNum(PartialPath pathPattern, boolean isPrefixMatch) throws 
MetadataException;
   // endregion
 
   // region Interfaces for level Node info Query
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java
index 34d76215dd..df606c0517 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java
@@ -883,35 +883,6 @@ public class SchemaRegionMemoryImpl implements 
ISchemaRegion {
 
   // region Interfaces for metadata count
 
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, Map<Integer, Template> templateMap, boolean 
isPrefixMatch)
-      throws MetadataException {
-    return mtree.getAllTimeseriesCount(pathPattern, templateMap, 
isPrefixMatch);
-  }
-
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, boolean isPrefixMatch, String key, String 
value, boolean isContains)
-      throws MetadataException {
-    return mtree.getAllTimeseriesCount(
-        pathPattern,
-        isPrefixMatch,
-        tagManager.getMatchedTimeseriesInIndex(key, value, isContains),
-        true);
-  }
-
-  /**
-   * To calculate the count of devices for given path pattern. If using prefix 
match, the path
-   * pattern is used to match prefix path. All timeseries start with the 
matched prefix path will be
-   * counted.
-   */
-  @Override
-  public long getDevicesNum(PartialPath pathPattern, boolean isPrefixMatch)
-      throws MetadataException {
-    return mtree.getDevicesNum(pathPattern, isPrefixMatch);
-  }
-
   @Override
   public Map<PartialPath, Long> getMeasurementCountGroupByLevel(
       PartialPath pathPattern, int level, boolean isPrefixMatch) throws 
MetadataException {
diff --git 
a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java
index 6cf068f5a9..1defa02119 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java
@@ -943,35 +943,6 @@ public class SchemaRegionSchemaFileImpl implements 
ISchemaRegion {
 
   // region Interfaces for metadata count
 
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, Map<Integer, Template> templateMap, boolean 
isPrefixMatch)
-      throws MetadataException {
-    return mtree.getAllTimeseriesCount(pathPattern, templateMap, 
isPrefixMatch);
-  }
-
-  @Override
-  public long getAllTimeseriesCount(
-      PartialPath pathPattern, boolean isPrefixMatch, String key, String 
value, boolean isContains)
-      throws MetadataException {
-    return mtree.getAllTimeseriesCount(
-        pathPattern,
-        isPrefixMatch,
-        tagManager.getMatchedTimeseriesInIndex(key, value, isContains),
-        true);
-  }
-
-  /**
-   * To calculate the count of devices for given path pattern. If using prefix 
match, the path
-   * pattern is used to match prefix path. All timeseries start with the 
matched prefix path will be
-   * counted.
-   */
-  @Override
-  public long getDevicesNum(PartialPath pathPattern, boolean isPrefixMatch)
-      throws MetadataException {
-    return mtree.getDevicesNum(pathPattern, isPrefixMatch);
-  }
-
   @Override
   public Map<PartialPath, Long> getMeasurementCountGroupByLevel(
       PartialPath pathPattern, int level, boolean isPrefixMatch) throws 
MetadataException {
diff --git 
a/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/DevicesCountOperator.java
 
b/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/DevicesCountOperator.java
index 6e91fd178b..014480a185 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/DevicesCountOperator.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/DevicesCountOperator.java
@@ -21,97 +21,45 @@ package org.apache.iotdb.db.mpp.execution.operator.schema;
 
 import org.apache.iotdb.commons.exception.MetadataException;
 import org.apache.iotdb.commons.path.PartialPath;
+import 
org.apache.iotdb.db.metadata.plan.schemaregion.impl.read.SchemaRegionReadPlanFactory;
+import org.apache.iotdb.db.metadata.query.info.IDeviceSchemaInfo;
+import org.apache.iotdb.db.metadata.query.reader.ISchemaReader;
 import org.apache.iotdb.db.mpp.common.header.ColumnHeader;
 import org.apache.iotdb.db.mpp.common.header.ColumnHeaderConstant;
-import org.apache.iotdb.db.mpp.execution.driver.SchemaDriverContext;
 import org.apache.iotdb.db.mpp.execution.operator.OperatorContext;
-import org.apache.iotdb.db.mpp.execution.operator.source.SourceOperator;
 import org.apache.iotdb.db.mpp.plan.planner.plan.node.PlanNodeId;
-import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
-import org.apache.iotdb.tsfile.read.common.block.TsBlock;
-import org.apache.iotdb.tsfile.read.common.block.TsBlockBuilder;
 
-import java.util.List;
 import java.util.stream.Collectors;
 
-public class DevicesCountOperator implements SourceOperator {
-  private final PlanNodeId sourceId;
-  private final OperatorContext operatorContext;
+public class DevicesCountOperator extends 
SchemaCountOperator<IDeviceSchemaInfo> {
+
   private final PartialPath partialPath;
   private final boolean isPrefixPath;
 
-  private boolean isFinished;
-
-  private final List<TSDataType> outputDataTypes;
-
-  @Override
-  public PlanNodeId getSourceId() {
-    return sourceId;
-  }
-
   public DevicesCountOperator(
       PlanNodeId sourceId,
       OperatorContext operatorContext,
       PartialPath partialPath,
       boolean isPrefixPath) {
-    this.sourceId = sourceId;
-    this.operatorContext = operatorContext;
-    this.partialPath = partialPath;
-    this.isPrefixPath = isPrefixPath;
-    this.outputDataTypes =
+    super(
+        sourceId,
+        operatorContext,
         ColumnHeaderConstant.countDevicesColumnHeaders.stream()
             .map(ColumnHeader::getColumnType)
-            .collect(Collectors.toList());
-  }
+            .collect(Collectors.toList()));
 
-  @Override
-  public OperatorContext getOperatorContext() {
-    return operatorContext;
+    this.partialPath = partialPath;
+    this.isPrefixPath = isPrefixPath;
   }
 
   @Override
-  public TsBlock next() {
-    isFinished = true;
-    TsBlockBuilder tsBlockBuilder = new TsBlockBuilder(outputDataTypes);
-    long count = 0;
+  protected ISchemaReader<IDeviceSchemaInfo> createSchemaReader() {
     try {
-      count =
-          ((SchemaDriverContext) 
operatorContext.getInstanceContext().getDriverContext())
-              .getSchemaRegion()
-              .getDevicesNum(partialPath, isPrefixPath);
+      return getSchemaRegion()
+          .getDeviceReader(
+              SchemaRegionReadPlanFactory.getShowDevicesPlan(partialPath, 
isPrefixPath));
     } catch (MetadataException e) {
       throw new RuntimeException(e.getMessage(), e);
     }
-    tsBlockBuilder.getTimeColumnBuilder().writeLong(0L);
-    tsBlockBuilder.getColumnBuilder(0).writeLong(count);
-    tsBlockBuilder.declarePosition();
-    return tsBlockBuilder.build();
-  }
-
-  @Override
-  public boolean hasNext() {
-    return !isFinished;
-  }
-
-  @Override
-  public boolean isFinished() {
-    return isFinished;
-  }
-
-  @Override
-  public long calculateMaxPeekMemory() {
-    // the integer used for count
-    return 4L;
-  }
-
-  @Override
-  public long calculateMaxReturnSize() {
-    // the integer used for count
-    return 4L;
-  }
-
-  @Override
-  public long calculateRetainedSizeAfterCallingNext() {
-    return 0L;
   }
 }
diff --git 
a/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/DevicesCountOperator.java
 
b/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/SchemaCountOperator.java
similarity index 62%
copy from 
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/DevicesCountOperator.java
copy to 
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/SchemaCountOperator.java
index 6e91fd178b..8918cd35df 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/DevicesCountOperator.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/SchemaCountOperator.java
@@ -19,10 +19,9 @@
 
 package org.apache.iotdb.db.mpp.execution.operator.schema;
 
-import org.apache.iotdb.commons.exception.MetadataException;
-import org.apache.iotdb.commons.path.PartialPath;
-import org.apache.iotdb.db.mpp.common.header.ColumnHeader;
-import org.apache.iotdb.db.mpp.common.header.ColumnHeaderConstant;
+import org.apache.iotdb.db.metadata.query.info.ISchemaInfo;
+import org.apache.iotdb.db.metadata.query.reader.ISchemaReader;
+import org.apache.iotdb.db.metadata.schemaregion.ISchemaRegion;
 import org.apache.iotdb.db.mpp.execution.driver.SchemaDriverContext;
 import org.apache.iotdb.db.mpp.execution.operator.OperatorContext;
 import org.apache.iotdb.db.mpp.execution.operator.source.SourceOperator;
@@ -32,38 +31,30 @@ import org.apache.iotdb.tsfile.read.common.block.TsBlock;
 import org.apache.iotdb.tsfile.read.common.block.TsBlockBuilder;
 
 import java.util.List;
-import java.util.stream.Collectors;
 
-public class DevicesCountOperator implements SourceOperator {
-  private final PlanNodeId sourceId;
-  private final OperatorContext operatorContext;
-  private final PartialPath partialPath;
-  private final boolean isPrefixPath;
+public abstract class SchemaCountOperator<T extends ISchemaInfo> implements 
SourceOperator {
 
-  private boolean isFinished;
-
-  private final List<TSDataType> outputDataTypes;
+  protected final PlanNodeId sourceId;
+  protected final OperatorContext operatorContext;
+  protected final List<TSDataType> outputDataTypes;
 
-  @Override
-  public PlanNodeId getSourceId() {
-    return sourceId;
-  }
+  private ISchemaReader<T> schemaReader;
+  private boolean isFinished;
 
-  public DevicesCountOperator(
-      PlanNodeId sourceId,
-      OperatorContext operatorContext,
-      PartialPath partialPath,
-      boolean isPrefixPath) {
+  public SchemaCountOperator(
+      PlanNodeId sourceId, OperatorContext operatorContext, List<TSDataType> 
outputDataTypes) {
     this.sourceId = sourceId;
     this.operatorContext = operatorContext;
-    this.partialPath = partialPath;
-    this.isPrefixPath = isPrefixPath;
-    this.outputDataTypes =
-        ColumnHeaderConstant.countDevicesColumnHeaders.stream()
-            .map(ColumnHeader::getColumnType)
-            .collect(Collectors.toList());
+    this.outputDataTypes = outputDataTypes;
+  }
+
+  protected final ISchemaRegion getSchemaRegion() {
+    return ((SchemaDriverContext) 
operatorContext.getInstanceContext().getDriverContext())
+        .getSchemaRegion();
   }
 
+  protected abstract ISchemaReader<T> createSchemaReader();
+
   @Override
   public OperatorContext getOperatorContext() {
     return operatorContext;
@@ -74,14 +65,14 @@ public class DevicesCountOperator implements SourceOperator 
{
     isFinished = true;
     TsBlockBuilder tsBlockBuilder = new TsBlockBuilder(outputDataTypes);
     long count = 0;
-    try {
-      count =
-          ((SchemaDriverContext) 
operatorContext.getInstanceContext().getDriverContext())
-              .getSchemaRegion()
-              .getDevicesNum(partialPath, isPrefixPath);
-    } catch (MetadataException e) {
-      throw new RuntimeException(e.getMessage(), e);
+    if (schemaReader == null) {
+      schemaReader = createSchemaReader();
+    }
+    while (schemaReader.hasNext()) {
+      schemaReader.next();
+      count++;
     }
+
     tsBlockBuilder.getTimeColumnBuilder().writeLong(0L);
     tsBlockBuilder.getColumnBuilder(0).writeLong(count);
     tsBlockBuilder.declarePosition();
@@ -100,18 +91,31 @@ public class DevicesCountOperator implements 
SourceOperator {
 
   @Override
   public long calculateMaxPeekMemory() {
-    // the integer used for count
-    return 4L;
+    // the long used for count
+    return 8L;
   }
 
   @Override
   public long calculateMaxReturnSize() {
-    // the integer used for count
-    return 4L;
+    // the long used for count
+    return 8L;
   }
 
   @Override
   public long calculateRetainedSizeAfterCallingNext() {
-    return 0L;
+    return 0;
+  }
+
+  @Override
+  public PlanNodeId getSourceId() {
+    return sourceId;
+  }
+
+  @Override
+  public void close() throws Exception {
+    if (schemaReader != null) {
+      schemaReader.close();
+      schemaReader = null;
+    }
   }
 }
diff --git 
a/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/TimeSeriesCountOperator.java
 
b/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/TimeSeriesCountOperator.java
index febb27a29b..1f9210e8bb 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/TimeSeriesCountOperator.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/schema/TimeSeriesCountOperator.java
@@ -21,35 +21,27 @@ package org.apache.iotdb.db.mpp.execution.operator.schema;
 
 import org.apache.iotdb.commons.exception.MetadataException;
 import org.apache.iotdb.commons.path.PartialPath;
+import 
org.apache.iotdb.db.metadata.plan.schemaregion.impl.read.SchemaRegionReadPlanFactory;
+import org.apache.iotdb.db.metadata.query.info.ITimeSeriesSchemaInfo;
+import org.apache.iotdb.db.metadata.query.reader.ISchemaReader;
 import org.apache.iotdb.db.metadata.template.Template;
 import org.apache.iotdb.db.mpp.common.header.ColumnHeader;
 import org.apache.iotdb.db.mpp.common.header.ColumnHeaderConstant;
-import org.apache.iotdb.db.mpp.execution.driver.SchemaDriverContext;
 import org.apache.iotdb.db.mpp.execution.operator.OperatorContext;
-import org.apache.iotdb.db.mpp.execution.operator.source.SourceOperator;
 import org.apache.iotdb.db.mpp.plan.planner.plan.node.PlanNodeId;
-import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
-import org.apache.iotdb.tsfile.read.common.block.TsBlock;
-import org.apache.iotdb.tsfile.read.common.block.TsBlockBuilder;
 
-import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
-public class TimeSeriesCountOperator implements SourceOperator {
-  private final PlanNodeId sourceId;
-  private final OperatorContext operatorContext;
+public class TimeSeriesCountOperator extends 
SchemaCountOperator<ITimeSeriesSchemaInfo> {
+
   private final PartialPath partialPath;
-  private final boolean isPrefixPath;
+  private final boolean isPrefixMatch;
   private final String key;
   private final String value;
   private final boolean isContains;
   private final Map<Integer, Template> templateMap;
 
-  private boolean isFinished;
-
-  private final List<TSDataType> outputDataTypes;
-
   @Override
   public PlanNodeId getSourceId() {
     return sourceId;
@@ -59,80 +51,35 @@ public class TimeSeriesCountOperator implements 
SourceOperator {
       PlanNodeId sourceId,
       OperatorContext operatorContext,
       PartialPath partialPath,
-      boolean isPrefixPath,
+      boolean isPrefixMatch,
       String key,
       String value,
       boolean isContains,
       Map<Integer, Template> templateMap) {
-    this.sourceId = sourceId;
-    this.operatorContext = operatorContext;
+    super(
+        sourceId,
+        operatorContext,
+        ColumnHeaderConstant.countTimeSeriesColumnHeaders.stream()
+            .map(ColumnHeader::getColumnType)
+            .collect(Collectors.toList()));
+
     this.partialPath = partialPath;
-    this.isPrefixPath = isPrefixPath;
+    this.isPrefixMatch = isPrefixMatch;
     this.key = key;
     this.value = value;
     this.isContains = isContains;
     this.templateMap = templateMap;
-    this.outputDataTypes =
-        ColumnHeaderConstant.countTimeSeriesColumnHeaders.stream()
-            .map(ColumnHeader::getColumnType)
-            .collect(Collectors.toList());
-  }
-
-  @Override
-  public OperatorContext getOperatorContext() {
-    return operatorContext;
   }
 
   @Override
-  public TsBlock next() {
-    isFinished = true;
-    TsBlockBuilder tsBlockBuilder = new TsBlockBuilder(outputDataTypes);
-    long count = 0;
+  protected ISchemaReader<ITimeSeriesSchemaInfo> createSchemaReader() {
     try {
-      if (key != null && value != null) {
-        count =
-            ((SchemaDriverContext) 
operatorContext.getInstanceContext().getDriverContext())
-                .getSchemaRegion()
-                .getAllTimeseriesCount(partialPath, isPrefixPath, key, value, 
isContains);
-      } else {
-        count =
-            ((SchemaDriverContext) 
operatorContext.getInstanceContext().getDriverContext())
-                .getSchemaRegion()
-                .getAllTimeseriesCount(partialPath, templateMap, isPrefixPath);
-      }
+      return getSchemaRegion()
+          .getTimeSeriesReader(
+              SchemaRegionReadPlanFactory.getShowTimeSeriesPlan(
+                  partialPath, templateMap, isContains, key, value, 0, 0, 
isPrefixMatch));
     } catch (MetadataException e) {
       throw new RuntimeException(e.getMessage(), e);
     }
-    tsBlockBuilder.getTimeColumnBuilder().writeLong(0L);
-    tsBlockBuilder.getColumnBuilder(0).writeLong(count);
-    tsBlockBuilder.declarePosition();
-    return tsBlockBuilder.build();
-  }
-
-  @Override
-  public boolean hasNext() {
-    return !isFinished;
-  }
-
-  @Override
-  public boolean isFinished() {
-    return isFinished;
-  }
-
-  @Override
-  public long calculateMaxPeekMemory() {
-    // the integer used for count
-    return 4L;
-  }
-
-  @Override
-  public long calculateMaxReturnSize() {
-    // the integer used for count
-    return 4L;
-  }
-
-  @Override
-  public long calculateRetainedSizeAfterCallingNext() {
-    return 0L;
   }
 }
diff --git 
a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGTest.java 
b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGTest.java
index 359ab39ff5..757265d26c 100644
--- 
a/server/src/test/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGTest.java
+++ 
b/server/src/test/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGTest.java
@@ -448,63 +448,6 @@ public abstract class MTreeBelowSGTest {
     assertFalse(node instanceof MeasurementMNode);
   }
 
-  @Test
-  public void testCountEntity() throws MetadataException {
-    storageGroup = getStorageGroup(new PartialPath("root.laptop"));
-    storageGroup.createTimeseries(
-        new PartialPath("root.laptop.s1"),
-        TSDataType.INT32,
-        TSEncoding.PLAIN,
-        CompressionType.GZIP,
-        null,
-        null);
-    storageGroup.createTimeseries(
-        new PartialPath("root.laptop.d1.s1"),
-        TSDataType.INT32,
-        TSEncoding.PLAIN,
-        CompressionType.GZIP,
-        null,
-        null);
-    storageGroup.createTimeseries(
-        new PartialPath("root.laptop.d2.s1.t1"),
-        TSDataType.INT32,
-        TSEncoding.PLAIN,
-        CompressionType.GZIP,
-        null,
-        null);
-    storageGroup.createTimeseries(
-        new PartialPath("root.laptop.d2.s2"),
-        TSDataType.INT32,
-        TSEncoding.PLAIN,
-        CompressionType.GZIP,
-        null,
-        null);
-    storageGroup.createTimeseries(
-        new PartialPath("root.laptop.a.d1.s1"),
-        TSDataType.INT32,
-        TSEncoding.PLAIN,
-        CompressionType.GZIP,
-        null,
-        null);
-
-    Assert.assertEquals(0, storageGroup.getDevicesNum(new PartialPath("root"), 
false));
-    Assert.assertEquals(1, storageGroup.getDevicesNum(new 
PartialPath("root.laptop"), false));
-    Assert.assertEquals(0, storageGroup.getDevicesNum(new 
PartialPath("root.laptop.s1"), false));
-    Assert.assertEquals(1, storageGroup.getDevicesNum(new 
PartialPath("root.laptop.d1"), false));
-    Assert.assertEquals(2, storageGroup.getDevicesNum(new 
PartialPath("root.laptop.*"), false));
-    Assert.assertEquals(2, storageGroup.getDevicesNum(new 
PartialPath("root.laptop.*.*"), false));
-    Assert.assertEquals(0, storageGroup.getDevicesNum(new 
PartialPath("root.laptop.*.*.*"), false));
-    Assert.assertEquals(4, storageGroup.getDevicesNum(new 
PartialPath("root.laptop.**"), false));
-    Assert.assertEquals(5, storageGroup.getDevicesNum(new 
PartialPath("root.**"), false));
-    Assert.assertEquals(4, storageGroup.getDevicesNum(new 
PartialPath("root.**.*"), false));
-    Assert.assertEquals(4, storageGroup.getDevicesNum(new 
PartialPath("root.*.**"), false));
-    Assert.assertEquals(2, storageGroup.getDevicesNum(new 
PartialPath("root.**.d1"), false));
-    Assert.assertEquals(1, storageGroup.getDevicesNum(new 
PartialPath("root.laptop.*.d1"), false));
-    Assert.assertEquals(3, storageGroup.getDevicesNum(new 
PartialPath("root.**.d*"), false));
-    Assert.assertEquals(1, storageGroup.getDevicesNum(new 
PartialPath("root.laptop.**.s1"), false));
-    Assert.assertEquals(1, storageGroup.getDevicesNum(new 
PartialPath("root.*.d2.*"), false));
-  }
-
   @Test
   public void testGetNodeListInLevel() throws MetadataException {
 
diff --git 
a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionBasicTest.java
 
b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionBasicTest.java
index d7622fdf59..58f5b0286b 100644
--- 
a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionBasicTest.java
+++ 
b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionBasicTest.java
@@ -50,6 +50,9 @@ import java.util.Map;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
+import static 
org.apache.iotdb.db.metadata.schemaRegion.SchemaRegionTestUtil.getAllTimeseriesCount;
+import static 
org.apache.iotdb.db.metadata.schemaRegion.SchemaRegionTestUtil.getDevicesNum;
+
 /**
  * This class define test cases for {@link ISchemaRegion}. All test cases will 
be run in both Memory
  * and Schema_File modes. In Schema_File mode, there are three kinds of test 
environment: full
@@ -328,38 +331,39 @@ public class SchemaRegionBasicTest extends 
AbstractSchemaRegionTest {
 
     // for Non prefix matched path
     Assert.assertEquals(
-        6, schemaRegion.getAllTimeseriesCount(new PartialPath("root.**"), 
null, false));
+        6, getAllTimeseriesCount(schemaRegion, new PartialPath("root.**"), 
null, false));
     Assert.assertEquals(
-        6, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.**"), null, false));
+        6, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.**"), null, false));
     Assert.assertEquals(
-        1, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.*"), null, false));
+        1, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.*"), null, false));
     Assert.assertEquals(
-        4, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.*.*"), null, false));
+        4, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.*.*"), null, false));
     Assert.assertEquals(
-        5, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.*.**"), null, false));
+        5, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.*.**"), null, false));
     Assert.assertEquals(
-        1, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.*.*.t1"), null, false));
+        1, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.*.*.t1"), null, false));
     Assert.assertEquals(
-        2, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.*.s1"), null, false));
+        2, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.*.s1"), null, false));
     Assert.assertEquals(
-        3, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.d1.**"), null, false));
+        3, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.d1.**"), null, false));
     Assert.assertEquals(
-        2, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.d1.*"), null, false));
+        2, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.d1.*"), null, false));
     Assert.assertEquals(
-        1, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.d2.s1"), null, false));
+        1, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.d2.s1"), null, false));
     Assert.assertEquals(
-        2, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.d2.**"), null, false));
+        2, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.d2.**"), null, false));
     Assert.assertEquals(
-        0, schemaRegion.getAllTimeseriesCount(new PartialPath("root.laptop"), 
null, false));
+        0, getAllTimeseriesCount(schemaRegion, new PartialPath("root.laptop"), 
null, false));
     Assert.assertEquals(
-        0, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.d3.s1"), null, false));
+        0, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.d3.s1"), null, false));
 
     // for prefix matched path
-    Assert.assertEquals(6, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root"), null, true));
     Assert.assertEquals(
-        6, schemaRegion.getAllTimeseriesCount(new PartialPath("root.laptop"), 
null, true));
+        6, getAllTimeseriesCount(schemaRegion, new PartialPath("root"), null, 
true));
+    Assert.assertEquals(
+        6, getAllTimeseriesCount(schemaRegion, new PartialPath("root.laptop"), 
null, true));
     Assert.assertEquals(
-        2, schemaRegion.getAllTimeseriesCount(new 
PartialPath("root.laptop.d2"), null, true));
+        2, getAllTimeseriesCount(schemaRegion, new 
PartialPath("root.laptop.d2"), null, true));
   }
 
   @Test
@@ -480,22 +484,23 @@ public class SchemaRegionBasicTest extends 
AbstractSchemaRegionTest {
             "root.laptop.d2.s1",
             "root.laptop.d2.s2"));
 
-    Assert.assertEquals(4, schemaRegion.getDevicesNum(new 
PartialPath("root.**"), false));
-    Assert.assertEquals(1, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop"), false));
-    Assert.assertEquals(2, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop.*"), false));
-    Assert.assertEquals(0, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop.d0"), false));
-    Assert.assertEquals(1, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop.d1"), false));
-    Assert.assertEquals(1, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop.d1.s2"), false));
-    Assert.assertEquals(1, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop.d2"), false));
-    Assert.assertEquals(2, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop.d*"), false));
-    Assert.assertEquals(2, schemaRegion.getDevicesNum(new 
PartialPath("root.*.d*"), false));
-    Assert.assertEquals(1, schemaRegion.getDevicesNum(new 
PartialPath("root.**.s2"), false));
+    Assert.assertEquals(4, getDevicesNum(schemaRegion, new 
PartialPath("root.**"), false));
+    Assert.assertEquals(1, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop"), false));
+    Assert.assertEquals(2, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop.*"), false));
+    Assert.assertEquals(0, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop.d0"), false));
+    Assert.assertEquals(1, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop.d1"), false));
+    Assert.assertEquals(
+        1, getDevicesNum(schemaRegion, new PartialPath("root.laptop.d1.s2"), 
false));
+    Assert.assertEquals(1, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop.d2"), false));
+    Assert.assertEquals(2, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop.d*"), false));
+    Assert.assertEquals(2, getDevicesNum(schemaRegion, new 
PartialPath("root.*.d*"), false));
+    Assert.assertEquals(1, getDevicesNum(schemaRegion, new 
PartialPath("root.**.s2"), false));
 
     // for prefix matched path
-    Assert.assertEquals(4, schemaRegion.getDevicesNum(new PartialPath("root"), 
true));
-    Assert.assertEquals(4, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop"), true));
-    Assert.assertEquals(3, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop.d*"), true));
-    Assert.assertEquals(1, schemaRegion.getDevicesNum(new 
PartialPath("root.laptop.d1.*"), true));
+    Assert.assertEquals(4, getDevicesNum(schemaRegion, new 
PartialPath("root"), true));
+    Assert.assertEquals(4, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop"), true));
+    Assert.assertEquals(3, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop.d*"), true));
+    Assert.assertEquals(1, getDevicesNum(schemaRegion, new 
PartialPath("root.laptop.d1.*"), true));
   }
 
   @Test
diff --git 
a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionTestUtil.java
 
b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionTestUtil.java
index ce15265946..d5fc8e8b58 100644
--- 
a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionTestUtil.java
+++ 
b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionTestUtil.java
@@ -23,8 +23,10 @@ import org.apache.iotdb.commons.path.PartialPath;
 import 
org.apache.iotdb.db.metadata.plan.schemaregion.impl.read.SchemaRegionReadPlanFactory;
 import 
org.apache.iotdb.db.metadata.plan.schemaregion.impl.write.SchemaRegionWritePlanFactory;
 import org.apache.iotdb.db.metadata.query.info.IDeviceSchemaInfo;
+import org.apache.iotdb.db.metadata.query.info.ITimeSeriesSchemaInfo;
 import org.apache.iotdb.db.metadata.query.reader.ISchemaReader;
 import org.apache.iotdb.db.metadata.schemaregion.ISchemaRegion;
+import org.apache.iotdb.db.metadata.template.Template;
 import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
 import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
 import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
@@ -141,6 +143,42 @@ public class SchemaRegionTestUtil {
     }
   }
 
+  public static long getAllTimeseriesCount(
+      ISchemaRegion schemaRegion,
+      PartialPath pathPattern,
+      Map<Integer, Template> templateMap,
+      boolean isPrefixMatch) {
+    try (ISchemaReader<ITimeSeriesSchemaInfo> timeSeriesReader =
+        schemaRegion.getTimeSeriesReader(
+            SchemaRegionReadPlanFactory.getShowTimeSeriesPlan(
+                pathPattern, templateMap, false, null, null, 0, 0, 
isPrefixMatch)); ) {
+      long count = 0;
+      while (timeSeriesReader.hasNext()) {
+        timeSeriesReader.next();
+        count++;
+      }
+      return count;
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public static long getDevicesNum(
+      ISchemaRegion schemaRegion, PartialPath pathPattern, boolean 
isPrefixMatch) {
+    try (ISchemaReader<IDeviceSchemaInfo> deviceReader =
+        schemaRegion.getDeviceReader(
+            SchemaRegionReadPlanFactory.getShowDevicesPlan(pathPattern, 
isPrefixMatch))) {
+      long count = 0;
+      while (deviceReader.hasNext()) {
+        deviceReader.next();
+        count++;
+      }
+      return count;
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+  }
+
   public static List<String> getPathsUsingTemplate(
       ISchemaRegion schemaRegion, PartialPath pathPattern, int templateId)
       throws MetadataException {
diff --git 
a/server/src/test/java/org/apache/iotdb/db/mpp/execution/operator/OperatorMemoryTest.java
 
b/server/src/test/java/org/apache/iotdb/db/mpp/execution/operator/OperatorMemoryTest.java
index 7a9e1aa662..b3e3183027 100644
--- 
a/server/src/test/java/org/apache/iotdb/db/mpp/execution/operator/OperatorMemoryTest.java
+++ 
b/server/src/test/java/org/apache/iotdb/db/mpp/execution/operator/OperatorMemoryTest.java
@@ -810,8 +810,8 @@ public class OperatorMemoryTest {
               false,
               Collections.emptyMap());
 
-      assertEquals(4L, operator.calculateMaxPeekMemory());
-      assertEquals(4L, operator.calculateMaxReturnSize());
+      assertEquals(8L, operator.calculateMaxPeekMemory());
+      assertEquals(8L, operator.calculateMaxReturnSize());
       assertEquals(0, operator.calculateRetainedSizeAfterCallingNext());
 
     } finally {
@@ -875,8 +875,8 @@ public class OperatorMemoryTest {
           new DevicesCountOperator(
               planNodeId, 
fragmentInstanceContext.getOperatorContexts().get(0), null, false);
 
-      assertEquals(4L, operator.calculateMaxPeekMemory());
-      assertEquals(4L, operator.calculateMaxReturnSize());
+      assertEquals(8L, operator.calculateMaxPeekMemory());
+      assertEquals(8L, operator.calculateMaxReturnSize());
       assertEquals(0, operator.calculateRetainedSizeAfterCallingNext());
 
     } finally {
diff --git 
a/server/src/test/java/org/apache/iotdb/db/mpp/execution/operator/schema/SchemaCountOperatorTest.java
 
b/server/src/test/java/org/apache/iotdb/db/mpp/execution/operator/schema/SchemaCountOperatorTest.java
index 3947480167..4f0ceaf0cd 100644
--- 
a/server/src/test/java/org/apache/iotdb/db/mpp/execution/operator/schema/SchemaCountOperatorTest.java
+++ 
b/server/src/test/java/org/apache/iotdb/db/mpp/execution/operator/schema/SchemaCountOperatorTest.java
@@ -21,6 +21,10 @@ package org.apache.iotdb.db.mpp.execution.operator.schema;
 import org.apache.iotdb.commons.concurrent.IoTDBThreadPoolFactory;
 import org.apache.iotdb.commons.exception.MetadataException;
 import org.apache.iotdb.commons.path.PartialPath;
+import 
org.apache.iotdb.db.metadata.plan.schemaregion.impl.read.SchemaRegionReadPlanFactory;
+import org.apache.iotdb.db.metadata.query.info.IDeviceSchemaInfo;
+import org.apache.iotdb.db.metadata.query.info.ITimeSeriesSchemaInfo;
+import org.apache.iotdb.db.metadata.query.reader.ISchemaReader;
 import org.apache.iotdb.db.metadata.schemaregion.ISchemaRegion;
 import org.apache.iotdb.db.mpp.common.FragmentInstanceId;
 import org.apache.iotdb.db.mpp.common.PlanFragmentId;
@@ -35,8 +39,11 @@ import org.apache.iotdb.tsfile.read.common.block.TsBlock;
 import org.junit.Test;
 import org.mockito.Mockito;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 
@@ -68,7 +75,31 @@ public class SchemaCountOperatorTest {
               1, planNodeId, DevicesCountOperator.class.getSimpleName());
       PartialPath partialPath = new PartialPath(SCHEMA_COUNT_OPERATOR_TEST_SG);
       ISchemaRegion schemaRegion = Mockito.mock(ISchemaRegion.class);
-      Mockito.when(schemaRegion.getDevicesNum(partialPath, 
true)).thenReturn(10L);
+
+      List<IDeviceSchemaInfo> deviceSchemaInfoList = new ArrayList<>(10);
+      for (int i = 0; i < 10; i++) {
+        deviceSchemaInfoList.add(Mockito.mock(IDeviceSchemaInfo.class));
+      }
+      Iterator<IDeviceSchemaInfo> iterator = deviceSchemaInfoList.iterator();
+      Mockito.when(
+              schemaRegion.getDeviceReader(
+                  SchemaRegionReadPlanFactory.getShowDevicesPlan(partialPath, 
true)))
+          .thenReturn(
+              new ISchemaReader<IDeviceSchemaInfo>() {
+                @Override
+                public void close() throws Exception {}
+
+                @Override
+                public boolean hasNext() {
+                  return iterator.hasNext();
+                }
+
+                @Override
+                public IDeviceSchemaInfo next() {
+                  return iterator.next();
+                }
+              });
+
       operatorContext
           .getInstanceContext()
           .setDriverContext(new SchemaDriverContext(fragmentInstanceContext, 
schemaRegion));
@@ -107,14 +138,11 @@ public class SchemaCountOperatorTest {
               1, planNodeId, TimeSeriesCountOperator.class.getSimpleName());
       PartialPath partialPath = new PartialPath(SCHEMA_COUNT_OPERATOR_TEST_SG);
       ISchemaRegion schemaRegion = Mockito.mock(ISchemaRegion.class);
-      Mockito.when(schemaRegion.getAllTimeseriesCount(partialPath, 
Collections.emptyMap(), true))
-          .thenReturn(100L);
       Mockito.when(
-              schemaRegion.getAllTimeseriesCount(
-                  new PartialPath(SCHEMA_COUNT_OPERATOR_TEST_SG + 
".device1.*"),
-                  Collections.emptyMap(),
-                  false))
-          .thenReturn(10L);
+              schemaRegion.getTimeSeriesReader(
+                  SchemaRegionReadPlanFactory.getShowTimeSeriesPlan(
+                      partialPath, Collections.emptyMap(), false, null, null, 
0, 0, true)))
+          .thenReturn(mockTimeSeriesReader(100));
       operatorContext
           .getInstanceContext()
           .setDriverContext(new SchemaDriverContext(fragmentInstanceContext, 
schemaRegion));
@@ -134,6 +162,19 @@ public class SchemaCountOperatorTest {
       }
       assertNotNull(tsBlock);
       assertEquals(100, tsBlock.getColumn(0).getLong(0));
+
+      Mockito.when(
+              schemaRegion.getTimeSeriesReader(
+                  SchemaRegionReadPlanFactory.getShowTimeSeriesPlan(
+                      new PartialPath(SCHEMA_COUNT_OPERATOR_TEST_SG + 
".device1.*"),
+                      Collections.emptyMap(),
+                      false,
+                      null,
+                      null,
+                      0,
+                      0,
+                      false)))
+          .thenReturn(mockTimeSeriesReader(10));
       TimeSeriesCountOperator timeSeriesCountOperator2 =
           new TimeSeriesCountOperator(
               planNodeId,
@@ -156,6 +197,28 @@ public class SchemaCountOperatorTest {
     }
   }
 
+  private ISchemaReader<ITimeSeriesSchemaInfo> mockTimeSeriesReader(int 
expectedCount) {
+    List<ITimeSeriesSchemaInfo> timeSeriesSchemaInfoList = new 
ArrayList<>(expectedCount);
+    for (int i = 0; i < expectedCount; i++) {
+      timeSeriesSchemaInfoList.add(Mockito.mock(ITimeSeriesSchemaInfo.class));
+    }
+    Iterator<ITimeSeriesSchemaInfo> iterator = 
timeSeriesSchemaInfoList.iterator();
+    return new ISchemaReader<ITimeSeriesSchemaInfo>() {
+      @Override
+      public void close() throws Exception {}
+
+      @Override
+      public boolean hasNext() {
+        return iterator.hasNext();
+      }
+
+      @Override
+      public ITimeSeriesSchemaInfo next() {
+        return iterator.next();
+      }
+    };
+  }
+
   @Test
   public void testLevelTimeSeriesCountOperator() {
     ExecutorService instanceNotificationExecutor =

Reply via email to