Search in sources :

Example 6 with IndexPlan

use of org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan in project jackrabbit-oak by apache.

the class QueryImpl method getBestSelectorExecutionPlan.

private SelectorExecutionPlan getBestSelectorExecutionPlan(NodeState rootState, FilterImpl filter, QueryIndexProvider indexProvider, boolean traversalEnabled) {
    QueryIndex bestIndex = null;
    if (LOG.isDebugEnabled()) {
        logDebug("cost using filter " + filter);
    }
    double bestCost = Double.POSITIVE_INFINITY;
    IndexPlan bestPlan = null;
    // Sort the indexes according to their minimum cost to be able to skip the remaining indexes if the cost of the
    // current index is below the minimum cost of the next index.
    List<? extends QueryIndex> queryIndexes = MINIMAL_COST_ORDERING.sortedCopy(indexProvider.getQueryIndexes(rootState));
    List<OrderEntry> sortOrder = getSortOrder(filter);
    for (int i = 0; i < queryIndexes.size(); i++) {
        QueryIndex index = queryIndexes.get(i);
        double minCost = index.getMinimumCost();
        if (minCost > bestCost) {
            // Stop looking if the minimum cost is higher than the current best cost
            break;
        }
        double cost;
        String indexName = index.getIndexName();
        IndexPlan indexPlan = null;
        if (index instanceof AdvancedQueryIndex) {
            AdvancedQueryIndex advIndex = (AdvancedQueryIndex) index;
            long maxEntryCount = limit;
            if (offset > 0) {
                if (offset + limit < 0) {
                    // long overflow
                    maxEntryCount = Long.MAX_VALUE;
                } else {
                    maxEntryCount = offset + limit;
                }
            }
            List<IndexPlan> ipList = advIndex.getPlans(filter, sortOrder, rootState);
            cost = Double.POSITIVE_INFINITY;
            for (IndexPlan p : ipList) {
                // TODO limit is after all conditions
                long entryCount = Math.min(maxEntryCount, p.getEstimatedEntryCount());
                double c = p.getCostPerExecution() + entryCount * p.getCostPerEntry();
                if (c < cost) {
                    cost = c;
                    indexPlan = p;
                }
            }
            if (indexPlan != null && indexPlan.getPlanName() != null) {
                indexName += "[" + indexPlan.getPlanName() + "]";
            }
        } else {
            cost = index.getCost(filter, rootState);
        }
        if (LOG.isDebugEnabled()) {
            logDebug("cost for " + indexName + " is " + cost);
        }
        if (cost < 0) {
            LOG.error("cost below 0 for " + indexName + " is " + cost);
        }
        if (cost < bestCost) {
            bestCost = cost;
            bestIndex = index;
            bestPlan = indexPlan;
        }
    }
    potentiallySlowTraversalQuery = bestIndex == null;
    if (traversalEnabled) {
        TraversingIndex traversal = new TraversingIndex();
        double cost = traversal.getCost(filter, rootState);
        if (LOG.isDebugEnabled()) {
            logDebug("cost for " + traversal.getIndexName() + " is " + cost);
        }
        if (cost < bestCost || bestCost == Double.POSITIVE_INFINITY) {
            bestCost = cost;
            bestPlan = null;
            bestIndex = traversal;
            if (potentiallySlowTraversalQuery) {
                potentiallySlowTraversalQuery = traversal.isPotentiallySlow(filter, rootState);
            }
        }
    }
    return new SelectorExecutionPlan(filter.getSelector(), bestIndex, bestPlan, bestCost);
}
Also used : SelectorExecutionPlan(org.apache.jackrabbit.oak.query.plan.SelectorExecutionPlan) AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) OrderEntry(org.apache.jackrabbit.oak.spi.query.QueryIndex.OrderEntry) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) TraversingIndex(org.apache.jackrabbit.oak.query.index.TraversingIndex) QueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex) AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex)

Example 7 with IndexPlan

use of org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan in project jackrabbit-oak by apache.

the class SelectorImpl method getPlan.

@Override
public String getPlan(NodeState rootState) {
    StringBuilder buff = new StringBuilder();
    buff.append(toString());
    buff.append(" /* ");
    QueryIndex index = getIndex();
    if (index != null) {
        if (index instanceof AdvancedQueryIndex) {
            AdvancedQueryIndex adv = (AdvancedQueryIndex) index;
            IndexPlan p = plan.getIndexPlan();
            buff.append(adv.getPlanDescription(p, rootState));
        } else {
            buff.append(index.getPlan(createFilter(true), rootState));
        }
    } else {
        buff.append("no-index");
    }
    if (!selectorConstraints.isEmpty()) {
        buff.append(" where ").append(new AndImpl(selectorConstraints).toString());
    }
    buff.append(" */");
    return buff.toString();
}
Also used : AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) QueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex) AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex)

Example 8 with IndexPlan

use of org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan in project jackrabbit-oak by apache.

the class SelectorImpl method execute.

@Override
public void execute(NodeState rootState) {
    QueryIndex index = plan.getIndex();
    if (index == null) {
        cursor = Cursors.newPathCursor(new ArrayList<String>(), query.getSettings());
        return;
    }
    IndexPlan p = plan.getIndexPlan();
    if (p != null) {
        p.setFilter(createFilter(false));
        AdvancedQueryIndex adv = (AdvancedQueryIndex) index;
        cursor = adv.query(p, rootState);
    } else {
        cursor = index.query(createFilter(false), rootState);
    }
}
Also used : AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) QueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex) AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex)

Example 9 with IndexPlan

use of org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan in project jackrabbit-oak by apache.

the class LuceneIndexTest method assertQuery.

private void assertQuery(IndexTracker tracker, NodeState indexed, String key, String value) {
    AdvancedQueryIndex queryIndex = new LucenePropertyIndex(tracker);
    FilterImpl filter = createFilter(NT_BASE);
    filter.restrictPath("/", Filter.PathRestriction.EXACT);
    filter.restrictProperty(key, Operator.EQUAL, PropertyValues.newString(value));
    List<IndexPlan> plans = queryIndex.getPlans(filter, null, indexed);
    Cursor cursor = queryIndex.query(plans.get(0), indexed);
    assertTrue(cursor.hasNext());
    assertEquals("/", cursor.next().getPath());
    assertFalse(cursor.hasNext());
}
Also used : AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) Cursor(org.apache.jackrabbit.oak.spi.query.Cursor)

Example 10 with IndexPlan

use of org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan in project jackrabbit-oak by apache.

the class LuceneIndexTest method testLucene3.

@Test
public void testLucene3() throws Exception {
    NodeBuilder index = newLucenePropertyIndexDefinition(builder.child(INDEX_DEFINITIONS_NAME), "lucene", ImmutableSet.of("foo"), null);
    NodeBuilder rules = index.child(INDEX_RULES);
    NodeBuilder fooProp = rules.child("nt:base").child(LuceneIndexConstants.PROP_NODE).child("foo");
    fooProp.setProperty(LuceneIndexConstants.PROP_PROPERTY_INDEX, true);
    fooProp.setProperty(LuceneIndexConstants.PROP_INCLUDED_TYPE, PropertyType.TYPENAME_STRING);
    NodeState before = builder.getNodeState();
    builder.setProperty("foo", "bar");
    builder.child("a").setProperty("foo", "bar");
    builder.child("a").child("b").setProperty("foo", "bar", Type.NAME);
    builder.child("a").child("b").child("c").setProperty("foo", "bar", Type.NAME);
    NodeState after = builder.getNodeState();
    NodeState indexed = HOOK.processCommit(before, after, CommitInfo.EMPTY);
    tracker = new IndexTracker();
    tracker.update(indexed);
    AdvancedQueryIndex queryIndex = new LucenePropertyIndex(tracker);
    FilterImpl filter = createFilter(NT_BASE);
    // filter.restrictPath("/", Filter.PathRestriction.EXACT);
    filter.restrictProperty("foo", Operator.EQUAL, PropertyValues.newString("bar"));
    List<IndexPlan> plans = queryIndex.getPlans(filter, null, indexed);
    Cursor cursor = queryIndex.query(plans.get(0), indexed);
    assertTrue(cursor.hasNext());
    assertEquals("/a", cursor.next().getPath());
    assertEquals("/", cursor.next().getPath());
    assertFalse(cursor.hasNext());
}
Also used : AdvancedQueryIndex(org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) FilterImpl(org.apache.jackrabbit.oak.query.index.FilterImpl) IndexPlan(org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Cursor(org.apache.jackrabbit.oak.spi.query.Cursor) Test(org.junit.Test)

Aggregations

IndexPlan (org.apache.jackrabbit.oak.spi.query.QueryIndex.IndexPlan)20 AdvancedQueryIndex (org.apache.jackrabbit.oak.spi.query.QueryIndex.AdvancedQueryIndex)12 Test (org.junit.Test)10 Cursor (org.apache.jackrabbit.oak.spi.query.Cursor)9 FilterImpl (org.apache.jackrabbit.oak.query.index.FilterImpl)8 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)7 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)7 QueryIndex (org.apache.jackrabbit.oak.spi.query.QueryIndex)5 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 ArrayList (java.util.ArrayList)2 OrderEntry (org.apache.jackrabbit.oak.spi.query.QueryIndex.OrderEntry)2 Function (com.google.common.base.Function)1 Blob (org.apache.jackrabbit.oak.api.Blob)1 LuceneIndexHelper.newLuceneIndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLuceneIndexDefinition)1 LuceneIndexHelper.newLucenePropertyIndexDefinition (org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.newLucenePropertyIndexDefinition)1 ArrayBasedBlob (org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob)1 QueryEngineSettings (org.apache.jackrabbit.oak.query.QueryEngineSettings)1 DynamicOperandImpl (org.apache.jackrabbit.oak.query.ast.DynamicOperandImpl)1 OrderingImpl (org.apache.jackrabbit.oak.query.ast.OrderingImpl)1 PropertyValueImpl (org.apache.jackrabbit.oak.query.ast.PropertyValueImpl)1