try { for (int i = 0; i < batchV; i++) { final JanusGraphVertex v = tx.addVertex(); v.property("k", random.nextInt(maxK)); v.property("q", random.nextInt(maxQ)); tx.commit(); } catch (Throwable e) { e.printStackTrace(); } finally { if (tx.isOpen()) tx.rollback(); final Set<Vertex> vs = new HashSet<>(); final Iterable<JanusGraphVertex> vertices = tx.query().has("k",random.nextInt(maxK)).has("q",random.nextInt(maxQ)).vertices(); for (JanusGraphVertex v : vertices) { if (!vs.add(v)) { tx.commit(); } catch (Throwable e) { e.printStackTrace(); } finally { if (tx.isOpen()) tx.rollback();
assertFalse(tx.containsVertexLabel("person")); assertFalse(tx.containsVertexLabel("person")); assertFalse(tx.containsRelationType("value")); assertNull(tx.getPropertyKey("value")); PropertyKey value = tx.getOrCreatePropertyKey("value"); assertNotNull(value); assertTrue(tx.containsRelationType("value")); JanusGraphVertex v = tx.addVertex("person"); assertTrue(tx.containsVertexLabel("person")); assertEquals("person", v.label()); assertFalse(tx.containsRelationType("knows")); Edge e = v.addEdge("knows", v); assertTrue(tx.containsRelationType("knows")); assertNotNull(tx.getEdgeLabel(e.label())); assertTrue(tx.containsRelationType("value")); assertTrue(tx.containsVertexLabel("person")); assertTrue(tx.containsRelationType("knows")); v = getV(tx, v); tx.addVertex("org"); fail(); } catch (IllegalArgumentException ignored) {
@Override public void run() { JanusGraphTransaction tx = graph.buildTransaction().enableBatchLoading().start(); try { for (Map.Entry<Long, Map<String, Object>> vertexProperty : properties) { Vertex v = tx.getVertex(vertexProperty.getKey()); if (v == null) { continue; } for (Map.Entry<String, Object> prop : vertexProperty.getValue().entrySet()) { v.property(VertexProperty.Cardinality.single, prop.getKey(), prop.getValue()); } } tx.commit(); } catch (Throwable e) { failures.incrementAndGet(); log.error("Encountered exception while trying to write properties: ", e); } finally { if (tx != null && tx.isOpen()) tx.rollback(); } } }
@Test public void testAutoSchemaMakerForEdgePropertyDataType(){ JanusGraphVertex v1 = tx.addVertex("user"); JanusGraphVertex v2 = tx.addVertex("user"); v1.addEdge("knows", v2, "id", 10, "created", new Date()); PropertyKey idPropertyKey = tx.getPropertyKey("id"); assertEquals("Data type not identified correctly by auto schema maker", Integer.class, idPropertyKey.dataType()); PropertyKey createdPropertyKey = tx.getPropertyKey("created"); assertEquals("Data type not identified correctly by auto schema maker", Date.class, createdPropertyKey.dataType()); }
tx.makePropertyKey("name").dataType(String.class).make(); tx.makeEdgeLabel("knows").make(); tx.makeVertexLabel("person").make(); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME); verifyLockingOverwrite(3); assertTrue(tx.containsRelationType("name")); assertTrue(tx.containsRelationType("knows")); assertTrue(tx.containsVertexLabel("person")); PropertyKey name = tx.getPropertyKey("name"); EdgeLabel knows = tx.getEdgeLabel("knows"); VertexLabel person = tx.getVertexLabel("person"); PropertyKey uid = tx.getPropertyKey("uid"); tx.commit(); JanusGraphVertex v = tx.addVertex(), u = tx.addVertex("person"); v.property(VertexProperty.Cardinality.single, "uid", 1); u.property(VertexProperty.Cardinality.single, "name", "juju"); Edge e = v.addEdge("knows",u); e.property("name", "edge"); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME); verifyLockingOverwrite(1); metricsPrefix = "op"+i+cache; tx = graph.buildTransaction().groupName(metricsPrefix).start(); v = getOnlyElement(tx.query().has("uid",1).vertices());
finishSchema(); JanusGraphVertex v1 = tx.addVertex(), v2 = tx.addVertex(), v3 = tx.addVertex(); e2.property("time", time2); tx.commit(); e2.id(); evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 1, new boolean[]{true, true}, "index2"); evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1"); v1 = getV(tx, v1.id()); v2 = getV(tx, v2.id()); evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 0, new boolean[]{true, true}, "index2"); evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 0, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1");
@Test public void testVertexCentricIndexOrderingOnEdgePropertyWithCardinalityList() { clopen(option(LOG_SEND_DELAY, MANAGEMENT_LOG), Duration.ofMillis(0), option(KCVSLog.LOG_READ_LAG_TIME, MANAGEMENT_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(250) ); PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).cardinality(Cardinality.LIST).make(); EdgeLabel friend = mgmt.makeEdgeLabel("friend").multiplicity(Multiplicity.MULTI).make(); mgmt.buildEdgeIndex(friend, "byTime", Direction.OUT, decr, time); finishSchema(); JanusGraphVertex v = tx.addVertex(); for (int i = 200; i < 210; i++) { JanusGraphVertex o = tx.addVertex(); v.addEdge("friend", o, "time", i); } assertEquals(SchemaStatus.ENABLED, mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime").getIndexStatus()); evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 201, 205).orderBy("time", decr), EDGE, 4, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC); tx.commit(); finishSchema(); }
@Test public void concurrentIndexTest() { makeVertexIndexedUniqueKey("uid", String.class); makeVertexIndexedKey("value", Object.class); finishSchema(); tx.addVertex("uid", "v"); clopen(); //Concurrent index addition JanusGraphTransaction tx1 = graph.newTransaction(); JanusGraphTransaction tx2 = graph.newTransaction(); getVertex(tx1, "uid", "v").property(VertexProperty.Cardinality.single, "value", 11); getVertex(tx2, "uid", "v").property(VertexProperty.Cardinality.single, "value", 11); tx1.commit(); tx2.commit(); assertEquals("v", Iterators.<String>getOnlyElement(Iterables.getOnlyElement(tx.query().has("value", 11).vertices()).values("uid"))); }
/** * Very simple graph operation to ensure minimal functionality and cleanup */ @Test public void testBasic() throws BackendException { PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class); finishSchema(); JanusGraphVertex n1 = tx.addVertex(); uid = tx.getPropertyKey("name"); n1.property(uid.name(), "abcd"); clopen(); long nid = n1.longId(); uid = tx.getPropertyKey("name"); assertTrue(getV(tx, nid) != null); assertTrue(getV(tx, uid.longId()) != null); assertMissing(tx, nid + 64); uid = tx.getPropertyKey(uid.name()); n1 = getV(tx, nid); assertEquals(n1, getOnlyVertex(tx.query().has(uid.name(), "abcd"))); assertEquals(1, Iterables.size(n1.query().relations())); //TODO: how to expose relations? assertEquals("abcd", n1.value(uid.name())); assertCount(1, tx.query().vertices()); close(); JanusGraphFactory.drop(graph); open(config); assertEmpty(tx.query().vertices()); }
int deleteV = 5; JanusGraphVertex previous = tx.addVertex("count", 0); for (int i = 1; i < numV; i++) { JanusGraphVertex next = tx.addVertex("count", i); previous.addEdge("next", next); previous = next; assertCount(numV, tx.query().vertices()); assertCount(numV, tx.query().vertices()); assertCount(numE, tx.query().edges()); assertCount(numE, tx.query().edges()); assertCount(numV, tx.query().vertices()); assertCount(numV, tx.query().vertices()); assertCount(numE, tx.query().edges()); assertCount(numE, tx.query().edges()); for (Object v : tx.query().limit(deleteV).vertices()) { ((JanusGraphVertex) v).remove(); assertCount(numV - deleteV, tx.query().vertices()); assertCount(numV - deleteV, tx.query().has("count", Cmp.GREATER_THAN_EQUAL, 0).vertices()); assertCount(numV - deleteV, tx.query().vertices()); assertCount(numV - deleteV, tx.query().has("count", Cmp.GREATER_THAN_EQUAL, 0).vertices());
PropertyKey weight = tx.makePropertyKey("weight").dataType(Float.class).cardinality(Cardinality.SINGLE).make(); EdgeLabel knows = tx.makeEdgeLabel("knows").make(); JanusGraphVertex n1 = tx.addVertex("weight", 10.5); tx.addProperties(knows, weight); newTx(); JanusGraphVertex v1 = tx2.addVertex("weight", 111.1); v1.addEdge("knows", v1); tx2.commit(); final long v1id = getId(v1); txTimes[1] = times.getTime(); tx2 = graph.buildTransaction().logIdentifier(userLogName).start(); JanusGraphVertex v2 = tx2.addVertex("weight", 222.2); v2.addEdge("knows", getV(tx2, v1id)); tx2.commit(); final long v2id = getId(v2); assertEquals(111.1, v1.<Float>value("weight").doubleValue(), 0.01); assertEquals(222.2, getV(tx2, v2).<Float>value("weight").doubleValue(), 0.01); tx2.commit(); assertEquals(222.2, v2.<Float>value("weight").doubleValue(), 0.01); v2.remove(); tx2.commit(); assertFalse(e.property("weight").isPresent()); e.property("weight", 44.4); tx2.commit(); close();
@Test public void testIndexQueryWithScore() throws InterruptedException { final PropertyKey textKey = mgmt.makePropertyKey("text").dataType(String.class).make(); mgmt.buildIndex("store1", Vertex.class).addKey(textKey).buildMixedIndex(INDEX); mgmt.commit(); final JanusGraphVertex v1 = tx.addVertex(); final JanusGraphVertex v2 = tx.addVertex(); final JanusGraphVertex v3 = tx.addVertex(); v1.property("text", "Hello Hello Hello Hello Hello Hello Hello Hello world"); v2.property("text", "Hello abab abab fsdfsd sfdfsd sdffs fsdsdf fdf fsdfsd aera fsad abab abab fsdfsd sfdf"); v3.property("text", "Hello Hello world world"); tx.commit(); final Set<Double> scores = graph.indexQuery("store1", "v.text:(Hello)").vertexStream() .map(JanusGraphIndexQuery.Result::getScore) .collect(Collectors.toSet()); Assert.assertEquals(3, scores.size()); }
@Override public void run() { for (int i = 0; i < vertexCount; i++) { for (int p = 0; p < propCount; p++) { Iterables.size(tx.query().has("p" + p, i).vertices()); } } } }
@Test public void testAllowEnforcedComplexConnections() { createStrictSchemaForComplexConnections(); JanusGraphVertex v1 = tx.addVertex("V1"); JanusGraphVertex v2 = tx.addVertex("V2"); JanusGraphVertex v3 = tx.addVertex("V3"); JanusGraphVertex v4 = tx.addVertex("V4"); v1.addEdge("E1", v2); v3.addEdge("E1", v4); v3.addEdge("E2", v2); v1.addEdge("E2", v4); }
v1 = tx.addVertex(); try { v1.property(VertexProperty.Cardinality.single, "domain", "unique1"); tx.rollback(); tx = null; v1 = tx.addVertex(); v1.property("domain", "unique1"); try { v2 = tx.addVertex(); v2.property("domain", "unique1"); fail(); tx.rollback(); tx = null; v1 = tx.addVertex(); v1.property("domain", "unique1"); assertCount(1, tx.query().has("domain", "unique1").vertices()); try { v2 = tx.addVertex(); v2.property("domain", "unique1"); fail(); tx.rollback(); tx = null;
/** * 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(); }
vertexLabel = record.get(vertexLabelFieldName); JanusGraphVertex v = graphTransaction.addVertex(vertexLabel); graphTransaction.getPropertyKey(propName).dataType()); v.property(propName, convertedValue); graphTransaction.commit(); graphTransaction.close(); graphTransaction = getGraph().newTransaction();
/** * This test exercises different types of updates against cardinality restricted properties * to ensure that the resulting behavior is fully consistent. */ @Test public void testPropertyCardinality() { PropertyKey uid = mgmt.makePropertyKey("uid").dataType(Long.class).cardinality(Cardinality.SINGLE).make(); PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); mgmt.buildIndex("byUid", Vertex.class).addKey(uid).unique().buildCompositeIndex(); mgmt.buildIndex("byName", Vertex.class).addKey(name).buildCompositeIndex(); finishSchema(); JanusGraphVertex v1 = tx.addVertex(); v1.property("name", "name1"); JanusGraphVertex v2 = tx.addVertex(); v2.property("uid", 512); newTx(); v1 = tx.getVertex(v1.longId()); v1.property("name", "name2"); //Ensure that the old index record gets removed v2 = tx.getVertex(v2.longId()); v2.property("uid", 512); //Ensure that replacement is allowed newTx(); assertCount(0, tx.query().has("name", "name1").vertices()); assertCount(1, tx.query().has("name", "name2").vertices()); assertCount(1, tx.query().has("uid", 512).vertices()); }