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