@Override public <V> Iterator<Property<V>> properties(final String... keyNames) { verifyAccess(); Stream<PropertyKey> keys; if (keyNames==null || keyNames.length==0) { keys = IteratorUtils.stream(it().getPropertyKeysDirect().iterator()); } else { keys = Stream.of(keyNames) .map(s -> tx().getPropertyKey(s)).filter(rt -> rt != null && getValueDirect(rt)!=null); } return keys.map( rt -> (Property<V>)new SimpleTitanProperty<V>(this,rt,valueInternal(rt))).iterator(); }
@Override public <V> Iterator<Property<V>> properties(final String... keyNames) { verifyAccess(); Stream<PropertyKey> keys; if (keyNames == null || keyNames.length == 0) { keys = IteratorUtils.stream(it().getPropertyKeysDirect().iterator()); } else { keys = Stream.of(keyNames) .map(s -> tx().getPropertyKey(s)).filter(rt -> rt != null && getValueDirect(rt) != null); } return keys.map(rt -> (Property<V>) new SimpleJanusGraphProperty<V>(this, rt, valueInternal(rt))).iterator(); }
@Override public void process(StaticBuffer key, Map<SliceQuery, EntryList> entries, ScanMetrics metrics) { //The queries are already tailored enough => everything should be removed try { BackendTransaction mutator = writeTx.getTxHandle(); final List<Entry> deletions; if (entries.size()==1) deletions = Iterables.getOnlyElement(entries.values()); else { int size = IteratorUtils.stream(entries.values().iterator()).map( e -> e.size()).reduce(0, (x,y) -> x+y); deletions = new ArrayList<>(size); entries.values().forEach(e -> deletions.addAll(e)); } metrics.incrementCustom(DELETED_RECORDS_COUNT,deletions.size()); if (isRelationTypeIndex()) { mutator.mutateEdges(key, KCVSCache.NO_ADDITIONS, deletions); } else { mutator.mutateIndex(key, KCVSCache.NO_ADDITIONS, deletions); } } catch (final Exception e) { mgmt.rollback(); writeTx.rollback(); metrics.incrementCustom(FAILED_TX); throw new TitanException(e.getMessage(), e); } }
public Stream<M> receiveMessages(MessageScope messageScope) { if (messageScope instanceof MessageScope.Global) { M message = vertexMemory.getMessage(vertexId,messageScope); if (message == null) return Stream.empty(); else return Stream.of(message); } else { final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope; final Traversal<Vertex, Edge> reverseIncident = FulgoraUtil.getReverseElementTraversal(localMessageScope,vertex,vertex.tx()); final BiFunction<M,Edge,M> edgeFct = localMessageScope.getEdgeFunction(); return IteratorUtils.stream(reverseIncident) .map(e -> { M msg = vertexMemory.getMessage(vertexMemory.getCanonicalId(((TitanEdge) e).otherVertex(vertex).longId()), localMessageScope); return msg == null ? null : edgeFct.apply(msg, e); }) .filter(m -> m != null); } }
@Override public void process(StaticBuffer key, Map<SliceQuery, EntryList> entries, ScanMetrics metrics) { //The queries are already tailored enough => everything should be removed try { BackendTransaction mutator = writeTx.getTxHandle(); final List<Entry> deletions; if (entries.size()==1) deletions = Iterables.getOnlyElement(entries.values()); else { final int size = IteratorUtils.stream(entries.values().iterator()).map(List::size).reduce(0, (x,y) -> x+y); deletions = new ArrayList<>(size); entries.values().forEach(deletions::addAll); } metrics.incrementCustom(DELETED_RECORDS_COUNT,deletions.size()); if (isRelationTypeIndex()) { mutator.mutateEdges(key, KCVSCache.NO_ADDITIONS, deletions); } else { mutator.mutateIndex(key, KCVSCache.NO_ADDITIONS, deletions); } } catch (final Exception e) { managementSystem.rollback(); writeTx.rollback(); metrics.incrementCustom(FAILED_TX); throw new JanusGraphException(e.getMessage(), e); } }
@Override public void execute(Vertex vertex, Messenger<Integer> messenger, Memory memory) { if (memory.isInitialIteration()) { messenger.sendMessage(DEG_MSG, 1); } else { int degree = IteratorUtils.stream(messenger.receiveMessages()).reduce(0, (a, b) -> a + b); vertex.property(VertexProperty.Cardinality.single, DEGREE, degree); if (memory.getIteration()<length) messenger.sendMessage(DEG_MSG, degree); } }
@Override public void execute(final Vertex vertex, Messenger<Long> messenger, final Memory memory) { if (memory.isInitialIteration()) { if (vertex.id().equals(Long.valueOf(seed).longValue())) { // The seed sends a single message to start the computation log.debug("Sent initial message from {}", vertex.id()); // The seed's distance to itself is zero vertex.property(VertexProperty.Cardinality.single, DISTANCE, 0L); messenger.sendMessage(incidentMessageScope, 0L); } } else { Iterator<Long> distances = messenger.receiveMessages(); // Find minimum distance among all incoming messages, or null if no messages came in Long shortestDistanceSeenOnThisIteration = IteratorUtils.stream(distances).reduce((a, b) -> Math.min(a, b)).orElse(null); if (null == shortestDistanceSeenOnThisIteration) return; // no messages to process or forward on this superstep VertexProperty<Long> currentShortestVP = vertex.property(DISTANCE); if (!currentShortestVP.isPresent() || currentShortestVP.value() > shortestDistanceSeenOnThisIteration) { // First/shortest distance seen by this vertex: store it and forward to neighbors vertex.property(VertexProperty.Cardinality.single, DISTANCE, shortestDistanceSeenOnThisIteration); messenger.sendMessage(incidentMessageScope, shortestDistanceSeenOnThisIteration); } // else: no new winner, ergo no reason to send message to neighbors } }
@Override public void execute(Vertex vertex, Messenger<Double> messenger, Memory memory) { if (memory.isInitialIteration()) { messenger.sendMessage(inE, 1D); } else if (1 == memory.getIteration()) { double initialPageRank = 1D / vertexCount; double edgeCount = IteratorUtils.stream(messenger.receiveMessages()).reduce(0D, (a, b) -> a + b); vertex.property(VertexProperty.Cardinality.single, PAGE_RANK, initialPageRank); vertex.property(VertexProperty.Cardinality.single, OUTGOING_EDGE_COUNT, edgeCount); messenger.sendMessage(outE, initialPageRank / edgeCount); } else { double newPageRank = IteratorUtils.stream(messenger.receiveMessages()).reduce(0D, (a, b) -> a + b); newPageRank = (dampingFactor * newPageRank) + ((1D - dampingFactor) / vertexCount); vertex.property(VertexProperty.Cardinality.single, PAGE_RANK, newPageRank); messenger.sendMessage(outE, newPageRank / vertex.<Double>value(OUTGOING_EDGE_COUNT)); } }
@Override public void execute(Vertex vertex, Messenger<Integer> messenger, Memory memory) { if (memory.isInitialIteration()) { messenger.sendMessage(DEG_MSG, 1); } else { int degree = IteratorUtils.stream(messenger.receiveMessages()).reduce(0, (a, b) -> a + b); vertex.property(VertexProperty.Cardinality.single, DEGREE, degree); if (memory.getIteration()<length) messenger.sendMessage(DEG_MSG, degree); } }
@Override public void execute(Vertex vertex, Messenger<Double> messenger, Memory memory) { if (memory.isInitialIteration()) { messenger.sendMessage(inE, 1D); } else if (1 == memory.getIteration()) { double initialPageRank = 1D / vertexCount; double edgeCount = IteratorUtils.stream(messenger.receiveMessages()).reduce(0D, (a, b) -> a + b); vertex.property(VertexProperty.Cardinality.single, PAGE_RANK, initialPageRank); vertex.property(VertexProperty.Cardinality.single, OUTGOING_EDGE_COUNT, edgeCount); messenger.sendMessage(outE, initialPageRank / edgeCount); } else { double newPageRank = IteratorUtils.stream(messenger.receiveMessages()).reduce(0D, (a, b) -> a + b); newPageRank = (dampingFactor * newPageRank) + ((1D - dampingFactor) / vertexCount); vertex.property(VertexProperty.Cardinality.single, PAGE_RANK, newPageRank); messenger.sendMessage(outE, newPageRank / vertex.<Double>value(OUTGOING_EDGE_COUNT)); } }
@Override public void execute(final Vertex vertex, Messenger<Long> messenger, final Memory memory) { if (memory.isInitialIteration()) { if (vertex.id().equals(seed)) { // The seed sends a single message to start the computation log.debug("Sent initial message from {}", vertex.id()); // The seed's distance to itself is zero vertex.property(VertexProperty.Cardinality.single, DISTANCE, 0L); messenger.sendMessage(incidentMessageScope, 0L); } } else { Iterator<Long> distances = messenger.receiveMessages(); // Find minimum distance among all incoming messages, or null if no messages came in Long shortestDistanceSeenOnThisIteration = IteratorUtils.stream(distances).reduce(Math::min).orElse(null); if (null == shortestDistanceSeenOnThisIteration) return; // no messages to process or forward on this superstep VertexProperty<Long> currentShortestVP = vertex.property(DISTANCE); if (!currentShortestVP.isPresent() || currentShortestVP.value() > shortestDistanceSeenOnThisIteration) { // First/shortest distance seen by this vertex: store it and forward to neighbors vertex.property(VertexProperty.Cardinality.single, DISTANCE, shortestDistanceSeenOnThisIteration); messenger.sendMessage(incidentMessageScope, shortestDistanceSeenOnThisIteration); } // else: no new winner, ergo no reason to send message to neighbors } }
public static <T> Stream<T> stream(final Iterable<T> iterable) { return IteratorUtils.stream(iterable.iterator()); }
private Stream<String> readVertexStrings(final InputStream inputStream) throws IOException { if (unwrapAdjacencyList) { final JsonNode root = mapper.readTree(inputStream); final JsonNode vertices = root.get(GraphSONTokens.VERTICES); if (!vertices.getNodeType().equals(JsonNodeType.ARRAY)) throw new IOException(String.format("The '%s' key must be an array", GraphSONTokens.VERTICES)); return IteratorUtils.stream(vertices.elements()).map(Object::toString); } else { final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); return br.lines(); } }
@Override public <V> Iterator<Property<V>> getProperties(final Neo4jVertexProperty vertexProperty, final String... keys) { final Neo4jNode vertexPropertyNode = Neo4jHelper.getVertexPropertyNode(vertexProperty); if (null == vertexPropertyNode) return Collections.emptyIterator(); else return IteratorUtils.stream(vertexPropertyNode.getKeys()) .filter(key -> ElementHelper.keyExists(key, keys)) .filter(key -> !key.equals(vertexProperty.key())) .map(key -> (Property<V>) new Neo4jProperty<>(vertexProperty, key, (V) vertexPropertyNode.getProperty(key))).iterator(); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES) public void shouldOverwriteEarlierKeyValuesWithLaterKeyValuesOnAddVertexIfMultiProperty() { final Vertex v = graph.addVertex("test", "A", "test", "B", "test", "C"); tryCommit(graph, graph -> { assertEquals(3, IteratorUtils.count(v.properties("test"))); assertTrue(IteratorUtils.stream(v.values("test")).anyMatch(t -> t.equals("A"))); assertTrue(IteratorUtils.stream(v.values("test")).anyMatch(t -> t.equals("B"))); assertTrue(IteratorUtils.stream(v.values("test")).anyMatch(t -> t.equals("C"))); }); }
@Override public <V> Iterator<VertexProperty<V>> getVertexProperties(final Neo4jVertex vertex, final String... keys) { return (Iterator) IteratorUtils.stream(vertex.getBaseVertex().getKeys()) .filter(key -> ElementHelper.keyExists(key, keys)) .map(key -> new Neo4jVertexProperty<>(vertex, key, (V) vertex.getBaseVertex().getProperty(key))).iterator(); }
@Override public <V> Iterator<VertexProperty<V>> getVertexProperties(final Neo4jVertex vertex, final String... keys) { if (Neo4jHelper.isDeleted(vertex.getBaseVertex())) return Collections.emptyIterator(); // TODO: I believe its because the vertex property is deleted, but then seen again in the iterator. ? return IteratorUtils.stream(vertex.getBaseVertex().getKeys()) .filter(key -> ElementHelper.keyExists(key, keys)) .flatMap(key -> { if (vertex.getBaseVertex().getProperty(key).equals(VERTEX_PROPERTY_TOKEN)) return IteratorUtils.stream(vertex.getBaseVertex().relationships(Neo4jDirection.OUTGOING, Graph.Hidden.hide(key))) .map(relationship -> (VertexProperty<V>) new Neo4jVertexProperty<>(vertex, relationship.end())); else return Stream.of(new Neo4jVertexProperty<>(vertex, key, (V) vertex.getBaseVertex().getProperty(key))); }).iterator(); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES, supported = false) public void shouldOverwriteEarlierKeyValuesWithLaterKeyValuesOnAddVertexIfNoMultiProperty() { final Vertex v = graph.addVertex("test", "A", "test", "B", "test", "C"); tryCommit(graph, graph -> { assertEquals(1, IteratorUtils.count(v.properties("test"))); assertTrue(IteratorUtils.stream(v.values("test")).anyMatch(t -> t.equals("C"))); }); }
/** * 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 @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_INTEGER_VALUES) public void shouldProcessEdges() { final Distribution dist = new NormalDistribution(2); final DistributionGenerator generator = DistributionGenerator.build(graph) .label("knows") .edgeProcessor(e -> e.<String>property("data", "test")) .outDistribution(dist) .inDistribution(dist) .expectedNumEdges(100).create(); final int edgesGenerated = generator.generate(); assertTrue(edgesGenerated > 0); tryCommit(graph, g -> { assertEquals(new Long(edgesGenerated), new Long(IteratorUtils.count(g.edges()))); assertTrue(IteratorUtils.count(g.vertices()) > 0); assertTrue(IteratorUtils.stream(g.edges()).allMatch(e -> e.value("data").equals("test"))); }); }