public boolean moveToRange()

in environment/src/main/java/jetbrains/exodus/tree/patricia/PatriciaTraverser.java [269:357]


    public boolean moveToRange(@NotNull ByteIterable key, @Nullable ByteIterable value) {
        final ByteIterator it = key.iterator();
        NodeBase node = top == 0 ? currentNode : stack[0].getParentNode(); // the most bottom node, ignoring lower bound
        int depth = 0;
        NodeChildrenIterator[] tmp = new NodeChildrenIterator[INITIAL_STACK_CAPACITY];
        // go down and search
        final boolean dive;
        boolean smaller = false;
        while (true) {
            final boolean hasNext = it.hasNext();
            final long matchResult = node.matchesKeySequence(it);
            if (NodeBase.MatchResult.getMatchingLength(matchResult) < 0) {
                if (value == null) {
                    smaller = NodeBase.MatchResult.hasNext(matchResult) && (!hasNext ||
                            NodeBase.MatchResult.getKeyByte(matchResult) < NodeBase.MatchResult.getNextByte(matchResult));
                    dive = !smaller;
                    break;
                }
                return false;
            }
            if (!it.hasNext()) {
                // key match
                if (!node.hasValue()) {
                    dive = true;
                    break;
                }
                if (value == null || value.compareTo(node.getValue()) <= 0) {
                    setCurrentNode(node);
                    getItr();
                    stack = tmp;
                    top = depth;
                    return true;
                }
                return false;
            }
            final byte nextByte = it.next();
            NodeChildrenIterator itr = node.getChildren(nextByte);
            ChildReference ref = itr.getNode();
            if (ref == null) {
                itr = node.getChildrenRange(nextByte);
                ref = itr.getNode();
                if (ref != null) {
                    tmp = pushIterator(tmp, itr, depth++);
                    node = ref.getNode(tree);
                    dive = true;
                    break;
                }
                smaller = true;
                dive = false;
                break;
            }
            tmp = pushIterator(tmp, itr, depth++);
            node = ref.getNode(tree);
        }
        if (smaller || !node.hasValue()) {
            if (dive && node.getChildrenCount() > 0) {
                final NodeChildrenIterator itr = node.getChildren().iterator();
                tmp = pushIterator(tmp, itr, depth);
                node = itr.next().getNode(tree);
            } else {
                // go up and try range search
                NodeChildrenIterator itr;
                do {
                    if (depth > 0) {
                        itr = tmp[--depth];
                    } else {
                        return false; // search already gave us the max
                    }
                } while (!itr.hasNext());
                node = itr.next().getNode(tree);
                // trick: tmp[depth] was already in stack
            }
            ++depth;
            while (!node.hasValue()) {
                final NodeChildrenIterator itr = node.getChildren().iterator();
                if (!itr.hasNext()) {
                    throw new IllegalStateException("Can't dive into tree branch");
                }
                final ChildReference ref = itr.next();
                tmp = pushIterator(tmp, itr, depth++);
                node = ref.getNode(tree);
            }
        }
        setCurrentNode(node);
        getItr();
        stack = tmp;
        top = depth;
        return true;
    }