TINKERPOP-1589 Re-introduced CloseableIterator https://issues.apache.org/jira/browse/TINKERPOP-1589 Add support for the closing of Iterators returned from Vertex.vertices() and Vertex.edges(). Iterators are closed once they are read to completion. Make FlatMapStep implement AutoCloseable in case iterator is not read to completion, should get closed when Traversal is closed. Remove unnecessary null check (null is never an instanceof). OLTP mode support only. More extensive changes required for OLAP. NOTE: Rethrowing checked Exception from close() as unchecked RuntimeException in order to retain Step.reset() and AbstractStep.processNextStart() signatured.
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/c8d5ff9d Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/c8d5ff9d Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/c8d5ff9d Branch: refs/heads/TINKERPOP-1599 Commit: c8d5ff9db6ec8908abfad434c4fb339c5ad53468 Parents: 47e5ae2 Author: PaulJackson123 <[email protected]> Authored: Wed Jan 25 20:43:22 2017 -0500 Committer: PaulJackson123 <[email protected]> Committed: Wed Jan 25 20:43:22 2017 -0500 ---------------------------------------------------------------------- .../process/traversal/step/map/FlatMapStep.java | 24 ++++++++++++++++++-- .../process/traversal/step/map/GraphStep.java | 4 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c8d5ff9d/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java index 3d7dc24..7afdd40 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/FlatMapStep.java @@ -28,7 +28,7 @@ import java.util.Iterator; /** * @author Marko A. Rodriguez (http://markorodriguez.com) */ -public abstract class FlatMapStep<S, E> extends AbstractStep<S, E> { +public abstract class FlatMapStep<S, E> extends AbstractStep<S, E> implements AutoCloseable { private Traverser.Admin<S> head = null; private Iterator<E> iterator = EmptyIterator.instance(); @@ -44,6 +44,7 @@ public abstract class FlatMapStep<S, E> extends AbstractStep<S, E> { return this.head.split(this.iterator.next(), this); } else { this.head = this.starts.next(); + closeIterator(); this.iterator = this.flatMap(this.head); } } @@ -54,6 +55,25 @@ public abstract class FlatMapStep<S, E> extends AbstractStep<S, E> { @Override public void reset() { super.reset(); - this.iterator = EmptyIterator.instance(); + closeIterator(); + } + + @Override + public void close() { + closeIterator(); + } + + private void closeIterator() { + if (this.iterator instanceof AutoCloseable) { + try { + ((AutoCloseable) this.iterator).close(); + } + catch (Exception e) { + throw new RuntimeException(e); + } + finally { + this.iterator = EmptyIterator.instance(); + } + } } } http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/c8d5ff9d/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/GraphStep.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/GraphStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/GraphStep.java index 87935d8..e6b2c38 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/GraphStep.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/GraphStep.java @@ -164,13 +164,13 @@ public class GraphStep<S, E extends Element> extends AbstractStep<S, E> implemen } /** - * Attemps to close an underlying iterator if it is of type {@link CloseableIterator}. Graph providers may choose + * Attempts to close an underlying iterator if it is of type {@link CloseableIterator}. Graph providers may choose * to return this interface containing their vertices and edges if there are expensive resources that might need to * be released at some point. */ @Override public void close() throws Exception { - if (iterator != null && iterator instanceof CloseableIterator) { + if (iterator instanceof CloseableIterator) { ((CloseableIterator) iterator).close(); } }
