Search in sources :

Example 1 with OTraverseRecordProcess

use of com.orientechnologies.orient.core.command.traverse.OTraverseRecordProcess in project orientdb by orientechnologies.

the class OSQLFunctionTraversedElement method evaluate.

protected Object evaluate(final Object[] iParams, final OCommandContext iContext, final String iClassName) {
    final int beginIndex = (Integer) iParams[0];
    final int items = iParams.length > 1 ? (Integer) iParams[1] : 1;
    final ArrayDeque stack = (ArrayDeque) iContext.getVariable("stack");
    if (stack == null)
        throw new OCommandExecutionException("Cannot invoke " + getName() + "() against non traverse command");
    final List<OIdentifiable> result = items > 1 ? new ArrayList<OIdentifiable>(items) : null;
    if (beginIndex < 0) {
        int i = -1;
        for (Iterator it = stack.iterator(); it.hasNext(); ) {
            final Object o = it.next();
            if (o instanceof OTraverseRecordProcess) {
                final OIdentifiable record = ((OTraverseRecordProcess) o).getTarget();
                if (iClassName == null || ODocumentInternal.getImmutableSchemaClass((ODocument) record.getRecord()).isSubClassOf(iClassName)) {
                    if (i <= beginIndex) {
                        if (items == 1)
                            return record;
                        else {
                            result.add(record);
                            if (result.size() >= items)
                                break;
                        }
                    }
                    i--;
                }
            }
        }
    } else {
        int i = 0;
        for (Iterator it = stack.descendingIterator(); it.hasNext(); ) {
            final Object o = it.next();
            if (o instanceof OTraverseRecordProcess) {
                final OIdentifiable record = ((OTraverseRecordProcess) o).getTarget();
                if (iClassName == null || ODocumentInternal.getImmutableSchemaClass((ODocument) record.getRecord()).isSubClassOf(iClassName)) {
                    if (i >= beginIndex) {
                        if (items == 1)
                            return record;
                        else {
                            result.add(record);
                            if (result.size() >= items)
                                break;
                        }
                    }
                    i++;
                }
            }
        }
    }
    if (items > 0 && result != null && !result.isEmpty())
        return result;
    return null;
}
Also used : OTraverseRecordProcess(com.orientechnologies.orient.core.command.traverse.OTraverseRecordProcess) Iterator(java.util.Iterator) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ArrayDeque(java.util.ArrayDeque)

Aggregations

OTraverseRecordProcess (com.orientechnologies.orient.core.command.traverse.OTraverseRecordProcess)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ArrayDeque (java.util.ArrayDeque)1 Iterator (java.util.Iterator)1