*/
private HeapRecord createRecordForDataFromFree(final int neededDataSize,
final DataRecordIdentifier nodeIdentifier,
final int neededRecordSize, final HeapFreeNode freeNode)
throws HeapException {
final HeapRecord heapRecord;
/* usable free area size */
final int freeRecordSize = freeNode.getRecordSize();
/* unused area size is usable minus needed area size */
final int newFreeRecordSize = freeRecordSize - neededRecordSize;
heapRecord = freeNode.getHeapRecord();
/* no more free */
freeNodeTreeDelete(heapRecord);
// NO NO NO do not delete, reused as data record
// heapElementManager.delete(heapRecord.getPositionInFile());
/* will update heap header for free and used size */
final HeapHeader heapHeader = (HeapHeader) heapElementManager
.getHeapHeader();
if (newFreeRecordSize >= HeapRecord.MAX_RECORD_HEADER_SIZE
+ MINIMUM_RECORD_DATA_SIZE + 4) {
/*
* split free record : one part used for data the other keep free
*/
final long nextRecordFilePosition = heapRecord
.getNextRecordFilePosition();
heapRecord.unfreeRecord(neededDataSize, neededRecordSize,
nodeIdentifier, false);
final long positionInFile = heapRecord.getPositionInFile();
final long newFreeRecordPositionInFile = positionInFile
+ neededRecordSize;
final HeapRecord newFreeHeapRecord = new HeapRecord(
heapElementManager,
newFreeRecordPositionInFile/* positionInFile */,
positionInFile/* previousPositionInFile */,
null/* nodeIdentifier */, true/* freeRecord */,
0/* dataAssociatedSize */, newFreeRecordSize/* recordSize */);
// final HeapRecord newFreeHeapRecord = heapElementManager
// .newHeapFileRecord(
// newFreeRecordPositionInFile/* positionInFile */,
// positionInFile/* previousPositionInFile */,
// null/* nodeIdentifier */, true/* freeRecord */,
// null/* dataAssociatedSize */, newFreeRecordSize/*recordSize*/);
heapElementManager.appendHeapFileRecord(newFreeHeapRecord);
if (heapRecord.getPositionInFile() == heapHeader
.getLastRecordPositionInFile()) {
heapHeader
.setLastRecordPositionInFile(newFreeRecordPositionInFile);
}
freeNodeTreeAppend(newFreeHeapRecord);
if (nextRecordFilePosition != -1) {
final HeapRecord nextHeapRecord = readHeapFileDataRecord(nextRecordFilePosition);
nextHeapRecord
.setPreviousRecordPositionInFile(newFreeRecordPositionInFile);
}
} else {
heapRecord.unfreeRecord(neededDataSize, nodeIdentifier);
}