Search in sources :

Example 1 with ArrayByteIterable

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

the class CompoundByteIterator method hasNextImpl.

private boolean hasNextImpl() {
    while (!current.hasNext()) {
        currentAddress += read;
        final int alignment = ((int) currentAddress) & (log.getCachePageSize() - 1);
        final long alignedAddress = currentAddress - alignment;
        final ArrayByteIterable page = log.cache.getPageIterable(log, alignedAddress);
        final int readBytes = page.getLength();
        if (readBytes <= alignment) {
            // alignment is >= 0 for sure
            read = 0;
            offset = 0;
            return false;
        }
        read = readBytes - alignment;
        current = page.iterator(alignment);
        offset = current.getOffset();
    }
    return true;
}
Also used : ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 2 with ArrayByteIterable

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

the class EnvironmentTestInMemory method deleteRandomKey.

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

Example 3 with ArrayByteIterable

use of jetbrains.exodus.ArrayByteIterable 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 4 with ArrayByteIterable

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

the class PatriciaTreeMutable method add.

@Override
public boolean add(@NotNull final ByteIterable key, @NotNull final ByteIterable value) {
    final ByteIterator it = key.iterator();
    NodeBase node = root;
    MutableNode mutableNode = null;
    final Deque<ChildReferenceTransient> stack = new ArrayDeque<>();
    while (true) {
        final long matchResult = node.matchesKeySequence(it);
        final int matchingLength = NodeBase.MatchResult.getMatchingLength(matchResult);
        if (matchingLength < 0) {
            final MutableNode prefix = node.getMutableCopy(this).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 (stack.isEmpty()) {
                root = new MutableRoot(prefix, root.sourceAddress);
            } else {
                final ChildReferenceTransient parent = stack.pop();
                mutableNode = parent.mutate(this);
                mutableNode.setChild(parent.firstByte, prefix);
            }
            break;
        }
        if (!it.hasNext()) {
            if (node.hasValue()) {
                return false;
            }
            mutableNode = node.getMutableCopy(this);
            mutableNode.setValue(value);
            break;
        }
        final byte nextByte = it.next();
        final NodeBase child = node.getChild(this, nextByte);
        if (child == null) {
            mutableNode = node.getMutableCopy(this);
            if (mutableNode.hasChildren() || mutableNode.hasKey() || mutableNode.hasValue()) {
                mutableNode.hang(nextByte, it).setValue(value);
            } else {
                mutableNode.setKeySequence(new ArrayByteIterable(nextByte, it));
                mutableNode.setValue(value);
            }
            break;
        }
        stack.push(new ChildReferenceTransient(nextByte, node));
        node = child;
    }
    ++size;
    mutateUp(stack, mutableNode);
    return true;
}
Also used : ByteIterator(jetbrains.exodus.ByteIterator) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable)

Example 5 with ArrayByteIterable

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

Aggregations

ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)29 ByteIterable (jetbrains.exodus.ByteIterable)15 Transaction (jetbrains.exodus.env.Transaction)4 TransactionalExecutable (jetbrains.exodus.env.TransactionalExecutable)4 Nullable (org.jetbrains.annotations.Nullable)4 Test (org.junit.Test)4 File (java.io.File)3 ByteIterator (jetbrains.exodus.ByteIterator)3 Cursor (jetbrains.exodus.env.Cursor)3 Store (jetbrains.exodus.env.Store)3 CompressedUnsignedLongByteIterable (jetbrains.exodus.log.CompressedUnsignedLongByteIterable)2 ITreeCursor (jetbrains.exodus.tree.ITreeCursor)2 NotNull (org.jetbrains.annotations.NotNull)2 ArrayList (java.util.ArrayList)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 CompoundByteIterable (jetbrains.exodus.CompoundByteIterable)1 StringBinding.entryToString (jetbrains.exodus.bindings.StringBinding.entryToString)1 Pair (jetbrains.exodus.core.dataStructures.Pair)1 PersistentLong23TreeSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLong23TreeSet)1 PersistentLongSet (jetbrains.exodus.core.dataStructures.persistent.PersistentLongSet)1