Search in sources :

Example 1 with RowIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.RowIteratorAdapter in project jackrabbit by apache.

the class QueryEngine method execute.

protected QueryResult execute(Column[] columns, Selector selector, Constraint constraint, Ordering[] orderings, long offset, long limit, int printIndentation) throws RepositoryException {
    long time = System.currentTimeMillis();
    Map<String, NodeType> selectorMap = getSelectorNames(selector);
    String[] selectorNames = selectorMap.keySet().toArray(new String[selectorMap.size()]);
    Map<String, PropertyValue> columnMap = getColumnMap(columns, selectorMap);
    String[] columnNames = columnMap.keySet().toArray(new String[columnMap.size()]);
    Sort sort = new Sort();
    if (NATIVE_SORT) {
        sort = new Sort(createSortFields(orderings, session));
    }
    // if true it means that the LuceneQueryFactory should just let the
    // QueryEngine take care of sorting and applying offset and limit
    // constraints
    boolean externalSort = !NATIVE_SORT;
    RowIterator rows = null;
    try {
        rows = new RowIteratorAdapter(lqf.execute(columnMap, selector, constraint, sort, externalSort, offset, limit));
    } catch (IOException e) {
        throw new RepositoryException("Failed to access the query index", e);
    } finally {
        log.debug("{}SQL2 SELECT took {} ms. selector: {}, columns: {}, constraint: {}, offset {}, limit {}", new Object[] { genString(printIndentation), System.currentTimeMillis() - time, selector, Arrays.toString(columnNames), constraint, offset, limit });
    }
    QueryResult result = new SimpleQueryResult(columnNames, selectorNames, rows);
    if (NATIVE_SORT) {
        return result;
    }
    long timeSort = System.currentTimeMillis();
    QueryResult sorted = sort(result, orderings, evaluator, offset, limit);
    log.debug("{}SQL2 SORT took {} ms.", genString(printIndentation), System.currentTimeMillis() - timeSort);
    return sorted;
}
Also used : PropertyValue(javax.jcr.query.qom.PropertyValue) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) QueryResult(javax.jcr.query.QueryResult) RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) NodeType(javax.jcr.nodetype.NodeType) RowIterator(javax.jcr.query.RowIterator) Sort(org.apache.lucene.search.Sort)

Example 2 with RowIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.RowIteratorAdapter in project jackrabbit by apache.

the class QueryEngine method execute.

protected QueryResult execute(JoinMerger merger, ConstraintSplitInfo csInfo, boolean isOuterJoin, int printIndentation) throws RepositoryException {
    Comparator<Row> leftCo = new RowPathComparator(merger.getLeftSelectors());
    long timeJoinLeftSide = System.currentTimeMillis();
    if (csInfo.isMultiple()) {
        log.debug("{} SQL2 JOIN execute: there are multiple inner splits.", genString(printIndentation));
        // first branch
        long bTime = System.currentTimeMillis();
        QueryResult branch1 = execute(merger, csInfo.getLeftInnerConstraints(), isOuterJoin, printIndentation + printIndentStep);
        Set<Row> allRows = new TreeSet<Row>(new RowPathComparator(Arrays.asList(merger.getSelectorNames())));
        RowIterator ri1 = branch1.getRows();
        while (ri1.hasNext()) {
            Row r = ri1.nextRow();
            allRows.add(r);
        }
        log.debug("{} SQL2 JOIN executed first branch, took {} ms.", genString(printIndentation), System.currentTimeMillis() - bTime);
        // second branch
        bTime = System.currentTimeMillis();
        QueryResult branch2 = execute(merger, csInfo.getRightInnerConstraints(), isOuterJoin, printIndentation + printIndentStep);
        RowIterator ri2 = branch2.getRows();
        while (ri2.hasNext()) {
            Row r = ri2.nextRow();
            allRows.add(r);
        }
        log.debug("{} SQL2 JOIN executed second branch, took {} ms.", genString(printIndentation), System.currentTimeMillis() - bTime);
        return new SimpleQueryResult(merger.getColumnNames(), merger.getSelectorNames(), new RowIteratorAdapter(allRows));
    }
    Set<Row> leftRows = buildLeftRowsJoin(csInfo, leftCo, printIndentation + printIndentStep);
    if (log.isDebugEnabled()) {
        timeJoinLeftSide = System.currentTimeMillis() - timeJoinLeftSide;
        log.debug(genString(printIndentation) + "SQL2 JOIN LEFT SIDE took " + timeJoinLeftSide + " ms. fetched " + leftRows.size() + " rows.");
    }
    // The join constraint information is split into:
    // - rightConstraints selects just the 'ON' constraints
    // - csInfo has the 'WHERE' constraints
    //
    // So, in the case of an OUTER JOIN we'll run 2 queries, one with
    // 'ON'
    // and one with 'ON' + 'WHERE' conditions
    // this way, at merge time in case of an outer join we can tell if
    // it's a 'null' row, or a bad row -> one that must not be returned.
    // This way at the end we'll have:
    // - rightRowsSet containing the 'ON' dataset
    // - excludingOuterJoinRowsSet: the 'ON' + 'WHERE' condition
    // dataset, or
    // NULL if there is no 'WHERE' condition
    long timeJoinRightSide = System.currentTimeMillis();
    List<Constraint> rightConstraints = merger.getRightJoinConstraints(leftRows);
    Comparator<Row> rightCo = new RowPathComparator(merger.getRightSelectors());
    if (leftRows == null || leftRows.isEmpty()) {
        return merger.merge(new RowIteratorAdapter((leftRows == null) ? Collections.emptySet() : leftRows), new RowIteratorAdapter(new TreeSet<Row>()), null, rightCo);
    }
    Set<Row> rightRows = buildRightRowsJoin(csInfo, rightConstraints, isOuterJoin, rightCo, printIndentation + printIndentStep);
    // this has to be initialized as null
    Set<Row> excludingOuterJoinRowsSet = null;
    if (isOuterJoin && csInfo.getRightConstraint() != null) {
        excludingOuterJoinRowsSet = buildRightRowsJoin(csInfo, rightConstraints, false, rightCo, printIndentation + printIndentStep);
    }
    if (log.isDebugEnabled()) {
        timeJoinRightSide = System.currentTimeMillis() - timeJoinRightSide;
        log.debug(genString(printIndentation) + "SQL2 JOIN RIGHT SIDE took " + timeJoinRightSide + " ms. fetched " + rightRows.size() + " rows.");
    }
    // merge left with right datasets
    return merger.merge(new RowIteratorAdapter(leftRows), new RowIteratorAdapter(rightRows), excludingOuterJoinRowsSet, rightCo);
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) Constraint(javax.jcr.query.qom.Constraint) TreeSet(java.util.TreeSet) RowIterator(javax.jcr.query.RowIterator) Row(javax.jcr.query.Row)

Example 3 with RowIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.RowIteratorAdapter in project jackrabbit by apache.

the class QueryEngine method sort.

/**
     * Sorts the given query results according to the given QOM orderings. If
     * one or more orderings have been specified, this method will iterate
     * through the entire original result set, order the collected rows, and
     * return a new result set based on the sorted collection of rows.
     * 
     * @param result
     *            original query results
     * @param orderings
     *            QOM orderings
     * @param offset
     *            result offset
     * @param limit
     *            result limit
     * @return sorted query results
     * @throws RepositoryException
     *             if the results can not be sorted
     */
protected static QueryResult sort(QueryResult result, final Ordering[] orderings, OperandEvaluator evaluator, long offset, long limit) throws RepositoryException {
    if ((orderings != null && orderings.length > 0) || offset != 0 || limit >= 0) {
        List<Row> rows = new ArrayList<Row>();
        RowIterator iterator = result.getRows();
        while (iterator.hasNext()) {
            rows.add(iterator.nextRow());
        }
        if (orderings != null && orderings.length > 0) {
            Collections.sort(rows, new RowComparator(orderings, evaluator));
        }
        if (offset > 0) {
            int size = rows.size();
            rows = rows.subList((int) Math.min(offset, size), size);
        }
        if (limit >= 0) {
            int size = rows.size();
            rows = rows.subList(0, (int) Math.min(limit, size));
        }
        return new SimpleQueryResult(result.getColumnNames(), result.getSelectorNames(), new RowIteratorAdapter(rows));
    } else {
        return result;
    }
}
Also used : RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) RowIterator(javax.jcr.query.RowIterator) RowComparator(org.apache.jackrabbit.core.query.lucene.sort.RowComparator) ArrayList(java.util.ArrayList) Row(javax.jcr.query.Row) Constraint(javax.jcr.query.qom.Constraint)

Example 4 with RowIteratorAdapter

use of org.apache.jackrabbit.commons.iterator.RowIteratorAdapter in project jackrabbit-oak by apache.

the class QueryResultImpl method getRows.

@Override
public RowIterator getRows() throws RepositoryException {
    Iterator<RowImpl> rowIterator = new Iterator<RowImpl>() {

        private final Iterator<? extends ResultRow> it = result.getRows().iterator();

        private final String pathSelector;

        private RowImpl current;

        private int rowCount;

        //Avoid log check for every row access
        private final boolean debugEnabled = queryOpsLogger.isDebugEnabled();

        {
            String[] columnSelectorNames = result.getColumnSelectorNames();
            if (columnSelectorNames.length == 1) {
                pathSelector = columnSelectorNames[0];
            } else {
                pathSelector = null;
            }
            fetch();
        }

        private void fetch() {
            if (it.hasNext()) {
                current = new RowImpl(QueryResultImpl.this, it.next(), pathSelector);
                if (debugEnabled) {
                    rowCount++;
                    if (rowCount % 100 == 0) {
                        queryOpsLogger.debug("Iterated over [{}] results so far", rowCount);
                    }
                }
            } else {
                current = null;
            }
        }

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public RowImpl next() {
            if (current == null) {
                throw new NoSuchElementException();
            }
            RowImpl r = current;
            fetch();
            return r;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    final PrefetchIterator<RowImpl> prefIt = new PrefetchIterator<RowImpl>(sessionDelegate.sync(rowIterator), new PrefetchOptions() {

        {
            size = result.getSize();
            fastSize = sessionContext.getFastQueryResultSize();
            fastSizeCallback = result;
        }
    });
    return new RowIteratorAdapter(prefIt) {

        @Override
        public long getSize() {
            return prefIt.size();
        }
    };
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) PrefetchOptions(org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions) Iterator(java.util.Iterator) RowIterator(javax.jcr.query.RowIterator) NodeIterator(javax.jcr.NodeIterator) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

RowIterator (javax.jcr.query.RowIterator)4 RowIteratorAdapter (org.apache.jackrabbit.commons.iterator.RowIteratorAdapter)4 QueryResult (javax.jcr.query.QueryResult)2 Row (javax.jcr.query.Row)2 Constraint (javax.jcr.query.qom.Constraint)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 TreeSet (java.util.TreeSet)1 NodeIterator (javax.jcr.NodeIterator)1 RepositoryException (javax.jcr.RepositoryException)1 NodeType (javax.jcr.nodetype.NodeType)1 PropertyValue (javax.jcr.query.qom.PropertyValue)1 RowComparator (org.apache.jackrabbit.core.query.lucene.sort.RowComparator)1 ResultRow (org.apache.jackrabbit.oak.api.ResultRow)1 PrefetchOptions (org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions)1 Sort (org.apache.lucene.search.Sort)1