Search in sources :

Example 1 with RowComparator

use of org.apache.jackrabbit.core.query.lucene.sort.RowComparator 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)

Aggregations

ArrayList (java.util.ArrayList)1 Row (javax.jcr.query.Row)1 RowIterator (javax.jcr.query.RowIterator)1 Constraint (javax.jcr.query.qom.Constraint)1 RowIteratorAdapter (org.apache.jackrabbit.commons.iterator.RowIteratorAdapter)1 RowComparator (org.apache.jackrabbit.core.query.lucene.sort.RowComparator)1