@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) public void shouldHashAndEqualCorrectly() { final Vertex v = graph.addVertex(); final Edge e = v.addEdge("test", v); final Set<Edge> set = new HashSet<>(); for (int i = 0; i < 100; i++) { set.add(ReferenceFactory.detach(e)); set.add(e); } assertEquals(1, set.size()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) public void shouldGetPropertyKeysOnEdge() { final Vertex v = graph.addVertex(); final Edge e = v.addEdge("friend", v, "name", "marko", "location", "desert", "status", "dope"); Set<String> keys = e.keys(); assertEquals(3, keys.size()); assertTrue(keys.contains("name")); assertTrue(keys.contains("location")); assertTrue(keys.contains("status")); final List<Property<Object>> m = IteratorUtils.list(e.properties()); assertEquals(3, m.size()); assertTrue(m.stream().anyMatch(p -> p.key().equals("name"))); assertTrue(m.stream().anyMatch(p -> p.key().equals("location"))); assertTrue(m.stream().anyMatch(p -> p.key().equals("status"))); assertEquals("marko", m.stream().filter(p -> p.key().equals("name")).map(Property::value).findAny().orElse(null)); assertEquals("desert", m.stream().filter(p -> p.key().equals("location")).map(Property::value).findAny().orElse(null)); assertEquals("dope", m.stream().filter(p -> p.key().equals("status")).map(Property::value).findAny().orElse(null)); e.property("status").remove(); keys = e.keys(); assertEquals(2, keys.size()); assertTrue(keys.contains("name")); assertTrue(keys.contains("location")); e.properties().forEachRemaining(Property::remove); keys = e.keys(); assertEquals(0, keys.size()); }
@Test public void testAddEdgeToSameVerticesWithMultiTimesAndOverrideEdge() { HugeGraph graph = graph(); Vertex james = graph.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); Vertex book = graph.addVertex(T.label, "book", "name", "Test-Book-1"); james.addEdge("write", book, "time", "2017-4-28"); james.addEdge("write", book, "time", "2017-5-21"); james.addEdge("write", book, "time", "2017-5-25"); james.addEdge("write", book, "time", "2017-4-28"); james.addEdge("write", book, "time", "2017-5-21"); List<Edge> edges = graph.traversal().E().toList(); Assert.assertEquals(3, edges.size()); assertContains(edges, "write", james, book, "time", "2017-4-28"); assertContains(edges, "write", james, book, "time", "2017-5-21"); assertContains(edges, "write", james, book, "time", "2017-5-25"); }
public static void testRemove(final HugeGraph graph) { List<Vertex> vertices = graph.traversal().V().hasLabel("person") .has("age", 19).toList(); assert vertices.size() == 1; Vertex james = vertices.get(0); Vertex book6 = graph.addVertex(T.label, "book", "name", "java-6"); james.addEdge("look", book6, "timestamp", "2017-5-2 12:00:08.0"); james.addEdge("look", book6, "timestamp", "2017-5-3 12:00:08.0"); graph.tx().commit(); assert graph.traversal().V(book6.id()).bothE().hasNext(); System.out.println(">>>> removing vertex: " + james); james.remove(); graph.tx().commit(); assert !graph.traversal().V(james.id()).hasNext(); assert !graph.traversal().V(book6.id()).bothE().hasNext();
@Test public void testRemoveEdgesOfVertex() { HugeGraph graph = graph(); Vertex java3 = graph.addVertex(T.label, "book", "name", "java-3"); james.addEdge("created", java); guido.addEdge("created", python); james.addEdge("authored", java1); james.addEdge("authored", java2); james.addEdge("authored", java3); guido.addEdge("write", java1, "time", "2017-6-7"); Assert.assertEquals(6, edges.size()); edges = graph.traversal().V(java1.id()).inE().toList(); Assert.assertEquals(1, edges.size()); edges = graph.traversal().V(java2.id()).inE().toList(); Assert.assertEquals(0, edges.size()); edges = graph.traversal().V(java3.id()).inE().toList(); Assert.assertEquals(0, edges.size());
@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_ADD_PROPERTY) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY) public void shouldHandleBothEdgesGraphFilterOnSelfLoop() { assertEquals(0l, IteratorUtils.count(graph.vertices())); assertEquals(0l, IteratorUtils.count(graph.edges())); // these vertex label, edge label, and property names/values were copied from existing tests StarGraph starGraph = StarGraph.open(); Vertex vertex = starGraph.addVertex(T.label, "person", "name", "furnace"); Edge edge = vertex.addEdge("self", vertex); edge.property("acl", "private"); // traversing a self-loop should yield the edge once for inE/outE // and the edge twice for bothE (one edge emitted two times, not two edges) assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().inE())); assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().outE())); assertEquals(2L, IteratorUtils.count(starGraph.traversal().V().bothE())); // Try a filter that retains BOTH GraphFilter graphFilter = new GraphFilter(); graphFilter.setEdgeFilter(__.bothE("self")); starGraph = starGraph.applyGraphFilter(graphFilter).get(); // Retest traversal counts after filtering assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().inE())); assertEquals(1L, IteratorUtils.count(starGraph.traversal().V().outE())); assertEquals(2L, IteratorUtils.count(starGraph.traversal().V().bothE())); }
String uuid = keysList.get(index); Vertex newVertex = graph.addVertex(T.id, uuid); if (addedVertexIds.size() > 1) { newVertex.addEdge("connected", randomVertex); Vertex vertex = Iterators.getOnlyElement(graph.vertices(test)); assertNotNull(vertex); assertEquals(test, vertex.id()); if (i >= 2) { assertEquals(1, Iterators.size(vertex.edges(Direction.OUT))); Vertex vertex = Iterators.getOnlyElement(graph.vertices(test)); assertNotNull(vertex); assertEquals(test, vertex.id()); if (i >= 2) { assertEquals(1, Iterators.size(vertex.edges(Direction.OUT))); Vertex v1 = Iterators.getOnlyElement(graph.vertices(vId1)); Vertex v2 = Iterators.getOnlyElement(graph.vertices(vId2)); v1.addEdge("additional", v2); batchSize++; if (batchSize >= maxBatchSize) {
@Test public void removingEdgesWorks() { Graph g = this.getGraph(); Vertex me = g.addVertex("kind", "person", "name", "martin"); Vertex john = g.addVertex("kind", "person", "name", "john"); me.addEdge("friend", john, "since", "forever"); g.tx().commit(); // find the edge Edge friendship = g.traversal().E().has("since", "forever").toSet().iterator().next(); assertNotNull(friendship); // remove the edge and commit friendship.remove(); g.tx().commit(); // check that the edge no longer exists in gremlin Set<Edge> edges = g.traversal().E().has("since", "forever").toSet(); assertTrue(edges.isEmpty()); // check that the edge no longer exists in the adjacent vertices Vertex me2 = g.traversal().V().has("name", "martin").toSet().iterator().next(); Vertex john2 = g.traversal().V().has("name", "john").toSet().iterator().next(); assertFalse(me2.edges(Direction.BOTH, "friend").hasNext()); assertFalse(john2.edges(Direction.BOTH, "friend").hasNext()); }
/** * Generate a graph with lots of edges, then iterate the edges and remove them from the graph */ @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_REMOVE_EDGES) public void shouldRemoveEdgesWithoutConcurrentModificationException() { for (int i = 0; i < 50; i++) { graph.addVertex().addEdge("link", graph.addVertex()); } final Iterator<Edge> edgeIterator = graph.edges(); assertTrue(edgeIterator.hasNext()); while (edgeIterator.hasNext()) { edgeIterator.next().remove(); } assertFalse(edgeIterator.hasNext()); tryCommit(graph, g -> assertFalse(g.edges().hasNext())); }
vJohn.addEdge("family", vJane, E_KIND, "married"); vJane.addEdge("family", vJohn, E_KIND, "married"); vJack.addEdge("family", vJane, E_KIND, "sister-in-law"); vJane.addEdge("family", vJack, E_KIND, "brother-in-law"); vJohn.addEdge("family", vSarah, E_KIND, "father"); vJane.addEdge("family", vSarah, E_KIND, "mother"); vJack.addEdge("family", vSarah, E_KIND, "uncle"); assertEquals(1, g.find().vertices().where(P_FIRST_NAME).isEqualToIgnoreCase("sarah").count()); assertEquals(1, g.find().vertices().where(P_NICKNAMES).containsIgnoreCase("sassie").count()); assertEquals(0, g.find().vertices().where(P_NICKNAMES).isEqualToIgnoreCase("JS").count()); assertEquals(vJane, g.traversal().V(vSarah).inE("family").has(E_KIND, "mother").outV().next()); assertEquals(vJohn, g.traversal().V(vSarah).inE("family").has(E_KIND, "father").outV().next()); assertEquals(vJack, g.traversal().V(vSarah).inE("family").has(E_KIND, "uncle").outV().next()); }, assertTrue(graph.getNow(SCENARIO_A) > graph.getNow());
assertEquals(4, graph.find().vertices().where(P_NICKNAMES).startsWithIgnoreCase("j").count()); v2.addEdge("knows", v3, E_KIND, "friend"); v3.addEdge("knows", v2, E_KIND, "friend"); assertEquals(1, g.traversal().V(v2).outE("knows").toSet().size()); assertEquals(1, g.traversal().V(v3).inE("knows").toSet().size()); assertEquals(1, g.traversal().V(v3).outE("knows").toSet().size()); assertEquals(1, g.traversal().V(v2).inE("knows").toSet().size());
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReturnOutThenInOnVertexIterator() { final Vertex a = graph.addVertex(); final Vertex b = graph.addVertex(); final Edge e = a.addEdge("knows", b); assertEquals(a, e.outVertex()); assertEquals(b, e.inVertex()); final Iterator<Vertex> iterator = e.vertices(Direction.BOTH); assertTrue(iterator.hasNext()); assertEquals(a, iterator.next()); assertTrue(iterator.hasNext()); assertEquals(b, iterator.next()); assertFalse(iterator.hasNext()); } }
@Test public void testAddEdgeToSameVerticesWithMultiTimes() { HugeGraph graph = graph(); Vertex james = graph.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); Vertex book = graph.addVertex(T.label, "book", "name", "Test-Book-1"); james.addEdge("write", book, "time", "2017-4-28"); james.addEdge("write", book, "time", "2017-5-21"); james.addEdge("write", book, "time", "2017-5-25"); List<Edge> edges = graph.traversal().E().toList(); Assert.assertEquals(3, edges.size()); assertContains(edges, "write", james, book, "time", "2017-4-28"); assertContains(edges, "write", james, book, "time", "2017-5-21"); assertContains(edges, "write", james, book, "time", "2017-5-25"); }
public static void addVertexColumn(SqlgGraph sqlgGraph, String schema, String prefixedTable, Map.Entry<String, PropertyType> column) { BatchManager.BatchModeType batchModeType = flushAndSetTxToNone(sqlgGraph); try { Preconditions.checkArgument(prefixedTable.startsWith(VERTEX_PREFIX), "prefixedTable must be for a vertex. prefixedTable = " + prefixedTable); GraphTraversalSource traversalSource = sqlgGraph.topology(); List<Vertex> vertices = traversalSource.V() .hasLabel(SQLG_SCHEMA + "." + SQLG_SCHEMA_SCHEMA) .has("name", schema) .out(SQLG_SCHEMA_SCHEMA_VERTEX_EDGE) .has("name", prefixedTable.substring(VERTEX_PREFIX.length())) .toList(); if (vertices.size() == 0) { throw new IllegalStateException("Found no vertex for " + schema + "." + prefixedTable); } if (vertices.size() > 1) { throw new IllegalStateException("Found more than one vertex for " + schema + "." + prefixedTable); } Vertex vertex = vertices.get(0); Vertex property = sqlgGraph.addVertex( T.label, SQLG_SCHEMA + "." + SQLG_SCHEMA_PROPERTY, "name", column.getKey(), "type", column.getValue().name(), CREATED_ON, LocalDateTime.now() ); vertex.addEdge(SQLG_SCHEMA_VERTEX_PROPERTIES_EDGE, property); } finally { sqlgGraph.tx().batchMode(batchModeType); } }
@Test public void testQueryOutEdgesOfVertexByRangeFilter() { HugeGraph graph = graph(); graph.schema().indexLabel("authoredByScore").onE("authored") .range().by("score").create(); Vertex james = graph.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); Vertex book1 = graph.addVertex(T.label, "book", "name", "Test-Book-1"); Vertex book2 = graph.addVertex(T.label, "book", "name", "Test-Book-2"); Vertex book3 = graph.addVertex(T.label, "book", "name", "Test-Book-3"); james.addEdge("authored", book1, "contribution", "1991 3 1", "score", 5); james.addEdge("authored", book2, "contribution", "1992 2 2", "score", 4); james.addEdge("authored", book3, "contribution", "1993 3 2", "score", 3); graph.tx().commit(); // Won't query by search index, just filter by property after outE() List<Edge> edges = graph.traversal().V(james).outE("authored") .has("score", P.gte(4)).toList(); Assert.assertEquals(2, edges.size()); }
@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) public void shouldValidateEquality() { final Vertex v = graph.addVertex(); final Edge e1 = v.addEdge("self", v); final Edge e2 = v.addEdge("self", v); assertEquals(e1, e1); assertEquals(e2, e2); assertNotEquals(e1, e2); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) public void shouldTriggerAddEdgePropertyAdded() { 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("v").addE("self").to("v").property("some", "thing").next(); tryCommit(graph, g -> assertEquals(1, IteratorUtils.count(gts.E().has("some", "thing")))); assertEquals(0, listener1.addVertexEventRecorded()); assertEquals(0, listener2.addVertexEventRecorded()); assertEquals(1, listener1.addEdgeEventRecorded()); assertEquals(1, listener2.addEdgeEventRecorded()); assertEquals(0, listener2.edgePropertyChangedEventRecorded()); assertEquals(0, listener1.edgePropertyChangedEventRecorded()); }
@Test public void testSimpleTinkerPopTraversal() { Vertex v1 = graph.addVertex("name", "josh"); Vertex v2 = graph.addVertex("name", "lop"); v1.addEdge("created", v2); //graph.tx().commit(); Object id = graph.traversal().V().has("name", "josh").outE("created").as("e").inV().has("name", "lop").<Edge>select("e").next().id(); assertNotNull(id); }
@Test public void removingEdgePropertiesWorks() { Graph g = this.getGraph(); Vertex me = g.addVertex("kind", "person", "name", "martin"); Vertex john = g.addVertex("kind", "person", "name", "john"); me.addEdge("friend", john, "since", "forever"); g.tx().commit(); // find the edge Edge friendship = g.traversal().E().has("since", "forever").toSet().iterator().next(); assertNotNull(friendship); // remove the edge property and commit friendship.property("since").remove(); g.tx().commit(); // assert that the property is gone Vertex me2 = g.traversal().V().has("name", "martin").toSet().iterator().next(); Iterator<Edge> edges = me2.edges(Direction.BOTH, "friend"); assertTrue(edges.hasNext()); Edge friendship2 = Iterators.getOnlyElement(edges); assertNotNull(friendship2); assertFalse(friendship2.property("since").isPresent()); }