public static final <S> Iterable<S> filter(final Iterable<S> iterable, final Predicate<S> predicate) { return () -> IteratorUtils.filter(iterable.iterator(), predicate); }
@Override public <V> Iterator<? extends Property<V>> properties(final String... propertyKeys) { return (Iterator) IteratorUtils.filter(this.element.properties(propertyKeys), property -> !computeKeys.contains(property.key())); }
private boolean sameInVertices(final Vertex v, final List<Vertex> inVertices) { return inVertices.stream() .allMatch(inVertex -> IteratorUtils.filter(v.vertices(Direction.IN), hv -> hv.value("oid").equals(inVertex.value("oid"))).hasNext()); }
private boolean sameOutVertices(final Vertex v, final List<Vertex> outVertices) { return outVertices.stream() .allMatch(outVertex -> IteratorUtils.filter(v.vertices(Direction.OUT), hv -> hv.value("oid").equals(outVertex.value("oid"))).hasNext()); } }
private <T extends Element> Iterator<T> createElementIterator(final Class<T> clazz, final Map<Object, T> elements, final IdManager idManager, final Object... ids) { final Iterator<T> iterator; if (0 == ids.length) { iterator = elements.values().iterator(); } else { final List<Object> idList = Arrays.asList(ids); validateHomogenousIds(idList); // if the type is of Element - have to look each up because it might be an Attachable instance or // other implementation. the assumption is that id conversion is not required for detached // stuff - doesn't seem likely someone would detach a Titan vertex then try to expect that // vertex to be findable in OrientDB return clazz.isAssignableFrom(ids[0].getClass()) ? IteratorUtils.filter(IteratorUtils.map(idList, id -> elements.get(clazz.cast(id).id())).iterator(), Objects::nonNull) : IteratorUtils.filter(IteratorUtils.map(idList, id -> elements.get(idManager.convert(id))).iterator(), Objects::nonNull); } return TinkerHelper.inComputerMode(this) ? (Iterator<T>) (clazz.equals(Vertex.class) ? IteratorUtils.filter((Iterator<Vertex>) iterator, t -> this.graphComputerView.legalVertex(t)) : IteratorUtils.filter((Iterator<Edge>) iterator, t -> this.graphComputerView.legalEdge(t.outVertex(), t))) : iterator; }
private HasContainer getIndexKey(final Class<? extends Element> indexedClass) { final Set<String> indexedKeys = ((TinkerGraph) this.getTraversal().getGraph().get()).getIndexedKeys(indexedClass); final Iterator<HasContainer> itty = IteratorUtils.filter(hasContainers.iterator(), c -> c.getPredicate().getBiPredicate() == Compare.eq && indexedKeys.contains(c.getKey())); return itty.hasNext() ? itty.next() : null; }
private Iterator<? extends Edge> edges() { if (null == this.ids) return Collections.emptyIterator(); return IteratorUtils.filter(this.getTraversal().getGraph().get().edges(this.ids), edge -> HasContainer.testAll(edge, this.hasContainers)); }
@Override public Iterator<Edge> edges(final Direction direction, final String... edgeLabels) { final Iterator<Edge> edgeIterator = (Iterator) TinkerHelper.getEdges(this, direction, edgeLabels); return TinkerHelper.inComputerMode(this.graph) ? IteratorUtils.filter(edgeIterator, edge -> this.graph.graphComputerView.legalEdge(this, edge)) : edgeIterator; }
public static JavaPairRDD<Object, VertexWritable> applyGraphFilter(final JavaPairRDD<Object, VertexWritable> graphRDD, final GraphFilter graphFilter) { return graphRDD.mapPartitionsToPair(partitionIterator -> { final GraphFilter gFilter = graphFilter.clone(); return IteratorUtils.filter(partitionIterator, tuple -> (tuple._2().get().applyGraphFilter(gFilter)).isPresent()); }, true); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper(); final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final String json = mapper.writeValueAsString(vp); final Map<String, Object> m = mapper.readValue(json, mapTypeReference); assertEquals(vp.label(), m.get(GraphSONTokens.LABEL)); assertNotNull(m.get(GraphSONTokens.ID)); assertEquals(vp.value(), m.get(GraphSONTokens.VALUE)); assertEquals(vp.values("startTime").next(), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("startTime")); assertEquals(vp.values("endTime").next(), ((Map) m.get(GraphSONTokens.PROPERTIES)).get("endTime")); }
@Override public <V> Iterator<Property<V>> properties(final String... propertyKeys) { this.graph.tx().readWrite(); Iterable<String> keys = this.baseElement.getKeys(); Iterator<String> filter = IteratorUtils.filter(keys.iterator(), key -> ElementHelper.keyExists(key, propertyKeys)); return IteratorUtils.map(filter, key -> new Neo4jProperty<>(this, key, (V) this.baseElement.getProperty(key))); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldHashAndEqualsCorrectly() { final Vertex gremlin = g.V(convertToVertexId("gremlin")).next(); final StarGraph gremlinStarGraph = StarGraph.of(gremlin); final StarGraph.StarVertex gremlinStar = gremlinStarGraph.getStarVertex(); final Vertex marko = g.V(convertToVertexId("marko")).next(); final StarGraph markoStarGraph = StarGraph.of(marko); final StarGraph.StarAdjacentVertex gremlinStarAdjacentGraph = (StarGraph.StarAdjacentVertex) IteratorUtils.filter(markoStarGraph.getStarVertex().edges(Direction.OUT, "uses"), x -> x.inVertex().id().equals(convertToVertexId("gremlin"))).next().inVertex(); final Set<Vertex> set = new HashSet<>(); for (int i = 0; i < 100; i++) { set.add(gremlin); set.add(gremlinStar); set.add(gremlinStarAdjacentGraph); } assertEquals(1, set.size()); }
private Iterator<? extends Vertex> vertices() { final TinkerGraph graph = (TinkerGraph) this.getTraversal().getGraph().get(); final HasContainer indexedContainer = getIndexKey(Vertex.class); // ids are present, filter on them first if (null == this.ids) return Collections.emptyIterator(); else if (this.ids.length > 0) return this.iteratorList(graph.vertices(this.ids)); else return null == indexedContainer ? this.iteratorList(graph.vertices()) : IteratorUtils.filter(TinkerHelper.queryVertexIndex(graph, indexedContainer.getKey(), indexedContainer.getPredicate().getValue()).iterator(), vertex -> HasContainer.testAll(vertex, this.hasContainers)); }
/** * Asserts that two graphs are the "same" in way of structure. It assumes that the graphs both have vertices * with an "oid" property that is an Integer value. It iterates each vertex in graph 1, using the "oid" to * lookup vertices in graph 2. For each one found it checks both IN and OUT vertices to ensure that they * attach to the same IN/OUT vertices given their "oid" properties. */ protected boolean same(final Graph g1, final Graph g2) { return IteratorUtils.stream(g1.vertices()) .map(v -> Triplet.<Integer, List<Vertex>, List<Vertex>>with(v.value("oid"), IteratorUtils.list(v.vertices(Direction.IN)), IteratorUtils.list(v.vertices(Direction.OUT)))) .allMatch(p -> { final Vertex v = IteratorUtils.filter(g2.vertices(), vx -> vx.value("oid").equals(p.getValue0())).next(); return sameInVertices(v, p.getValue1()) && sameOutVertices(v, p.getValue2()); }); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithPropertiesAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final VertexProperty<?> vertexProperty = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, vertexProperty); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final VertexProperty<?> detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class); assertNotNull(detached); assertEquals(vertexProperty.label(), detached.label()); assertEquals(vertexProperty.id(), detached.id()); assertEquals(vertexProperty.value(), detached.value()); assertEquals(vertexProperty.values("startTime").next(), detached.values("startTime").next()); assertEquals(vertexProperty.properties("startTime").next().key(), detached.properties("startTime").next().key()); assertEquals(vertexProperty.values("endTime").next(), detached.values("endTime").next()); assertEquals(vertexProperty.properties("endTime").next().key(), detached.properties("endTime").next().key()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithPropertiesAsDetached() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final VertexProperty<?> vertexProperty = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, vertexProperty); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final VertexProperty<?> detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class); assertNotNull(detached); assertEquals(vertexProperty.label(), detached.label()); assertEquals(vertexProperty.id(), detached.id()); assertEquals(vertexProperty.value(), detached.value()); assertEquals(vertexProperty.values("startTime").next(), detached.values("startTime").next()); assertEquals(vertexProperty.properties("startTime").next().key(), detached.properties("startTime").next().key()); assertEquals(vertexProperty.values("endTime").next(), detached.values("endTime").next()); assertEquals(vertexProperty.properties("endTime").next().key(), detached.properties("endTime").next().key()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final String json = mapper.writeValueAsString(vp); final VertexProperty<?> detached = mapper.readValue(json, VertexProperty.class); assertNotNull(detached); assertEquals(vp.label(), detached.label()); assertEquals(vp.id(), detached.id()); assertEquals(vp.value(), detached.value()); assertEquals(vp.values("startTime").next(), detached.values("startTime").next()); assertEquals(((Property) vp.properties("startTime").next()).key(), ((Property) detached.properties("startTime").next()).key()); assertEquals(vp.values("endTime").next(), detached.values("endTime").next()); assertEquals(((Property) vp.properties("endTime").next()).key(), ((Property) detached.properties("endTime").next()).key()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final String json = mapper.writeValueAsString(vp); final VertexProperty<?> detached = mapper.readValue(json, VertexProperty.class); assertNotNull(detached); assertEquals(vp.label(), detached.label()); assertEquals(vp.id(), detached.id()); assertEquals(vp.value(), detached.value()); assertEquals(vp.values("startTime").next(), detached.values("startTime").next()); assertEquals(((Property) vp.properties("startTime").next()).key(), ((Property) detached.properties("startTime").next()).key()); assertEquals(vp.values("endTime").next(), detached.values("endTime").next()); assertEquals(((Property) vp.properties("endTime").next()).key(), ((Property) detached.properties("endTime").next()).key()); }
@Override public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) { this.graph.tx().readWrite(); return new Iterator<Vertex>() { final Iterator<Neo4jRelationship> relationshipIterator = IteratorUtils.filter(0 == edgeLabels.length ? BOTH == direction ? IteratorUtils.concat(getBaseVertex().relationships(Neo4jHelper.mapDirection(OUT)).iterator(), getBaseVertex().relationships(Neo4jHelper.mapDirection(IN)).iterator()) : getBaseVertex().relationships(Neo4jHelper.mapDirection(direction)).iterator() : BOTH == direction ? IteratorUtils.concat(getBaseVertex().relationships(Neo4jHelper.mapDirection(OUT), (edgeLabels)).iterator(), getBaseVertex().relationships(Neo4jHelper.mapDirection(IN), (edgeLabels)).iterator()) : getBaseVertex().relationships(Neo4jHelper.mapDirection(direction), (edgeLabels)).iterator(), graph.trait.getRelationshipPredicate()); @Override public boolean hasNext() { return this.relationshipIterator.hasNext(); } @Override public Neo4jVertex next() { return new Neo4jVertex(this.relationshipIterator.next().other(getBaseVertex()), graph); } }; }
@Override public TraversalVertexProgram generateProgram(final Graph graph, final Memory memory) { final Traversal.Admin<?, ?> computerSpecificTraversal = this.computerTraversal.getPure(); final TraversalStrategies computerSpecificStrategies = this.getTraversal().getStrategies().clone(); IteratorUtils.filter(TraversalStrategies.GlobalCache.getStrategies(graph.getClass()).toList(), s -> s instanceof TraversalStrategy.ProviderOptimizationStrategy).forEach(computerSpecificStrategies::addStrategies); computerSpecificTraversal.setStrategies(computerSpecificStrategies); computerSpecificTraversal.setSideEffects(new MemoryTraversalSideEffects(this.getTraversal().getSideEffects())); computerSpecificTraversal.setParent(this); final TraversalVertexProgram.Builder builder = TraversalVertexProgram.build().traversal(computerSpecificTraversal); if (memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS)) builder.haltedTraversers(memory.get(TraversalVertexProgram.HALTED_TRAVERSERS)); return builder.create(graph); }