@Override public void run() { while (true) { // Make or break relType between two (possibly same) random nodes TitanVertex source = Iterables.<TitanVertex>getOnlyElement(tx.query().has(idKey, 0).vertices()); TitanVertex sink = Iterables.<TitanVertex>getOnlyElement(tx.query().has(idKey, 1).vertices()); for (Edge r : source.query().direction(Direction.OUT).labels(elabel).edges()) { if (getId(r.inVertex()) == getId(sink)) { r.remove(); continue; } } source.addEdge(elabel, sink); if (Thread.interrupted()) break; } }
@Override public void run() { while (true) { // Make or break relType between two (possibly same) random nodes final JanusGraphVertex source = Iterables.getOnlyElement(tx.query().has(idKey, 0).vertices()); final JanusGraphVertex sink = Iterables.getOnlyElement(tx.query().has(idKey, 1).vertices()); for (Object o : source.query().direction(Direction.OUT).labels(edgeLabel).edges()) { Edge r = (Edge) o; if (getId(r.inVertex()) == getId(sink)) { r.remove(); } } source.addEdge(edgeLabel, sink); if (Thread.interrupted()) break; } }
e.remove(); sign(v.addEdge("es",u),transactionId); e = Iterables.getOnlyElement(v.query().direction(OUT).labels("o2o").edges()); e = Iterables.getOnlyElement(v.query().direction(OUT).labels("o2m").edges()); assertEquals(1,e.<Integer>value("sig").intValue()); e.remove(); sign(v.addEdge("o2m",u),transactionId); for (String label : new String[]{"em","emf"}) {
/** * Tests that timestamped edges can be updated */ @Test public void testTimestampedEdgeUpdates() { clopen(option(GraphDatabaseConfiguration.STORE_META_TIMESTAMPS, "edgestore"), true, option(GraphDatabaseConfiguration.STORE_META_TTL, "edgestore"), true); // Transaction 1: Init graph with two vertices and one edge TitanTransaction tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(100)).start(); TitanVertex v1 = tx.addVertex(); TitanVertex v2 = tx.addVertex(); Edge e = v1.addEdge("related",v2); e.property("time", 25); tx.commit(); tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(200)).start(); v1 = tx.getVertex(v1.longId()); assertNotNull(v1); e = Iterators.getOnlyElement(v1.edges(Direction.OUT, "related")); assertNotNull(e); assertEquals(Integer.valueOf(25), e.value("time")); e.property("time", 125); tx.commit(); tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(300)).start(); v1 = tx.getVertex(v1.longId()); assertNotNull(v1); e = Iterators.getOnlyElement(v1.edges(Direction.OUT, "related")); assertEquals(Integer.valueOf(125), e.value("time")); e.remove(); tx.commit(); }
e.remove(); sign(v.addEdge("es",u),txid); e = getOnlyElement(v.query().direction(OUT).labels("o2o").edges()); e = getOnlyElement(v.query().direction(OUT).labels("o2m").edges()); assertEquals(1,e.<Integer>value("sig").intValue()); e.remove(); sign(v.addEdge("o2m",u),txid); for (String label : new String[]{"em","emf"}) {
/** * Tests that timestamped edges can be updated */ @Test public void testTimestampedEdgeUpdates() { clopen(option(GraphDatabaseConfiguration.STORE_META_TIMESTAMPS, "edgestore"), true, option(GraphDatabaseConfiguration.STORE_META_TTL, "edgestore"), true); // Transaction 1: Init graph with two vertices and one edge JanusGraphTransaction tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(100)).start(); JanusGraphVertex v1 = tx.addVertex(); JanusGraphVertex v2 = tx.addVertex(); Edge e = v1.addEdge("related",v2); e.property("time", 25); tx.commit(); tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(200)).start(); v1 = tx.getVertex(v1.longId()); assertNotNull(v1); e = Iterators.getOnlyElement(v1.edges(Direction.OUT, "related")); assertNotNull(e); assertEquals(Integer.valueOf(25), e.value("time")); e.property("time", 125); tx.commit(); tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(300)).start(); v1 = tx.getVertex(v1.longId()); assertNotNull(v1); e = Iterators.getOnlyElement(v1.edges(Direction.OUT, "related")); assertEquals(Integer.valueOf(125), e.value("time")); e.remove(); tx.commit(); }
@DELETE @Timed @Path("{id}") @Consumes(APPLICATION_JSON) public void delete(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") String id) { LOG.debug("Graph [{}] remove vertex by id '{}'", graph, id); HugeGraph g = graph(manager, graph); // TODO: add removeEdge(id) to improve commit(g, () -> { Edge edge; try { edge = g.edges(id).next(); } catch (NotFoundException e) { throw new IllegalArgumentException(e.getMessage()); } catch (NoSuchElementException e) { throw new IllegalArgumentException(String.format( "No such edge with id: '%s'", id)); } edge.remove(); }); }
for (Edge e : edges) { counter = counter + 1; e.remove();
@Test public void testRemoveEdgeNotExists() { HugeGraph graph = graph(); Vertex james = graph.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); Vertex java = graph.addVertex(T.label, "language", "name", "java"); Edge created = james.addEdge("created", java); created.remove(); List<Edge> edges = graph.traversal().E().toList(); Assert.assertEquals(0, edges.size()); // Remove again created.remove(); }
/** * 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())); }
@Test public void testUpdateEdgePropertyOfRemovingEdge() { Edge edge = initEdgeTransfer(); edge.remove(); Assert.assertThrows(IllegalArgumentException.class, () -> { edge.property("message").remove(); }); Assert.assertThrows(IllegalArgumentException.class, () -> { edge.property("message", "*"); }); }
@Test public void testRemoveEdgeAfterAddEdgeWithTx() { HugeGraph graph = graph(); GraphTransaction tx = graph.openTransaction(); Vertex james = tx.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); Vertex java = tx.addVertex(T.label, "language", "name", "java"); Edge created = james.addEdge("created", java); created.remove(); try { tx.commit(); } finally { tx.close(); } List<Edge> edges = graph.traversal().E().toList(); Assert.assertEquals(0, edges.size()); }
@Test public void testRemoveEdgeOneByOne() { HugeGraph graph = graph(); Vertex james = graph.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); Vertex guido = graph.addVertex(T.label, "author", "id", 2, "name", "Guido van Rossum", "age", 61, "lived", "California"); Vertex java = graph.addVertex(T.label, "language", "name", "java"); Vertex python = graph.addVertex(T.label, "language", "name", "python", "dynamic", true); Vertex java1 = graph.addVertex(T.label, "book", "name", "java-1"); Vertex java2 = graph.addVertex(T.label, "book", "name", "java-2"); 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); List<Edge> edges = graph.traversal().E().toList(); Assert.assertEquals(5, edges.size()); for (int i = 0; i < edges.size(); i++) { edges.get(i).remove(); Assert.assertEquals(4 - i, graph.traversal().E().toList().size()); } }
@Test @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) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldNotGetConcurrentModificationException() { for (int i = 0; i < 25; i++) { final Vertex v = graph.addVertex(); v.addEdge("friend", v); } tryCommit(graph, getAssertVertexEdgeCounts(25, 25)); for (Edge e : g.E().toList()) { e.remove(); tryCommit(graph); } tryCommit(graph, getAssertVertexEdgeCounts(25, 0)); }
Edge edge = edges.get(0); System.out.println(">>>> removing edge: " + edge); edge.remove(); graph.tx().commit(); assert !graph.traversal().E(edgeId).hasNext();
@Test public void testRemoveEdge() { HugeGraph graph = graph(); Vertex james = graph.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); Vertex java = graph.addVertex(T.label, "language", "name", "java"); Vertex java1 = graph.addVertex(T.label, "book", "name", "java-1"); Vertex java2 = graph.addVertex(T.label, "book", "name", "java-2"); Vertex java3 = graph.addVertex(T.label, "book", "name", "java-3"); james.addEdge("created", java); Edge authored1 = james.addEdge("authored", java1); james.addEdge("authored", java2); james.addEdge("authored", java3); List<Edge> edges = graph.traversal().E().toList(); Assert.assertEquals(4, edges.size()); authored1.remove(); edges = graph.traversal().E().toList(); Assert.assertEquals(3, edges.size()); Assert.assertFalse(Utils.contains(edges, new FakeEdge("authored", james, java1))); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_REMOVE_EDGES) public void shouldNotHaveAConcurrentModificationExceptionWhenIteratingAndRemovingAddingEdges() { final Vertex v1 = graph.addVertex("name", "marko"); final Vertex v2 = graph.addVertex("name", "puppy"); v1.addEdge("knows", v2, "since", 2010); v1.addEdge("pets", v2); v1.addEdge("walks", v2, "location", "arroyo"); v2.addEdge("knows", v1, "since", 2010); assertEquals(4l, IteratorUtils.count(v1.edges(Direction.BOTH))); assertEquals(4l, IteratorUtils.count(v2.edges(Direction.BOTH))); v1.edges(Direction.BOTH).forEachRemaining(edge -> { v1.addEdge("livesWith", v2); v1.addEdge("walks", v2, "location", "river"); edge.remove(); }); //assertEquals(8, v1.outE().count().next().intValue()); TODO: Neo4j is not happy //assertEquals(8, v2.outE().count().next().intValue()); v1.edges(Direction.BOTH).forEachRemaining(Edge::remove); assertEquals(0, IteratorUtils.count(v1.edges(Direction.BOTH))); assertEquals(0, IteratorUtils.count(v2.edges(Direction.BOTH))); }
/** * Deletes the edge between two concepts and adds both those concepts for re-validation in case something goes wrong */ public void delete(){ element().remove(); }
protected void clearData() { HugeGraph graph = graph(); // Clear uncommitted data(maybe none) graph.tx().rollback(); int count = 0; // Clear edge do { count = 0; for (Edge e : graph().traversal().E().limit(TX_BATCH).toList()) { count++; e.remove(); } graph.tx().commit(); } while (count == TX_BATCH); // Clear vertex do { count = 0; for (Vertex v : graph().traversal().V().limit(TX_BATCH).toList()) { count++; v.remove(); } graph.tx().commit(); } while (count == TX_BATCH); }