All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sirix.axis.temporal.AllTimeAxis 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.api.xml.XmlNodeReadOnlyTrx;
import io.sirix.axis.AbstractTemporalAxis;

import static java.util.Objects.requireNonNull;

/**
 * Retrieve a node by node key in all revisions. In each revision a {@link XmlNodeReadOnlyTrx} is
 * opened which is moved to the node with the given node key if it exists. Otherwise the iterator
 * has no more elements (the {@link XmlNodeReadOnlyTrx} moved to the node by it's node key).
 *
 * @author Johannes Lichtenberger
 *
 */
public final class AllTimeAxis
    extends AbstractTemporalAxis {

  /** The revision number. */
  private int revision;

  /** Sirix {@link ResourceSession}. */
  private final ResourceSession resourceSession;

  /** Node key to lookup and retrieve. */
  private final long nodeKey;

  /** Determines if node has been found before and now has been deleted. */
  private boolean hasMoved;

  /**
   * Constructor.
   *
   * @param resourceSession the resource manager
   * @param rtx the read only transactional cursor
   */
  public AllTimeAxis(final ResourceSession resourceSession, final R rtx) {
    this.resourceSession = requireNonNull(resourceSession);
    revision = 1;
    nodeKey = rtx.getNodeKey();
  }

  @Override
  protected R computeNext() {
    while (revision <= resourceSession.getMostRecentRevisionNumber()) {
      final R rtx = resourceSession.beginNodeReadOnlyTrx(revision);
      revision++;
      if (rtx.moveTo(nodeKey)) {
        hasMoved = true;
        return rtx;
      } else if (hasMoved) {
        rtx.close();
        return endOfData();
      }
    }

    return endOfData();
  }

  @Override
  public ResourceSession getResourceManager() {
    return resourceSession;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy