public static JanusGraphVertex getOnlyVertex(JanusGraphQuery<?> query) { return getOnlyElement(query.vertices()); }
@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()); } } } }
public static JanusGraphVertex getVertex(JanusGraphTransaction tx, String key, Object value) { return getOnlyElement(tx.query().has(key,value).vertices(),null); }
@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())); }
@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; } }
@Test public void testLargeJointIndexRetrieval() { makeVertexIndexedKey("sid", Integer.class); makeVertexIndexedKey("color", String.class); finishSchema(); int sids = 17; String[] colors = {"blue", "red", "yellow", "brown", "green", "orange", "purple"}; int multiplier = 200; int numV = sids * colors.length * multiplier; for (int i = 0; i < numV; i++) { JanusGraphVertex v = graph.addVertex( "color", colors[i % colors.length], "sid", i % sids); } clopen(); assertCount(numV / sids, graph.query().has("sid", 8).vertices()); assertCount(numV / colors.length, graph.query().has("color", colors[2]).vertices()); assertCount(multiplier, graph.query().has("sid", 11).has("color", colors[3]).vertices()); }
private void testInstant(Instant firstTimestamp, Instant secondTimestamp, JanusGraphVertex v1, JanusGraphVertex v2) { assertEquals(v1, getOnlyVertex(graph.query().has("instant", Cmp.EQUAL, firstTimestamp))); assertEquals(v2, getOnlyVertex(graph.query().has("instant", Cmp.GREATER_THAN, firstTimestamp))); assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("instant", Cmp.GREATER_THAN_EQUAL, firstTimestamp).vertices())); assertEquals(v1, getOnlyVertex(graph.query().has("instant", Cmp.LESS_THAN, secondTimestamp))); assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("instant", Cmp.LESS_THAN_EQUAL, secondTimestamp).vertices())); assertEquals(v2, getOnlyVertex(graph.query().has("instant", Cmp.NOT_EQUAL, firstTimestamp))); clopen();//Flush the index assertEquals(v1, getOnlyVertex(graph.query().has("instant", Cmp.EQUAL, firstTimestamp))); assertEquals(v2, getOnlyVertex(graph.query().has("instant", Cmp.GREATER_THAN, firstTimestamp))); assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("instant", Cmp.GREATER_THAN_EQUAL, firstTimestamp).vertices())); assertEquals(v1, getOnlyVertex(graph.query().has("instant", Cmp.LESS_THAN, secondTimestamp))); assertEquals(Sets.newHashSet(v1, v2), Sets.newHashSet(graph.query().has("instant", Cmp.LESS_THAN_EQUAL, secondTimestamp).vertices())); assertEquals(v2, getOnlyVertex(graph.query().has("instant", Cmp.NOT_EQUAL, firstTimestamp))); }
public static void assertGraphOfTheGods(JanusGraph graphOfTheGods) { assertCount(12, graphOfTheGods.query().vertices()); assertCount(3, graphOfTheGods.query().has(LABEL_NAME, "god").vertices()); final JanusGraphVertex h = getOnlyVertex(graphOfTheGods.query().has("name", "hercules")); assertEquals(30, h.<Integer>value("age").intValue()); assertEquals("demigod", h.label()); assertCount(5, h.query().direction(Direction.BOTH).edges()); graphOfTheGods.tx().commit(); }
@Test public void testForceIndexUsage() { PropertyKey age = makeKey("age", Integer.class); PropertyKey time = makeKey("time", Long.class); mgmt.buildIndex("time", Vertex.class).addKey(time).buildCompositeIndex(); finishSchema(); for (int i = 1; i <= 10; i++) { JanusGraphVertex v = tx.addVertex("time", i, "age", i); } //Graph query with and with-out index support assertCount(1, tx.query().has("time", 5).vertices()); assertCount(1, tx.query().has("age", 6).vertices()); clopen(option(FORCE_INDEX_USAGE), true); //Query with-out index support should now throw exception assertCount(1, tx.query().has("time", 5).vertices()); try { assertCount(1, tx.query().has("age", 6).vertices()); fail(); } catch (Exception ignored) { } }
@Override protected void doRun() { JanusGraphVertex v = Iterables.getOnlyElement(tx.query().has(idKey, vertexId).vertices()); for (int i = 0; i < nodeTraversalCount; i++) { assertCount(expectedEdges, v.query().labels(label2Traverse).direction(Direction.BOTH).edges()); for (Object r : v.query().direction(Direction.OUT).labels(label2Traverse).edges()) { v = ((JanusGraphEdge) r).vertex(Direction.IN); } } } }
@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"); }
public void testBatchLoadingLocking(boolean batchLoading) { PropertyKey uid = makeKey("uid",Long.class); JanusGraphIndex uidIndex = mgmt.buildIndex("uid",Vertex.class).unique().addKey(uid).buildCompositeIndex(); mgmt.setConsistency(uid, ConsistencyModifier.LOCK); mgmt.setConsistency(uidIndex,ConsistencyModifier.LOCK); EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.ONE2ONE).make(); mgmt.setConsistency(knows,ConsistencyModifier.LOCK); finishSchema(); TestLockerManager.ERROR_ON_LOCKING=true; clopen(option(GraphDatabaseConfiguration.STORAGE_BATCH),batchLoading, option(GraphDatabaseConfiguration.LOCK_BACKEND),"test"); int numV = 10000; for (int i=0;i<numV;i++) { JanusGraphVertex v = tx.addVertex("uid",i+1); v.addEdge("knows",v); } clopen(); for (int i=0;i<Math.min(numV,300);i++) { assertEquals(1, Iterables.size(graph.query().has("uid", i + 1).vertices())); JanusGraphVertex v = Iterables.getOnlyElement(graph.query().has("uid", i + 1).vertices()); assertEquals(1, Iterables.size(v.query().direction(OUT).labels("knows").edges())); } }
@Test public void testStaleVertex() { PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make(); mgmt.makePropertyKey("age").dataType(Integer.class).make(); mgmt.buildIndex("byName", Vertex.class).addKey(name).unique().buildCompositeIndex(); finishSchema(); JanusGraphVertex cartman = graph.addVertex("name", "cartman", "age", 10); graph.addVertex("name", "stan", "age", 8); graph.tx().commit(); cartman = Iterables.getOnlyElement(graph.query().has("name", "cartman").vertices()); graph.tx().commit(); JanusGraphVertexProperty p = (JanusGraphVertexProperty) cartman.properties().next(); assertTrue((p.longId()) > 0); graph.tx().commit(); }
@Test public void testVertexTTLWithCompositeIndex() throws Exception { if (!features.hasCellTTL()) { return; } PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make(); PropertyKey time = mgmt.makePropertyKey("time").dataType(Long.class).make(); final JanusGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name) .buildCompositeIndex(); final JanusGraphIndex index2 = mgmt.buildIndex("index2", Vertex.class).addKey(name).addKey(time) .buildCompositeIndex(); VertexLabel label1 = mgmt.makeVertexLabel("event").setStatic().make(); mgmt.setTTL(label1, Duration.ofSeconds(1)); assertEquals(Duration.ZERO, mgmt.getTTL(name)); assertEquals(Duration.ZERO, mgmt.getTTL(time)); assertEquals(Duration.ofSeconds(1), mgmt.getTTL(label1)); mgmt.commit(); JanusGraphVertex v1 = tx.addVertex(T.label, "event", "name", "some event", "time", System.currentTimeMillis()); tx.commit(); Object id = v1.id(); v1 = getV(graph, id); assertNotNull(v1); assertNotEmpty(graph.query().has("name", "some event").vertices()); Thread.sleep(1001); graph.tx().rollback(); v1 = getV(graph, id); assertNull(v1); assertEmpty(graph.query().has("name", "some event").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"))); }
/** * 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 checkPropertyLockingAndIndex() { PropertyKey uid = makeKey("uid",String.class); JanusGraphIndex index = mgmt.buildIndex("uid",Vertex.class).unique().addKey(uid).buildCompositeIndex(); mgmt.setConsistency(index, ConsistencyModifier.LOCK); mgmt.makePropertyKey("name").dataType(String.class).make(); mgmt.makePropertyKey("age").dataType(Integer.class).make(); finishSchema(); metricsPrefix = "checkPropertyLockingAndIndex"; JanusGraphTransaction tx = graph.buildTransaction().groupName(metricsPrefix).start(); JanusGraphVertex v = tx.addVertex("uid", "v1", "age", 25, "name", "john"); assertEquals(25,v.property("age").value()); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME); verifyLockingOverwrite(1); verifyStoreMetrics(METRICS_STOREMANAGER_NAME, ImmutableMap.of(M_MUTATE, 1L)); resetMetrics(); tx = graph.buildTransaction().groupName(metricsPrefix).start(); v = Iterables.getOnlyElement(tx.query().has("uid", Cmp.EQUAL, "v1").vertices()); assertEquals(25,v.property("age").value()); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME, ImmutableMap.of(M_GET_SLICE, 1L)); verifyStoreMetrics(INDEXSTORE_NAME, ImmutableMap.of(M_GET_SLICE, 1L)); verifyStoreMetrics(METRICS_STOREMANAGER_NAME); }
@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()); } }
/** * 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()); }