Search in sources :

Example 26 with ByteIterable

use of jetbrains.exodus.ByteIterable 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;
    // noinspection TryFinallyCanBeTryWithResources
    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 27 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class PatriciaCursorDecorator method getLast.

@Override
public boolean getLast() {
    if (patriciaCursor.getLast()) {
        final ByteIterable keyLengthIterable = patriciaCursor.getValue();
        final ByteIterable sourceKey = new UnEscapingByteIterable(patriciaCursor.getKey());
        keyBytes = sourceKey.getBytesUnsafe();
        keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
        valueLength = sourceKey.getLength() - keyLength - 1;
        // forget computed next pair
        nextKeyLength = UNKNOWN;
        // forget computed prev pair
        prevKeyLength = UNKNOWN;
        return true;
    }
    return false;
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 28 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class PatriciaCursorDecorator method getNextLazy.

private boolean getNextLazy() {
    if (nextKeyLength < 0) {
        // UNKNOWN
        if (patriciaCursor.getNext()) {
            final ByteIterable keyLengthIterable = patriciaCursor.getValue();
            final ByteIterable noDupKey = new UnEscapingByteIterable(patriciaCursor.getKey());
            nextKeyBytes = noDupKey.getBytesUnsafe();
            nextKeyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
            nextValueLength = noDupKey.getLength() - nextKeyLength - 1;
            return true;
        }
        return false;
    }
    return nextKeyBytes != null;
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 29 with ByteIterable

use of jetbrains.exodus.ByteIterable in project xodus by JetBrains.

the class PatriciaCursorDecorator method getSearchBothRange.

@Nullable
@Override
public ByteIterable getSearchBothRange(@NotNull ByteIterable key, @NotNull ByteIterable value) {
    final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
    ITreeCursor cursorToClose = cursor;
    // noinspection TryFinallyCanBeTryWithResources
    try {
        ByteIterable keyLengthIterable = cursor.getSearchKeyRange(new EscapingByteIterable(key));
        if (keyLengthIterable != null) {
            final int srcKeyLength = key.getLength();
            final int valueLength = value.getLength();
            while (true) {
                int keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
                if (srcKeyLength == keyLength) {
                    final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
                    final byte[] srcKeyBytes = key.getBytesUnsafe();
                    final byte[] noDupKeyBytes = noDupKey.getBytesUnsafe();
                    if (ByteIterableUtil.compare(srcKeyBytes, keyLength, noDupKeyBytes, keyLength) == 0) {
                        // skip separator
                        final int noDupKeyLength = noDupKey.getLength() - keyLength - 1;
                        final int cmp = ByteIterableUtil.compare(noDupKeyBytes, noDupKeyLength, keyLength + 1, value.getBytesUnsafe(), valueLength);
                        if (cmp >= 0) {
                            keyBytes = noDupKeyBytes;
                            this.keyLength = keyLength;
                            this.valueLength = noDupKeyLength;
                            cursorToClose = patriciaCursor;
                            patriciaCursor = cursor;
                            // forget computed next pair
                            nextKeyLength = UNKNOWN;
                            // forget computed prev pair
                            prevKeyLength = UNKNOWN;
                            return getValue();
                        }
                    }
                }
                if (cursor.getNext()) {
                    keyLengthIterable = cursor.getValue();
                } else {
                    break;
                }
            }
        }
    } 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 30 with ByteIterable

use of jetbrains.exodus.ByteIterable 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;
    // noinspection TryFinallyCanBeTryWithResources
    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)

Aggregations

ByteIterable (jetbrains.exodus.ByteIterable)86 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)45 Test (org.junit.Test)26 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)15 Nullable (org.jetbrains.annotations.Nullable)11 ITreeCursor (jetbrains.exodus.tree.ITreeCursor)8 Cursor (jetbrains.exodus.env.Cursor)7 RandomAccessLoggable (jetbrains.exodus.log.RandomAccessLoggable)7 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)6 Store (jetbrains.exodus.env.Store)4 TreeSet (java.util.TreeSet)3 Transaction (jetbrains.exodus.env.Transaction)3 Exchange (com.persistit.Exchange)2 ExodusException (jetbrains.exodus.ExodusException)2 TokyoCabinetBenchmark (jetbrains.exodus.benchmark.TokyoCabinetBenchmark)2 Pair (jetbrains.exodus.core.dataStructures.Pair)2 HashSet (jetbrains.exodus.core.dataStructures.hash.HashSet)2 PersistentLongSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet)2 PersistentStoreTransaction (jetbrains.exodus.entitystore.PersistentStoreTransaction)2 Loggable (jetbrains.exodus.log.Loggable)2