Query optimizations

master
Andrea Cavalli 3 years ago
parent dc56da3bbb
commit d047d1b24a
  1. 18
      src/main/java/it/cavallium/strangedb/database/blocks/DatabaseBlocksIO.java
  2. 11
      src/main/java/it/cavallium/strangedb/database/references/DatabaseReferencesIO.java

@ -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() {

@ -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);
}
}

Loading…
Cancel
Save