Search in sources :

Example 16 with ByteIterable

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

the class PatriciaTreeMutable method put.

@Override
public boolean put(@NotNull final ByteIterable key, @NotNull final ByteIterable value) {
    final ByteIterator it = key.iterator();
    MutableNode node = root;
    MutableNode prev = null;
    byte prevFirstByte = (byte) 0;
    while (true) {
        final long matchResult = node.matchesKeySequence(it);
        final int matchingLength = NodeBase.MatchResult.getMatchingLength(matchResult);
        if (matchingLength < 0) {
            final MutableNode prefix = node.splitKey(-matchingLength - 1, NodeBase.MatchResult.getKeyByte(matchResult));
            if (NodeBase.MatchResult.hasNext(matchResult)) {
                prefix.hang(NodeBase.MatchResult.getNextByte(matchResult), it).setValue(value);
            } else {
                prefix.setValue(value);
            }
            if (prev == null) {
                root = new MutableRoot(prefix, root.sourceAddress);
            } else {
                prev.setChild(prevFirstByte, prefix);
            }
            ++size;
            return true;
        }
        if (!it.hasNext()) {
            final ByteIterable oldValue = node.getValue();
            node.setValue(value);
            if (oldValue == null) {
                ++size;
                return true;
            }
            return !oldValue.equals(value);
        }
        final byte nextByte = it.next();
        final NodeBase child = node.getChild(this, nextByte);
        if (child == null) {
            if (node.hasChildren() || node.hasKey() || node.hasValue()) {
                node.hang(nextByte, it).setValue(value);
            } else {
                node.setKeySequence(new ArrayByteIterable(nextByte, it));
                node.setValue(value);
            }
            ++size;
            return true;
        }
        prev = node;
        prevFirstByte = nextByte;
        final MutableNode mutableChild = child.getMutableCopy(this);
        if (!child.isMutable()) {
            node.setChild(nextByte, mutableChild);
        }
        node = mutableChild;
    }
}
Also used : ByteIterator(jetbrains.exodus.ByteIterator) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 17 with ByteIterable

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

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

the class PatriciaTreeWithDuplicatesMutable method delete.

@Override
public boolean delete(@NotNull final ByteIterable key) {
    boolean wasDeleted = false;
    try (ITreeCursor cursor = treeNoDuplicates.openCursor()) {
        final byte[] keyBytes = key.getBytesUnsafe();
        final int keyLength = key.getLength();
        @Nullable ByteIterable value = cursor.getSearchKeyRange(getEscapedKeyWithSeparator(key));
        while (value != null) {
            if (keyLength != CompressedUnsignedLongByteIterable.getInt(value)) {
                break;
            }
            final ByteIterable noDupKey = new UnEscapingByteIterable(cursor.getKey());
            if (ByteIterableUtil.compare(keyBytes, keyLength, noDupKey.getBytesUnsafe(), keyLength) != 0) {
                break;
            }
            cursor.deleteCurrent();
            wasDeleted = true;
            value = cursor.getNext() ? cursor.getValue() : null;
        }
    }
    return wasDeleted;
}
Also used : CompressedUnsignedLongByteIterable(jetbrains.exodus.log.CompressedUnsignedLongByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with ByteIterable

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

the class GarbageCollectorInterleavingTest method fill.

private void fill(@NotNull final String table) {
    final ByteIterable val0 = StringBinding.stringToEntry("val0");
    env.executeInTransaction(new TransactionalExecutable() {

        @Override
        public void execute(@NotNull Transaction txn) {
            final StoreImpl store = env.openStore(table, getStoreConfig(), txn);
            for (int i = 0; i < getRecordsNumber(); ++i) {
                final ArrayByteIterable key = StringBinding.stringToEntry("key " + i);
                store.put(txn, key, val0);
            }
        }
    });
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 20 with ByteIterable

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

the class GarbageCollectorInterleavingTest method check.

private void check(@NotNull final String table) {
    final ByteIterable val0 = StringBinding.stringToEntry("val0");
    env.executeInReadonlyTransaction(new TransactionalExecutable() {

        @Override
        public void execute(@NotNull Transaction txn) {
            final Store store = env.openStore(table, getStoreConfig(), txn);
            for (int i = 0; i < getRecordsNumber(); ++i) {
                final ArrayByteIterable key = StringBinding.stringToEntry("key " + i);
                Assert.assertTrue(store.exists(txn, key, val0));
            }
        }
    });
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

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