Refine search
@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 } }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldTriggerAddVertexPropertyChanged() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex vSome = graph.addVertex("some", "thing"); vSome.property(VertexProperty.Cardinality.single, "that", "thing"); final GraphTraversalSource gts = create(eventStrategy); final Vertex vAny = gts.V().addV().property("any", "thing").next(); gts.V(vAny).property(VertexProperty.Cardinality.single, "any", "thing else").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("any", "thing else")))); assertEquals(1, listener1.addVertexEventRecorded()); assertEquals(1, listener2.addVertexEventRecorded()); assertEquals(1, listener2.vertexPropertyChangedEventRecorded()); assertEquals(1, listener1.vertexPropertyChangedEventRecorded()); }
public static void assertNoEdgeGraph(final Graph g1, final boolean assertDouble, final boolean lossyForId) { assertEquals(2, IteratorUtils.count(g1.vertices())); assertEquals(1, IteratorUtils.count(g1.edges())); final Vertex v1 = g1.traversal().V().has("name", "marko").next(); assertEquals(29, v1.<Integer>value("age").intValue()); assertEquals(2, v1.keys().size()); assertEquals(Vertex.DEFAULT_LABEL, v1.label()); assertId(g1, lossyForId, v1, 1); final List<Edge> v1Edges = IteratorUtils.list(v1.edges(Direction.BOTH)); assertEquals(1, v1Edges.size()); v1Edges.forEach(e -> { if (e.inVertex().value("name").equals("vadas")) { assertEquals(Edge.DEFAULT_LABEL, e.label()); if (assertDouble) assertWeightLoosely(0.5d, e); else assertWeightLoosely(0.5f, e); assertEquals(1, e.keys().size()); assertId(g1, lossyForId, e, 7); } else { fail("Edge not expected"); } }); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_CUSTOM_IDS) public void shouldIterateEdgesWithCustomIdSupportUsingStringRepresentations() { final Vertex v = graph.addVertex(); final Edge e1 = v.addEdge("self", v); final Edge e2 = v.addEdge("self", v); v.addEdge("self", v); tryCommit(graph, graph -> { assertEquals(2, IteratorUtils.count(graph.edges(e1.id().toString(), e2.id().toString()))); }); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) public void shouldTriggerAddEdgeByPath() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex v = graph.addVertex(); v.addEdge("self", v); final GraphTraversalSource gts = create(eventStrategy); gts.V(v).as("a").addE("self").to("a").next(); tryCommit(graph, g -> assertEquals(2, IteratorUtils.count(gts.E()))); assertEquals(0, listener1.addVertexEventRecorded()); assertEquals(0, listener2.addVertexEventRecorded()); assertEquals(1, listener1.addEdgeEventRecorded()); assertEquals(1, listener2.addEdgeEventRecorded()); }
init100Books(); GraphTraversal<Vertex, Vertex> itor = graph.traversal().V() .has("~page", "").limit(10); Assert.assertEquals(10, IteratorUtils.count(itor)); String page = TraversalUtil.page(itor); vertexes = graph.traversal().V() .has("~page", page).limit(33) .toList(); Assert.assertEquals(33, vertexes.size()); Vertex vertex2 = vertexes.get(0); Assert.assertEquals(vertex1.id(), vertex2.id()); Assert.assertEquals(vertex1.label(), vertex2.label()); Assert.assertEquals(IteratorUtils.asList(vertex1.properties()), IteratorUtils.asList(vertex2.properties())); vertexes = graph.traversal().V() Assert.assertEquals(vertex3.id(), vertex4.id()); Assert.assertEquals(vertex3.label(), vertex4.label()); Assert.assertEquals(IteratorUtils.asList(vertex3.properties()), IteratorUtils.asList(vertex4.properties())); vertexes = graph.traversal().V()
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) public void shouldTriggerEdgePropertyChanged() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex v = graph.addVertex(); final Edge e = v.addEdge("self", v); e.property("some", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.E(e).property("some", "other thing").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.E().has("some", "other thing")))); assertEquals(0, listener1.addVertexEventRecorded()); assertEquals(0, listener2.addVertexEventRecorded()); assertEquals(0, listener1.addEdgeEventRecorded()); assertEquals(0, listener2.addEdgeEventRecorded()); assertEquals(1, listener2.edgePropertyChangedEventRecorded()); assertEquals(1, listener1.edgePropertyChangedEventRecorded()); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_NUMERIC_IDS) public void shouldIterateVerticesWithNumericIdSupportUsingLongRepresentations() { // if the graph supports id assigned, it should allow it. if the graph does not, it will generate one final Vertex v1 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, 1l) : graph.addVertex(); final Vertex v2 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, 2l) : graph.addVertex(); graph.addVertex(); tryCommit(graph, graph -> { assertEquals(2, IteratorUtils.count(graph.vertices(Long.parseLong(v1.id().toString()), Long.parseLong(v2.id().toString())))); }); }
@Test @LoadGraphWith(GraphData.MODERN) @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.EdgePropertyFeatures.class, feature = Graph.Features.EdgePropertyFeatures.FEATURE_DOUBLE_VALUES) public void shouldConstructDetachedEdgeAsReference() { g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("year", 2002); final DetachedEdge detachedEdge = DetachedFactory.detach(g.E(convertToEdgeId("marko", "knows", "vadas")).next(), false); assertEquals(convertToEdgeId("marko", "knows", "vadas"), detachedEdge.id()); assertEquals("knows", detachedEdge.label()); assertEquals(DetachedVertex.class, detachedEdge.vertices(Direction.OUT).next().getClass()); assertEquals(convertToVertexId("marko"), detachedEdge.vertices(Direction.OUT).next().id()); assertEquals("person", detachedEdge.vertices(Direction.IN).next().label()); assertEquals(DetachedVertex.class, detachedEdge.vertices(Direction.IN).next().getClass()); assertEquals(convertToVertexId("vadas"), detachedEdge.vertices(Direction.IN).next().id()); assertEquals("person", detachedEdge.vertices(Direction.IN).next().label()); assertEquals(0, IteratorUtils.count(detachedEdge.properties())); }
@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()); }
@Test @LoadGraphWith(MODERN) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY) public void g_addVXpersonX_propertyXname_stephenX() { final Traversal<Vertex, Vertex> traversal = get_g_addVXpersonX_propertyXname_stephenX(); printTraversalForm(traversal); final Vertex stephen = traversal.next(); assertFalse(traversal.hasNext()); assertEquals("person", stephen.label()); assertEquals("stephen", stephen.value("name")); assertEquals(1, IteratorUtils.count(stephen.properties())); assertEquals(7, IteratorUtils.count(g.V())); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES) public void shouldAppendPartitionToVertexPropertyOverMultiProperty() { final PartitionStrategy partitionStrategy = PartitionStrategy.build() .includeMetaProperties(true) .partitionKey(partition).writePartition("A").readPartitions("A").create(); final Vertex v = g.withStrategies(partitionStrategy).addV().property(VertexProperty.Cardinality.list, "any", "thing") .property(VertexProperty.Cardinality.list, "any", "more").next(); assertNotNull(v); assertThat((List<String>) IteratorUtils.asList(g.V(v).properties("any")).stream().map(p -> ((VertexProperty) p).value()).collect(Collectors.toList()), containsInAnyOrder("thing", "more")); assertEquals("A", v.property(partition).value()); assertThat((List<String>) IteratorUtils.asList(g.V(v).properties("any")).stream().map(p -> ((VertexProperty) p).value(partition)).collect(Collectors.toList()), containsInAnyOrder("A", "A")); }
init100LookEdges(); GraphTraversal<Edge, Edge> itor = graph.traversal().E() .has("~page", "").limit(10); Assert.assertEquals(10, IteratorUtils.count(itor)); String page = TraversalUtil.page(itor); edges = graph.traversal().E() .has("~page", page).limit(33) .toList(); Assert.assertEquals(33, edges.size()); Edge edge2 = edges.get(0); Assert.assertEquals(edge1.id(), edge2.id()); Assert.assertEquals(edge1.label(), edge2.label()); Assert.assertEquals(IteratorUtils.asList(edge1.properties()), IteratorUtils.asList(edge2.properties())); edges = graph.traversal().E() Assert.assertEquals(edge3.id(), edge4.id()); Assert.assertEquals(edge3.label(), edge4.label()); Assert.assertEquals(IteratorUtils.asList(edge3.properties()), IteratorUtils.asList(edge4.properties())); edges = graph.traversal().E()
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldGenerateDefaultIdOnAddVWithGeneratedDefaultId() throws Exception { final ElementIdStrategy strategy = ElementIdStrategy.build().create(); final GraphTraversalSource sg = create(strategy); sg.addV().next(); assertEquals(1, IteratorUtils.count(sg.V())); final Vertex v = sg.V().addV().property("name", "stephen").next(); assertEquals("stephen", v.value("name")); assertNotNull(UUID.fromString(sg.V(v).id().next().toString())); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldTriggerAddVertexFromStart() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); graph.addVertex("some", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.addV().property("any", "thing").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.V().has("any", "thing")))); assertEquals(1, listener1.addVertexEventRecorded()); assertEquals(1, listener2.addVertexEventRecorded()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_REMOVE_PROPERTY) public void shouldTriggerRemoveEdgeProperty() { final StubMutationListener listener1 = new StubMutationListener(); final StubMutationListener listener2 = new StubMutationListener(); final EventStrategy.Builder builder = EventStrategy.build() .addListener(listener1) .addListener(listener2); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final Vertex v = graph.addVertex(); v.addEdge("self", v, "some", "thing"); final GraphTraversalSource gts = create(eventStrategy); gts.E().properties().drop().iterate(); tryCommit(graph, g -> assertEquals(0, IteratorUtils.count(gts.E().properties()))); assertEquals(1, listener1.edgePropertyRemovedEventRecorded()); assertEquals(1, listener2.edgePropertyRemovedEventRecorded()); }
@Test public void testQueryByPageResultsMatched() { Assume.assumeTrue("Not support paging", storeFeatures().supportsQueryByPage()); HugeGraph graph = graph(); init100Books(); List<Vertex> all = graph.traversal().V().toList(); GraphTraversal<Vertex, Vertex> itor; String page = ""; int size = 20; for (int i = 0; i < 100 / size; i++) { itor = graph.traversal().V() .has("~page", page).limit(size); List<?> vertexes = IteratorUtils.asList(itor); Assert.assertEquals(size, vertexes.size()); List<Vertex> expected = all.subList(i * size, (i + 1) * size); Assert.assertEquals(expected, vertexes); page = TraversalUtil.page(itor); } Assert.assertNull(page); }
@Test @LoadGraphWith(MODERN) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_PROPERTY) public void g_V_properties_drop() { final Traversal<Vertex, VertexProperty> traversal = get_g_V_properties_drop(); printTraversalForm(traversal); assertFalse(traversal.hasNext()); assertEquals(6, IteratorUtils.count(g.V())); assertEquals(6, IteratorUtils.count(g.E())); g.V().forEachRemaining(vertex -> assertEquals(0, IteratorUtils.count(vertex.properties()))); }