use of javax.jcr.query.qom.Ordering in project jackrabbit by apache.
the class QueryEngine method createSortFields.
public SortField[] createSortFields(Ordering[] orderings, Session session) throws RepositoryException {
if (orderings == null || orderings.length == 0) {
return new SortField[] { SortField.FIELD_SCORE };
}
// orderings[] -> (property, ordering)
Map<String, Ordering> orderByProperties = new HashMap<String, Ordering>();
for (Ordering o : orderings) {
final String p = o.toString();
if (!orderByProperties.containsKey(p)) {
orderByProperties.put(p, o);
}
}
final DynamicOperandFieldComparatorSource dofcs = new DynamicOperandFieldComparatorSource(session, evaluator, orderByProperties);
List<SortField> sortFields = new ArrayList<SortField>();
// insertion order
for (Ordering o : orderings) {
final String p = o.toString();
// order on jcr:score does not use the natural order as
// implemented in lucene. score ascending in lucene means that
// higher scores are first. JCR specs that lower score values
// are first.
boolean isAsc = QueryObjectModelConstants.JCR_ORDER_ASCENDING.equals(o.getOrder());
if (JcrConstants.JCR_SCORE.equals(p)) {
sortFields.add(new SortField(null, SortField.SCORE, !isAsc));
} else {
// TODO use native sort if available
sortFields.add(new SortField(p, dofcs, !isAsc));
}
}
return sortFields.toArray(new SortField[sortFields.size()]);
}
use of javax.jcr.query.qom.Ordering in project jackrabbit by apache.
the class RowComparator method compare.
public int compare(Row a, Row b) {
try {
for (Ordering ordering : orderings) {
Operand operand = ordering.getOperand();
Value[] va = evaluator.getValues(operand, a);
Value[] vb = evaluator.getValues(operand, b);
int d = comparator.compare(va, vb);
if (d != 0) {
if (JCR_ORDER_DESCENDING.equals(ordering.getOrder())) {
return -d;
} else {
return d;
}
}
}
return 0;
} catch (RepositoryException e) {
throw new RuntimeException("Unable to compare rows " + a + " and " + b, e);
}
}
use of javax.jcr.query.qom.Ordering in project jackrabbit by apache.
the class Parser method createQueryObjectModel.
/**
* Parse a JCR-SQL2 query and return the query object model
*
* @param query the query string
* @return the query object model
* @throws RepositoryException if parsing failed
*/
public QueryObjectModel createQueryObjectModel(String query) throws RepositoryException {
initialize(query);
selectors = new ArrayList<Selector>();
expected = new ArrayList<String>();
bindVariables = new HashMap<String, BindVariableValue>();
read();
read("SELECT");
int columnParseIndex = parseIndex;
ArrayList<ColumnOrWildcard> list = parseColumns();
read("FROM");
Source source = parseSource();
Column[] columnArray = resolveColumns(columnParseIndex, list);
Constraint constraint = null;
if (readIf("WHERE")) {
constraint = parseConstraint();
}
Ordering[] orderings = null;
if (readIf("ORDER")) {
read("BY");
orderings = parseOrder();
}
if (currentToken.length() > 0) {
throw getSyntaxError("<end>");
}
return factory.createQuery(source, constraint, orderings, columnArray);
}
use of javax.jcr.query.qom.Ordering in project jackrabbit by apache.
the class Parser method parseOrder.
private Ordering[] parseOrder() throws RepositoryException {
ArrayList<Ordering> orderList = new ArrayList<Ordering>();
do {
Ordering ordering;
DynamicOperand op = parseDynamicOperand();
if (readIf("DESC")) {
ordering = factory.descending(op);
} else {
readIf("ASC");
ordering = factory.ascending(op);
}
orderList.add(ordering);
} while (readIf(","));
Ordering[] orderings = new Ordering[orderList.size()];
orderList.toArray(orderings);
return orderings;
}
use of javax.jcr.query.qom.Ordering in project jackrabbit by apache.
the class QOMFormatter method append.
private void append(Ordering[] orderings) {
String comma = "";
for (Ordering ordering : orderings) {
append(comma);
comma = ", ";
append(ordering.getOperand());
if (JCR_ORDER_DESCENDING.equals(ordering.getOrder())) {
append(" DESC");
}
}
}
Aggregations