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

zhangyi89817 pushed a commit to branch close-iterator
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git

commit ead9a00df5f0cc7388b105305bb22e0a23b9b3b1
Author: zhoney <[email protected]>
AuthorDate: Mon Apr 11 17:48:38 2022 +0800

    close LimitIterator when reach limit (#410)
---
 .../hugegraph/backend/store/BackendEntryIterator.java     | 10 ++++++++++
 .../baidu/hugegraph/backend/tx/GraphIndexTransaction.java | 11 +++++++++++
 .../com/baidu/hugegraph/backend/tx/GraphTransaction.java  | 15 ++++++++++++++-
 3 files changed, 35 insertions(+), 1 deletion(-)

diff --git 
a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendEntryIterator.java
 
b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendEntryIterator.java
index 151b7fa8a..c0bf7f31c 100644
--- 
a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendEntryIterator.java
+++ 
b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/store/BackendEntryIterator.java
@@ -52,6 +52,11 @@ public abstract class BackendEntryIterator implements 
CIter<BackendEntry> {
     @Override
     public boolean hasNext() {
         if (this.reachLimit()) {
+            try {
+                this.close();
+            } catch (Exception e) {
+                throw new BackendException("Failed to close iterator", e);
+            }
             return false;
         }
 
@@ -69,6 +74,11 @@ public abstract class BackendEntryIterator implements 
CIter<BackendEntry> {
 
         // Stop if reach limit
         if (this.reachLimit()) {
+            try {
+                this.close();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
             throw new NoSuchElementException();
         }
 
diff --git 
a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java
 
b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java
index 26b896d46..9db7d5476 100644
--- 
a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java
+++ 
b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java
@@ -69,6 +69,7 @@ import com.baidu.hugegraph.config.HugeConfig;
 import com.baidu.hugegraph.exception.NoIndexException;
 import com.baidu.hugegraph.exception.NotAllowException;
 import com.baidu.hugegraph.exception.NotSupportException;
+import com.baidu.hugegraph.iterator.CIter;
 import com.baidu.hugegraph.iterator.Metadatable;
 import com.baidu.hugegraph.job.EphemeralJob;
 import com.baidu.hugegraph.job.EphemeralJobBuilder;
@@ -710,6 +711,16 @@ public class GraphIndexTransaction extends 
AbstractTransaction {
                 this.removeExpiredIndexIfNeeded(index, query.showExpired());
                 ids.addAll(index.elementIds());
                 if (query.reachLimit(ids.size())) {
+                    try {
+                        if (entries instanceof CIter) {
+                            ((CIter<?>) entries).close();
+                        } else {
+                            LOG.warn("Failed to close iterator {}({})",
+                                     entries, entries.getClass());
+                        }
+                    } catch (Exception e) {
+                        throw new HugeException("Failed to close iterator", e);
+                    }
                     break;
                 }
                 Query.checkForceCapacity(ids.size());
diff --git 
a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java
 
b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java
index 2dbd76776..dac8ee91e 100644
--- 
a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java
+++ 
b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphTransaction.java
@@ -1753,7 +1753,20 @@ public class GraphTransaction extends 
IndexableTransaction {
         // Stop if reach limit
         return new LimitIterator<>(results, elem -> {
             long count = query.goOffset(1L);
-            return query.reachLimit(count - 1L);
+            if (query.reachLimit(count - 1L)) {
+                try {
+                    if (results instanceof CIter) {
+                        ((CIter<?>) results).close();
+                    } else {
+                        LOG.warn("Failed to close iterator {}({})", results,
+                                 results.getClass());
+                    }
+                } catch (Exception e) {
+                    throw new HugeException("Failed to close iterator", e);
+                }
+                return true;
+            }
+            return false;
         });
     }
 

Reply via email to