Search in sources :

Example 1 with OQueryOperatorFactory

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OQueryOperatorFactory(com.orientechnologies.orient.core.sql.operator.OQueryOperatorFactory) ArrayList(java.util.ArrayList) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) Iterator(java.util.Iterator) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) OQueryOperator(com.orientechnologies.orient.core.sql.operator.OQueryOperator)

Aggregations

OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OQueryOperator (com.orientechnologies.orient.core.sql.operator.OQueryOperator)1 OQueryOperatorFactory (com.orientechnologies.orient.core.sql.operator.OQueryOperatorFactory)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1