* @return a pair file-id/file-pos
* @throws IOException
*/
protected long[] allocateSpace(final int iRecordSize) throws IOException {
// IT'S PREFEREABLE TO FIND SPACE WITHOUT ENLARGE ANY FILES: FIND THE FIRST FILE WITH FREE SPACE TO USE
OFile file;
for (int i = 0; i < files.length; ++i) {
file = files[i];
if (file.getFreeSpace() >= iRecordSize)
// FOUND: RETURN THIS OFFSET
return new long[] { i, file.allocateSpace(iRecordSize) };
}
// NOT FOUND: CHECK IF CAN OVERSIZE SOME FILES
for (int i = 0; i < files.length; ++i) {
file = files[i];
if (file.canOversize(iRecordSize)) {
// FOUND SPACE: ENLARGE IT
return new long[] { i, file.allocateSpace(iRecordSize) };
}
}
// TRY TO CREATE A NEW FILE
if (maxSize > 0 && getSize() >= maxSize)
// OUT OF MAX SIZE
throw new OStorageException("Unable to allocate the requested space of " + iRecordSize
+ " bytes because the segment is full: max-Size=" + maxSize + ", currentSize=" + getFilledUpTo());
// COPY THE OLD ARRAY TO THE NEW ONE
OFile[] newFiles = new OFile[files.length + 1];
for (int i = 0; i < files.length; ++i)
newFiles[i] = files[i];
files = newFiles;
// CREATE THE NEW FILE AND PUT IT AS LAST OF THE ARRAY
file = createNewFile();
file.allocateSpace(iRecordSize);
config.root.update();
return new long[] { files.length - 1, 0 };
}