org.apache.tinkerpop.gremlin.structure.util.CloseableIterator Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.CloseableIterator. 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: VertexAPI.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@DELETE
@Timed
@Path("{id}")
@Consumes(APPLICATION_JSON)
@RolesAllowed({"admin", "$owner=$graph $action=vertex_delete"})
public void delete(@Context GraphManager manager,
                   @PathParam("graph") String graph,
                   @PathParam("id") String idValue) {
    LOG.debug("Graph [{}] remove vertex by id '{}'", graph, idValue);

    Id id = checkAndParseVertexId(idValue);
    HugeGraph g = graph(manager, graph);
    // TODO: add removeVertex(id) to improve
    commit(g, () -> {
        Iterator<Vertex> iter = g.vertices(id);
        try {
            E.checkArgument(iter.hasNext(),
                            "No such vertex with id: '%s'", idValue);
            iter.next().remove();
        } finally {
            CloseableIterator.closeIterator(iter);
        }
    });
}
 
Example #2
Source File: SubgraphStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private Vertex getOrCreate(final Vertex vertex) {
    final Iterator<Vertex> vertexIterator = subgraph.vertices(vertex.id());

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

    final Vertex subgraphVertex = subgraph.addVertex(T.id, vertex.id(), T.label, vertex.label());

    vertex.properties().forEachRemaining(vertexProperty -> {
        final VertexProperty.Cardinality cardinality = parentGraphFeatures.getCardinality(vertexProperty.key());
        final VertexProperty<?> subgraphVertexProperty = subgraphVertex.property(cardinality, vertexProperty.key(), vertexProperty.value(), T.id, vertexProperty.id());

        // only iterate the VertexProperties if the current graph can have them and if the subgraph can support
        // them. unfortunately we don't have a way to write a test for this as we dont' have a graph that supports
        // user supplied ids and doesn't support metaproperties.
        if (parentGraphFeatures.supportsMetaProperties() && subgraphSupportsMetaProperties) {
            vertexProperty.properties().forEachRemaining(property -> subgraphVertexProperty.property(property.key(), property.value()));
        }
    });
    return subgraphVertex;
}
 
Example #3
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 #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: 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 #6
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 #7
Source File: TraversalUtil.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static final <S, E> boolean test(final Traverser.Admin<S> traverser, final Traversal.Admin<S, E> traversal, E end) {
    if (null == end) return TraversalUtil.test(traverser, traversal);

    final Traverser.Admin<S> split = traverser.split();
    split.setSideEffects(traversal.getSideEffects());
    split.setBulk(1l);
    traversal.reset();
    traversal.addStart(split);
    final Step<?, E> endStep = traversal.getEndStep();
    boolean result = false;
    while (traversal.hasNext()) {
        if (endStep.next().get().equals(end)) {
            result = true;
            break;
        }
    }

    // The traversal might not have been fully consumed in the loop above. Close the traversal to release any underlying
    // resources.
    CloseableIterator.closeIterator(traversal);

    return result;
}
 
Example #8
Source File: CloseableIteratorUtils.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
public static <S, E> Iterator<E> map(final Iterator<S> iterator, final Function<S, E> function) {
    return new CloseableIterator<E>() {
        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

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

        @Override
        public E next() {
            return function.apply(iterator.next());
        }

        @Override
        public void close() {
            CloseableIterator.closeIterator(iterator);
        }
    };
}
 
Example #9
Source File: TraversalUtil.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static final <S, E> boolean test(final S start, final Traversal.Admin<S, E> traversal, final E end) {
    if (null == end) return TraversalUtil.test(start, traversal);

    traversal.reset();
    traversal.addStart(traversal.getTraverserGenerator().generate(start, traversal.getStartStep(), 1l));
    final Step<?, E> endStep = traversal.getEndStep();

    boolean result = false;
    while (traversal.hasNext()) {
        if (endStep.next().get().equals(end)) {
            result = true;
            break;
        }
    }

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

    return result;
}
 
Example #10
Source File: QueryResults.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static <T> T one(Iterator<T> iterator) {
    try {
        if (iterator.hasNext()) {
            T result = iterator.next();
            if (iterator.hasNext()) {
                throw new HugeException("Expect just one result, " +
                                        "but got at least two: [%s, %s]",
                                        result, iterator.next());
            }
            return result;
        }
    } finally {
        CloseableIterator.closeIterator(iterator);
    }
    return null;
}
 
Example #11
Source File: DefaultTraversal.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public E next() {
    try {
        if (!this.locked) this.applyStrategies();
        if (this.lastTraverser.bulk() == 0L)
            this.lastTraverser = this.finalEndStep.next();
        this.lastTraverser.setBulk(this.lastTraverser.bulk() - 1L);
        return this.lastTraverser.get();
    } catch (final FastNoSuchElementException e) {
        // No more elements will be produced by this traversal. Close this traversal
        // and release the resources.
        CloseableIterator.closeIterator(this);

        throw this.isRoot() ? new NoSuchElementException() : e;
    }
}
 
Example #12
Source File: StoreDumper.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public void dump(HugeType table, long offset, long limit) {
    BackendStore store = this.backendStore(table);

    Query query = new Query(table);
    Iterator<BackendEntry> rs = store.query(query);
    for (long i = 0; i < offset && rs.hasNext(); i++) {
        rs.next();
    }
    String title = String.format("Dump table %s (offset %d limit %d):",
                                 table, offset, limit);
    System.out.println(title);
    for (long i = 0; i < limit && rs.hasNext(); i++) {
        BackendEntry entry = rs.next();
        System.out.println(entry);
    }

    CloseableIterator.closeIterator(rs);
}
 
Example #13
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 #14
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 #15
Source File: SchemaTransaction.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
protected <T extends SchemaElement> List<T> getAllSchema(HugeType type) {
    List<T> results = new ArrayList<>();
    Query query = new Query(type);
    Iterator<BackendEntry> entries = this.query(query).iterator();
    try {
        while (entries.hasNext()) {
            results.add(this.deserialize(entries.next(), type));
            Query.checkForceCapacity(results.size());
        }
    } finally {
        CloseableIterator.closeIterator(entries);
    }
    return results;
}
 
Example #16
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 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);
    try {
        return traversal.next(); // map
    } catch (final NoSuchElementException e) {
        throw new IllegalArgumentException("The provided traverser does not map to a value: " + split + "->" + traversal);
    } finally {
        //Close the traversal to release any underlying resources.
        CloseableIterator.closeIterator(traversal);
    }
}
 
Example #17
Source File: RangeGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean filter(final Traverser.Admin<S> traverser) {
    if (this.bypass) return true;

    if (this.high != -1 && this.counter.get() >= this.high) {
        // This is a global step and this place would be the end of the traversal.
        // Close the traversal to free up resources.
        CloseableIterator.closeIterator(traversal);
        throw FastNoSuchElementException.instance();
    }

    long avail = traverser.bulk();
    if (this.counter.get() + avail <= this.low) {
        // Will not surpass the low w/ this traverser. Skip and filter the whole thing.
        this.counter.getAndAdd(avail);
        return false;
    }

    // Skip for the low and trim for the high. Both can happen at once.

    long toSkip = 0;
    if (this.counter.get() < this.low) {
        toSkip = this.low - this.counter.get();
    }

    long toTrim = 0;
    if (this.high != -1 && this.counter.get() + avail >= this.high) {
        toTrim = this.counter.get() + avail - this.high;
    }

    long toEmit = avail - toSkip - toTrim;
    this.counter.getAndAdd(toSkip + toEmit);
    traverser.setBulk(toEmit);

    return true;
}
 
Example #18
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 S start, final Traversal.Admin<S, E> traversal) {
    traversal.reset();
    traversal.addStart(traversal.getTraverserGenerator().generate(start, traversal.getStartStep(), 1l));
    boolean result = traversal.hasNext(); // filter

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

    return result;
}
 
Example #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
Source File: GraphIndexTransaction.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private boolean existUniqueValueInStore(IndexLabel indexLabel,
                                        Object value) {
    ConditionQuery query = new ConditionQuery(HugeType.UNIQUE_INDEX);
    query.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
    query.eq(HugeKeys.FIELD_VALUES, value);
    boolean exist;
    Iterator<BackendEntry> iterator = this.query(query).iterator();
    try {
        exist = iterator.hasNext();
        if (exist) {
            HugeIndex index = this.serializer.readIndex(graph(), query,
                                                        iterator.next());
            this.removeExpiredIndexIfNeeded(index, query.showExpired());
            // Memory backend might return empty BackendEntry
            if (index.elementIds().isEmpty()) {
                return false;
            }
            LOG.debug("Already has existed unique index record {}",
                      index.elementId());
        }
        while (iterator.hasNext()) {
            LOG.warn("Unique constraint conflict found by record {}",
                     iterator.next());
        }
    } finally {
        CloseableIterator.closeIterator(iterator);
    }
    return exist;
}
 
Example #26
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 #27
Source File: GraphIndexTransaction.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Watched(prefix = "index")
private PageIds doIndexQueryOnce(IndexLabel indexLabel,
                                 ConditionQuery query) {
    // Query all or one page
    Iterator<BackendEntry> entries = null;
    LockUtil.Locks locks = new LockUtil.Locks(this.graphName());
    try {
        locks.lockReads(LockUtil.INDEX_LABEL_DELETE, indexLabel.id());
        locks.lockReads(LockUtil.INDEX_LABEL_REBUILD, indexLabel.id());

        Set<Id> ids = InsertionOrderUtil.newSet();
        entries = super.query(query).iterator();
        while (entries.hasNext()) {
            HugeIndex index = this.serializer.readIndex(graph(), query,
                                                        entries.next());
            this.removeExpiredIndexIfNeeded(index, query.showExpired());
            ids.addAll(index.elementIds());
            if (query.reachLimit(ids.size())) {
                break;
            }
            Query.checkForceCapacity(ids.size());
        }
        // If there is no data, the entries is not a Metadatable object
        if (ids.isEmpty()) {
            return PageIds.EMPTY;
        }
        // NOTE: Memory backend's iterator is not Metadatable
        if (!query.paging()) {
            return new PageIds(ids, PageState.EMPTY);
        }
        E.checkState(entries instanceof Metadatable,
                     "The entries must be Metadatable when query " +
                     "in paging, but got '%s'",
                     entries.getClass().getName());
        return new PageIds(ids, PageInfo.pageState(entries));
    } finally {
        locks.unlock();
        CloseableIterator.closeIterator(entries);
    }
}
 
Example #28
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 #29
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 #30
Source File: GraphTransaction.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private void checkVertexExistIfCustomizedId(Map<Id, HugeVertex> vertices) {
    Set<Id> ids = new HashSet<>();
    for (HugeVertex vertex : vertices.values()) {
        VertexLabel vl = vertex.schemaLabel();
        if (!vl.hidden() && vl.idStrategy().isCustomized()) {
            ids.add(vertex.id());
        }
    }
    if (ids.isEmpty()) {
        return;
    }
    IdQuery idQuery = new IdQuery(HugeType.VERTEX, ids);
    Iterator<HugeVertex> results = this.queryVerticesFromBackend(idQuery);
    try {
        if (!results.hasNext()) {
            return;
        }
        HugeVertex existedVertex = results.next();
        HugeVertex newVertex = vertices.get(existedVertex.id());
        if (!existedVertex.label().equals(newVertex.label())) {
            throw new HugeException(
                      "The newly added vertex with id:'%s' label:'%s' " +
                      "is not allowed to insert, because already exist " +
                      "a vertex with same id and different label:'%s'",
                      newVertex.id(), newVertex.label(),
                      existedVertex.label());
        }
    } finally {
        CloseableIterator.closeIterator(results);
    }
}