Search in sources :

Example 11 with ITreeCursor

use of jetbrains.exodus.tree.ITreeCursor in project xodus by JetBrains.

the class PatriciaCursorDecorator method getNextNoDup.

@Override
public boolean getNextNoDup() {
    if (keyBytes == null) {
        // init
        return getNext();
    }
    if (getNextLazy() && ByteIterableUtil.compare(keyBytes, keyLength, nextKeyBytes, nextKeyLength) != 0) {
        advance();
        return true;
    }
    // we must create new cursor 'cause we don't know if next "no dup" pair exists
    final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
    ITreeCursor cursorToClose = cursor;
    try {
        if (cursor.getSearchKeyRange(getEscapedKeyValue(getKey(), getValue())) != null) {
            while (cursor.getNext()) {
                final ByteIterable keyLengthIterable = cursor.getValue();
                final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
                final int keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
                final byte[] noDupKeyBytes = noDupKey.getBytesUnsafe();
                if (ByteIterableUtil.compare(keyBytes, this.keyLength, noDupKeyBytes, keyLength) != 0) {
                    keyBytes = noDupKey.getBytesUnsafe();
                    this.keyLength = keyLength;
                    valueLength = noDupKey.getLength() - keyLength - 1;
                    cursorToClose = patriciaCursor;
                    patriciaCursor = cursor;
                    // forget computed next pair
                    nextKeyLength = UNKNOWN;
                    // forget computed prev pair
                    prevKeyLength = UNKNOWN;
                    return true;
                }
            }
        }
    } finally {
        cursorToClose.close();
    }
    return false;
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ITreeCursor(jetbrains.exodus.tree.ITreeCursor)

Example 12 with ITreeCursor

use of jetbrains.exodus.tree.ITreeCursor in project xodus by JetBrains.

the class CursorImpl method deleteCurrent.

@Override
public boolean deleteCurrent() {
    final ReadWriteTransaction txn = EnvironmentImpl.throwIfReadonly(this.txn, "Can't delete a key/value pair of cursor in read-only transaction");
    if (treeCursor == null) {
        treeCursor = txn.getMutableTree(store).openCursor();
    } else {
        if (!treeCursor.isMutable()) {
            final ByteIterable key = treeCursor.getKey();
            final ByteIterable value = treeCursor.getValue();
            final ITreeCursor newCursor = txn.getMutableTree(store).openCursor();
            if (newCursor.getSearchBoth(key, value)) {
                // navigated to same pair, ready to delete
                treeCursor = newCursor;
            } else {
                throw new ConcurrentModificationException(CANT_DELETE_MODIFIED_MSG);
            }
        }
    }
    return treeCursor.deleteCurrent();
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) ByteIterable(jetbrains.exodus.ByteIterable) ITreeCursor(jetbrains.exodus.tree.ITreeCursor)

Aggregations

ITreeCursor (jetbrains.exodus.tree.ITreeCursor)12 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)8 ByteIterable (jetbrains.exodus.ByteIterable)8 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)7 Nullable (org.jetbrains.annotations.Nullable)4 Test (org.junit.Test)3 ConcurrentModificationException (java.util.ConcurrentModificationException)1 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)1 RandomAccessLoggable (jetbrains.exodus.log.RandomAccessLoggable)1 SingleByteIterable (jetbrains.exodus.log.SingleByteIterable)1