Skip to content

Commit 0324785

Browse files
fix: Added throws IOException to propagate exceptions upstream
Signed-off-by: Matt Peterson <[email protected]>
1 parent f246ff2 commit 0324785

File tree

7 files changed

+26
-30
lines changed

7 files changed

+26
-30
lines changed

server/src/main/java/com/hedera/block/server/persistence/BlockPersistenceHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@ public interface BlockPersistenceHandler<U, V> {
3232
void persist(final U blockItem) throws IOException;
3333

3434
/**
35-
* Reads a block.
35+
* Retrieves a block using the block number.
3636
*
3737
* @param blockNumber the number of the block to read
3838
* @return an Optional of the block
3939
*/
40-
Optional<V> read(final long blockNumber);
40+
Optional<V> retrieve(final long blockNumber) throws IOException;
4141

4242
/**
43-
* Reads a range of blocks.
43+
* Retrieves an ordered collection of items (e.g. Blocks) within a range.
4444
*
4545
* @param startBlockNumber the id of the first block to read
4646
* @param endBlockNumber the id of the last block to read
4747
* @return a queue of blocks
4848
*/
49-
Queue<V> readRange(final long startBlockNumber, final long endBlockNumber);
49+
Queue<V> retrieveRange(final long startBlockNumber, final long endBlockNumber)
50+
throws IOException;
5051
}

server/src/main/java/com/hedera/block/server/persistence/WriteThroughCacheHandler.java renamed to server/src/main/java/com/hedera/block/server/persistence/FileSystemPersistenceHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ public WriteThroughCacheHandler(
5050
* @return a queue of blocks
5151
*/
5252
@Override
53-
public Queue<Block> readRange(final long startBlockId, final long endBlockId) {
53+
public Queue<Block> retrieveRange(final long startBlockId, final long endBlockId)
54+
throws IOException {
5455
final Queue<Block> blocks = new LinkedList<>();
5556
for (long count = startBlockId; count <= endBlockId; count++) {
56-
final Optional<Block> blockOpt = read(count);
57+
final Optional<Block> blockOpt = retrieve(count);
5758
blockOpt.ifPresent(blocks::add);
5859
}
5960

@@ -68,7 +69,7 @@ public Queue<Block> readRange(final long startBlockId, final long endBlockId) {
6869
* @return an Optional with the block
6970
*/
7071
@Override
71-
public Optional<Block> read(final long id) {
72+
public Optional<Block> retrieve(final long id) throws IOException {
7273
return blockReader.read(id);
7374
}
7475

server/src/main/java/com/hedera/block/server/persistence/storage/BlockAsDirReader.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public BlockAsDirReader(final String key, final Config config) {
4747
this.blockNodeRootPath = blockNodeRootPath;
4848
}
4949

50-
public Optional<Block> read(final long blockNumber) {
50+
public Optional<Block> read(final long blockNumber) throws IOException {
5151

5252
// Construct the path to the requested Block
5353
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber));
@@ -66,17 +66,10 @@ public Optional<Block> read(final long blockNumber) {
6666
final Builder builder = Block.newBuilder();
6767
for (int i = 1; ; i++) {
6868
final Path blockItemPath = blockPath.resolve(i + BLOCK_FILE_EXTENSION);
69-
try {
70-
final Optional<BlockItem> blockItemOpt = readBlockItem(blockItemPath.toString());
71-
if (blockItemOpt.isPresent()) {
72-
builder.addBlockItems(blockItemOpt.get());
73-
continue;
74-
}
75-
} catch (IOException io) {
76-
// Return an empty Optional signaling an error. It's all or nothing
77-
// when retrieving a Block
78-
LOGGER.log(System.Logger.Level.ERROR, "Error reading file: " + blockItemPath, io);
79-
return Optional.empty();
69+
final Optional<BlockItem> blockItemOpt = readBlockItem(blockItemPath.toString());
70+
if (blockItemOpt.isPresent()) {
71+
builder.addBlockItems(blockItemOpt.get());
72+
continue;
8073
}
8174

8275
break;

server/src/main/java/com/hedera/block/server/persistence/storage/BlockReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
package com.hedera.block.server.persistence.storage;
1818

19+
import java.io.IOException;
1920
import java.util.Optional;
2021

2122
public interface BlockReader<V> {
22-
Optional<V> read(final long blockNumber);
23+
Optional<V> read(final long blockNumber) throws IOException;
2324
}

server/src/test/java/com/hedera/block/server/persistence/WriteThroughCacheHandlerTest.java renamed to server/src/test/java/com/hedera/block/server/persistence/FileSystemPersistenceHandlerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ private static void persistBlockItems(
9191
}
9292

9393
private static void verifyBlocks(
94-
int numOfBlocks, BlockPersistenceHandler<BlockItem, Block> blockPersistenceHandler) {
94+
int numOfBlocks, BlockPersistenceHandler<BlockItem, Block> blockPersistenceHandler)
95+
throws IOException {
9596

9697
for (int i = 1; i <= numOfBlocks; i++) {
97-
Optional<Block> blockOpt = blockPersistenceHandler.read(i);
98+
Optional<Block> blockOpt = blockPersistenceHandler.retrieve(i);
9899
if (blockOpt.isEmpty()) {
99100
fail("Block not found: " + i);
100101
} else {

server/src/test/java/com/hedera/block/server/persistence/RangeTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public void testReadRangeWithEvenEntries() throws IOException {
5050
}
5151

5252
@Test
53-
public void testReadRangeWithNoBlocks() {
53+
public void testReadRangeWithNoBlocks() throws IOException {
5454

5555
BlockPersistenceHandler<BlockItem, Block> blockPersistenceHandler =
5656
generateInMemoryTestBlockPersistenceHandler();
57-
Queue<Block> results = blockPersistenceHandler.readRange(1, 100);
57+
Queue<Block> results = blockPersistenceHandler.retrieveRange(1, 100);
5858
assertNotNull(results);
5959
assertEquals(0, results.size());
6060
}
@@ -72,21 +72,21 @@ public void testReadRangeWhenBlocksLessThanWindow() throws IOException {
7272

7373
int window = 10;
7474

75-
Queue<Block> results = blockPersistenceHandler.readRange(1, window);
75+
Queue<Block> results = blockPersistenceHandler.retrieveRange(1, window);
7676
assertNotNull(results);
7777
assertEquals(numOfBlocks, results.size());
7878
}
7979

8080
private static void verifyReadRange(
8181
int window,
8282
int numOfWindows,
83-
BlockPersistenceHandler<BlockItem, Block> blockPersistenceHandler) {
83+
BlockPersistenceHandler<BlockItem, Block> blockPersistenceHandler) throws IOException {
8484

8585
for (int j = 0; j < numOfWindows; ++j) {
8686

8787
int startBlockId = (j * window) + 1;
8888
int endBlockId = (startBlockId + window) - 1;
89-
Queue<Block> results = blockPersistenceHandler.readRange(startBlockId, endBlockId);
89+
Queue<Block> results = blockPersistenceHandler.retrieveRange(startBlockId, endBlockId);
9090

9191
for (int i = startBlockId; i <= endBlockId && results.peek() != null; ++i) {
9292
Block block = results.poll();

server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirectoryTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void testWriterAndReaderHappyPath() throws IOException {
104104
}
105105

106106
@Test
107-
public void testBlockDoesNotExist() {
107+
public void testBlockDoesNotExist() throws IOException {
108108
BlockReader<Block> blockReader = new BlockAsDirReader(JUNIT, testConfig);
109109
Optional<Block> blockOpt = blockReader.read(10000);
110110
assertTrue(blockOpt.isEmpty());
@@ -137,8 +137,7 @@ public void testRemoveBlockItemReadPerms() throws IOException {
137137
removeBlockItemReadPerms(1, 1, testConfig);
138138

139139
BlockReader<Block> blockReader = new BlockAsDirReader(JUNIT, testConfig);
140-
Optional<Block> blockOpt = blockReader.read(1);
141-
assertTrue(blockOpt.isEmpty());
140+
assertThrows(IOException.class, () -> blockReader.read(1));
142141
}
143142

144143
@Test

0 commit comments

Comments
 (0)