directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel L├ęcharny <elecha...@gmail.com>
Subject [Mavinot] Value handling and multiple values
Date Fri, 04 Oct 2013 20:18:39 GMT
Hi,

I'm currently implementing the ValueHodler for the managed btrees, and
I'm coming with some nice solution for the duplicate values. The idea is
to make the ValueHolder class Iterable, so that we can get any of the
stored values in it (even if we have none or only one).

This is what it will looks like :

public class ValueHolder<K, V> implements Iterable<V>
{
    /** The deserialized value */
    V[] valueArray;

    /** The BTree storing multiple value, if we have moe than a
threashold values */
    BTree<V, V> valueBtree;

    /** The serialized value */
    ByteBuffer raw;

    /** A flag set to true if the values are stored in a BTree */
    boolean isBtree = false;

    private class ValueArrayIterator implements Iterator<V>
    {
        /** Store the current position in the array or in the BTree */

        private int currentPos;


        private ValueArrayIterator()
        {
            // Start at -1 to be positionned before the first element
            currentPos = -1;
        }


        @Override
        public boolean hasNext()
        {
            if ( valueArray == null )
            {
                // Load the array from the raw data
                return false;
            }
            else
            {
                return ( valueArray != null ) && ( currentPos <
valueArray.length );
            }
        }


        public V next()
        {
            if ( valueArray == null )
            {
                // Deserialize the array
                return null;
            }
            else
            {
                currentPos++;

                if ( currentPos == valueArray.length )
                {
                    // We have reached the end of the array
                    return null;
                }
                else
                {
                    return valueArray[currentPos];
                }
            }
        }


        public void remove()
        {
            // Nothing to do, we don't allow any mutation on this class
        }
    }

    private class ValueBtreeIterator implements Iterator<V>
    {
        /** Store the current position in the array or in the BTree */

        private int currentPos;


        private ValueBtreeIterator()
        {
            // Start at -1 to be positionned before the first element
            currentPos = -1;
        }


        @Override
        public boolean hasNext()
        {
            if ( valueBtree == null )
            {
                return true;
            }
            else
            {
                return true;
            }
        }


        public V next()
        {
            return null;
        }


        public void remove()
        {
            // Nothing to do, we don't allow any mutation on this class
        }
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public Iterator<V> iterator()
    {
        if ( isBtree )
        {
            return new ValueBtreeIterator();
        }
        else
        {
            return new ValueArrayIterator();
        }
    }
}

As youc an see, we store either an array of Value, or a Btree. If we
have one single value, we use an array with one single element in it.

In any case, we define two different iterators which will be used
internally, but the caller will just have to use it as is.

It seems convenient, at least for a very first version, and it can be
used by the in-memory btree too, eliminating the need of many tests in
the implementation (things like : if ( btree.isAllowDuplicates() ).

wdyt ?

-- 
Regards,
Cordialement,
Emmanuel L├ęcharny
www.iktek.com 


Mime
View raw message