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

io.sirix.axis.temporal.PastAxis 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 io.sirix.axis.IncludeSelf;

import static java.util.Objects.requireNonNull;

/**
 * Retrieve a node by node key in all earlier 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 PastAxis
    extends AbstractTemporalAxis {

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

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

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

  /**
   * Constructor.
   *
   * @param rtx Sirix {@link XmlNodeReadOnlyTrx}
   */
  public PastAxis(final ResourceSession resourceSession, final R rtx) {
    // Using telescope pattern instead of builder (only one optional parameter).
    this(resourceSession, rtx, IncludeSelf.NO);
  }

  /**
   * Constructor.
   *
   * @param resourceSession the resource manager
   * @param rtx the transactional read only cursor
   * @param includeSelf determines if current revision must be included or not
   */
  public PastAxis(final ResourceSession resourceSession, final R rtx, final IncludeSelf includeSelf) {
    this.resourceSession = requireNonNull(resourceSession);
    revision = 0;
    nodeKey = rtx.getNodeKey();
    revision = requireNonNull(includeSelf) == IncludeSelf.YES
        ? rtx.getRevisionNumber()
        : rtx.getRevisionNumber() - 1;
  }

  @Override
  protected R computeNext() {
    if (revision > 0) {
      final R rtx = resourceSession.beginNodeReadOnlyTrx(revision);
      revision--;

      if (rtx.moveTo(nodeKey))
        return rtx;
      else {
        rtx.close();
        return endOfData();
      }
    } else {
      return endOfData();
    }
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy