private void addEdgeToSubgraph(final Edge edge) { final Iterator<Edge> edgeIterator = subgraph.edges(edge.id()); if (edgeIterator.hasNext()) return; final Iterator<Vertex> vertexIterator = edge.vertices(Direction.BOTH); final Vertex subGraphOutVertex = getOrCreate(vertexIterator.next()); final Vertex subGraphInVertex = getOrCreate(vertexIterator.next()); final Edge subGraphEdge = subGraphOutVertex.addEdge(edge.label(), subGraphInVertex, T.id, edge.id()); edge.properties().forEachRemaining(property -> subGraphEdge.property(property.key(), property.value())); } }
@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; } }
/** * 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(); }
Edge e = v3.addEdge("knows", v1, "uid", 111); Edge e2 = v1.addEdge("friend", v3); assertEquals(111, e.<Integer>value("uid").intValue()); graph.tx().commit(); assertEquals(445, v3.<Integer>value("uid").intValue()); e = getOnlyElement(v3.query().direction(Direction.OUT).labels("knows").edges()); assertEquals(111, e.<Integer>value("uid").intValue()); assertEquals(e, getE(graph, e.id())); assertEquals(e, getE(graph, e.id().toString())); VertexProperty p = getOnlyElement(v3.properties("uid")); p.remove(); e.property("uid", 222); e2.property("uid", 1); e2.property("weight", 2.0); assertEquals(1, e2.<Integer>value("uid").intValue()); assertEquals(2.0, e2.<Double>value("weight").doubleValue(), 0.0001); assertEquals(222, e.<Integer>value("uid").intValue());
@Override public void edgePropertyRemoved(final Edge element, final Property property) { assertThat(element, instanceOf(DetachedEdge.class)); assertEquals(label, element.label()); assertEquals(inId, element.inVertex().id()); assertEquals(outId, element.outVertex().id()); assertEquals("dah", property.value()); assertEquals("to-drop", property.key()); triggered.set(true); } };
public static Edge createEdge(final Attachable<Edge> attachableEdge, final Graph hostGraph) { final Edge baseEdge = attachableEdge.get(); Iterator<Vertex> vertices = hostGraph.vertices(baseEdge.outVertex().id()); final Vertex outV = vertices.hasNext() ? vertices.next() : hostGraph.features().vertex().willAllowId(baseEdge.outVertex().id()) ? hostGraph.addVertex(T.id, baseEdge.outVertex().id()) : hostGraph.addVertex(); vertices = hostGraph.vertices(baseEdge.inVertex().id()); final Vertex inV = vertices.hasNext() ? vertices.next() : hostGraph.features().vertex().willAllowId(baseEdge.inVertex().id()) ? hostGraph.addVertex(T.id, baseEdge.inVertex().id()) : hostGraph.addVertex(); if (ElementHelper.areEqual(outV, inV)) { final Iterator<Edge> itty = outV.edges(Direction.OUT, baseEdge.label()); while (itty.hasNext()) { final Edge e = itty.next(); if (ElementHelper.areEqual(baseEdge, e)) return e; } } final Edge e = hostGraph.features().edge().willAllowId(baseEdge.id()) ? outV.addEdge(baseEdge.label(), inV, T.id, baseEdge.id()) : outV.addEdge(baseEdge.label(), inV); baseEdge.properties().forEachRemaining(p -> e.property(p.key(), p.value())); return e; }
@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 @LoadGraphWith(MODERN) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) public void g_V_hasXname_markoX_asXaX_outEXcreatedX_asXbX_inV_addEXselectXbX_labelX_toXaX() { final Traversal<Vertex, Edge> traversal = get_g_V_hasXname_markoX_asXaX_outEXcreatedX_asXbX_inV_addEXselectXbX_labelX_toXaX(); printTraversalForm(traversal); final Edge edge = traversal.next(); assertFalse(traversal.hasNext()); assertEquals("created", edge.label()); assertEquals(convertToVertexId("marko"), edge.inVertex().id()); assertEquals(convertToVertexId("lop"), edge.outVertex().id()); assertEquals(6L, g.V().count().next().longValue()); assertEquals(7L, g.E().count().next().longValue()); }
@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 @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializePropertyAsDetached() 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 Property property = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, property); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Property detached = gryoReader.readObject(inputStream, DetachedProperty.class); assertNotNull(detached); assertEquals(property.key(), detached.key()); assertEquals(property.value(), detached.value()); }
@Test @LoadGraphWith(GraphData.MODERN) @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.EdgePropertyFeatures.class, feature = Graph.Features.EdgePropertyFeatures.FEATURE_DOUBLE_VALUES) public void shouldConstructDetachedEdge() { g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("year", 2002); final DetachedEdge detachedEdge = DetachedFactory.detach(g.E(convertToEdgeId("marko", "knows", "vadas")).next(), true); 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(2, IteratorUtils.count(detachedEdge.properties())); assertEquals(1, IteratorUtils.count(detachedEdge.properties("year"))); assertEquals(0.5d, detachedEdge.properties("weight").next().value()); }
private void assertGeoshape(Function<Geoshape,Geoshape> makeGeoshape) { graph.traversal().E().has("place").toList().forEach(e-> { assertTrue(e.property("shape").isPresent()); Geoshape place = (Geoshape) e.property("place").value(); Geoshape expected = makeGeoshape.apply(place); Geoshape actual = (Geoshape) e.property("shape").value(); assertEquals(expected, actual); }); }
@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(); }
@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(LoadGraphWith.GraphData.MODERN) public void shouldSerializeEdge() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final Edge e = g.E(convertToEdgeId("marko", "knows", "vadas")).next(); final String json = mapper.writeValueAsString(e); final Edge detached = mapper.readValue(json, Edge.class); assertNotNull(detached); assertEquals(e.label(), detached.label()); assertEquals(e.id(), detached.id()); assertEquals((Double) e.value("weight"), detached.value("weight")); }
@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) @FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS) public void shouldRollbackElementAutoTransactionByDefault() { final Vertex v1 = graph.addVertex(); final Edge e1 = v1.addEdge("l", v1); assertVertexEdgeCounts(graph, 1, 1); assertEquals(v1.id(), graph.vertices(v1.id()).next().id()); assertEquals(e1.id(), graph.edges(e1.id()).next().id()); g.tx().rollback(); assertVertexEdgeCounts(graph, 0, 0); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.EdgePropertyFeatures.class, feature = Graph.Features.EdgePropertyFeatures.FEATURE_DOUBLE_VALUES) public void shouldNotBeEqualPropertiesAsThereIsDifferentKey() { final Object joshCreatedLopEdgeId = convertToEdgeId("josh", "created", "lop"); final Edge e = g.V(convertToVertexId("josh")).next().addEdge("created", g.V(convertToVertexId("lop")).next(), "weight", 0.4d); assertFalse(DetachedFactory.detach(e.property("weight")).equals(DetachedFactory.detach(g.E(joshCreatedLopEdgeId).next().property("weight")))); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void testAttachableGetMethod() { // vertex host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, DetachedFactory.detach(vertex, true).attach(Attachable.Method.get(vertex)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, DetachedFactory.detach(vertexProperty, true).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(vertex)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, DetachedFactory.detach(edge, true).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(vertex)))))); // graph host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, DetachedFactory.detach(vertex, true).attach(Attachable.Method.get(graph)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, DetachedFactory.detach(vertexProperty, true).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(graph)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, DetachedFactory.detach(edge, true).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(graph)))))); }