Search in sources :

Example 1 with ByteIterator

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

the class InternalPage method loadAddressLengths.

@Override
protected void loadAddressLengths(final int length) {
    super.loadAddressLengths(length);
    final ByteIterator it = getDataIterator(0);
    it.skip(size * keyAddressLen);
    checkAddressLength(childAddressLen = it.next());
}
Also used : ByteIterator(jetbrains.exodus.ByteIterator)

Example 2 with ByteIterator

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

the class PatriciaTraverser method moveTo.

@Override
public boolean moveTo(@NotNull ByteIterable key, @Nullable ByteIterable value) {
    final ByteIterator it = key.iterator();
    // the most bottom node, ignoring lower bound
    NodeBase node = top == 0 ? currentNode : stack[0].getParentNode();
    int depth = 0;
    NodeChildrenIterator[] tmp = new NodeChildrenIterator[INITIAL_STACK_CAPACITY];
    // go down and search
    while (true) {
        if (NodeBase.MatchResult.getMatchingLength(node.matchesKeySequence(it)) < 0) {
            return false;
        }
        if (!it.hasNext()) {
            break;
        }
        final NodeChildrenIterator itr = node.getChildren(it.next());
        final ChildReference ref = itr.getNode();
        if (ref == null) {
            return false;
        }
        tmp = pushIterator(tmp, itr, depth++);
        node = ref.getNode(tree);
    }
    // key match
    if (node.hasValue() && (value == null || value.compareTo(node.getValue()) == 0)) {
        setCurrentNode(node);
        getItr();
        stack = tmp;
        top = depth;
        return true;
    }
    return false;
}
Also used : ByteIterator(jetbrains.exodus.ByteIterator)

Example 3 with ByteIterator

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

the class PatriciaTreeMutable method deleteImpl.

private boolean deleteImpl(@NotNull final ByteIterable key) {
    final ByteIterator it = key.iterator();
    NodeBase node = root;
    final Deque<ChildReferenceTransient> stack = new ArrayDeque<>();
    for (; ; ) {
        if (node == null || NodeBase.MatchResult.getMatchingLength(node.matchesKeySequence(it)) < 0) {
            return false;
        }
        if (!it.hasNext()) {
            break;
        }
        final byte nextByte = it.next();
        stack.push(new ChildReferenceTransient(nextByte, node));
        node = node.getChild(this, nextByte);
    }
    if (!node.hasValue()) {
        return false;
    }
    --size;
    MutableNode mutableNode = node.getMutableCopy(this);
    ChildReferenceTransient parent = stack.peek();
    final boolean hasChildren = mutableNode.hasChildren();
    if (!hasChildren && parent != null) {
        stack.pop();
        mutableNode = parent.mutate(this);
        mutableNode.removeChild(parent.firstByte);
        if (!mutableNode.hasValue() && mutableNode.getChildrenCount() == 1) {
            mutableNode.mergeWithSingleChild(this);
        }
    } else {
        mutableNode.setValue(null);
        if (!hasChildren) {
            mutableNode.setKeySequence(ByteIterable.EMPTY);
        } else if (mutableNode.getChildrenCount() == 1) {
            mutableNode.mergeWithSingleChild(this);
        }
    }
    mutateUp(stack, mutableNode);
    return true;
}
Also used : ByteIterator(jetbrains.exodus.ByteIterator)

Example 4 with ByteIterator

use of jetbrains.exodus.ByteIterator 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 5 with ByteIterator

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

Aggregations

ByteIterator (jetbrains.exodus.ByteIterator)13 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)3 ByteIterable (jetbrains.exodus.ByteIterable)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1