Search in sources :

Example 1 with ITreeCursor

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

the class PatriciaCursorDecorator method getPrevNoDup.

@Override
public boolean getPrevNoDup() {
    if (keyBytes == null) {
        // init
        return getPrev();
    }
    if (getPrevLazy() && ByteIterableUtil.compare(keyBytes, keyLength, prevKeyBytes, prevKeyLength) != 0) {
        advance();
        return true;
    }
    // we must create new cursor 'cause we don't know if prev "no dup" pair exists
    final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
    ITreeCursor cursorToClose = cursor;
    try {
        if (cursor.getSearchKeyRange(getEscapedKeyValue(getKey(), getValue())) != null) {
            while (cursor.getPrev()) {
                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 prev pair
                    prevKeyLength = UNKNOWN;
                    // forget computed next pair
                    nextKeyLength = 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 2 with ITreeCursor

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

the class PatriciaCursorDecorator method getSearchKey.

@Nullable
@Override
public ByteIterable getSearchKey(@NotNull ByteIterable key) {
    final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
    ITreeCursor cursorToClose = cursor;
    try {
        ByteIterable keyLengthIterable = cursor.getSearchKeyRange(new EscapingByteIterable(key));
        if (keyLengthIterable != null) {
            int keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
            if (key.getLength() == keyLength) {
                final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
                final byte[] keyBytes = key.getBytesUnsafe();
                final byte[] noDupKeyBytes = noDupKey.getBytesUnsafe();
                if (ByteIterableUtil.compare(keyBytes, keyLength, noDupKeyBytes, keyLength) == 0) {
                    this.keyBytes = noDupKeyBytes;
                    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 getValue();
                }
            }
        }
    } finally {
        cursorToClose.close();
    }
    return null;
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ITreeCursor(jetbrains.exodus.tree.ITreeCursor) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ITreeCursor

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

the class PatriciaTreeWithDuplicates method get.

@Nullable
@Override
public ByteIterable get(@NotNull final ByteIterable key) {
    try (ITreeCursor cursor = treeNoDuplicates.openCursor()) {
        final ByteIterable value = cursor.getSearchKeyRange(getEscapedKeyWithSeparator(key));
        if (value != null && value != ByteIterable.EMPTY) {
            int keyLength = CompressedUnsignedLongByteIterable.getInt(value);
            if (key.getLength() == keyLength) {
                final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
                final byte[] noDupKeyBytes = noDupKey.getBytesUnsafe();
                if (ByteIterableUtil.compare(key.getBytesUnsafe(), keyLength, noDupKeyBytes, keyLength) == 0) {
                    return new ArrayByteIterable(Arrays.copyOfRange(noDupKeyBytes, // skip separator
                    keyLength + 1, noDupKey.getLength()));
                }
            }
        }
        return null;
    }
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) CompoundByteIterable(jetbrains.exodus.CompoundByteIterable) CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) SingleByteIterable(jetbrains.exodus.log.SingleByteIterable) ITreeCursor(jetbrains.exodus.tree.ITreeCursor) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ITreeCursor

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

the class BTreePutSpecificTest method testNextDup.

@Test
public void testNextDup() {
    tm = createEmptyTreeForCursor(1).getMutableCopy();
    getTreeMutable().put(kv("1", "1"));
    getTreeMutable().put(kv("1", "2"));
    getTreeMutable().put(kv("1", "3"));
    getTreeMutable().put(kv("1", "4"));
    getTreeMutable().put(kv("1", "5"));
    final ITreeCursor cursor = tm.openCursor();
    assertTrue(cursor.getNextDup());
    assertTrue(cursor.getNextDup());
    assertTrue(cursor.getNextDup());
    assertTrue(cursor.getNextDup());
    assertTrue(cursor.getNextDup());
}
Also used : ITreeCursor(jetbrains.exodus.tree.ITreeCursor) Test(org.junit.Test)

Example 5 with ITreeCursor

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

the class BTreeCursorDupConcurrentCursorModificationTest method deleteImpl.

@Override
protected void deleteImpl(@NotNull final ByteIterable key, @NotNull final ByteIterable value) {
    final ITreeCursor cursor = tm.openCursor();
    assertTrue(cursor.getSearchBoth(key, value));
    assertTrue(cursor.deleteCurrent());
}
Also used : 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