Search in sources :

Example 1 with GraphCentricQueryBuilder

use of org.janusgraph.graphdb.query.graph.GraphCentricQueryBuilder in project janusgraph by JanusGraph.

the class JanusGraphTest method evaluateQuery.

public static void evaluateQuery(JanusGraphQuery query, ElementCategory resultType, int expectedResults, boolean[] subQuerySpecs, Map<PropertyKey, Order> orderMap, String... intersectingIndexes) {
    if (intersectingIndexes == null)
        intersectingIndexes = new String[0];
    SimpleQueryProfiler profiler = new SimpleQueryProfiler();
    ((GraphCentricQueryBuilder) query).profiler(profiler);
    Iterable<? extends JanusGraphElement> result;
    switch(resultType) {
        case PROPERTY:
            result = query.properties();
            break;
        case EDGE:
            result = query.edges();
            break;
        case VERTEX:
            result = query.vertices();
            break;
        default:
            throw new AssertionError();
    }
    OrderList orders = profiler.getAnnotation(QueryProfiler.ORDERS_ANNOTATION);
    // Check elements and that they are returned in the correct order
    int no = 0;
    JanusGraphElement previous = null;
    for (JanusGraphElement e : result) {
        assertNotNull(e);
        no++;
        if (previous != null && !orders.isEmpty()) {
            assertTrue(orders.compare(previous, e) <= 0);
        }
        previous = e;
    }
    assertEquals(expectedResults, no);
    // Check OrderList of query
    assertNotNull(orders);
    assertEquals(orderMap.size(), orders.size());
    for (int i = 0; i < orders.size(); i++) {
        assertEquals(orderMap.get(orders.getKey(i)), orders.getOrder(i));
    }
    for (PropertyKey key : orderMap.keySet()) assertTrue(orders.containsKey(key));
    // Check subqueries
    SimpleQueryProfiler simpleQueryProfiler = Iterables.getOnlyElement(StreamSupport.stream(profiler.spliterator(), false).filter(p -> !p.getGroupName().equals(QueryProfiler.OPTIMIZATION)).collect(Collectors.toList()));
    if (subQuerySpecs.length == 2) {
        // 0=>fitted, 1=>ordered
        assertEquals(subQuerySpecs[0], simpleQueryProfiler.getAnnotation(QueryProfiler.FITTED_ANNOTATION));
        assertEquals(subQuerySpecs[1], simpleQueryProfiler.getAnnotation(QueryProfiler.ORDERED_ANNOTATION));
    }
    Set<String> indexNames = new HashSet<>();
    int indexQueries = 0;
    boolean fullScan = false;
    for (SimpleQueryProfiler indexProfiler : simpleQueryProfiler) {
        if (indexProfiler.getAnnotation(QueryProfiler.FULLSCAN_ANNOTATION) != null) {
            fullScan = true;
        } else {
            indexNames.add(indexProfiler.getAnnotation(QueryProfiler.INDEX_ANNOTATION));
            indexQueries++;
        }
    }
    if (indexQueries > 0)
        assertFalse(fullScan);
    if (fullScan)
        assertEquals(0, intersectingIndexes.length);
    assertEquals(intersectingIndexes.length, indexQueries);
    assertEquals(Sets.newHashSet(intersectingIndexes), indexNames);
}
Also used : GraphCentricQueryBuilder(org.janusgraph.graphdb.query.graph.GraphCentricQueryBuilder) JanusGraphElement(org.janusgraph.core.JanusGraphElement) OrderList(org.janusgraph.graphdb.internal.OrderList) SimpleQueryProfiler(org.janusgraph.graphdb.query.profile.SimpleQueryProfiler) PropertyKey(org.janusgraph.core.PropertyKey) HashSet(java.util.HashSet)

Example 2 with GraphCentricQueryBuilder

use of org.janusgraph.graphdb.query.graph.GraphCentricQueryBuilder in project janusgraph by JanusGraph.

the class JanusGraphStep method buildGraphCentricQuery.

private GraphCentricQuery buildGraphCentricQuery(JanusGraphQuery query, final QueryProfiler queryProfiler) {
    Preconditions.checkArgument(query instanceof GraphCentricQueryBuilder);
    final QueryProfiler optProfiler = queryProfiler.addNested(QueryProfiler.CONSTRUCT_GRAPH_CENTRIC_QUERY);
    optProfiler.startTimer();
    final GraphCentricQueryBuilder centricQueryBuilder = ((GraphCentricQueryBuilder) query);
    if (traversal.getEndStep() instanceof CountGlobalStep) {
        centricQueryBuilder.disableSmartLimit();
    }
    final GraphCentricQuery graphCentricQuery = centricQueryBuilder.constructQueryWithoutProfile(Vertex.class.isAssignableFrom(this.returnClass) ? ElementCategory.VERTEX : ElementCategory.EDGE);
    optProfiler.stopTimer();
    return graphCentricQuery;
}
Also used : GraphCentricQueryBuilder(org.janusgraph.graphdb.query.graph.GraphCentricQueryBuilder) GraphCentricQuery(org.janusgraph.graphdb.query.graph.GraphCentricQuery) QueryProfiler(org.janusgraph.graphdb.query.profile.QueryProfiler) CountGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.CountGlobalStep)

Example 3 with GraphCentricQueryBuilder

use of org.janusgraph.graphdb.query.graph.GraphCentricQueryBuilder in project janusgraph by JanusGraph.

the class IndexHelper method getQuery.

public static GraphCentricQueryBuilder getQuery(CompositeIndexType index, Object[] values, StandardJanusGraphTx tx) {
    Preconditions.checkArgument(index != null && values != null && values.length > 0 && tx != null);
    Preconditions.checkArgument(values.length == index.getFieldKeys().length);
    GraphCentricQueryBuilder gb = tx.query();
    IndexField[] fields = index.getFieldKeys();
    for (int i = 0; i < fields.length; i++) {
        IndexField f = fields[i];
        Object value = values[i];
        Preconditions.checkNotNull(value);
        PropertyKey key = f.getFieldKey();
        Preconditions.checkArgument(key.dataType().equals(value.getClass()), "Incompatible data types for: %s", value);
        gb.has(key, Cmp.EQUAL, value);
    }
    if (index.hasSchemaTypeConstraint()) {
        gb.has(ImplicitKey.LABEL, Cmp.EQUAL, index.getSchemaTypeConstraint().name());
    }
    return gb;
}
Also used : GraphCentricQueryBuilder(org.janusgraph.graphdb.query.graph.GraphCentricQueryBuilder) IndexField(org.janusgraph.graphdb.types.IndexField) PropertyKey(org.janusgraph.core.PropertyKey)

Aggregations

GraphCentricQueryBuilder (org.janusgraph.graphdb.query.graph.GraphCentricQueryBuilder)3 PropertyKey (org.janusgraph.core.PropertyKey)2 HashSet (java.util.HashSet)1 CountGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.CountGlobalStep)1 JanusGraphElement (org.janusgraph.core.JanusGraphElement)1 OrderList (org.janusgraph.graphdb.internal.OrderList)1 GraphCentricQuery (org.janusgraph.graphdb.query.graph.GraphCentricQuery)1 QueryProfiler (org.janusgraph.graphdb.query.profile.QueryProfiler)1 SimpleQueryProfiler (org.janusgraph.graphdb.query.profile.SimpleQueryProfiler)1 IndexField (org.janusgraph.graphdb.types.IndexField)1