Refine search
@Test public void testTinkerPopCardinality() { PropertyKey id = mgmt.makePropertyKey("id").cardinality(Cardinality.SINGLE).dataType(Integer.class).make(); PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.SINGLE).dataType(String.class).make(); PropertyKey names = mgmt.makePropertyKey("names").cardinality(Cardinality.LIST).dataType(String.class).make(); mgmt.buildIndex("byId", Vertex.class).addKey(id).buildCompositeIndex(); finishSchema(); GraphTraversalSource gts; Vertex v; v = graph.addVertex("id", 1); v.property(single, "name", "t1"); graph.addVertex("id", 2, "names", "n1", "names", "n2"); graph.tx().commit(); gts = graph.traversal(); v = gts.V().has("id", 1).next(); v.property(single, "name", "t2"); v = gts.V().has("id", 1).next(); v.property(single, "name", "t3"); assertCount(1, gts.V(v).properties("name")); assertCount(2, gts.V().has("id", 2).properties("names")); assertCount(2, gts.V().hasLabel("vertex")); }
@Override public void close() { tx().close(); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS) public void shouldRollbackPropertyAutoTransactionByDefault() { final Vertex v1 = graph.addVertex("name", "marko"); final Edge e1 = v1.addEdge("l", v1, "name", "xxx"); assertVertexEdgeCounts(graph, 1, 1); assertEquals(v1.id(), graph.vertices(v1.id()).next().id()); assertEquals(e1.id(), graph.edges(e1.id()).next().id()); assertEquals("marko", v1.<String>value("name")); assertEquals("xxx", e1.<String>value("name")); g.tx().commit(); assertEquals("marko", v1.<String>value("name")); assertEquals("marko", graph.vertices(v1.id()).next().<String>value("name")); v1.property(VertexProperty.Cardinality.single, "name", "stephen"); assertEquals("stephen", v1.<String>value("name")); assertEquals("stephen", graph.vertices(v1.id()).next().<String>value("name")); g.tx().rollback(); assertEquals("marko", v1.<String>value("name")); assertEquals("marko", graph.vertices(v1.id()).next().<String>value("name")); e1.property("name", "yyy"); assertEquals("yyy", e1.<String>value("name")); assertEquals("yyy", graph.edges(e1.id()).next().<String>value("name")); g.tx().rollback(); assertEquals("xxx", e1.<String>value("name")); assertEquals("xxx", graph.edges(e1.id()).next().<String>value("name")); assertVertexEdgeCounts(graph, 1, 1); }
final AtomicLong counter = new AtomicLong(0); final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions(); final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge(); cache.put((StarGraph.StarVertex) attachable.get(), attachable.attach(Attachable.Method.create(graphToWriteTo))); if (supportsTx && counter.incrementAndGet() % batchSize == 0) graphToWriteTo.tx().commit(); }); cache.entrySet().forEach(kv -> kv.getKey().edges(Direction.IN).forEachRemaining(e -> { final Vertex cachedOutV = cache.get(e.outVertex()); final Vertex cachedInV = cache.get(e.inVertex()); if (null == cachedOutV) throw new IllegalStateException(String.format("Could not find outV with id [%s] to create edge with id [%s]", e.outVertex().id(), e.id())); if (null == cachedInV) throw new IllegalStateException(String.format("Could not find inV with id [%s] to create edge with id [%s]", e.inVertex().id(), e.id())); final Edge newEdge = edgeFeatures.willAllowId(e.id()) ? cachedOutV.addEdge(e.label(), cachedInV, T.id, e.id()) : cachedOutV.addEdge(e.label(), cachedInV); e.properties().forEachRemaining(p -> newEdge.property(p.key(), p.value())); if (supportsTx && counter.incrementAndGet() % batchSize == 0) graphToWriteTo.tx().commit(); })); if (supportsTx) graphToWriteTo.tx().commit();
@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); }
private void testOr(final Graph aGraph) { final GraphTraversalSource g = aGraph.traversal(); final Vertex hiro = g.addV().property("name", "Hiro").property("age", 2).property("length", 90).next(); final Vertex totoro = g.addV().property("name", "Totoro").property("age", 1).next(); final Vertex john = g.addV().property("name", "John").property("age", 3).property("length", 110).next(); final Vertex mike = g.addV().property("name", "Mike").property("age", 4).property("length", 130).next(); aGraph.tx().commit(); assertCount(1, g.V().has("name", "Totoro"));
private void updateVertexWithProperties(String propertyKey, Object propertyValue, Map<Object, Object> map) { if (graph.traversal().V().has(propertyKey, propertyValue).hasNext()) { final Vertex v = graph.traversal().V().has(propertyKey, propertyValue).next(); map.forEach((key, value) -> v.property((String) key, value)); graph.tx().commit(); } }
@Test public void testAddEdgeProperty() { HugeGraph graph = graph(); Vertex louise = graph.addVertex(T.label, "person", "name", "Louise", "city", "Beijing", "age", 21); Vertex sean = graph.addVertex(T.label, "person", "name", "Sean", "city", "Beijing", "age", 23); long current = System.currentTimeMillis(); Edge edge = louise.addEdge("transfer", sean, "id", 1, "amount", 500.00F, "timestamp", current); graph.tx().commit(); // Add property edge.property("message", "Happy birthday!"); graph.tx().commit(); List<Edge> edges = graph.traversal().E().toList(); Assert.assertEquals(1, edges.size()); assertContains(edges, "transfer", louise, sean, "id", 1, "amount", 500.00F, "timestamp", current, "message", "Happy birthday!"); }
@Test public void testDeleteAndInsertVertex() { HugeGraph graph = graph(); graph.addVertex(T.label, "author", "id", 1, "name", "Tom", "lived", "Beijing"); graph.traversal().V().hasLabel("author").has("id", 1).next().remove(); graph.addVertex(T.label, "author", "id", 1, "name", "Tom", "lived", "Shanghai"); graph.tx().commit(); Vertex vertex = vertex("author", "id", 1); Assert.assertTrue(vertex.property("lived").isPresent()); Assert.assertEquals("Shanghai", vertex.property("lived").value()); }
@Test public void testReadWideVertexWithManyProperties() { int numProps = 1 << 16; long numV = 1; mgmt.makePropertyKey("p").cardinality(Cardinality.LIST).dataType(Integer.class).make(); mgmt.commit(); finishSchema(); for (int j = 0; j < numV; j++) { Vertex v = graph.addVertex(); for (int i = 0; i < numProps; i++) { v.property("p", i); } } graph.tx().commit(); assertEquals(numV, (long) graph.traversal().V().count().next()); Map<String, Object> propertiesOnVertex = graph.traversal().V().valueMap().next(); List<?> valuesOnP = (List)propertiesOnVertex.values().iterator().next(); assertEquals(numProps, valuesOnP.size()); Graph g = GraphFactory.open("target/test-classes/cassandra-read.properties"); GraphTraversalSource t = g.traversal(GraphTraversalSource.computer(SparkGraphComputer.class)); assertEquals(numV, (long) t.V().count().next()); propertiesOnVertex = t.V().valueMap().next(); valuesOnP = (List)propertiesOnVertex.values().iterator().next(); assertEquals(numProps, valuesOnP.size()); }
final Random random = new Random(); if (random.nextBoolean()) { final Vertex a = graph.addVertex(); final Vertex b = graph.addVertex(); final Edge e = a.addEdge("friend", b); a.property(VertexProperty.Cardinality.single, "test", this.getId()); b.property(VertexProperty.Cardinality.single, "blah", random.nextDouble()); e.property("bloop", random.nextInt()); edges.getAndAdd(1); graph.tx().commit(); } else { final Vertex a = graph.addVertex(); a.property(VertexProperty.Cardinality.single, "test", this.getId()); b.property(VertexProperty.Cardinality.single, "blah", random.nextDouble()); e.property("bloop", random.nextInt()); graph.tx().commit(); vertices.getAndAdd(2); edges.getAndAdd(1); } else { graph.tx().rollback();
@Test public void testOrForceIndexComposite() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("ageridx", Vertex.class).addKey(ageProperty).buildCompositeIndex(); management.commit(); customGraph.tx().commit(); final GraphTraversalSource g = customGraph.traversal(); g.addV().property("name", "Hiro").property("age", 2).next(); g.addV().property("name", "Totoro").property("age", 1).next(); customGraph.tx().commit(); g.V().has("age", P.gte(4).or(P.lt(2))).hasNext(); fail("should fail"); } catch (final JanusGraphException e){ assertTrue(e.getMessage().contains("Could not find a suitable index to answer graph query and graph scans are disabled")); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } } }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS) public void shouldAllowReferenceOfEdgeOutsideOfOriginalTransactionalContextManual() { g.tx().onReadWrite(Transaction.READ_WRITE_BEHAVIOR.MANUAL); g.tx().open(); final Vertex v1 = graph.addVertex(); final Edge e = v1.addEdge("self", v1, "weight", 0.5d); g.tx().commit(); g.tx().open(); assertEquals(0.5d, e.value("weight"), 0.00001d); g.tx().rollback(); g.tx().open(); assertEquals(0.5d, e.value("weight"), 0.00001d); g.tx().close(); }
@Test public void testAndForceIndex() throws Exception { JanusGraph customGraph = null; try { customGraph = this.getForceIndexGraph(); final JanusGraphManagement management = customGraph.openManagement(); final PropertyKey nameProperty = management.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); final PropertyKey ageProperty = management.makePropertyKey("age").dataType(Integer.class).cardinality(Cardinality.SINGLE).make(); management.buildIndex("oridx", Vertex.class).addKey(nameProperty, getStringMapping()).addKey(ageProperty).buildMixedIndex(INDEX); management.commit(); customGraph.tx().commit(); final GraphTraversalSource g = customGraph.traversal(); g.addV().property("name", "Hiro").property("age", 2).next(); g.addV().property("name", "Totoro").property("age", 1).next(); customGraph.tx().commit(); assertCount(1, g.V().has("name", "Totoro")); assertCount(1, g.V().has("age", 2)); assertCount(1, g.V().and(__.has("name", "Hiro"),__.has("age", 2))); assertCount(0, g.V().and(__.has("name", "Totoro"),__.has("age", 2))); } finally { if (customGraph != null) { JanusGraphFactory.close(customGraph); } } }
@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 shouldCountVerticesEdgesOnPreTransactionCommit() { // see a more complex version of this test at GraphTest.shouldProperlyCountVerticesAndEdgesOnAddRemove() Vertex v1 = graph.addVertex(); graph.tx().commit(); assertVertexEdgeCounts(graph, 1, 0); final Vertex v2 = graph.addVertex(); v1 = graph.vertices(v1.id()).next(); v1.addEdge("friend", v2); assertVertexEdgeCounts(graph, 2, 1); graph.tx().commit(); assertVertexEdgeCounts(graph, 2, 1); }