Search in sources :

Example 71 with ByteIterable

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

the class EnvironmentTestInMemory method putRandomKeyValue.

private void putRandomKeyValue(Store primary, Store secondary, Transaction txn, int keysCount, int valuesCount, Persistent23TreeMap.MutableMap<Integer, Integer> testMap) {
    final int key = rnd.nextInt(keysCount);
    final ArrayByteIterable keyEntry = IntegerBinding.intToCompressedEntry(key);
    final int value = rnd.nextInt(valuesCount);
    testMap.put(key, value);
    final ArrayByteIterable valueEntry = IntegerBinding.intToCompressedEntry(value);
    final ByteIterable oldValue = primary.get(txn, keyEntry);
    primary.put(txn, keyEntry, valueEntry);
    if (oldValue != null) {
        try (Cursor cursor = secondary.openCursor(txn)) {
            Assert.assertTrue(cursor.getSearchBoth(oldValue, keyEntry));
            Assert.assertTrue(cursor.deleteCurrent());
        }
    }
    secondary.put(txn, valueEntry, keyEntry);
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 72 with ByteIterable

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

the class TreeCursorNoDuplicatesTest method testRandomInserts.

@Test
public void testRandomInserts() {
    final ByteIterable value = value("value");
    final Set<String> keys = new HashSet<>();
    for (int i = 0; i < 10000; ++i) {
        keys.add(rndString());
    }
    tm = createMutableTree(false, 1);
    for (final String key : keys) {
        Assert.assertTrue(tm.add(key(key), value));
    }
    testCursorOrder(new TreeSet<>(keys));
}
Also used : ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) HashSet(jetbrains.exodus.core.dataStructures.hash.HashSet) Test(org.junit.Test)

Example 73 with ByteIterable

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

the class PatriciaCursorDecorator method getSearchKeyRange.

@Nullable
@Override
public ByteIterable getSearchKeyRange(@NotNull ByteIterable key) {
    final ByteIterable keyLengthIterable = patriciaCursor.getSearchKeyRange(new EscapingByteIterable(key));
    if (keyLengthIterable == null) {
        return null;
    }
    final ByteIterable noDupKey = new UnEscapingByteIterable(patriciaCursor.getKey());
    keyBytes = noDupKey.getBytesUnsafe();
    keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
    valueLength = noDupKey.getLength() - keyLength - 1;
    // forget computed next pair
    nextKeyLength = UNKNOWN;
    // forget computed prev pair
    prevKeyLength = UNKNOWN;
    return getValue();
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Nullable(org.jetbrains.annotations.Nullable)

Example 74 with ByteIterable

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

the class PatriciaCursorDecorator method getSearchBoth.

@Override
public boolean getSearchBoth(@NotNull ByteIterable key, @NotNull ByteIterable value) {
    final ITreeCursor cursor = patriciaCursor.getTree().openCursor();
    ITreeCursor cursorToClose = cursor;
    try {
        ByteIterable keyLengthIterable = cursor.getSearchKey(getEscapedKeyValue(key, value));
        if (keyLengthIterable == null) {
            return false;
        }
        final int keyLength = CompressedUnsignedLongByteIterable.getInt(keyLengthIterable);
        if (keyLength == key.getLength()) {
            keyBytes = new UnEscapingByteIterable(cursor.getKey()).getBytesUnsafe();
            this.keyLength = keyLength;
            valueLength = value.getLength();
            cursorToClose = patriciaCursor;
            patriciaCursor = cursor;
            // forget computed next pair
            nextKeyLength = UNKNOWN;
            // forget computed prev pair
            prevKeyLength = UNKNOWN;
            return true;
        }
        return false;
    } finally {
        cursorToClose.close();
    }
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ITreeCursor(jetbrains.exodus.tree.ITreeCursor)

Example 75 with ByteIterable

use of jetbrains.exodus.ByteIterable 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)

Aggregations

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