Ejemplo n.º 1
0
  public boolean hasPrevious() {

    if (isPrevAvailable()) {
      return true;
    }
    if (!isNextAvailable()) {
      return false;
    }
    DataCursor cursor = null;
    try {
      cursor = new DataCursor(coll.view, writeAllowed);
      int last = keys.length - 1;
      int next = nextIndex;
      boolean found = false;

      /* Reposition to the last known key/data pair. */
      int repos = cursor.repositionRange(keys[next], priKeys[next], values[next], false);

      if (repos == DataCursor.REPOS_EXACT || repos == DataCursor.REPOS_NEXT) {

        /*
         * The last known key/data pair, or the record following it,
         * was found and will now be in the last slot.
         */
        found = true;
        nextIndex = last;

        /* The data object is now in the last slot or not available. */
        if (dataIndex == next && repos == DataCursor.REPOS_EXACT) {
          dataIndex = last;
        } else {
          dataIndex = -1;
          dataObject = null;
        }
      } else {
        if (repos != DataCursor.REPOS_EOF) {
          throw new IllegalStateException();
        }
      }

      if (found) {
        /* Clear all slots first in case an exception occurs below. */
        clearSlots();

        /* Attempt to fill all slots with records. */
        int i = last;
        boolean done = false;
        while (!done) {
          setSlot(i, cursor);
          i -= 1;
          if (i >= 0) {
            OperationStatus status =
                coll.iterateDuplicates() ? cursor.getPrev(false) : cursor.getPrevNoDup(false);
            if (status != OperationStatus.SUCCESS) {
              done = true;
            }
          } else {
            done = true;
          }
        }
      }

      /*
       * Make sure we retrieved the preceding record after the reposition
       * above.
       */
      return isPrevAvailable();
    } catch (DatabaseException e) {
      throw StoredContainer.convertException(e);
    } finally {
      closeCursor(cursor);
    }
  }
Ejemplo n.º 2
0
  public boolean hasNext() {

    if (isNextAvailable()) {
      return true;
    }
    DataCursor cursor = null;
    try {
      cursor = new DataCursor(coll.view, writeAllowed);
      int prev = nextIndex - 1;
      boolean found = false;

      if (keys[prev] == null) {
        /* Get the first record for an uninitialized iterator. */
        OperationStatus status = cursor.getFirst(false);
        if (status == OperationStatus.SUCCESS) {
          found = true;
          nextIndex = 0;
        }
      } else {
        /* Reposition to the last known key/data pair. */
        int repos = cursor.repositionRange(keys[prev], priKeys[prev], values[prev], false);

        if (repos == DataCursor.REPOS_EXACT) {

          /*
           * The last known key/data pair was found and will now be
           * in slot zero.
           */
          found = true;
          nextIndex = 1;

          /* The data object is now in slot zero or not available. */
          if (dataIndex == prev) {
            dataIndex = 0;
          } else {
            dataIndex = -1;
            dataObject = null;
          }
        } else if (repos == DataCursor.REPOS_NEXT) {

          /*
           * The last known key/data pair was not found, but the
           * following record was found and it will be in slot zero.
           */
          found = true;
          nextIndex = 0;

          /* The data object is no longer available. */
          dataIndex = -1;
          dataObject = null;
        } else {
          if (repos != DataCursor.REPOS_EOF) {
            throw new IllegalStateException();
          }
        }
      }

      if (found) {
        /* Clear all slots first in case an exception occurs below. */
        clearSlots();

        /* Attempt to fill all slots with records. */
        int i = 0;
        boolean done = false;
        while (!done) {
          setSlot(i, cursor);
          i += 1;
          if (i < keys.length) {
            OperationStatus status =
                coll.iterateDuplicates() ? cursor.getNext(false) : cursor.getNextNoDup(false);
            if (status != OperationStatus.SUCCESS) {
              done = true;
            }
          } else {
            done = true;
          }
        }
      }

      /*
       * If REPOS_EXACT was returned above, make sure we retrieved
       * the following record.
       */
      return isNextAvailable();
    } catch (DatabaseException e) {
      throw StoredContainer.convertException(e);
    } finally {
      closeCursor(cursor);
    }
  }