// The idea is to use a ChildrenCursor each time we have an entry with chidren,
// and process recursively.
if ( hasNext )
{
IndexEntry cursorEntry = currentCursor.get();
ParentIdAndRdn parentIdAndRdn = ( ( ParentIdAndRdn ) ( cursorEntry.getKey() ) );
// Check that we aren't out of the cursor's limit
if ( !parentIdAndRdn.getParentId().equals( currentParentId ) )
{
// Ok, we went too far. Unstack the cursor and return
finished = cursorStack.size() == 0;
if ( !finished )
{
currentCursor.close();
currentCursor = ( Cursor<IndexEntry<ParentIdAndRdn, String>> ) cursorStack.pop();
currentParentId = ( String ) parentIdStack.pop();
}
// And continue...
}
else
{
// We have a candidate, it will be returned.
if ( topLevel )
{
prefetched = new IndexEntry();
prefetched.setId( cursorEntry.getId() );
prefetched.setKey( baseId );
}
else
{
prefetched = cursorEntry;
}
// Check if the current entry has children or not.
if ( parentIdAndRdn.getNbDescendants() > 0 )
{
String newParentId = ( String ) cursorEntry.getId();
// Yes, then create a new cursor and go down one level
Cursor<IndexEntry<ParentIdAndRdn, String>> cursor = db.getRdnIndex().forwardCursor();
IndexEntry<ParentIdAndRdn, String> startingPos = new IndexEntry<ParentIdAndRdn, String>();
startingPos.setKey( new ParentIdAndRdn( newParentId, ( Rdn[] ) null ) );
cursor.before( startingPos );
cursorStack.push( currentCursor );
parentIdStack.push( currentParentId );