Skip to content

Commit

Permalink
fix: spotless
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Jul 16, 2024
1 parent 84240b1 commit 04dd9c5
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 84 deletions.
7 changes: 3 additions & 4 deletions server/src/main/java/com/hedera/block/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.hedera.block.server;

import static com.hedera.block.protos.BlockStreamService.*;
import static com.hedera.block.server.Constants.*;

import com.hedera.block.server.mediator.LiveStreamMediatorImpl;
import com.hedera.block.server.persistence.WriteThroughCacheHandler;
import com.hedera.block.server.persistence.storage.BlockStorage;
Expand All @@ -25,12 +28,8 @@
import io.helidon.config.Config;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.grpc.GrpcRouting;

import java.io.IOException;

import static com.hedera.block.protos.BlockStreamService.*;
import static com.hedera.block.server.Constants.*;

/** Main class for the block node server */
public class Server {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class ConsumerBlockItemObserver

private final StreamMediator<ObjectEvent<BlockItem>, SubscribeStreamRequest> streamMediator;


/**
* Constructor for the LiveStreamObserverImpl class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.hedera.block.server.mediator;

import static com.hedera.block.protos.BlockStreamService.*;

import com.hedera.block.server.consumer.BlockItemEventHandler;
import com.hedera.block.server.data.ObjectEvent;
import com.hedera.block.server.persistence.BlockPersistenceHandler;
Expand All @@ -24,14 +26,11 @@
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.hedera.block.protos.BlockStreamService.*;

/**
* LiveStreamMediatorImpl is the implementation of the StreamMediator interface. It is responsible
* for managing the subscribe and unsubscribe operations of downstream consumers. It also proxies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
import java.util.Queue;

/**
* The BlockPersistenceHandler interface defines operations to persist and read blocks.
* The interface is used to abstract underlying storage mechanisms.
* The BlockPersistenceHandler interface defines operations to persist and read blocks. The
* interface is used to abstract underlying storage mechanisms.
*
* @param <V> the type of block to persist
*/
public interface BlockPersistenceHandler<U, V> {

/**
* Persists a block.
*
*/
/** Persists a block. */
void persist(final V blockItem);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package com.hedera.block.server.persistence;

import com.hedera.block.server.persistence.storage.BlockStorage;

import static com.hedera.block.protos.BlockStreamService.Block;
import static com.hedera.block.protos.BlockStreamService.BlockItem;

import com.hedera.block.server.persistence.storage.BlockStorage;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Queue;
Expand All @@ -42,10 +41,7 @@ public WriteThroughCacheHandler(final BlockStorage<Block, BlockItem> blockStorag
this.blockStorage = blockStorage;
}

/**
* Persists the block to the block storage and cache the block.
*
*/
/** Persists the block to the block storage and cache the block. */
@Override
public void persist(final BlockItem blockItem) {
blockStorage.write(blockItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
*/
public interface BlockStorage<U, V> {

/**
* Writes a block to storage.
*/
/** Writes a block to storage. */
void write(final V blockItem);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

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

import io.helidon.config.Config;
import static com.hedera.block.protos.BlockStreamService.Block;
import static com.hedera.block.protos.BlockStreamService.Block.Builder;
import static com.hedera.block.protos.BlockStreamService.BlockItem;
import static com.hedera.block.server.Constants.BLOCKNODE_STORAGE_ROOT_PATH_KEY;

import io.helidon.config.Config;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand All @@ -26,11 +30,6 @@
import java.nio.file.Path;
import java.util.Optional;

import static com.hedera.block.protos.BlockStreamService.Block;
import static com.hedera.block.protos.BlockStreamService.Block.Builder;
import static com.hedera.block.protos.BlockStreamService.BlockItem;
import static com.hedera.block.server.Constants.BLOCKNODE_STORAGE_ROOT_PATH_KEY;

/**
* The FileSystemBlockStorage class implements the BlockStorage interface to store blocks to the
* filesystem.
Expand Down Expand Up @@ -79,18 +78,18 @@ public FileSystemBlockStorage(final String key, final Config config) throws IOEx
}
}

/**
* Writes a block to the filesystem.
*
*/
/** Writes a block to the filesystem. */
@Override
public void write(final BlockItem blockItem) {

try {
final String blockItemFilePath = getAbsoluteFilePath(blockItem);
try (FileOutputStream fos = new FileOutputStream(blockItemFilePath)) {
blockItem.writeTo(fos);
LOGGER.log(System.Logger.Level.INFO, "Successfully wrote the block item file: {0}", blockItemFilePath);
LOGGER.log(
System.Logger.Level.INFO,
"Successfully wrote the block item file: {0}",
blockItemFilePath);
} catch (IOException e) {
LOGGER.log(System.Logger.Level.ERROR, "Error writing the protobuf to a file", e);
}
Expand Down Expand Up @@ -118,7 +117,7 @@ private Optional<Block> read(final Path blockPath, final Builder builder) {
// Directly count and add BlockItems into the Block
// to keep the retrieval process O(BlockItems)
boolean isEnd = false;
for (int i = 1;!isEnd;i++) {
for (int i = 1; !isEnd; i++) {
final Path blockItemPath = blockPath.resolve(i + BLOCK_FILE_EXTENSION);
final Optional<BlockItem> blockItemOpt = readBlockItem(blockItemPath.toString());
if (blockItemOpt.isPresent()) {
Expand Down Expand Up @@ -177,12 +176,12 @@ private void createPath(Path blockNodePath) throws IOException {
}
}

// private String resolvePath(final long blockNumber) {
//
// String fileName = blockNumber + BLOCK_FILE_EXTENSION;
// Path fullPath = blockNodeRootPath.resolve(fileName);
// LOGGER.log(System.Logger.Level.DEBUG, "Resolved fullPath: " + fullPath);
//
// return fullPath.toString();
// }
// private String resolvePath(final long blockNumber) {
//
// String fileName = blockNumber + BLOCK_FILE_EXTENSION;
// Path fullPath = blockNodeRootPath.resolve(fileName);
// LOGGER.log(System.Logger.Level.DEBUG, "Resolved fullPath: " + fullPath);
//
// return fullPath.toString();
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

package com.hedera.block.server.persistence;

import com.hedera.block.protos.BlockStreamService;
import static com.hedera.block.protos.BlockStreamService.*;
import static com.hedera.block.protos.BlockStreamService.Block.Builder;

import com.hedera.block.protos.BlockStreamService;
import java.util.ArrayList;
import java.util.List;

import static com.hedera.block.protos.BlockStreamService.*;
import static com.hedera.block.protos.BlockStreamService.Block.Builder;

public final class PersistTestUtils {

private PersistTestUtils() {}
Expand All @@ -40,20 +39,24 @@ public static List<Block> generateBlocks(int numOfBlocks) {
blockItems.add(
BlockItem.newBuilder()
.setBlockHeader(
BlockStreamService.BlockHeader.newBuilder().setBlockNumber(i + 1).build()
).setValue("block-item-" + (j + 1)).build());
BlockStreamService.BlockHeader.newBuilder()
.setBlockNumber(i + 1)
.build())
.setValue("block-item-" + (j + 1))
.build());
break;
case 9:
blockItems.add(
BlockItem.newBuilder()
.setStateProof(
BlockStreamService.BlockProof.newBuilder().setBlock(i + 1).build()
).build());
BlockStreamService.BlockProof.newBuilder()
.setBlock(i + 1)
.build())
.build());
break;
default:
blockItems.add(
BlockItem.newBuilder()
.setValue("block-item-" + (j + 1)).build());
BlockItem.newBuilder().setValue("block-item-" + (j + 1)).build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@

package com.hedera.block.server.persistence;

import com.hedera.block.server.persistence.storage.BlockStorage;
import org.junit.jupiter.api.Test;

import java.util.*;

import static com.hedera.block.protos.BlockStreamService.Block;
import static com.hedera.block.protos.BlockStreamService.Block.Builder;
import static com.hedera.block.protos.BlockStreamService.BlockItem;
import static com.hedera.block.server.persistence.PersistTestUtils.generateBlocks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.hedera.block.server.persistence.storage.BlockStorage;
import java.util.*;
import org.junit.jupiter.api.Test;

public class RangeTest {

@Test
Expand Down Expand Up @@ -102,7 +101,8 @@ private static void verifyReadRange(
}
}

private static BlockPersistenceHandler<Block, BlockItem> generateInMemoryTestBlockPersistenceHandler(int maxEntries) {
private static BlockPersistenceHandler<Block, BlockItem>
generateInMemoryTestBlockPersistenceHandler(int maxEntries) {

// Mock up a simple, in-memory persistence handler
BlockStorage<Block, BlockItem> blockStorage = new NoOpTestBlockStorage();
Expand All @@ -124,15 +124,17 @@ public void write(BlockItem blockItem) {
index = blockItem.getBlockHeader().getBlockNumber();
}

cache.compute(index, (k, v) -> {
if (v == null) {
v = new ArrayList<>();
}
cache.compute(
index,
(k, v) -> {
if (v == null) {
v = new ArrayList<>();
}

v.add(blockItem);
v.add(blockItem);

return v;
});
return v;
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@

package com.hedera.block.server.persistence;

import static com.hedera.block.protos.BlockStreamService.Block;
import static com.hedera.block.protos.BlockStreamService.BlockItem;
import static com.hedera.block.server.persistence.PersistTestUtils.generateBlocks;
import static com.hedera.block.server.persistence.Util.getBlockNumber;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.hedera.block.server.persistence.storage.FileSystemBlockStorage;
import com.hedera.block.server.util.TestUtils;
import io.helidon.config.Config;
import io.helidon.config.MapConfigSource;
import io.helidon.config.spi.ConfigSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import static com.hedera.block.protos.BlockStreamService.Block;
import static com.hedera.block.protos.BlockStreamService.BlockItem;
import static com.hedera.block.server.persistence.PersistTestUtils.generateBlocks;
import static com.hedera.block.server.persistence.Util.getBlockNumber;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class WriteThroughCacheHandlerTest {

Expand Down Expand Up @@ -85,7 +84,7 @@ private static void verifyPersistenceHandler(
for (Block block : blocks) {
final long blockNumber = getBlockNumber(block);
List<BlockItem> blockItems = block.getBlockItemsList();
for (int i = 0;i < blockItems.size();i++) {
for (int i = 0; i < blockItems.size(); i++) {

// Persist the blockItem
BlockItem blockItem = blockItems.get(i);
Expand All @@ -98,16 +97,13 @@ private static void verifyPersistenceHandler(
}

private static void verifyFileExists(
final BlockItem blockItem,
final int index,
final long blockNumber,
final Path testPath)
final BlockItem blockItem, final int index, final long blockNumber, final Path testPath)
throws IOException {

// Verify the block was saved on the filesystem
Path fullTestPath = testPath
.resolve(String.valueOf(blockNumber))
.resolve(index + FileSystemBlockStorage.BLOCK_FILE_EXTENSION);
Path fullTestPath =
testPath.resolve(String.valueOf(blockNumber))
.resolve(index + FileSystemBlockStorage.BLOCK_FILE_EXTENSION);
try (FileInputStream fis = new FileInputStream(fullTestPath.toFile())) {
BlockItem fetchedBlockItem = BlockItem.parseFrom(fis);
assertEquals(blockItem.getValue(), fetchedBlockItem.getValue());
Expand Down

0 comments on commit 04dd9c5

Please sign in to comment.