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;
}
}
Aggregations