public TupleCursor browse()

in mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedLeaf.java [735:850]


    public TupleCursor<K, V> browse( K key, ReadTransaction<K, V> transaction, ParentPos<K, V>[] stack, int depth )
    {
        int pos = findPos( key );

        // First use case : the leaf is empty (this is a root page)
        if ( nbElems == 0 )
        {
            // We have to return an empty cursor
            return new EmptyTupleCursor<K, V>();
        }

        // The cursor we will return
        TupleCursor<K, V> cursor = new TupleCursor<K, V>( transaction, stack, depth );

        // Depending on the position, we will proceed differently :
        // 1) if the key is found in the page, the cursor will be 
        // set to this position.
        // 2) The key has not been found, but is in the middle of the
        // page (ie, other keys above the one we are looking for exist),
        // the cursor will be set to the current position
        // 3) The key has not been found, and we are at the end of
        // the page. We have to fetch the next key in yhe B-tree
        if ( pos < 0 )
        {
            // The key has been found.
            pos = -( pos + 1 );

            // Start at the found position in the page
            ParentPos<K, V> parentPos = new ParentPos<K, V>( this, pos );

            // Create the value cursor
            parentPos.valueCursor = values[pos].getCursor();

            // And store this position in the stack
            stack[depth] = parentPos;

            return cursor;
        }
        else
        {
            // The key has not been found, there are keys above this one. 
            // Select the value just above
            if ( pos < nbElems )
            {
                // There is at least one key above the one we are looking for.
                // This will be the starting point.
                ParentPos<K, V> parentPos = new ParentPos<K, V>( this, pos );

                // Create the value cursor
                parentPos.valueCursor = values[pos].getCursor();

                stack[depth] = parentPos;

                return cursor;
            }
            else
            {
                // We are at the end of a leaf. We have to see if we have other
                // keys on the right.
                if ( depth == 0 )
                {
                    // No children, we are at the end of the root page
                    stack[depth] = new ParentPos<K, V>( this, pos );

                    // As we are done, set the cursor at the end
                    try
                    {
                        cursor.afterLast();
                    }
                    catch ( IOException e )
                    {
                        e.printStackTrace();
                    }

                    return cursor;
                }
                else
                {
                    // We have to find the adjacent key in the B-tree
                    boolean isLast = true;
                    stack[depth] = new ParentPos<K, V>( this, pos );

                    // Check each upper node, starting from the direct parent
                    int stackIndex = depth - 1;

                    for ( int i = stackIndex; i >= 0; i-- )
                    {
                        if ( stack[i].pos < stack[i].page.getNbElems() )
                        {
                            isLast = false;
                            break;
                        }

                        stackIndex--;
                    }

                    if ( isLast )
                    {
                        // We don't have any more elements
                        try
                        {
                            cursor.afterLast();
                        }
                        catch ( IOException e )
                        {
                            e.printStackTrace();
                        }

                        return cursor;
                    }

                    return cursor;
                }
            }
        }
    }