Exemplo n.º 1
0
  public void remove() {

    if (dataObject == null) {
      throw new IllegalStateException();
    }
    DataCursor cursor = null;
    boolean doAutoCommit = coll.beginAutoCommit();
    try {
      cursor = new DataCursor(coll.view, writeAllowed);
      if (moveCursor(dataIndex, cursor)) {
        cursor.delete();
        deleteSlot(dataIndex);
        dataObject = null;

        /*
         * Repopulate the block after removing all records, using the
         * cursor position of the deleted record as a starting point.
         * First try moving forward, since the user was moving forward.
         * (It is possible to delete all records in the block only by
         * moving forward, i.e, when nextIndex is greater than
         * dataIndex.)
         */
        if (nextIndex == 0 && keys[0] == null) {
          OperationStatus status;
          for (int i = 0; i < keys.length; i += 1) {
            status = coll.iterateDuplicates() ? cursor.getNext(false) : cursor.getNextNoDup(false);
            if (status == OperationStatus.SUCCESS) {
              setSlot(i, cursor);
            } else {
              break;
            }
          }

          /*
           * If no records are found past the cursor position, try
           * moving backward.  If no records are found before the
           * cursor position, leave nextIndex set to keys.length,
           * which is the same as the initial iterator state and is
           * appropriate for an empty key range.
           */
          if (keys[0] == null) {
            nextIndex = keys.length;
            for (int i = nextIndex - 1; i >= 0; i -= 1) {
              status =
                  coll.iterateDuplicates() ? cursor.getPrev(false) : cursor.getPrevNoDup(false);
              if (status == OperationStatus.SUCCESS) {
                setSlot(i, cursor);
              } else {
                break;
              }
            }
          }
        }
        coll.closeCursor(cursor);
        coll.commitAutoCommit(doAutoCommit);
      } else {
        throw new IllegalStateException();
      }
    } catch (Exception e) {
      coll.closeCursor(cursor);
      throw coll.handleException(e, doAutoCommit);
    }
  }
Exemplo 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);
    }
  }