// Unknown parent. Does not matter (parent is only in-memory)
BPTreeNode bptNode = mgr.createNode(-1) ;
bptNode.setIsLeaf(leafLayer) ;
RecordBuffer recBuff = bptNode.getRecordBuffer() ;
PtrBuffer ptrBuff = bptNode.getPtrBuffer() ;
recBuff.setSize(0) ;
ptrBuff.setSize(0) ; // Creation leaves this junk.
final boolean debug = false ;
int rMax = recBuff.maxSize() ;
int pMax = ptrBuff.maxSize() ;
if ( debug ) System.out.printf("Max: (%d, %d)\n", rMax, pMax) ;
for ( ; iter.hasNext() ; )
{
int X = bptNode.getCount() ;
int X2 = bptNode.getMaxSize() ;
int P = ptrBuff.size() ;
int P2 = ptrBuff.maxSize() ;
int R = recBuff.size() ;
int R2 = recBuff.maxSize() ;
// bptNode.getMaxSize() is drivel
//System.out.printf("N: %d/%d : P %d/%d : R %d/%d\n", X, X2, P, P2, R, R2) ;
Pair<Integer, Record> pair = iter.next() ;
if ( debug ) System.out.println("** Item: "+pair) ;
Record r = pair.cdr() ;
// [Issue: FREC]
// The record buffer size is wrong.
// Writes the whole record, only need to write the key part.
// **** r = recordFactory.createKeyOnly(r) ;
// [Issue: FREC]
// The record is key-only (which is correct) but until FREC fixed, we need key,value
r = recordFactory.create(r.getKey()) ;
// -- End FREC
// Always add - so ptrBuff is one ahead when we finish.
// There is always one more ptr than record in a B+Tree node.
if ( ptrBuff.isFull() )
System.err.println("PtrBuffer is full") ;
// Add pointer.
ptrBuff.add(pair.car()) ;
// [Issue: FREC]
// Either test shoudl work but due to the missetting of record buffer size
// testing recBuff does not work.
//if ( recBuff.isFull() )
// .... test ptrBuff
if ( ptrBuff.isFull() )
{
// End of this block.
// Does not add to ptrBuff so the one extra slot is done.
// Instead, the high point goes to the next level up.
// Internal consistency check.
if ( ! ptrBuff.isFull() )
System.err.println("PtrBuffer is not full") ;
// The split point for the next level up.
slot = new Pair<Integer,Record>(bptNode.getId(), pair.cdr()) ;
if ( debug ) System.out.printf("Write(1): %d\n", bptNode.getId()) ;
if ( debug ) System.out.println(bptNode) ;
if ( debug ) System.out.println("Slot = "+slot) ;
mgr.put(bptNode) ;
// No count increment needed.
return true ;
}
recBuff.add(r) ;
bptNode.setCount(bptNode.getCount()+1) ;
}
// If we get here, the input stream ran out before we finished a complete block.
// Fix up block (remove the last record)
Record r = recBuff.getHigh() ;
recBuff.removeTop() ;
bptNode.setCount(bptNode.getCount()-1) ;
slot = new Pair<Integer,Record>(bptNode.getId(), r) ;
if ( debug ) System.out.printf("Write(2): %d\n", bptNode.getId()) ;
if ( debug ) System.out.println(bptNode) ;