use byte offsets instead of block numbers
We want to allow arbitrary allocations in DiskStorage. The first step was to change the hard coded block size into a dynamic one.
This commit is contained in:
@@ -59,6 +59,8 @@ public class BSFile implements AutoCloseable {
|
||||
|
||||
private static final TimeStampDeltaDecoder TIME_DELTA_DECODER = new TimeStampDeltaDecoder();
|
||||
|
||||
public static final int BLOCK_SIZE = 512;
|
||||
|
||||
/*
|
||||
* The last disk block of this sequence. This is the block new values will be
|
||||
* appended to.
|
||||
@@ -69,7 +71,7 @@ public class BSFile implements AutoCloseable {
|
||||
|
||||
private boolean dirty = false;
|
||||
|
||||
private final long rootBlockNumber;
|
||||
private final long rootBlockOffset;
|
||||
|
||||
private final DiskStorage diskStorage;
|
||||
|
||||
@@ -77,26 +79,26 @@ public class BSFile implements AutoCloseable {
|
||||
|
||||
private long lastEpochMilli;
|
||||
|
||||
BSFile(final long rootBlockNumber, final DiskStorage diskStorage) throws IOException {
|
||||
BSFile(final long rootBlockOffset, final DiskStorage diskStorage) throws IOException {
|
||||
|
||||
this(diskStorage.getDiskBlock(rootBlockNumber), diskStorage);
|
||||
this(diskStorage.getDiskBlock(rootBlockOffset, BLOCK_SIZE), diskStorage);
|
||||
}
|
||||
|
||||
BSFile(final DiskBlock rootDiskBlock, final DiskStorage diskStorage) throws IOException {
|
||||
|
||||
this.rootDiskBlock = rootDiskBlock;
|
||||
this.rootBlockNumber = rootDiskBlock.getBlockNumber();
|
||||
this.rootBlockOffset = rootDiskBlock.getBlockOffset();
|
||||
this.diskStorage = diskStorage;
|
||||
|
||||
final long lastBlockNumber = rootDiskBlock.getLastBlockPointer();
|
||||
if (lastBlockNumber == rootBlockNumber || lastBlockNumber == 0) {
|
||||
if (lastBlockNumber == rootBlockOffset || lastBlockNumber == 0) {
|
||||
buffer = rootDiskBlock;
|
||||
} else {
|
||||
buffer = diskStorage.getDiskBlock(lastBlockNumber);
|
||||
buffer = diskStorage.getDiskBlock(lastBlockNumber, BLOCK_SIZE);
|
||||
}
|
||||
offsetInBuffer = determineWriteOffsetInExistingBuffer(buffer);
|
||||
lastEpochMilli = determineLastEpochMilli(buffer);
|
||||
LOGGER.trace("create bsFile={} lastBlockNumber={}", rootBlockNumber, lastBlockNumber);
|
||||
LOGGER.trace("create bsFile={} lastBlockNumber={}", rootBlockOffset, lastBlockNumber);
|
||||
}
|
||||
|
||||
private long determineLastEpochMilli(final DiskBlock diskBlock) {
|
||||
@@ -136,9 +138,9 @@ public class BSFile implements AutoCloseable {
|
||||
}
|
||||
|
||||
public static BSFile newFile(final DiskStorage diskStorage) throws IOException {
|
||||
final long rootBlockNumber = diskStorage.appendNewBlock();
|
||||
LOGGER.trace("create new bsFile={}", rootBlockNumber);
|
||||
return new BSFile(rootBlockNumber, diskStorage);
|
||||
final long rootBlockOffset = diskStorage.allocateBlock(BLOCK_SIZE);
|
||||
LOGGER.trace("create new bsFile={}", rootBlockOffset);
|
||||
return new BSFile(rootBlockOffset, diskStorage);
|
||||
}
|
||||
|
||||
public void appendTimeValue(final long epochMilli, final long value) throws IOException {
|
||||
@@ -176,33 +178,31 @@ public class BSFile implements AutoCloseable {
|
||||
|
||||
private void flushFullBufferAndCreateNew() throws IOException {
|
||||
|
||||
final long start = System.nanoTime();
|
||||
final long newBlockNumber = diskStorage.appendNewBlock();
|
||||
final long newBlockOffset = diskStorage.allocateBlock(BLOCK_SIZE);
|
||||
|
||||
if (buffer == rootDiskBlock) {
|
||||
// root block and current block are the same, so we need
|
||||
// to update only one
|
||||
buffer.setLastBlockNumber(newBlockNumber);
|
||||
buffer.setNextBlockNumber(newBlockNumber);
|
||||
buffer.setLastBlockOffset(newBlockOffset);
|
||||
buffer.setNextBlockOffset(newBlockOffset);
|
||||
buffer.writeAsync();
|
||||
} else {
|
||||
rootDiskBlock.writeLastBlockNumber(newBlockNumber);
|
||||
rootDiskBlock.writeLastBlockOffset(newBlockOffset);
|
||||
|
||||
buffer.setNextBlockNumber(newBlockNumber);
|
||||
buffer.setNextBlockOffset(newBlockOffset);
|
||||
buffer.writeAsync();
|
||||
}
|
||||
|
||||
// set the new buffer
|
||||
buffer = diskStorage.getDiskBlock(newBlockNumber);
|
||||
buffer = diskStorage.getDiskBlock(newBlockOffset, BLOCK_SIZE);
|
||||
offsetInBuffer = 0;
|
||||
dirty = false;
|
||||
LOGGER.trace("flushFullBufferAndCreateNew bsFile={} newBlock={}: {}ms", rootBlockNumber, newBlockNumber,
|
||||
(System.nanoTime() - start) / 1_000_000.0);
|
||||
LOGGER.trace("flushFullBufferAndCreateNew bsFile={} newBlock={}", rootBlockOffset, newBlockOffset);
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
|
||||
LOGGER.trace("flush bsFile={} dirty={}", rootBlockNumber, dirty);
|
||||
LOGGER.trace("flush bsFile={} dirty={}", rootBlockOffset, dirty);
|
||||
if (dirty) {
|
||||
buffer.writeAsync();
|
||||
}
|
||||
@@ -224,7 +224,7 @@ public class BSFile implements AutoCloseable {
|
||||
}
|
||||
|
||||
public Stream<LongList> streamOfLongLists() {
|
||||
final Iterator<LongList> iterator = new LongListIterator(rootBlockNumber, diskStorage);
|
||||
final Iterator<LongList> iterator = new LongListIterator(rootBlockOffset, diskStorage);
|
||||
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
|
||||
}
|
||||
|
||||
@@ -236,29 +236,29 @@ public class BSFile implements AutoCloseable {
|
||||
private static class LongListIterator implements Iterator<LongList> {
|
||||
|
||||
private LongList next = null;
|
||||
private long nextBlockNumber;
|
||||
private long nextBlockOffset;
|
||||
|
||||
private final DiskStorage diskStorage;
|
||||
|
||||
public LongListIterator(final long nextBlockNumber, final DiskStorage diskStorage) {
|
||||
this.nextBlockNumber = nextBlockNumber;
|
||||
this.nextBlockOffset = nextBlockNumber;
|
||||
this.diskStorage = diskStorage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextBlockNumber != DiskBlock.NO_NEXT_POINTER;
|
||||
return nextBlockOffset != DiskBlock.NO_NEXT_POINTER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongList next() {
|
||||
try {
|
||||
if (nextBlockNumber == DiskBlock.NO_NEXT_POINTER) {
|
||||
if (nextBlockOffset == DiskBlock.NO_NEXT_POINTER) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
final DiskBlock diskBlock = diskStorage.getDiskBlock(nextBlockNumber);
|
||||
nextBlockNumber = diskBlock.getNextBlockNumber();
|
||||
final DiskBlock diskBlock = diskStorage.getDiskBlock(nextBlockOffset, BLOCK_SIZE);
|
||||
nextBlockOffset = diskBlock.getNextBlockNumber();
|
||||
|
||||
final byte[] buf = diskBlock.getBuffer();
|
||||
next = VariableByteEncoder.decode(buf);
|
||||
@@ -283,9 +283,9 @@ public class BSFile implements AutoCloseable {
|
||||
return result;
|
||||
}
|
||||
|
||||
public long getRootBlockNumber() {
|
||||
public long getRootBlockOffset() {
|
||||
|
||||
return rootBlockNumber;
|
||||
return rootBlockOffset;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,14 +19,14 @@ public class DiskBlock {
|
||||
+ 8; // last block pointer;
|
||||
|
||||
private byte[] buffer = null;
|
||||
private final long blockNumber;
|
||||
private long nextBlockNumber = 0;
|
||||
private long lastBlockNumber = 0;
|
||||
private final long blockOffset;
|
||||
private long nextBlockOffset = 0;
|
||||
private long lastBlockOffset = 0;
|
||||
|
||||
private final MappedByteBuffer byteBuffer;
|
||||
|
||||
public DiskBlock(final long blockNumber, final MappedByteBuffer byteBuffer) {
|
||||
this.blockNumber = blockNumber;
|
||||
public DiskBlock(final long blockOffset, final MappedByteBuffer byteBuffer) {
|
||||
this.blockOffset = blockOffset;
|
||||
this.byteBuffer = byteBuffer;
|
||||
}
|
||||
|
||||
@@ -41,12 +41,12 @@ public class DiskBlock {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public long getBlockNumber() {
|
||||
return blockNumber;
|
||||
public long getBlockOffset() {
|
||||
return blockOffset;
|
||||
}
|
||||
|
||||
public void setNextBlockNumber(final long nextBlockNumber) {
|
||||
this.nextBlockNumber = nextBlockNumber;
|
||||
public void setNextBlockOffset(final long nextBlockOffset) {
|
||||
this.nextBlockOffset = nextBlockOffset;
|
||||
}
|
||||
|
||||
private void writeBufferToByteBuffer() {
|
||||
@@ -55,8 +55,8 @@ public class DiskBlock {
|
||||
}
|
||||
|
||||
private void writeBlockHeader() {
|
||||
byteBuffer.putLong(NEXT_POINTER_OFFSET, nextBlockNumber);
|
||||
byteBuffer.putLong(LAST_BLOCK_POINTER_POSITION, lastBlockNumber);
|
||||
byteBuffer.putLong(NEXT_POINTER_OFFSET, nextBlockOffset);
|
||||
byteBuffer.putLong(LAST_BLOCK_POINTER_POSITION, lastBlockOffset);
|
||||
}
|
||||
|
||||
public void writeAsync() {
|
||||
@@ -64,43 +64,43 @@ public class DiskBlock {
|
||||
writeBlockHeader();
|
||||
writeBufferToByteBuffer();
|
||||
final long duration = System.nanoTime() - start;
|
||||
LOGGER.trace("write() of block={}: {}ms", blockNumber, duration / 1_000_000.0);
|
||||
LOGGER.trace("write() of block={}: {}ms", blockOffset, duration / 1_000_000.0);
|
||||
}
|
||||
|
||||
public void force() {
|
||||
final long start = System.nanoTime();
|
||||
byteBuffer.force();
|
||||
LOGGER.trace("force of block={}: {}ms", blockNumber, (System.nanoTime() - start) / 1_000_000.0);
|
||||
LOGGER.trace("force of block={}: {}ms", blockOffset, (System.nanoTime() - start) / 1_000_000.0);
|
||||
}
|
||||
|
||||
public long getLastBlockPointer() {
|
||||
|
||||
if (lastBlockNumber <= 0) {
|
||||
lastBlockNumber = byteBuffer.getLong(LAST_BLOCK_POINTER_POSITION);
|
||||
if (lastBlockOffset <= 0) {
|
||||
lastBlockOffset = byteBuffer.getLong(LAST_BLOCK_POINTER_POSITION);
|
||||
}
|
||||
|
||||
return lastBlockNumber;
|
||||
return lastBlockOffset;
|
||||
}
|
||||
|
||||
public long getNextBlockNumber() {
|
||||
if (nextBlockNumber <= 0) {
|
||||
nextBlockNumber = byteBuffer.getLong(NEXT_POINTER_OFFSET);
|
||||
if (nextBlockOffset <= 0) {
|
||||
nextBlockOffset = byteBuffer.getLong(NEXT_POINTER_OFFSET);
|
||||
}
|
||||
return nextBlockNumber;
|
||||
return nextBlockOffset;
|
||||
}
|
||||
|
||||
public void setLastBlockNumber(final long lastBlockNumber) {
|
||||
this.lastBlockNumber = lastBlockNumber;
|
||||
public void setLastBlockOffset(final long lastBlockOffset) {
|
||||
this.lastBlockOffset = lastBlockOffset;
|
||||
}
|
||||
|
||||
public void writeLastBlockNumber(final long lastBlockNumber) {
|
||||
this.lastBlockNumber = lastBlockNumber;
|
||||
byteBuffer.putLong(LAST_BLOCK_POINTER_POSITION, lastBlockNumber);
|
||||
public void writeLastBlockOffset(final long lastBlockOffset) {
|
||||
this.lastBlockOffset = lastBlockOffset;
|
||||
byteBuffer.putLong(LAST_BLOCK_POINTER_POSITION, lastBlockOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final LongList bufferDecoded = VariableByteEncoder.decode(buffer);
|
||||
return "DiskBlock[" + blockNumber + ", bufferDecoded=" + bufferDecoded + "]";
|
||||
return "DiskBlock[" + blockOffset + ", bufferDecoded=" + bufferDecoded + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@ public class DiskStorage implements AutoCloseable {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DiskStorage.class);
|
||||
|
||||
public static final int BLOCK_SIZE = 512;
|
||||
|
||||
private final FileChannel fileChannel;
|
||||
|
||||
public DiskStorage(final Path databaseFile) throws IOException {
|
||||
@@ -27,25 +25,27 @@ public class DiskStorage implements AutoCloseable {
|
||||
|
||||
fileChannel = FileChannel.open(databaseFile, StandardOpenOption.READ, StandardOpenOption.WRITE,
|
||||
StandardOpenOption.CREATE);
|
||||
|
||||
if (fileChannel.size() == 0) {
|
||||
// file is new -> add root of the free list
|
||||
// TODO implement a real free list
|
||||
final ByteBuffer src = ByteBuffer.allocate(8);
|
||||
fileChannel.write(src, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public DiskBlock getDiskBlock(final long blockNumber) throws IOException {
|
||||
|
||||
// block numbers start with 1, so that the uninitialized value
|
||||
// (0) means 'no block'. That way we do not have to write data to a newly
|
||||
// created block, which reduces IO.
|
||||
final long position = (blockNumber - 1) * BLOCK_SIZE;
|
||||
public DiskBlock getDiskBlock(final long blockOffset, final int blockSize) throws IOException {
|
||||
|
||||
final long start = System.nanoTime();
|
||||
|
||||
try (final FileLock lock = fileChannel.lock(position, BLOCK_SIZE, true)) {
|
||||
try (final FileLock lock = fileChannel.lock(blockOffset, blockSize, true)) {
|
||||
|
||||
final MappedByteBuffer byteBuffer = fileChannel.map(MapMode.READ_WRITE, position, BLOCK_SIZE);
|
||||
final MappedByteBuffer byteBuffer = fileChannel.map(MapMode.READ_WRITE, blockOffset, blockSize);
|
||||
|
||||
return new DiskBlock(blockNumber, byteBuffer);
|
||||
return new DiskBlock(blockOffset, byteBuffer);
|
||||
|
||||
} finally {
|
||||
LOGGER.trace("read block={}: {}ms", blockNumber, (System.nanoTime() - start) / 1_000_000.0);
|
||||
LOGGER.trace("read block={}: {}ms", blockOffset, (System.nanoTime() - start) / 1_000_000.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,40 +55,30 @@ public class DiskStorage implements AutoCloseable {
|
||||
fileChannel.close();
|
||||
}
|
||||
|
||||
public long getNumBlocks() throws IOException {
|
||||
return fileChannel.size() / BLOCK_SIZE;
|
||||
}
|
||||
|
||||
public long[] appendNewBlocks(final int numNewBlocks) throws IOException {
|
||||
public long[] allocateBlocks(final int numNewBlocks, final int blockSize) throws IOException {
|
||||
|
||||
final long[] result = new long[numNewBlocks];
|
||||
synchronized (fileChannel) {
|
||||
for (int i = 0; i < numNewBlocks; i++) {
|
||||
final long blockNumber = appendNewBlock();
|
||||
result[i] = blockNumber;
|
||||
final long blockOffset = allocateBlock(blockSize);
|
||||
result[i] = blockOffset;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public long appendNewBlock() throws IOException {
|
||||
public long allocateBlock(final int blockSize) throws IOException {
|
||||
|
||||
final byte[] buffer = new byte[BLOCK_SIZE];
|
||||
final byte[] buffer = new byte[blockSize];
|
||||
final ByteBuffer src = ByteBuffer.wrap(buffer);
|
||||
|
||||
synchronized (fileChannel) {
|
||||
// block numbers start with 1, so that the uninitialized value
|
||||
// (0) means 'no block'. That way we do not have to write
|
||||
// data to a newly created block, which reduces IO.
|
||||
final long blockNumber = getNumBlocks() + 1;
|
||||
final long blockOffset = fileChannel.size();
|
||||
fileChannel.write(src, fileChannel.size());
|
||||
return blockNumber;
|
||||
return blockOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public DiskBlock getNewBlock() throws IOException {
|
||||
final long blockNumber = appendNewBlock();
|
||||
return getDiskBlock(blockNumber);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user