io.sirix.axis.temporal.FirstAxis Maven / Gradle / Ivy
package io.sirix.axis.temporal;
import io.sirix.api.NodeCursor;
import io.sirix.api.NodeReadOnlyTrx;
import io.sirix.api.NodeTrx;
import io.sirix.api.ResourceSession;
import io.sirix.axis.AbstractTemporalAxis;
import static java.util.Objects.requireNonNull;
/**
* Open the first revision and try to move to the node with the given node key.
*
* @author Johannes Lichtenberger
*
*/
public final class FirstAxis
extends AbstractTemporalAxis {
/** Sirix {@link ResourceSession}. */
private final ResourceSession resourceSession;
/** Node key to lookup and retrieve. */
private final long nodeKey;
/** Determines if it's the first call. */
private boolean first;
/**
* Constructor.
*
* @param resourceSession the resource manager
* @param rtx the transactional cursor
*/
public FirstAxis(final ResourceSession resourceSession, final R rtx) {
this.resourceSession = requireNonNull(resourceSession);
nodeKey = rtx.getNodeKey();
first = true;
}
@Override
protected R computeNext() {
if (first) {
first = false;
final R rtx = resourceSession.beginNodeReadOnlyTrx(1);
if (rtx.moveTo(nodeKey)) {
return rtx;
} else {
rtx.close();
return endOfData();
}
} else {
return endOfData();
}
}
@Override
public ResourceSession getResourceManager() {
return resourceSession;
}
}