ensure BSFiles use blocks that are aligned to 512 Byte offsets

This commit is contained in:
2018-10-14 09:00:26 +02:00
parent a2520c0238
commit bd88c63aff
3 changed files with 26 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ public class DiskStorage implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(DiskStorage.class);
private static final long FREE_LIST_ROOT_OFFSET = 0;
private static final long NO_POINTER = 0;
private static final int FREE_LIST_NEXT_POINTER = 0;
private static final int FREE_LIST_PREV_POINTER = 8;
private static final int FREE_LIST_SIZE = 16;
@@ -31,11 +32,13 @@ public class DiskStorage implements AutoCloseable {
fileChannel = FileChannel.open(databaseFile, StandardOpenOption.READ, StandardOpenOption.WRITE,
StandardOpenOption.CREATE);
initIfNew();
}
private void initIfNew() throws IOException {
if (fileChannel.size() == 0) {
// file is new -> add root of the free list
// TODO implement a real free list
final var src = ByteBuffer.allocate(8);
fileChannel.write(src, 0);
writeFreeListRootNodePosition(NO_POINTER);
}
}
@@ -233,4 +236,14 @@ public class DiskStorage implements AutoCloseable {
freeListFirstBlock.putLong(0, freeListRootNodePosition);
fileChannel.write(freeListFirstBlock, FREE_LIST_ROOT_OFFSET);
}
public synchronized void ensureAlignmentForNewBlocks(final int alignment) throws IOException {
final long size = fileChannel.size();
final int alignmentMismatch = Math.floorMod(size, alignment);
if (alignmentMismatch != 0) {
// The next allocated block would not be aligned. Therefore we allocate a
// throw-away block.
allocateNewBlock(alignment - alignmentMismatch);
}
}
}