Java Code Examples for org.apache.tinkerpop.gremlin.structure.util.CloseableIterator#closeIterator()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.CloseableIterator#closeIterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Traversal.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate all the {@link Traverser} instances in the traversal.
 * What is returned is the empty traversal.
 * It is assumed that what is desired from the computation is are the sideEffects yielded by the traversal.
 *
 * @return the fully drained traversal
 */
public default <A, B> Traversal<A, B> iterate() {
    try {
        if (!this.asAdmin().isLocked()) {
            this.none();
            this.asAdmin().applyStrategies();
        }
        // use the end step so the results are bulked
        final Step<?, E> endStep = this.asAdmin().getEndStep();
        while (true) {
            endStep.next();
        }
    } catch (final NoSuchElementException ignored) {
    } finally {
        CloseableIterator.closeIterator(this);
    }
    return (Traversal<A, B>) this;
}
 
Example 2
Source File: GraphMLWriter.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static Map<String, String> determineVertexTypes(final Graph graph) {
    final Map<String, String> vertexKeyTypes = new HashMap<>();
    final Iterator<Vertex> vertices = graph.vertices();
    try {
        while (vertices.hasNext()) {
            final Vertex vertex = vertices.next();
            for (String key : vertex.keys()) {
                if (!vertexKeyTypes.containsKey(key)) {
                    final VertexProperty<Object> currentValue = getCheckedVertexProperty(vertex, key);

                    vertexKeyTypes.put(key, GraphMLWriter.getStringType(currentValue.value()));
                }
            }
        }
    } finally {
        CloseableIterator.closeIterator(vertices);
    }

    return vertexKeyTypes;
}
 
Example 3
Source File: GraphMLWriter.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static Map<String, String> determineEdgeTypes(final Graph graph) {
    final Map<String, String> edgeKeyTypes = new HashMap<>();
    final Iterator<Edge> edges = graph.edges();
    try {
        while (edges.hasNext()) {
            final Edge edge = edges.next();
            for (String key : edge.keys()) {
                if (!edgeKeyTypes.containsKey(key))
                    edgeKeyTypes.put(key, GraphMLWriter.getStringType(edge.property(key).value()));
            }
        }
    } finally {
        CloseableIterator.closeIterator(edges);
    }

    return edgeKeyTypes;
}
 
Example 4
Source File: Traversal.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Add all the results of the traversal to the provided collection.
 *
 * @param collection the collection to fill
 * @return the collection now filled
 */
public default <C extends Collection<E>> C fill(final C collection) {
    try {
        if (!this.asAdmin().isLocked()) this.asAdmin().applyStrategies();
        // use the end step so the results are bulked
        final Step<?, E> endStep = this.asAdmin().getEndStep();
        while (true) {
            final Traverser<E> traverser = endStep.next();
            TraversalHelper.addToCollection(collection, traverser.get(), traverser.bulk());
        }
    } catch (final NoSuchElementException ignored) {
    } finally {
        CloseableIterator.closeIterator(this);
    }
    return collection;
}
 
Example 5
Source File: TraversalUtil.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static final <S, E> boolean test(final Traverser.Admin<S> traverser, final Traversal.Admin<S, E> traversal) {
    final Traverser.Admin<S> split = traverser.split();
    split.setSideEffects(traversal.getSideEffects());
    split.setBulk(1l);
    traversal.reset();
    traversal.addStart(split);
    boolean val =  traversal.hasNext(); // filter

    //Close the traversal to release any underlying resources.
    CloseableIterator.closeIterator(traversal);

    return val;
}
 
Example 6
Source File: SubgraphStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void addEdgeToSubgraph(final Edge edge) {
    final Iterator<Edge> edgeIterator = subgraph.edges(edge.id());

    try {
        if (edgeIterator.hasNext()) return;
    } finally {
        CloseableIterator.closeIterator(edgeIterator);
    }

    final Iterator<Vertex> vertexIterator = edge.vertices(Direction.BOTH);
    final Vertex subGraphOutVertex = getOrCreate(vertexIterator.next());
    final Vertex subGraphInVertex = getOrCreate(vertexIterator.next());
    final Edge subGraphEdge = subGraphOutVertex.addEdge(edge.label(), subGraphInVertex, T.id, edge.id());
    edge.properties().forEachRemaining(property -> subGraphEdge.property(property.key(), property.value()));
}
 
Example 7
Source File: Traversal.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public default void forEachRemaining(final Consumer<? super E> action) {
    try {
        while (true) {
            action.accept(next());
        }
    } catch (final NoSuchElementException ignore) {

    }  finally {
        CloseableIterator.closeIterator(this);
    }
}
 
Example 8
Source File: GraphComputerTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldSupportTransientKeys() throws Exception {
    final ComputerResult result = graphProvider.getGraphComputer(graph).program(new VertexProgramO()).mapReduce(new MapReduceK()).submit().get();
    result.graph().vertices().forEachRemaining(vertex -> {
        assertFalse(vertex.property("v1").isPresent());
        assertFalse(vertex.property("v2").isPresent());
        assertTrue(vertex.property("v3").isPresent());
        assertEquals("shouldExist", vertex.value("v3"));
        assertTrue(vertex.property("name").isPresent());
        if (vertex.label().equals("software"))
            assertTrue(vertex.property("lang").isPresent());
        else
            assertTrue(vertex.property("age").isPresent());
        assertEquals(3, IteratorUtils.count(vertex.properties()));
        assertEquals(0, IteratorUtils.count(vertex.properties("v1")));
        assertEquals(0, IteratorUtils.count(vertex.properties("v2")));
        assertEquals(1, IteratorUtils.count(vertex.properties("v3")));
        assertEquals(1, IteratorUtils.count(vertex.properties("name")));
    });
    assertEquals(6l, result.graph().traversal().V().properties("name").count().next().longValue());
    assertEquals(0l, result.graph().traversal().V().properties("v1").count().next().longValue());
    assertEquals(0l, result.graph().traversal().V().properties("v2").count().next().longValue());
    assertEquals(6l, result.graph().traversal().V().properties("v3").count().next().longValue());
    assertEquals(6l, result.graph().traversal().V().<String>values("name").dedup().count().next().longValue());
    assertEquals(1l, result.graph().traversal().V().<String>values("v3").dedup().count().next().longValue());

    final Traversal<Vertex,String> t = result.graph().traversal().V().<String>values("v3").dedup();
    assertEquals("shouldExist", t.next());
    CloseableIterator.closeIterator(t);

    ///
    assertFalse(result.memory().exists("m1"));
    assertFalse(result.memory().exists("m2"));
    assertTrue(result.memory().exists("m3"));
    assertEquals(24l, result.memory().<Long>get("m3").longValue());
    assertEquals(2, result.memory().keys().size());  // mapReduceK
}
 
Example 9
Source File: QueryResults.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static <T> void fillList(Iterator<T> iterator, List<T> list) {
    try {
        while (iterator.hasNext()) {
            T result = iterator.next();
            list.add(result);
            Query.checkForceCapacity(list.size());
        }
    } finally {
        CloseableIterator.closeIterator(iterator);
    }
}
 
Example 10
Source File: TraversalUtil.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static final <S, E> E apply(final S start, final Traversal.Admin<S, E> traversal) {
    traversal.reset();
    traversal.addStart(traversal.getTraverserGenerator().generate(start, traversal.getStartStep(), 1l));
    try {
        return traversal.next(); // map
    } catch (final NoSuchElementException e) {
        throw new IllegalArgumentException("The provided start does not map to a value: " + start + "->" + traversal);
    } finally {
        //Close the traversal to release any underlying resources.
        CloseableIterator.closeIterator(traversal);
    }
}
 
Example 11
Source File: IdHolder.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    if (this.exhausted) {
        return;
    }
    this.exhausted = true;

    CloseableIterator.closeIterator(this.entries);
}
 
Example 12
Source File: Traversal.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * A traversal can be rewritten such that its defined end type E may yield objects of a different type.
 * This helper method allows for the casting of the output to the known the type.
 *
 * @param endType  the true output type of the traversal
 * @param consumer a {@link Consumer} to process each output
 * @param <E2>     the known output type of the traversal
 */
public default <E2> void forEachRemaining(final Class<E2> endType, final Consumer<E2> consumer) {
    try {
        while (true) {
            consumer.accept((E2) next());
        }
    } catch (final NoSuchElementException ignore) {

    }  finally {
        CloseableIterator.closeIterator(this);
    }
}
 
Example 13
Source File: API.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
protected static void checkExist(Iterator<?> iter,
                                 HugeType type,
                                 String id) {
    if (!iter.hasNext()) {
        try {
            CloseableIterator.closeIterator(iter);
        } catch (Exception ignored) {}

        throw new NotFoundException(String.format(
                  "%s with id '%s' does not exist",
                  type.readableName(), id));
    }
}
 
Example 14
Source File: TinkerGraphStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private <E extends Element> Iterator<E> iteratorList(final Iterator<E> iterator) {
    final List<E> list = new ArrayList<>();
    while (iterator.hasNext()) {
        final E e = iterator.next();
        if (HasContainer.testAll(e, this.hasContainers))
            list.add(e);
    }

    // close the old iterator to release resources since we are returning a new iterator (over list)
    // out of this function.
    CloseableIterator.closeIterator(iterator);

    return new TinkerGraphIterator<>(list.iterator());
}
 
Example 15
Source File: CloseableIteratorUtils.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
public static <S, E> Iterator<E> flatMap(final Iterator<S> iterator, final Function<S, Iterator<E>> function) {
    return new CloseableIterator<E>() {

        private Iterator<E> currentIterator = Collections.emptyIterator();

        @Override
        public boolean hasNext() {
            if (this.currentIterator.hasNext())
                return true;
            else {
                while (iterator.hasNext()) {
                    this.currentIterator = function.apply(iterator.next());
                    if (this.currentIterator.hasNext())
                        return true;
                }
            }
            return false;
        }

        @Override
        public void remove() {
            iterator.remove();
        }

        @Override
        public E next() {
            if (this.hasNext())
                return this.currentIterator.next();
            else
                throw FastNoSuchElementException.instance();
        }

        @Override
        public void close() {
            CloseableIterator.closeIterator(iterator);
        }
    };
}
 
Example 16
Source File: SqlgFlatMapStep.java    From sqlg with MIT License 4 votes vote down vote up
protected void closeIterator() {
    CloseableIterator.closeIterator(iterator);
}
 
Example 17
Source File: CloseableIteratorUtils.java    From hgraphdb with Apache License 2.0 4 votes vote down vote up
public static <S> Iterator<S> filter(final Iterator<S> iterator, final Predicate<S> predicate) {
    return new CloseableIterator<S>() {
        S nextResult = null;

        @Override
        public boolean hasNext() {
            if (null != this.nextResult) {
                return true;
            } else {
                advance();
                return null != this.nextResult;
            }
        }

        @Override
        public void remove() {
            iterator.remove();
        }

        @Override
        public S next() {
            try {
                if (null != this.nextResult) {
                    return this.nextResult;
                } else {
                    advance();
                    if (null != this.nextResult)
                        return this.nextResult;
                    else
                        throw FastNoSuchElementException.instance();
                }
            } finally {
                this.nextResult = null;
            }
        }

        private void advance() {
            this.nextResult = null;
            while (iterator.hasNext()) {
                final S s = iterator.next();
                if (predicate.test(s)) {
                    this.nextResult = s;
                    return;
                }
            }
        }

        @Override
        public void close() {
            CloseableIterator.closeIterator(iterator);
        }
    };
}
 
Example 18
Source File: PageEntryIterator.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private void closePageResults() {
    if (this.pageResults != QueryList.PageResults.EMPTY) {
        CloseableIterator.closeIterator(this.pageResults.get());
    }
}
 
Example 19
Source File: SubgraphStrategyProcessTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldFilterEdgeCriterion() throws Exception {
    final Traversal<Edge, ?> edgeCriterion = __.or(
            has("weight", 1.0d).hasLabel("knows"), // 8
            has("weight", 0.4d).hasLabel("created").outV().has("name", "marko"), // 9
            has("weight", 1.0d).hasLabel("created") // 10
    );

    final SubgraphStrategy strategy = SubgraphStrategy.build().edges(edgeCriterion).create();
    final GraphTraversalSource sg = g.withStrategies(strategy);

    // all vertices are here
    assertEquals(6, g.V().count().next().longValue());
    final Traversal t = sg.V();
    t.hasNext();
    printTraversalForm(t);
    CloseableIterator.closeIterator(t);
    assertEquals(6, sg.V().count().next().longValue());

    // only the given edges are included
    assertEquals(6, g.E().count().next().longValue());
    assertEquals(3, sg.E().count().next().longValue());

    assertEquals(2, g.V(convertToVertexId("marko")).outE("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("marko")).outE("knows").count().next().longValue());

    // wrapped Traversal<Vertex, Vertex> takes into account the edges it must pass through
    assertEquals(2, g.V(convertToVertexId("marko")).out("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("marko")).out("knows").count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());

    // from vertex

    assertEquals(2, g.V(convertToVertexId("josh")).outE().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).outE().count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).out().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).out().count().next().longValue());

    assertEquals(1, g.V(convertToVertexId("josh")).inE().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).inE().count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).in().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).in().count().next().longValue());

    assertEquals(3, g.V(convertToVertexId("josh")).bothE().count().next().longValue());
    assertEquals(2, sg.V(convertToVertexId("josh")).bothE().count().next().longValue());
    assertEquals(3, g.V(convertToVertexId("josh")).both().count().next().longValue());
    assertEquals(2, sg.V(convertToVertexId("josh")).both().count().next().longValue());

    // with label

    assertEquals(2, g.V(convertToVertexId("josh")).outE("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).outE("created").count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).out("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).out("created").count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).bothE("created").count().next().longValue());
    assertEquals(2, g.V(convertToVertexId("josh")).both("created").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).both("created").count().next().longValue());

    assertEquals(1, g.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).inE("knows").count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).in("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).in("knows").count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).bothE("knows").count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).both("knows").count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).both("knows").count().next().longValue());

    // with branch factor

    assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).inV().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).limit(1).local(bothE().limit(1)).inV().count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).count().next().longValue());
    assertEquals(1, g.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).inV().count().next().longValue());
    assertEquals(1, sg.V(convertToVertexId("josh")).local(bothE("knows", "created").limit(1)).inV().count().next().longValue());

    // from edge

    assertEquals(2, g.E(convertToEdgeId("marko", "knows", "josh")).bothV().count().next().longValue());
    assertEquals(2, sg.E(convertToEdgeId("marko", "knows", "josh")).bothV().count().next().longValue());

    assertEquals(3, g.E(convertToEdgeId("marko", "knows", "josh")).outV().outE().count().next().longValue());
    assertEquals(2, sg.E(convertToEdgeId("marko", "knows", "josh")).outV().outE().count().next().longValue());
}
 
Example 20
Source File: GraphStep.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * 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() {
    CloseableIterator.closeIterator(iterator);
}