in mavibot/src/main/java/org/apache/directory/mavibot/btree/BulkLoader.java [247:326]
public static <K, V> BTree<K, V> load( BTree<K, V> btree, Iterator<Tuple<K, V>> iterator, int chunkSize )
throws IOException
{
if ( btree == null )
{
throw new RuntimeException( "Invalid BTree : it's null" );
}
if ( iterator == null )
{
// Nothing to do...
return null;
}
// Iterate through the elements by chunk
boolean inMemory = true;
// The list of files we will use to store the sorted chunks
List<File> sortedFiles = new ArrayList<File>();
// An array of chukSize tuple max
List<Tuple<K, V>> tuples = new ArrayList<Tuple<K, V>>( chunkSize );
// Now, start to read all the tuples to sort them. We may use intermediate files
// for that purpose if we hit the threshold.
int nbElems = readElements( btree, iterator, sortedFiles, tuples, chunkSize );
// If the tuple list is empty, we have to process the load based on files, not in memory
if ( nbElems > 0 )
{
inMemory = tuples.size() > 0;
}
// Now that we have processed all the data, we can start storing them in the btree
Iterator<Tuple<K, Set<V>>> dataIterator = null;
FileInputStream[] streams = null;
BTree<K, V> resultBTree = null;
if ( inMemory )
{
// Here, we have all the data in memory, no need to merge files
// We will build a simple iterator over the data
dataIterator = createTupleIterator( btree, tuples );
resultBTree = bulkLoad( btree, dataIterator, nbElems );
}
else
{
// We first have to build an iterator over the files
int nbFiles = sortedFiles.size();
streams = new FileInputStream[nbFiles];
for ( int i = 0; i < nbFiles; i++ )
{
streams[i] = new FileInputStream( sortedFiles.get( i ) );
}
dataIterator = createIterator( btree, streams );
// Process the files, and construct one single file with an iterator
Tuple<Iterator<Tuple<K, Set<V>>>, SortedFile> result = processFiles( btree, dataIterator );
resultBTree = bulkLoad( btree, result.key, result.value.nbValues );
result.value.file.delete();
}
// Ok, we have an iterator over sorted elements, we can now load them in the
// target btree.
// Now, close the FileInputStream, and delete them if we have some
if ( !inMemory )
{
int nbFiles = sortedFiles.size();
for ( int i = 0; i < nbFiles; i++ )
{
streams[i].close();
sortedFiles.get( i ).delete();
}
}
return resultBTree;
}