@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()); } } } }
private GraphCentricQuery buildGraphCentricQuery(final JanusGraphTransaction tx, final Entry<List<HasContainer>, QueryInfo> containers) { final JanusGraphQuery query = tx.query(); addConstraint(query, containers.getKey()); final List<OrderEntry> realOrders = orders.isEmpty() ? containers.getValue().getOrders() : orders; for (final OrderEntry order : realOrders) query.orderBy(order.key, order.order); if (highLimit != BaseQuery.NO_LIMIT || containers.getValue().getHighLimit() != BaseQuery.NO_LIMIT) query.limit(Math.min(containers.getValue().getHighLimit(), highLimit)); Preconditions.checkArgument(query instanceof GraphCentricQueryBuilder); final GraphCentricQueryBuilder centricQueryBuilder = ((GraphCentricQueryBuilder) query); centricQueryBuilder.profiler(queryProfiler); final GraphCentricQuery graphCentricQuery = centricQueryBuilder.constructQuery(Vertex.class.isAssignableFrom(this.returnClass) ? ElementCategory.VERTEX: ElementCategory.EDGE); return graphCentricQuery; }
public static JanusGraphVertex getVertex(JanusGraphTransaction tx, String key, Object value) { return getOnlyElement(tx.query().has(key,value).vertices(),null); }
private GraphCentricQuery buildGlobalGraphCentricQuery(final JanusGraphTransaction tx) { //If a query have a local offset or have a local order without a global order and if a query have a limit lower than the global different from other query we can not build globalquery final Iterator<QueryInfo> itQueryInfo = hasLocalContainers.values().iterator(); QueryInfo queryInfo = itQueryInfo.next(); if (queryInfo.getLowLimit() > 0 || orders.isEmpty() && !queryInfo.getOrders().isEmpty()) { return null; } final Integer limit = queryInfo.getHighLimit(); while (itQueryInfo.hasNext()) { queryInfo = itQueryInfo.next(); if (queryInfo.getLowLimit() > 0 || (orders.isEmpty() && !queryInfo.getOrders().isEmpty()) || (queryInfo.getHighLimit() < highLimit && !limit.equals(queryInfo.getHighLimit()))) { return null; } } final JanusGraphQuery query = tx.query(); for(final List<HasContainer> localContainers : hasLocalContainers.keySet()) { final JanusGraphQuery localQuery = tx.query(); addConstraint(localQuery, localContainers); query.or(localQuery); } for (final OrderEntry order : orders) query.orderBy(order.key, order.order); if (highLimit != BaseQuery.NO_LIMIT || limit != BaseQuery.NO_LIMIT) query.limit(Math.min(limit, highLimit)); Preconditions.checkArgument(query instanceof GraphCentricQueryBuilder); final GraphCentricQueryBuilder centricQueryBuilder = ((GraphCentricQueryBuilder) query); centricQueryBuilder.profiler(queryProfiler); final GraphCentricQuery graphCentricQuery = centricQueryBuilder.constructQuery(Vertex.class.isAssignableFrom(this.returnClass) ? ElementCategory.VERTEX: ElementCategory.EDGE); return graphCentricQuery; }
@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? } } }
public JanusGraphStep(final GraphStep<S, E> originalStep) { super(originalStep.getTraversal(), originalStep.getReturnClass(), originalStep.isStartStep(), originalStep.getIds()); originalStep.getLabels().forEach(this::addLabel); this.setIteratorSupplier(() -> { if (this.ids == null) { return Collections.emptyIterator(); } else if (this.ids.length > 0) { final Graph graph = (Graph)traversal.asAdmin().getGraph().get(); return iteratorList((Iterator)graph.vertices(this.ids)); } if (hasLocalContainers.isEmpty()) { hasLocalContainers.put(new ArrayList<>(), new QueryInfo(new ArrayList<>(), 0, BaseQuery.NO_LIMIT)); } final JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(traversal); final GraphCentricQuery globalQuery = buildGlobalGraphCentricQuery(tx); final Multimap<Integer, GraphCentricQuery> queries = ArrayListMultimap.create(); if (globalQuery != null && !globalQuery.getSubQuery(0).getBackendQuery().isEmpty()) { queries.put(0, globalQuery); } else { hasLocalContainers.entrySet().forEach(c -> queries.put(c.getValue().getLowLimit(), buildGraphCentricQuery(tx, c))); } final GraphCentricQueryBuilder builder = (GraphCentricQueryBuilder) tx.query(); final List<Iterator<E>> responses = new ArrayList<>(); queries.entries().forEach(q -> executeGraphCentryQuery(builder, responses, q)); return new MultiDistinctOrderedIterator<E>(lowLimit, highLimit, responses, orders); }); }
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());
@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 testSetUpdate() { if (!indexFeatures.supportsCardinality(Cardinality.SET)) { return; } final PropertyKey name = makeKey("name", String.class); final PropertyKey alias = mgmt.makePropertyKey("alias").dataType(String.class).cardinality(Cardinality.SET).make(); mgmt.buildIndex("namev", Vertex.class).addKey(name).addKey(alias, indexFeatures.supportsStringMapping(Mapping.TEXTSTRING) ?Mapping.TEXTSTRING.asParameter(): Mapping.DEFAULT.asParameter()).buildMixedIndex(INDEX); finishSchema(); JanusGraphVertex v = tx.addVertex("name", "Marko Rodriguez"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.set, "alias", "Marko"); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); clopen(); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.set, "alias", "mRodriguez"); assertCount(1, tx.query().has("alias", Text.CONTAINS, "mRodriguez").vertices()); clopen(); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); assertCount(1, tx.query().has("alias", Text.CONTAINS, "mRodriguez").vertices()); if (indexFeatures.supportsStringMapping(Mapping.TEXTSTRING)) { assertCount(1, tx.query().has("alias", Cmp.EQUAL, "Marko").vertices()); assertCount(1, tx.query().has("alias", Cmp.EQUAL, "mRodriguez").vertices()); } }
finishSchema(); JanusGraphVertex v = tx.addVertex("name", "Marko Rodriguez"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.list, "alias", "Marko"); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); clopen(); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.list, "alias", "mRodriguez"); assertCount(1, tx.query().has("alias", Text.CONTAINS, "mRodriguez").vertices()); clopen(); assertCount(1, tx.query().has("alias", Text.CONTAINS, "Marko").vertices()); assertCount(1, tx.query().has("alias", Text.CONTAINS, "mRodriguez").vertices()); if (indexFeatures.supportsStringMapping(Mapping.TEXTSTRING)) { assertCount(1, tx.query().has("alias", Cmp.EQUAL, "Marko").vertices()); assertCount(1, tx.query().has("alias", Cmp.EQUAL, "mRodriguez").vertices());
@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 public void testSimpleUpdate() { final PropertyKey name = makeKey("name", String.class); makeLabel("knows"); mgmt.buildIndex("namev", Vertex.class).addKey(name).buildMixedIndex(INDEX); mgmt.buildIndex("namee", Edge.class).addKey(name).buildMixedIndex(INDEX); finishSchema(); JanusGraphVertex v = tx.addVertex("name", "Marko Rodriguez"); Edge e = v.addEdge("knows", v, "name", "Hulu Bubab"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (final Vertex u : tx.getVertices()) assertEquals("Marko Rodriguez", u.value("name")); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (final Vertex u : tx.getVertices()) assertEquals("Marko Rodriguez", u.value("name")); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.single, "name", "Marko"); e = getOnlyEdge(v.query().direction(Direction.OUT)); e.property("name", "Tubu Rubu"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Rubu").edges()); assertCount(0, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (final Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name")); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Rubu").edges()); assertCount(0, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (final Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name")); }
@Test public void testLimitWithMixedIndexCoverage() { final String vt = "vt"; final String fn = "firstname"; final String user = "user"; final String alice = "alice"; final String bob = "bob"; PropertyKey vtk = makeVertexIndexedKey(vt, String.class); PropertyKey fnk = makeKey(fn, String.class); finishSchema(); JanusGraphVertex a = tx.addVertex(vt, user, fn, "alice"); JanusGraphVertex b = tx.addVertex(vt, user, fn, "bob"); JanusGraphVertex v; v = Iterables.getOnlyElement(tx.query().has(vt, user).has(fn, bob).limit(1).vertices()); assertEquals(bob, v.value(fn)); assertEquals(user, v.value(vt)); v = Iterables.getOnlyElement(tx.query().has(vt, user).has(fn, alice).limit(1).vertices()); assertEquals(alice, v.value(fn)); assertEquals(user, v.value(vt)); tx.commit(); tx = graph.newTransaction(); v = Iterables.getOnlyElement(tx.query().has(vt, user).has(fn, bob).limit(1).vertices()); assertEquals(bob, v.value(fn)); assertEquals(user, v.value(vt)); v = Iterables.getOnlyElement(tx.query().has(vt, user).has(fn, alice).limit(1).vertices()); assertEquals(alice, v.value(fn)); assertEquals(user, v.value(vt)); }
/** * 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()); }
@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 testKeyBasedGraphPartitioning() { Object[] options = {option(GraphDatabaseConfiguration.IDS_FLUSH), false, option(VertexIDAssigner.PLACEMENT_STRATEGY), PropertyPlacementStrategy.class.getName(), option(PropertyPlacementStrategy.PARTITION_KEY), "clusterId"}; clopen(options); int[] groupDegrees = {5,5,5,5,5,5,5,5}; int numVertices = setupGroupClusters(groupDegrees,CommitMode.PER_VERTEX); IntSet partitionIds = new IntHashSet(numVertices); //to track the "spread" of partition ids for (int i=0;i<groupDegrees.length;i++) { JanusGraphVertex g = getOnlyVertex(tx.query().has("groupid","group"+i)); int partitionId = -1; for (Object o : g.query().direction(Direction.IN).labels("member").vertices()) { JanusGraphVertex v = (JanusGraphVertex) o; if (partitionId<0) partitionId = getPartitionID(v); assertEquals(partitionId,getPartitionID(v)); partitionIds.add(partitionId); } } assertTrue(partitionIds.size()>numPartitions/2); //This is a probabilistic test that might fail }
/** * 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); }
private void testPartitionSpread(boolean flush, boolean batchCommit) { Object[] options = {option(GraphDatabaseConfiguration.IDS_FLUSH), flush}; clopen(options); int[] groupDegrees = {10,15,10,17,10,4,7,20,11}; int numVertices = setupGroupClusters(groupDegrees,batchCommit?CommitMode.BATCH:CommitMode.PER_VERTEX); IntSet partitionIds = new IntHashSet(numVertices); //to track the "spread" of partition ids for (int i=0;i<groupDegrees.length;i++) { JanusGraphVertex g = getOnlyVertex(tx.query().has("groupid","group"+i)); assertCount(groupDegrees[i],g.edges(Direction.OUT,"contain")); assertCount(groupDegrees[i],g.edges(Direction.IN,"member")); assertCount(groupDegrees[i],g.query().direction(Direction.OUT).edges()); assertCount(groupDegrees[i],g.query().direction(Direction.IN).edges()); assertCount(groupDegrees[i]*2,g.query().edges()); for (Object o : g.query().direction(Direction.IN).labels("member").vertices()) { JanusGraphVertex v = (JanusGraphVertex) o; int pid = getPartitionID(v); partitionIds.add(pid); assertEquals(g, getOnlyElement(v.query().direction(Direction.OUT).labels("member").vertices())); VertexList vertexList = v.query().direction(Direction.IN).labels("contain").vertexIds(); assertEquals(1,vertexList.size()); assertEquals(pid,idManager.getPartitionId(vertexList.getID(0))); assertEquals(g,vertexList.get(0)); } } if (flush || !batchCommit) { //In these cases we would expect significant spread across partitions assertTrue(partitionIds.size()>numPartitions/2); //This is a probabilistic test that might fail } else { assertEquals(1,partitionIds.size()); //No spread in this case } }