private void checkIndexingCounts(String[] words, int numV, int originalNumV, boolean checkOrder) { for (final String word : words) { final int expectedSize = numV / words.length; assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, word).vertices()); assertCount(expectedSize, tx.query().has("text", Text.CONTAINS, word).edges()); for (final Order order : Order.values()) { for (final JanusGraphQuery traversal : ImmutableList.of( tx.query().has("text", Text.CONTAINS, word).orderBy(orderKey, order.getTP()), tx.query().has("text", Text.CONTAINS, word).orderBy(orderKey, order.getTP()) )) { verifyElementOrder(traversal.vertices(), orderKey, order, expectedSize); assertCount(3, tx.query().has("group", 3).orderBy("time", incr).limit(3).vertices()); assertCount(3, tx.query().has("group", 3).orderBy("time", decr).limit(3).edges()); assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices()); assertCount(i, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges()); assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).vertices()); assertCount(4, tx.query().has("category", 1).interval("time", 10, 28).edges()); assertCount(5, tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices()); final double offset = (19 * 50.0 / originalNumV); final double distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20; assertCount(5, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices()); assertCount(5, tx.query().has("boundary", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices()); assertCount(numV, tx.query().vertices()); assertCount(numV, tx.query().edges());
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; }
@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? } } }
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());
@Test public void testWithoutIndex() { PropertyKey kid = mgmt.makePropertyKey("kid").dataType(Long.class).make(); mgmt.makePropertyKey("name").dataType(String.class).make(); mgmt.makeEdgeLabel("knows").signature(kid).make(); finishSchema(); Random random = new Random(); int numV = 1000; JanusGraphVertex previous = null; for (int i = 0; i < numV; i++) { JanusGraphVertex v = graph.addVertex( "kid", random.nextInt(numV), "name", "v" + i); if (previous != null) { Edge e = v.addEdge("knows", previous, "kid", random.nextInt(numV / 2)); } previous = v; } verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500); verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300); verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400); verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200); clopen(); //Copied from above verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500); verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300); verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400); verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200); }
assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices()); assertCount(i + 1, tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges()); assertCount(i + 1, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).vertices()); assertCount(i + 1, tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).edges()); assertCount(numV-(i + 1), tx.query().has("location", Geo.DISJOINT, Geoshape.circle(0.0, 0.0, distance)).vertices()); assertCount(numV-(i + 1), tx.query().has("location", Geo.DISJOINT, Geoshape.circle(0.0, 0.0, distance)).edges()); assertCount(i + 1, tx.query().has("boundary", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).vertices()); assertCount(i + 1, tx.query().has("boundary", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).edges()); if (i > 0) { assertCount(i, tx.query().has("boundary", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance-bufferKm)).vertices()); assertCount(i, tx.query().has("boundary", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance-bufferKm)).edges()); assertCount(numV-(i + 1), tx.query().has("boundary", Geo.DISJOINT, Geoshape.circle(0.0, 0.0, distance)).vertices()); assertCount(numV-(i + 1), tx.query().has("boundary", Geo.DISJOINT, Geoshape.circle(0.0, 0.0, distance)).edges()); assertCount(i % 2, tx.query().has("boundary", Geo.CONTAINS, Geoshape.point(-offset, -offset)).vertices()); assertCount(i % 2, tx.query().has("boundary", Geo.CONTAINS, Geoshape.point(-offset, -offset)).edges()); final double max = Math.abs(offset); final Geoshape bufferedBox = Geoshape.box(min-buffer, min-buffer, max+buffer, max+buffer); assertCount(i + 1, tx.query().has("location", Geo.WITHIN, bufferedBox).vertices()); assertCount(i + 1, tx.query().has("location", Geo.WITHIN, bufferedBox).edges()); assertCount(i + 1, tx.query().has("location", Geo.INTERSECT, bufferedBox).vertices()); assertCount(i + 1, tx.query().has("location", Geo.INTERSECT, bufferedBox).edges()); assertCount(numV-(i + 1), tx.query().has("location", Geo.DISJOINT, bufferedBox).vertices()); assertCount(numV-(i + 1), tx.query().has("location", Geo.DISJOINT, bufferedBox).edges()); if (i > 0) { final Geoshape exactBox = Geoshape.box(min, min, max, max); assertCount(i, tx.query().has("boundary", Geo.WITHIN, exactBox).vertices());
@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)); }
evaluateQuery(graph.query().has("text", Text.CONTAINS, "ducks"), ElementCategory.VERTEX, numV / strings.length * 2, new boolean[]{true, true}, VINDEX); assertCount(numV / strings.length * 2, graph.query().has("text", Text.CONTAINS, "ducks").vertices()); assertCount(numV / strings.length * 2, graph.query().has("text", Text.CONTAINS, "farm").vertices()); assertCount(numV / strings.length, graph.query().has("text", Text.CONTAINS, "beautiful").vertices()); evaluateQuery(graph.query().has("text", Text.CONTAINS_PREFIX, "beauti"), ElementCategory.VERTEX, numV / strings.length, new boolean[]{true, true}, VINDEX); assertCount(numV / strings.length, graph.query().has("text", Text.CONTAINS_REGEX, "be[r]+y").vertices()); assertCount(0, graph.query().has("text", Text.CONTAINS, "lolipop").vertices()); assertCount(numV / strings.length, graph.query().has("name", Cmp.EQUAL, strings[1]).vertices()); assertCount(numV / strings.length, graph.query().has("name", Cmp.EQUAL, strings[1]).vertices()); assertCount(numV / strings.length * (strings.length - 1), graph.query().has("name", Cmp.NOT_EQUAL, strings[2]).vertices()); assertCount(0, graph.query().has("name", Cmp.EQUAL, "farm").vertices()); assertCount(numV / strings.length, graph.query().has("name", Text.PREFIX, "ducks").vertices()); assertCount(numV / strings.length * 2, graph.query().has("name", Text.REGEX, "(.*)ducks(.*)").vertices()); evaluateQuery(graph.query().has("text", Text.CONTAINS, "ducks"), ElementCategory.EDGE, numV / strings.length * 2, new boolean[]{true, true}, EINDEX); assertCount(numV / strings.length * 2, graph.query().has("text", Text.CONTAINS, "ducks").edges()); assertCount(numV / strings.length * 2, graph.query().has("text", Text.CONTAINS, "farm").edges()); assertCount(numV / strings.length, graph.query().has("text", Text.CONTAINS, "beautiful").edges()); evaluateQuery(graph.query().has("text", Text.CONTAINS_PREFIX, "beauti"), ElementCategory.EDGE, numV / strings.length, new boolean[]{true, true}, EINDEX); assertCount(numV / strings.length, graph.query().has("text", Text.CONTAINS_REGEX, "be[r]+y").edges()); assertCount(0, graph.query().has("text", Text.CONTAINS, "lolipop").edges()); assertCount(numV / strings.length, graph.query().has("name", Cmp.EQUAL, strings[1]).edges()); assertCount(numV / strings.length, graph.query().has("name", Cmp.EQUAL, strings[1]).edges()); assertCount(numV / strings.length * (strings.length - 1), graph.query().has("name", Cmp.NOT_EQUAL, strings[2]).edges()); assertCount(0, graph.query().has("name", Cmp.EQUAL, "farm").edges());
v2.property(VertexProperty.Cardinality.single, "time", time2); evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr), ElementCategory.VERTEX, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1"); evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"), ElementCategory.VERTEX, 1, new boolean[]{true, true}, "index2"); final Object v2Id = v2.id(); evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr), ElementCategory.VERTEX, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1"); evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"), ElementCategory.VERTEX, 1, new boolean[]{true, true}, "index2"); evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "event"), ElementCategory.VERTEX, 0, new boolean[]{true, true}, "index2"); evaluateQuery(tx.query().has("name", "first event").orderBy("time", decr), ElementCategory.VERTEX, 0, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1");
@Override public Iterable<AtlasVertex<AtlasJanusVertex, AtlasJanusEdge>> vertices(int limit) { Iterable<JanusGraphVertex> it = query.limit(limit).vertices(); if (LOG.isDebugEnabled()) { if (query instanceof GraphCentricQueryBuilder) { LOG.debug("NativeJanusGraphQuery.vertices({}): resultSize={}, {}", limit, getCountForDebugLog(it), ((GraphCentricQueryBuilder) query).constructQuery(ElementCategory.VERTEX)); } else { LOG.debug("NativeJanusGraphQuery.vertices({}): resultSize={}, {}", limit, getCountForDebugLog(it), query); } } return graph.wrapVertices(it); }
public static JanusGraphVertex getOnlyVertex(JanusGraphQuery<?> query) { return getOnlyElement(query.vertices()); }
evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[]{true, sorted}, edge1.name()); evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(10, 20, 30)).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 3, new boolean[]{true, sorted}, edge1.name()); evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0) .has("text", Cmp.EQUAL, strings[10 % strings.length]), ElementCategory.EDGE, 1, new boolean[]{false, sorted}, edge1.name()); evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 1), ElementCategory.EDGE, 1, new boolean[]{true, sorted}, edge1.name()); evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[]{true, sorted}, edge1.name()); evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 3), ElementCategory.EDGE, 0, new boolean[]{true, sorted}, edge1.name()); evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strings.length, new boolean[]{true, sorted}, edge2.name()); evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[]{true, sorted}, edge2.name()); evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]), ElementCategory.EDGE, numV / strings.length * 2, new boolean[]{false, sorted}); evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[]{false, sorted}); evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50), ElementCategory.PROPERTY, 2, new boolean[]{true, sorted}, prop1.name()); evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strings[0]), ElementCategory.PROPERTY, 2 * numV / (4 * 5), new boolean[]{true, sorted}, prop2.name()); evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strings[0]) .has("time", Cmp.EQUAL, 0), ElementCategory.PROPERTY, 2, new boolean[]{true, sorted}, prop2.name(), prop1.name());
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; }
evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 1, new boolean[]{true, true}, "theIndex"); evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 1, new boolean[]{false, true}); evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 1, new boolean[]{false, true}); evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 1, new boolean[]{false, true}); evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 1, new boolean[]{false, true}, "theIndex"); if (indexFeatures.supportsCardinality(Cardinality.LIST)) { evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 1, new boolean[]{false, true}); evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "5678"), ElementCategory.VERTEX, 1, new boolean[]{false, true}); evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks"), ElementCategory.VERTEX, 3, new boolean[]{true, true}, "theIndex"); evaluateQuery(tx.query().has("time", 5), ElementCategory.VERTEX, 2, new boolean[]{true, true}, "theIndex"); evaluateQuery(tx.query().interval("height", 100, 200), ElementCategory.VERTEX, 3, new boolean[]{false, true}); evaluateQuery(tx.query().interval("height", 100, 200).has("time", 5), ElementCategory.VERTEX, 2, new boolean[]{false, true}, "theIndex"); evaluateQuery(tx.query().has("text", Text.CONTAINS, "rocks").has("time", 5).interval("height", 100, 200), ElementCategory.VERTEX, 2, new boolean[]{false, true}, "theIndex"); if (indexFeatures.supportsCardinality(Cardinality.LIST)) { evaluateQuery(tx.query().has("phone", Cmp.EQUAL, "1234"), ElementCategory.VERTEX, 3, new boolean[]{false, true});
switch (resultType) { case PROPERTY: result = query.properties(); break; case EDGE: result = query.edges(); break; case VERTEX: result = query.vertices(); break; default:
@Override public Iterable<AtlasEdge<AtlasJanusVertex, AtlasJanusEdge>> edges(int limit) { Iterable<JanusGraphEdge> it = query.limit(limit).edges(); if (LOG.isDebugEnabled()) { if (query instanceof GraphCentricQueryBuilder) { LOG.debug("NativeJanusGraphQuery.vertices({}): resultSize={}, {}", limit, getCountForDebugLog(it), ((GraphCentricQueryBuilder) query).constructQuery(ElementCategory.EDGE)); } else { LOG.debug("NativeJanusGraphQuery.vertices({}): resultSize={}, {}", limit, getCountForDebugLog(it), query); } } return graph.wrapEdges(it); }