use of com.orientechnologies.orient.core.sql.operator.OQueryOperatorFactory in project orientdb by orientechnologies.
the class OSQLEngine method getRecordOperators.
public synchronized OQueryOperator[] getRecordOperators() {
if (SORTED_OPERATORS != null) {
return SORTED_OPERATORS;
}
// sort operators, will happen only very few times since we cache the
// result
final Iterator<OQueryOperatorFactory> ite = getOperatorFactories();
final List<OQueryOperator> operators = new ArrayList<OQueryOperator>();
while (ite.hasNext()) {
final OQueryOperatorFactory factory = ite.next();
operators.addAll(factory.getOperators());
}
final List<OQueryOperator> sorted = new ArrayList<OQueryOperator>();
final Set<Pair> pairs = new LinkedHashSet<Pair>();
for (final OQueryOperator ca : operators) {
for (final OQueryOperator cb : operators) {
if (ca != cb) {
switch(ca.compare(cb)) {
case BEFORE:
pairs.add(new Pair(ca, cb));
break;
case AFTER:
pairs.add(new Pair(cb, ca));
break;
}
switch(cb.compare(ca)) {
case BEFORE:
pairs.add(new Pair(cb, ca));
break;
case AFTER:
pairs.add(new Pair(ca, cb));
break;
}
}
}
}
boolean added;
do {
added = false;
scan: for (final Iterator<OQueryOperator> it = operators.iterator(); it.hasNext(); ) {
final OQueryOperator candidate = it.next();
for (final Pair pair : pairs) {
if (pair.after == candidate) {
continue scan;
}
}
sorted.add(candidate);
it.remove();
for (final Iterator<Pair> itp = pairs.iterator(); itp.hasNext(); ) {
if (itp.next().before == candidate) {
itp.remove();
}
}
added = true;
}
} while (added);
if (!operators.isEmpty()) {
throw new ODatabaseException("Invalid sorting. " + OCollections.toString(pairs));
}
SORTED_OPERATORS = sorted.toArray(new OQueryOperator[sorted.size()]);
return SORTED_OPERATORS;
}
Aggregations