Search in sources :

Example 1 with SimpleQueryProfiler

use of org.janusgraph.graphdb.query.profile.SimpleQueryProfiler in project janusgraph by JanusGraph.

the class JanusGraphTest method evaluateQuery.

public static void evaluateQuery(JanusGraphVertexQuery query, RelationCategory resultType, int expectedResults, int numSubQueries, boolean[] subQuerySpecs, Map<PropertyKey, Order> orderMap) {
    SimpleQueryProfiler profiler = new SimpleQueryProfiler();
    ((BasicVertexCentricQueryBuilder) query).profiler(profiler);
    Iterable<? extends JanusGraphElement> result;
    switch(resultType) {
        case PROPERTY:
            result = query.properties();
            break;
        case EDGE:
            result = query.edges();
            break;
        case RELATION:
            result = query.relations();
            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
    assertEquals(Integer.valueOf(1), profiler.getAnnotation(QueryProfiler.NUMVERTICES_ANNOTATION));
    int subQueryCounter = 0;
    for (SimpleQueryProfiler subProfiler : profiler) {
        assertNotNull(subProfiler);
        if (subProfiler.getGroupName().equals(QueryProfiler.OPTIMIZATION))
            continue;
        if (subQuerySpecs.length == 2) {
            // 0=>fitted, 1=>ordered
            assertEquals(subQuerySpecs[0], subProfiler.getAnnotation(QueryProfiler.FITTED_ANNOTATION));
            assertEquals(subQuerySpecs[1], subProfiler.getAnnotation(QueryProfiler.ORDERED_ANNOTATION));
        }
        // assertEquals(1,Iterables.size(subProfiler)); This only applies if a disk call is necessary
        subQueryCounter++;
    }
    assertEquals(numSubQueries, subQueryCounter);
}
Also used : JanusGraphElement(org.janusgraph.core.JanusGraphElement) BasicVertexCentricQueryBuilder(org.janusgraph.graphdb.query.vertex.BasicVertexCentricQueryBuilder) OrderList(org.janusgraph.graphdb.internal.OrderList) SimpleQueryProfiler(org.janusgraph.graphdb.query.profile.SimpleQueryProfiler) PropertyKey(org.janusgraph.core.PropertyKey)

Example 2 with SimpleQueryProfiler

use of org.janusgraph.graphdb.query.profile.SimpleQueryProfiler 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)
        assertTrue(intersectingIndexes.length == 0);
    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)

Aggregations

JanusGraphElement (org.janusgraph.core.JanusGraphElement)2 PropertyKey (org.janusgraph.core.PropertyKey)2 OrderList (org.janusgraph.graphdb.internal.OrderList)2 SimpleQueryProfiler (org.janusgraph.graphdb.query.profile.SimpleQueryProfiler)2 HashSet (java.util.HashSet)1 GraphCentricQueryBuilder (org.janusgraph.graphdb.query.graph.GraphCentricQueryBuilder)1 BasicVertexCentricQueryBuilder (org.janusgraph.graphdb.query.vertex.BasicVertexCentricQueryBuilder)1