in mavibot/src/main/java/org/apache/directory/mavibot/btree/AbstractValueHolder.java [288:386]
private void addInArray( final V value )
{
// We have to check that we have reached the threshold or not
if ( size() >= valueThresholdUp )
{
// Ok, transform the array into a btree
createSubTree();
Iterator<Tuple<V, V>> valueIterator = new Iterator<Tuple<V, V>>()
{
int pos = 0;
@Override
public Tuple<V, V> next()
{
// We can now return the found value
if ( pos == valueArray.length )
{
// Special case : deal with the added value
pos++;
return new Tuple<V, V>( value, value );
}
else
{
V oldValue = valueArray[pos];
pos++;
return new Tuple<V, V>( oldValue, oldValue );
}
}
@Override
public boolean hasNext()
{
// Check that we have at least one element to read
return pos < valueArray.length + 1;
}
@Override
public void remove()
{
}
};
try
{
BulkLoader.load( valueBtree, valueIterator, valueArray.length );
}
catch ( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
manageSubTree();
// And make the valueArray to be null now
valueArray = null;
}
else
{
// Create the array if it's null
if ( valueArray == null )
{
valueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), 1 );
nbArrayElems = 1;
valueArray[0] = value;
}
else
{
// check that the value is not already present in the ValueHolder
int pos = findPos( value );
if ( pos >= 0 )
{
// The value exists : nothing to do
return;
}
// Ok, we just have to insert the new element at the right position
// We transform the position to a positive value
pos = -( pos + 1 );
// First, copy the array
V[] newValueArray = ( V[] ) Array.newInstance( valueSerializer.getType(), valueArray.length + 1 );
System.arraycopy( valueArray, 0, newValueArray, 0, pos );
newValueArray[pos] = value;
System.arraycopy( valueArray, pos, newValueArray, pos + 1, valueArray.length - pos );
// And switch the arrays
valueArray = newValueArray;
}
}
}