Search in sources :

Example 1 with MeasuringIterator

use of org.apache.jackrabbit.oak.query.QueryImpl.MeasuringIterator in project jackrabbit-oak by apache.

the class UnionQueryImpl method getRows.

@Override
public Iterator<ResultRowImpl> getRows() {
    prepare();
    if (explain) {
        String plan = getPlan();
        columns = new ColumnImpl[] { new ColumnImpl("explain", "plan", "plan") };
        ResultRowImpl r = new ResultRowImpl(this, Tree.EMPTY_ARRAY, new PropertyValue[] { PropertyValues.newString(plan) }, null, null);
        return Arrays.asList(r).iterator();
    }
    if (LOG.isDebugEnabled()) {
        if (isInternal) {
            LOG.trace("query union plan {}", getPlan());
        } else {
            LOG.debug("query union plan {}", getPlan());
        }
    }
    boolean distinct = !unionAll;
    Comparator<ResultRowImpl> orderBy = ResultRowImpl.getComparator(orderings);
    Iterator<ResultRowImpl> it;
    final Iterator<ResultRowImpl> leftRows = left.getRows();
    final Iterator<ResultRowImpl> rightRows = right.getRows();
    Iterator<ResultRowImpl> leftIter = leftRows;
    Iterator<ResultRowImpl> rightIter = rightRows;
    // if measure retrieve the backing delegate iterator instead
    if (measure) {
        leftIter = ((MeasuringIterator) leftRows).getDelegate();
        rightIter = ((MeasuringIterator) rightRows).getDelegate();
    }
    // Since sorted by index use a merge iterator
    if (isSortedByIndex()) {
        it = FilterIterators.newCombinedFilter(Iterators.mergeSorted(ImmutableList.of(leftIter, rightIter), orderBy), distinct, limit, offset, null, settings);
    } else {
        it = FilterIterators.newCombinedFilter(Iterators.concat(leftIter, rightIter), distinct, limit, offset, orderBy, settings);
    }
    if (measure) {
        // return the measuring iterator for the union
        it = new MeasuringIterator(this, it) {

            MeasuringIterator left = (MeasuringIterator) leftRows;

            MeasuringIterator right = (MeasuringIterator) rightRows;

            @Override
            protected void setColumns(ColumnImpl[] cols) {
                columns = cols;
                left.setColumns(cols);
                right.setColumns(cols);
            }

            @Override
            protected Map<String, Long> getSelectorScanCount() {
                // Merge the 2 maps from the left and right queries to get the selector counts
                Map<String, Long> leftSelectorScan = left.getSelectorScanCount();
                Map<String, Long> rightSelectorScan = right.getSelectorScanCount();
                Map<String, Long> unionScan = Maps.newHashMap(leftSelectorScan);
                for (String key : rightSelectorScan.keySet()) {
                    if (unionScan.containsKey(key)) {
                        unionScan.put(key, rightSelectorScan.get(key) + unionScan.get(key));
                    } else {
                        unionScan.put(key, rightSelectorScan.get(key));
                    }
                }
                return unionScan;
            }

            @Override
            protected long getReadCount() {
                return left.getReadCount() + right.getReadCount();
            }
        };
    }
    return it;
}
Also used : MeasuringIterator(org.apache.jackrabbit.oak.query.QueryImpl.MeasuringIterator) ColumnImpl(org.apache.jackrabbit.oak.query.ast.ColumnImpl) Map(java.util.Map)

Aggregations

Map (java.util.Map)1 MeasuringIterator (org.apache.jackrabbit.oak.query.QueryImpl.MeasuringIterator)1 ColumnImpl (org.apache.jackrabbit.oak.query.ast.ColumnImpl)1