Query optimizations

This commit is contained in:
Andrea Cavalli 2019-04-22 00:10:10 +02:00
parent dc56da3bbb
commit d047d1b24a
2 changed files with 29 additions and 0 deletions

View File

@ -55,6 +55,24 @@ public class DatabaseBlocksIO implements IBlocksIO {
return fileIO.readAt(blockInfo.getIndex(), blockInfo.getSize());
}
public ByteBuffer readBlockSizeAndLastElementOfReferencesList(long blockId) throws IOException {
if (blockId == EMPTY_BLOCK_ID) {
return ByteBuffer.wrap(new byte[0]);
}
if (blockId == ERROR_BLOCK_ID) {
throw new IOException("Errored block id");
}
if (blockId < 0) {
throw new IOException("Block id " + blockId + " is not valid");
}
BlockInfo blockInfo = blocksMetadata.getBlockInfo(blockId);
if (blockInfo.getSize() >= Integer.BYTES * 2 + Long.BYTES) {
return fileIO.readAt(blockInfo.getIndex() + blockInfo.getSize() - (Integer.BYTES + Long.BYTES), Integer.BYTES + Long.BYTES);
} else {
return fileIO.readAt(blockInfo.getIndex(), blockInfo.getSize());
}
}
@Override
public void close() {

View File

@ -53,4 +53,15 @@ public class DatabaseReferencesIO implements IReferencesIO {
}
return blocksIO.readBlock(blockId);
}
public ByteBuffer readFromReferenceSizeAndLastElementOfReferencesList(long reference) throws IOException {
long blockId;
lock.readLock().lock();
try {
blockId = referencesMetadata.getReferenceBlockId(reference);
} finally {
lock.readLock().unlock();
}
return blocksIO.readBlockSizeAndLastElementOfReferencesList(blockId);
}
}