Add lazy traversal on MATCH execution
Resolves:
@@ -330,19 +330,49 @@ public class MatchEdgeTraverser {
return Collections.singleton(new OResultInternal((OIdentifiable) qR));
}
if (qR instanceof Iterable) {
- Iterable iterable = (Iterable) qR;
- List<OResultInternal> result = new ArrayList<>();
- for (Object o : iterable) {
+ final Iterator<Object> iter = ((Iterable) qR).iterator();
+ Iterable<OResultInternal> result = () -> new Iterator<OResultInternal>() {
+ private OResultInternal nextElement;
+
+ @Override
+ public boolean hasNext() {
+ if (nextElement == null) {
+ fetchNext();
+ }
+ return nextElement != null;
+ }
+
+ @Override
+ public OResultInternal next() {
+ if (nextElement == null) {
+ fetchNext();
+ }
+ if (nextElement == null) {
+ throw new IllegalStateException();
+ }
+ OResultInternal res = nextElement;
+ nextElement = null;
+ return res;
+ }
+
+ public void fetchNext() {
+ while (iter.hasNext()) {
+ Object o = iter.next();
if (o instanceof OIdentifiable) {
- result.add(new OResultInternal((OIdentifiable) o));
+ nextElement = new OResultInternal((OIdentifiable) o);
+ break;
} else if (o instanceof OResultInternal) {
- result.add((OResultInternal) o);
+ nextElement = (OResultInternal) o;
+ break;
} else if (o == null) {
continue;
} else {
throw new UnsupportedOperationException();
}
}
+ }
+ };
+
return result;
}
return Collections.EMPTY_LIST;
