⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blockiterator.java

📁 嵌入式数据库Berkeley DB-4.5.20源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                            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);        }    }    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);        }    }    public Object next() {        if (hasNext()) {            dataIndex = nextIndex;            nextIndex += 1;            makeDataObject();            return dataObject;        } else {            throw new NoSuchElementException();        }    }    public Object previous() {        if (hasPrevious()) {            nextIndex -= 1;            dataIndex = nextIndex;            makeDataObject();            return dataObject;        } else {            throw new NoSuchElementException();        }    }    public int nextIndex() {        if (!coll.view.recNumAccess) {            throw new UnsupportedOperationException(                "Record number access not supported");        }        return hasNext() ? (getRecordNumber(nextIndex) -                            coll.getIndexOffset())                         : Integer.MAX_VALUE;    }    public int previousIndex() {        if (!coll.view.recNumAccess) {            throw new UnsupportedOperationException(                "Record number access not supported");        }        return hasPrevious() ? (getRecordNumber(nextIndex - 1) -                                coll.getIndexOffset())                             : (-1);    }    public void set(Object value) {        if (dataObject == null) {            throw new IllegalStateException();        }        if (!coll.hasValues()) {            throw new UnsupportedOperationException();        }        DataCursor cursor = null;        try {            cursor = new DataCursor(coll.view, writeAllowed);            if (moveCursor(dataIndex, cursor)) {                cursor.putCurrent(value);                setSlot(dataIndex, cursor);            } else {                throw new IllegalStateException();            }        } catch (DatabaseException e) {            throw StoredContainer.convertException(e);        } finally {            closeCursor(cursor);        }    }    public void remove() {        if (dataObject == null) {            throw new IllegalStateException();        }        DataCursor cursor = null;        try {            cursor = new DataCursor(coll.view, writeAllowed);            if (moveCursor(dataIndex, cursor)) {                cursor.delete();                deleteSlot(dataIndex);                dataObject = null;            } else {                throw new IllegalStateException();            }        } catch (DatabaseException e) {            throw StoredContainer.convertException(e);        } finally {            closeCursor(cursor);        }    }    public void add(Object value) {        /*         * The checkIterAddAllowed method ensures that one of the following two         * conditions holds and throws UnsupportedOperationException otherwise:         * 1- This is a list iterator for a recno-renumber database.         * 2- This is a collection iterator for a duplicates database.         */        coll.checkIterAddAllowed();        OperationStatus status = OperationStatus.SUCCESS;        DataCursor cursor = null;        try {            if (coll.view.keysRenumbered || !coll.areDuplicatesOrdered()) {                /*                 * This is a recno-renumber database or a btree/hash database                 * with unordered duplicates.                 */                boolean hasPrev = hasPrevious();                if (!hasPrev && !hasNext()) {                    /* The collection is empty. */                    if (coll.view.keysRenumbered) {                        /* Append to an empty recno-renumber database. */                        status = coll.view.append(value, null, null);                    } else if (coll.view.dupsAllowed &&                               coll.view.range.isSingleKey()) {                        /*                         * When inserting a duplicate into a single-key range,                         * the main key is fixed, so we can always insert into                         * an empty collection.                         */                        cursor = new DataCursor(coll.view, writeAllowed);                        cursor.useRangeKey();                        status = cursor.putNoDupData(null, value, null, true);                        cursor.close();                        cursor = null;                    } else {                        throw new IllegalStateException                            ("Collection is empty, cannot add() duplicate");                    }                    /*                     * Move past the record just inserted (the cursor should be                     * closed above to prevent multiple open cursors in certain                     * DB core modes).                     */                    if (status == OperationStatus.SUCCESS) {                        next();                        dataIndex = nextIndex - 1;                    }                } else {                    /*                     * The collection is non-empty.  If hasPrev is true then                     * the element at (nextIndex - 1) is available; otherwise                     * the element at nextIndex is available.                     */                    cursor = new DataCursor(coll.view, writeAllowed);                    int insertIndex = hasPrev ? (nextIndex - 1) : nextIndex;                    if (!moveCursor(insertIndex, cursor)) {                        throw new IllegalStateException();                    }                    /*                     * For a recno-renumber database or a database with                     * unsorted duplicates, insert before the iterator 'next'                     * position, or after the 'prev' position.  Then adjust                     * the slots to account for the inserted record.                     */                    status = hasPrev ? cursor.putAfter(value)                                     : cursor.putBefore(value);                    if (status == OperationStatus.SUCCESS) {                        insertSlot(nextIndex, cursor);                    }                }            } else {                /* This is a btree/hash database with ordered duplicates. */                cursor = new DataCursor(coll.view, writeAllowed);                if (coll.view.range.isSingleKey()) {                    /*                     * When inserting a duplicate into a single-key range,                     * the main key is fixed.                     */                    cursor.useRangeKey();                } else {                    /*                     * When inserting into a multi-key range, the main key                     * is the last dataIndex accessed by next(), previous()                     * or add().                     */                    if (dataIndex < 0 || !moveCursor(dataIndex, cursor)) {                        throw new IllegalStateException();                    }                }                /*                 * For a hash/btree with duplicates, insert the duplicate,                 * put the new record in slot zero, and set the next index                 * to slot one (past the new record).                 */                status = cursor.putNoDupData(null, value, null, true);                if (status == OperationStatus.SUCCESS) {                    clearSlots();                    setSlot(0, cursor);                    dataIndex = 0;                    nextIndex = 1;                }            }            if (status == OperationStatus.KEYEXIST) {                throw new IllegalArgumentException("Duplicate value");            } else if (status != OperationStatus.SUCCESS) {                throw new IllegalArgumentException("Could not insert: " +                                                    status);            }            /* Prevent subsequent set() or remove() call. */            dataObject = null;        } catch (DatabaseException e) {            throw StoredContainer.convertException(e);        } finally {            closeCursor(cursor);        }    }    // --- end Iterator/ListIterator methods ---    // --- begin BaseIterator methods ---    public final ListIterator dup() {        return new BlockIterator(this);    }    public final boolean isCurrentData(Object currentData) {        return (dataObject == currentData);    }    public final boolean moveToIndex(int index) {        DataCursor cursor = null;        try {            cursor = new DataCursor(coll.view, writeAllowed);            OperationStatus status =                cursor.getSearchKey(new Integer(index), null, false);            if (status == OperationStatus.SUCCESS) {                clearSlots();                setSlot(0, cursor);                nextIndex = 0;                return true;            } else {                return false;            }        } catch (DatabaseException e) {            throw StoredContainer.convertException(e);        } finally {            closeCursor(cursor);        }    }    // --- end BaseIterator methods ---}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -