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