use var keyword
This commit is contained in:
@@ -2,7 +2,6 @@ package org.lucares.pdb.diskstorage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileChannel.MapMode;
|
||||
import java.nio.file.Files;
|
||||
@@ -35,7 +34,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
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);
|
||||
final var src = ByteBuffer.allocate(8);
|
||||
fileChannel.write(src, 0);
|
||||
}
|
||||
}
|
||||
@@ -44,7 +43,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
|
||||
LOGGER.trace("read block={}", blockOffset);
|
||||
|
||||
final MappedByteBuffer byteBuffer = fileChannel.map(MapMode.READ_WRITE, blockOffset, blockSize);
|
||||
final var byteBuffer = fileChannel.map(MapMode.READ_WRITE, blockOffset, blockSize);
|
||||
|
||||
return new DiskBlock(blockOffset, byteBuffer);
|
||||
}
|
||||
@@ -73,7 +72,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
throw new IllegalArgumentException("The minimal allocation size is 32 byte.");
|
||||
}
|
||||
|
||||
final Optional<FreeListNode> optionalFreeBlock = findFreeBlockWithSize(blockSize);
|
||||
final var optionalFreeBlock = findFreeBlockWithSize(blockSize);
|
||||
if (optionalFreeBlock.isPresent()) {
|
||||
final FreeListNode freeBlock = optionalFreeBlock.get();
|
||||
removeBlockFromFreeList(freeBlock);
|
||||
@@ -85,24 +84,24 @@ public class DiskStorage implements AutoCloseable {
|
||||
}
|
||||
|
||||
private long allocateNewBlock(final int blockSize) throws IOException {
|
||||
final byte[] buffer = new byte[blockSize];
|
||||
final ByteBuffer src = ByteBuffer.wrap(buffer);
|
||||
final var buffer = new byte[blockSize];
|
||||
final var src = ByteBuffer.wrap(buffer);
|
||||
|
||||
// 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 blockOffset = fileChannel.size();
|
||||
final var blockOffset = fileChannel.size();
|
||||
fileChannel.write(src, fileChannel.size());
|
||||
return blockOffset;
|
||||
}
|
||||
|
||||
public synchronized void free(final long blockOffset, final int blockSize) throws IOException {
|
||||
|
||||
final Optional<FreeListNode> neighboringFreeListNode = getNeighboringFreeListNode(blockOffset);
|
||||
final var neighboringFreeListNode = getNeighboringFreeListNode(blockOffset);
|
||||
|
||||
if (neighboringFreeListNode.isPresent()) {
|
||||
// insert new free node into the free list
|
||||
final FreeListNode prev = neighboringFreeListNode.get();
|
||||
final var prev = neighboringFreeListNode.get();
|
||||
|
||||
insertFreeListNode(prev, blockOffset, blockSize);
|
||||
|
||||
@@ -113,13 +112,13 @@ public class DiskStorage implements AutoCloseable {
|
||||
}
|
||||
|
||||
private void insertFreeListNodeAsNewRoot(final long blockOffset, final int blockSize) throws IOException {
|
||||
final long freeListRootNodePosition = readFreeListRootNodePosition();
|
||||
final var freeListRootNodePosition = readFreeListRootNodePosition();
|
||||
|
||||
if (freeListRootNodePosition > 0) {
|
||||
// there are free list nodes, but they are after the new node
|
||||
|
||||
final FreeListNode next = readFreeListNode(freeListRootNodePosition);
|
||||
final FreeListNode newNode = new FreeListNode(blockOffset, blockSize);
|
||||
final var next = readFreeListNode(freeListRootNodePosition);
|
||||
final var newNode = new FreeListNode(blockOffset, blockSize);
|
||||
|
||||
FreeListNode.link(newNode, next);
|
||||
|
||||
@@ -128,7 +127,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
|
||||
} else {
|
||||
// this is the first free list node
|
||||
final FreeListNode newNode = new FreeListNode(blockOffset, blockSize);
|
||||
final var newNode = new FreeListNode(blockOffset, blockSize);
|
||||
writeFreeListNode(newNode);
|
||||
writeFreeListRootNodePosition(blockOffset);
|
||||
}
|
||||
@@ -137,8 +136,8 @@ public class DiskStorage implements AutoCloseable {
|
||||
private void insertFreeListNode(final FreeListNode prev, final long blockOffset, final int blockSize)
|
||||
throws IOException {
|
||||
|
||||
final FreeListNode newNode = new FreeListNode(blockOffset, blockSize);
|
||||
final FreeListNode next = prev.hasNext() ? readFreeListNode(prev.getNext()) : null;
|
||||
final var newNode = new FreeListNode(blockOffset, blockSize);
|
||||
final var next = prev.hasNext() ? readFreeListNode(prev.getNext()) : null;
|
||||
|
||||
FreeListNode.link(prev, newNode, next);
|
||||
|
||||
@@ -158,7 +157,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
|
||||
long nextFreeListNodeOffset = freeListRootNodePosition;
|
||||
while (nextFreeListNodeOffset > 0) {
|
||||
final FreeListNode freeListNode = readFreeListNode(nextFreeListNodeOffset);
|
||||
final var freeListNode = readFreeListNode(nextFreeListNodeOffset);
|
||||
|
||||
if (freeListNode.getOffset() > blockOffset) {
|
||||
break;
|
||||
@@ -176,7 +175,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
|
||||
long nextFreeListNodeOffset = freeListRootNodePosition;
|
||||
while (nextFreeListNodeOffset > 0) {
|
||||
final FreeListNode freeListNode = readFreeListNode(nextFreeListNodeOffset);
|
||||
final var freeListNode = readFreeListNode(nextFreeListNodeOffset);
|
||||
|
||||
if (freeListNode.getSize() == blockSize) {
|
||||
result = freeListNode;
|
||||
@@ -189,7 +188,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
}
|
||||
|
||||
private void clearBlock(final FreeListNode freeBlock) throws IOException {
|
||||
final ByteBuffer src = ByteBuffer.allocate(freeBlock.getSize());
|
||||
final var src = ByteBuffer.allocate(freeBlock.getSize());
|
||||
fileChannel.write(src, freeBlock.getOffset());
|
||||
}
|
||||
|
||||
@@ -213,7 +212,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
}
|
||||
|
||||
private FreeListNode readFreeListNode(final long freeListNodePosition) throws IOException {
|
||||
final ByteBuffer freeListNode = ByteBuffer.allocate(FREE_LIST_NODE_SIZE);
|
||||
final var freeListNode = ByteBuffer.allocate(FREE_LIST_NODE_SIZE);
|
||||
fileChannel.read(freeListNode, freeListNodePosition);
|
||||
final long offset = freeListNodePosition;
|
||||
final long next = freeListNode.getLong(FREE_LIST_NEXT_POINTER);
|
||||
@@ -226,7 +225,7 @@ public class DiskStorage implements AutoCloseable {
|
||||
|
||||
for (final FreeListNode node : nodes) {
|
||||
if (node != null) {
|
||||
final ByteBuffer src = ByteBuffer.allocate(FREE_LIST_NODE_SIZE);
|
||||
final var src = ByteBuffer.allocate(FREE_LIST_NODE_SIZE);
|
||||
src.putLong(FREE_LIST_NEXT_POINTER, node.getNext());
|
||||
src.putLong(FREE_LIST_PREV_POINTER, node.getPrev());
|
||||
src.putInt(FREE_LIST_SIZE, node.getSize());
|
||||
@@ -236,13 +235,13 @@ public class DiskStorage implements AutoCloseable {
|
||||
}
|
||||
|
||||
private long readFreeListRootNodePosition() throws IOException {
|
||||
final ByteBuffer freeListFirstBlock = ByteBuffer.allocate(8);
|
||||
final var freeListFirstBlock = ByteBuffer.allocate(8);
|
||||
fileChannel.read(freeListFirstBlock, FREE_LIST_ROOT_OFFSET);
|
||||
return freeListFirstBlock.getLong(0);
|
||||
}
|
||||
|
||||
private void writeFreeListRootNodePosition(final long freeListRootNodePosition) throws IOException {
|
||||
final ByteBuffer freeListFirstBlock = ByteBuffer.allocate(8);
|
||||
final var freeListFirstBlock = ByteBuffer.allocate(8);
|
||||
freeListFirstBlock.putLong(0, freeListRootNodePosition);
|
||||
fileChannel.write(freeListFirstBlock, FREE_LIST_ROOT_OFFSET);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user