private Page addLeaves()

in mavibot/src/main/java/org/apache/directory/mavibot/btree/InMemoryBTreeBuilder.java [234:367]


    private Page<K, V> addLeaves( BTree<K, V> btree, List<Tuple<K, V>> tuples, int maxElements )
    {
        if ( tuples.size() == 0 )
        {
            // No element to inject in the BTree
            return null;
        }

        // The insertion position in the leaf
        int leafPos = 0;

        // Deal with special cases : 
        // First, everything will fit in a single page
        if ( tuples.size() < btree.getPageSize() )
        {
            // The leaf will be the rootPage
            // creates a first leaf
            InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) BTreeFactory.createLeaf( btree, 0,
                btreeConfiguration.getPageSize() );

            // Iterate on the tuples
            for ( Tuple<K, V> tuple : tuples )
            {
                injectTuple( btree, leaf, leafPos, tuple );
                leafPos++;
            }

            return leaf;
        }

        // Second, the data will fit into a 2 level tree
        if ( tuples.size() < maxElements )
        {
            // We will just create the necessary leaves and the upper node if needed
            // We have enough tuples to fulfill the uper node.
            // First, create the new node
            InMemoryNode<K, V> node = ( InMemoryNode<K, V> ) BTreeFactory.createNode( btree, 0,
                btreeConfiguration.getPageSize() );

            // creates a first leaf
            InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) BTreeFactory.createLeaf( btree, 0,
                btreeConfiguration.getPageSize() );

            int nodePos = 0;

            // Then iterate on the tuples, creating the needed pages
            for ( Tuple<K, V> tuple : tuples )
            {
                if ( leafPos == btree.getPageSize() )
                {
                    // The leaf is full, we need to attach it to its parent's node
                    // and to create a new leaf
                    BTreeFactory.setKey( btree, node, nodePos, tuple.getKey() );
                    PageHolder<K, V> pageHolder = new PageHolder<K, V>( btree, leaf );
                    node.setPageHolder( nodePos, pageHolder );
                    nodePos++;

                    // When done, we need to create a new leaf
                    leaf = ( InMemoryLeaf<K, V> ) BTreeFactory.createLeaf( btree, 0,
                        btree.getPageSize() );

                    // and inject the tuple in the leaf
                    injectTuple( btree, leaf, 0, tuple );
                    leafPos = 1;
                }
                else
                {
                    // Inject the tuple in the leaf
                    injectTuple( btree, leaf, leafPos, tuple );
                    leafPos++;
                }
            }

            // Last, not least, deal with the last created leaf, which has to be injected in its parent's node
            if ( leafPos > 0 )
            {
                PageHolder<K, V> pageHolder = new PageHolder<K, V>( btree, leaf );
                node.setPageHolder( nodePos, pageHolder );
            }

            return node;
        }
        else
        {
            // We have enough tuples to fulfill the upper node.
            // First, create the new node
            InMemoryNode<K, V> node = ( InMemoryNode<K, V> ) BTreeFactory.createNode( btree, 0,
                btreeConfiguration.getPageSize() );

            // creates a first leaf
            InMemoryLeaf<K, V> leaf = ( InMemoryLeaf<K, V> ) BTreeFactory.createLeaf( btree, 0,
                btreeConfiguration.getPageSize() );

            int nodePos = 0;

            // Then iterate on the tuples, creating the needed pages
            for ( Tuple<K, V> tuple : tuples )
            {
                if ( leafPos == btree.getPageSize() )
                {
                    // The leaf is full, we need to attach it to its parent's node
                    // and to create a new node
                    BTreeFactory.setKey( btree, node, nodePos, tuple.getKey() );
                    PageHolder<K, V> pageHolder = new PageHolder<K, V>( btree, leaf );
                    node.setPageHolder( nodePos, pageHolder );
                    nodePos++;

                    // When done, we need to create a new leaf
                    leaf = ( InMemoryLeaf<K, V> ) BTreeFactory.createLeaf( btree, 0,
                        btree.getPageSize() );

                    // and inject the tuple in the leaf
                    injectTuple( btree, leaf, 0, tuple );
                    leafPos = 1;
                }
                else
                {
                    // Inject the tuple in the leaf
                    injectTuple( btree, leaf, leafPos, tuple );
                    leafPos++;
                }
            }

            // Last, not least, deal with the last created leaf, which has to be injected in its parent's node
            if ( leafPos > 0 )
            {
                PageHolder<K, V> pageHolder = new PageHolder<K, V>( btree, leaf );
                node.setPageHolder( nodePos, pageHolder );
            }

            // And return the node
            return node;
        }
    }