shizy818 commented on code in PR #16353:
URL: https://github.com/apache/iotdb/pull/16353#discussion_r2645065828


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/CteScanOperator.java:
##########
@@ -0,0 +1,134 @@
+/*
+ *
+ *  * 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.queryengine.execution.operator.source.relational;
+
+import org.apache.iotdb.commons.utils.TestOnly;
+import org.apache.iotdb.db.queryengine.execution.MemoryEstimationHelper;
+import org.apache.iotdb.db.queryengine.execution.operator.OperatorContext;
+import 
org.apache.iotdb.db.queryengine.execution.operator.source.SourceOperator;
+import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanNodeId;
+import org.apache.iotdb.db.utils.cte.CteDataReader;
+import org.apache.iotdb.db.utils.cte.CteDataStore;
+import org.apache.iotdb.db.utils.cte.MemoryReader;
+
+import org.apache.tsfile.read.common.block.TsBlock;
+import org.apache.tsfile.utils.RamUsageEstimator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CteScanOperator implements SourceOperator {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(CteScanOperator.class);
+  private static final long INSTANCE_SIZE =
+      RamUsageEstimator.shallowSizeOfInstance(CteScanOperator.class);
+
+  private final OperatorContext operatorContext;
+  private final PlanNodeId sourceId;
+
+  private final CteDataStore dataStore;
+  private final CteDataReader dataReader;
+  private final int dataStoreRefCount;
+
+  public CteScanOperator(
+      OperatorContext operatorContext, PlanNodeId sourceId, CteDataStore 
dataStore) {
+    this.operatorContext = operatorContext;
+    this.sourceId = sourceId;
+    this.dataStore = dataStore;
+    this.dataReader = new MemoryReader(dataStore.getCachedData());
+    this.dataStoreRefCount = dataStore.increaseRefCount();
+  }
+
+  @Override
+  public TsBlock next() throws Exception {
+    if (dataReader == null) {
+      return null;
+    }
+    return dataReader.next();
+  }
+
+  @Override
+  public boolean hasNext() throws Exception {
+    if (dataReader == null) {
+      return false;
+    }
+    return dataReader.hasNext();
+  }
+
+  @Override
+  public void close() throws Exception {
+    try {
+      if (dataReader != null) {
+        dataReader.close();
+      }
+    } catch (Exception e) {
+      LOGGER.error("Fail to close fileChannel", e);
+    }
+  }
+
+  @Override
+  public boolean isFinished() throws Exception {
+    return !hasNextWithTimer();
+  }
+
+  @Override
+  public long calculateMaxPeekMemory() {
+    return calculateRetainedSizeAfterCallingNext() + calculateMaxReturnSize();
+  }
+
+  @Override
+  public long calculateMaxReturnSize() {
+    // The returned object is a reference to TsBlock in CteDataReader
+    return RamUsageEstimator.NUM_BYTES_OBJECT_REF;
+  }
+
+  @Override
+  public long calculateRetainedSizeAfterCallingNext() {
+    return 0L;
+  }
+
+  @Override
+  public long ramBytesUsed() {
+    long bytes =
+        INSTANCE_SIZE
+            + 
MemoryEstimationHelper.getEstimatedSizeOfAccountableObject(operatorContext)
+            + dataReader.bytesUsed();
+    if (dataStoreRefCount == 1) {
+      bytes += dataStore.getCachedBytes();
+    }

Review Comment:
   > what if the FI which this CteScanOperator belongs to has finished, but 
other CteScanOperator with the same dataStore hasn't finished?
   
   Yes currently it just ensures dataStore be counted once.  If the FI which 
this CteScanOperator belongs to has finished, the number of used memory 
decreases. But actually it is still used until other CteScanOperator with the 
same dataStore finish.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to