@Override <V> JanusGraphVertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues);
@Override public void run(JanusGraphTransaction tx) { JanusGraphVertex u = getV(tx, vertexIds[pos++]); u.property(VertexProperty.Cardinality.single, "uid", 5); } });
/** * This is essentially an adjusted copy&paste from TinkerPop's ElementHelper class. * The reason for copying it is so that we can determine the cardinality of a property key based on * JanusGraph's schema which is tied to this particular transaction and not the graph. * * @param vertex * @param propertyKeyValues */ public static void attachProperties(final JanusGraphVertex vertex, final Object... propertyKeyValues) { if (null == vertex) throw Graph.Exceptions.argumentCanNotBeNull("vertex"); for (int i = 0; i < propertyKeyValues.length; i = i + 2) { if (!propertyKeyValues[i].equals(T.id) && !propertyKeyValues[i].equals(T.label)) vertex.property((String) propertyKeyValues[i], propertyKeyValues[i + 1]); } }
private void addVertex(int time, String text, double height, String[] phones) { newTx(); final JanusGraphVertex v = tx.addVertex("text", text, "time", time, "height", height); for (final String phone : phones) { v.property("phone", phone); } newTx(); }
@Test public void testEnforcedSchemaAllowsDefinedVertexProperties() { createStrictSchemaForVertexProperties(); JanusGraphVertex v = tx.addVertex("user"); v.property("id", 10); }
@Test public void testAutoSchemaMakerForVertexPropertyConstraints() { clopen(option(SCHEMA_CONSTRAINTS), true); JanusGraphVertex v1 = tx.addVertex("user"); v1.property("test", 10); clopen(option(AUTO_TYPE), "none", option(SCHEMA_CONSTRAINTS), true); JanusGraphVertex v2 = tx.addVertex("user"); v2.property("test", 10); try { v2.property("id", 10); fail("This should never reached!"); } catch (IllegalArgumentException ignored) { } }
@Test public void testAutoSchemaMakerForVertexPropertyDataType(){ JanusGraphVertex v1 = tx.addVertex("user"); v1.property("id", 10); v1.property("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 properly by auto schema maker", Date.class, createdPropertyKey.dataType()); }
@Override public void run() { while (true) { // Set propType to a random value on a random node JanusGraphVertex n = getOnlyVertex(tx.query().has(idKey, RandomGenerator.randomInt(0, nodeCount))); String propVal = RandomGenerator.randomString(); n.property(randomKey, propVal); if (Thread.interrupted()) break; // Is creating the same property twice an error? } } }
@Test public void testSchemaIsEnforcedForVertexProperties() { createStrictSchemaForVertexProperties(); JanusGraphVertex v = tx.addVertex("user"); try { v.property("test", 10); fail("This should never reached!"); } catch (IllegalArgumentException ignored) { } }
@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()); }
@Test // this tests a case when there as AND with a single CONTAINS condition inside AND(name:(was here)) // which (in case of Solr) spans multiple conditions such as AND(AND(name:was, name:here)) // so we need to make sure that we don't apply AND twice. public void testContainsWithMultipleValues() throws Exception { final PropertyKey name = makeKey("name", String.class); mgmt.buildIndex("store1", Vertex.class).addKey(name).buildMixedIndex(INDEX); mgmt.commit(); final JanusGraphVertex v1 = tx.addVertex(); v1.property("name", "hercules was here"); tx.commit(); final JanusGraphVertex r = Iterables.get(graph.query().has("name", Text.CONTAINS, "hercules here").vertices(), 0); Assert.assertEquals(r.property("name").value(), "hercules was here"); }
/** * Tests indexing using _all virtual field */ @Test public void testWidcardQuery() { if (supportsWildcardQuery()) { final PropertyKey p1 = makeKey("p1", String.class); final PropertyKey p2 = makeKey("p2", String.class); mgmt.buildIndex("mixedIndex", Vertex.class).addKey(p1).addKey(p2).buildMixedIndex(INDEX); finishSchema(); clopen(); final JanusGraphVertex v1 = graph.addVertex(); v1.property("p1", "test1"); v1.property("p2", "test2"); clopen();//Flush the index assertEquals(v1, graph.indexQuery("mixedIndex", "v.*:\"test1\"").vertexStream().findFirst().orElseThrow(IllegalStateException::new).getElement()); assertEquals(v1, graph.indexQuery("mixedIndex", "v.*:\"test2\"").vertexStream().findFirst().orElseThrow(IllegalStateException::new).getElement()); } }
@Test public void testArrayEqualityUsingImplicitKey() { JanusGraphVertex v = graph.addVertex(); byte singleDimension[] = new byte[]{127, 0, 0, 1}; byte singleDimensionCopy[] = new byte[]{127, 0, 0, 1}; final String singlePropName = "single"; v.property(singlePropName, singleDimension); assertEquals(1, Iterables.size(graph.query().has(singlePropName, singleDimension).vertices())); assertEquals(1, Iterables.size(graph.query().has(singlePropName, singleDimensionCopy).vertices())); graph.tx().commit(); assertEquals(1, Iterables.size(graph.query().has(singlePropName, singleDimension).vertices())); assertEquals(1, Iterables.size(graph.query().has(singlePropName, singleDimensionCopy).vertices())); }
@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"))); }
@Test public void testAllowDisablingSchemaConstraintForVertexProperty() { clopen(option(AUTO_TYPE), "none", option(SCHEMA_CONSTRAINTS), false); mgmt.makeVertexLabel("user").make(); mgmt.makePropertyKey("test").cardinality(Cardinality.SINGLE).dataType(Integer.class).make(); finishSchema(); JanusGraphVertex v = tx.addVertex("user"); v.property("test", 10); }
@Test public void testIndexQueryWithLabelsAndContainsIN() { // This test is based on the steps to reproduce #882 String labelName = "labelName"; VertexLabel label = mgmt.makeVertexLabel(labelName).make(); PropertyKey uid = mgmt.makePropertyKey("uid").dataType(String.class).make(); JanusGraphIndex uidCompositeIndex = mgmt.buildIndex("uidIndex", Vertex.class) .indexOnly(label).addKey(uid).unique().buildCompositeIndex(); mgmt.setConsistency(uidCompositeIndex, ConsistencyModifier.LOCK); finishSchema(); JanusGraphVertex foo = graph.addVertex(labelName); JanusGraphVertex bar = graph.addVertex(labelName); foo.property("uid", "foo"); bar.property("uid", "bar"); graph.tx().commit(); Iterable<JanusGraphVertex> vertexes = graph.query() .has("uid", Contain.IN, ImmutableList.of("foo", "bar")) .has(LABEL_NAME, labelName) .vertices(); assertEquals(2, Iterables.size(vertexes)); for (JanusGraphVertex v : vertexes) { assertEquals(labelName, v.vertexLabel().name()); } }
/** * 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()); }
@Test public void testVertexCentricPropertyIndexOnSetCardinalityShouldWork() { 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.SINGLE).make(); PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SET).make(); mgmt.buildPropertyIndex(name, "byTime", decr, time); finishSchema(); assertEquals(SchemaStatus.ENABLED, mgmt.getRelationIndex(mgmt.getRelationType("name"), "byTime").getIndexStatus()); JanusGraphVertex v = tx.addVertex(); v = getV(tx, v); for (int i = 200; i < 210; i++) { v.property("name", String.valueOf(i), "time", i); } evaluateQuery(v.query().keys("name").interval("time", 199, 210).orderBy("time", decr), PROPERTY, 10, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC); tx.commit(); finishSchema(); }
@Test public void testVertexCentricIndexOrderingOnMetaPropertyWithCardinalityList() { 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(); PropertyKey sensor = mgmt.makePropertyKey("sensor").dataType(Integer.class).cardinality(Cardinality.LIST).make(); mgmt.buildPropertyIndex(sensor, "byTime", decr, time); finishSchema(); JanusGraphVertex v = tx.addVertex(); for (int i = 200; i < 210; i++) { v.property("sensor", i, "time", i); } assertEquals(SchemaStatus.ENABLED, mgmt.getRelationIndex(mgmt.getRelationType("sensor"), "byTime").getIndexStatus()); evaluateQuery(v.query().keys("sensor").interval("time", 201, 205).orderBy("time", decr), PROPERTY, 4, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC); tx.commit(); finishSchema(); }
@Override public void process(JanusGraphVertex vertex, ScanMetrics metrics) { long outDegree = vertex.query().labels("knows").direction(Direction.OUT).edgeCount(); assertEquals(0, vertex.query().labels("knows").direction(Direction.IN).edgeCount()); assertEquals(1, vertex.query().labels("uid").propertyCount()); assertTrue(vertex.<Integer>property("uid").orElse(0) > 0); metrics.incrementCustom(DEGREE_COUNT,outDegree); metrics.incrementCustom(VERTEX_COUNT); }