package com.datastax.dse.driver.internal.core.graph;
import com.datastax.dse.driver.api.core.graph.GraphNode;
import com.datastax.oss.driver.internal.core.util.CountingIterator;
import com.datastax.oss.driver.shaded.guava.common.base.Preconditions;
import java.util.Queue;
import net.jcip.annotations.NotThreadSafe;
import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
@NotThreadSafe
class GraphResultIterator extends CountingIterator<GraphNode> {
private final Queue<GraphNode> data;
private final GraphProtocol graphProtocol;
private long repeat = 0;
private GraphNode lastGraphNode = null;
GraphResultIterator(Queue<GraphNode> data, GraphProtocol graphProtocol) {
super(data.size());
this.data = data;
this.graphProtocol = graphProtocol;
}
@Override
protected GraphNode computeNext() {
if (repeat > 1) {
repeat -= 1;
return lastGraphNode;
}
GraphNode container = data.poll();
if (container == null) {
return endOfData();
}
if (graphProtocol.isGraphBinary()) {
Preconditions.checkState(
container.as(Object.class) instanceof Traverser,
"Graph protocol error. Received object should be a Traverser but it is not.");
Traverser t = container.as(Traverser.class);
this.repeat = t.bulk();
this.lastGraphNode = new ObjectGraphNode(t.get());
return lastGraphNode;
} else {
GraphNode b = container.getByKey("bulk");
if (b != null) {
this.repeat = b.asLong();
}
lastGraphNode = container.getByKey("result");
return lastGraphNode;
}
}
}