Exemplo n.º 1
0
  /**
   * Removes the specified element from this set if it is present (optional operation). This method
   * conforms to the {@link Set#remove} interface.
   *
   * @param mapEntry is a {@link java.util.Map.Entry} instance to be removed.
   * @return true if the key-value pair was removed from the set, or false if the mapEntry is not a
   *     {@link java.util.Map.Entry} instance or is not present in the set.
   * @throws UnsupportedOperationException if the collection is read-only.
   * @throws RuntimeExceptionWrapper if a {@link DatabaseException} is thrown.
   */
  public boolean remove(Object mapEntry) {

    if (!(mapEntry instanceof Map.Entry)) {
      return false;
    }
    Map.Entry entry = (Map.Entry) mapEntry;
    DataCursor cursor = null;
    boolean doAutoCommit = beginAutoCommit();
    try {
      cursor = new DataCursor(view, true);
      OperationStatus status = cursor.findBoth(entry.getKey(), entry.getValue(), true);
      if (status == OperationStatus.SUCCESS) {
        cursor.delete();
      }
      closeCursor(cursor);
      commitAutoCommit(doAutoCommit);
      return (status == OperationStatus.SUCCESS);
    } catch (Exception e) {
      closeCursor(cursor);
      throw handleException(e, doAutoCommit);
    }
  }
Exemplo n.º 2
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);
    }
  }