Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Atanasov <[email protected]>
  • Loading branch information
ata-nas committed Dec 12, 2024
1 parent 34a8261 commit e61a459
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ public static byte[] readFileBytesUnsafe(
}

/**
* This method creates a new file at the given path.
* This method creates a new file at the given path. The method ensures that
* the full path to the target file will be created, including all missing
* intermediary directories.
*
* @param pathToCreate the path to create
* @throws IOException if the file cannot be created or if it already exists
Expand All @@ -206,7 +208,7 @@ public static void createFile(@NonNull final Path pathToCreate) throws IOExcepti
*/
@NonNull
public static Path appendExtension(@NonNull final Path path, @NonNull final String extension) {
return path.resolveSibling(path.getFileName() + extension); // todo add startswith . if not add it
return path.resolveSibling(path.getFileName() + Objects.requireNonNull(extension));
}

private FileUtilities() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public interface PersistenceInjectionModule {
* @param blockNodeContext the application context
* @param blockRemover the block remover
* @param blockPathResolver the block path resolver
* @param compression the compression used
* @return a block writer singleton
*/
@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @param archiveRootPath provides the root path for archived blocks
* @param type storage type
* @param compression compression type to use for the storage
* @param compressionLevel compression level used by the compression algorithm
* Non-PRODUCTION values should only be used for troubleshooting and development purposes.
*/
@ConfigData("persistence.storage")
Expand All @@ -48,9 +49,7 @@ public record PersistenceStorageConfig(
@ConfigProperty(defaultValue = "") String archiveRootPath,
@ConfigProperty(defaultValue = "BLOCK_AS_LOCAL_FILE") StorageType type,
@ConfigProperty(defaultValue = "ZSTD") CompressionType compression,
// todo for the default compression level, should we use the Zstd#defaultCompressionLevel()
// (should be 3 based on some docs) or should we go with 6 as proposed in PR review?
@ConfigProperty(defaultValue = "6") int compressionLevel) {
@ConfigProperty(defaultValue = "3") int compressionLevel) {
// @todo(#371) - the default life/archive root path must be absolute starting from /opt
private static final String LIVE_ROOT_PATH =
Path.of("hashgraph/blocknode/data/live/").toAbsolutePath().toString();
Expand All @@ -64,9 +63,6 @@ public record PersistenceStorageConfig(
public PersistenceStorageConfig {
Objects.requireNonNull(type);
Objects.requireNonNull(compression);
// todo should we use the Zstd#minCompressionLevel() and Zstd#maxCompressionLevel() or do we?
// have a range that is defined by us internally that should be allowed, as proposed in one
// PR comment, 0 - 9 with 6 default?
Preconditions.requireInRange(compressionLevel, Zstd.minCompressionLevel(), Zstd.maxCompressionLevel());
liveRootPath = resolvePath(liveRootPath, LIVE_ROOT_PATH, BLOCK_NODE_LIVE_ROOT_DIRECTORY_SEMANTIC_NAME);
archiveRootPath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public interface Compression {
* resulting {@link OutputStream} is then returned.
*
* @param streamToWrap a valid {@code non-null} {@link OutputStream} to wrap
* @return a {@code non-null} {@link OutputStream} that wraps the in
* @return a {@code non-null} {@link OutputStream} that wraps the provided
* {@link OutputStream} with the specific compression algorithm
* implementation
* @throws IOException if an I/O exception occurs
*/
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ public static BlockAsLocalDirPathResolver of(@NonNull final Path liveRootPath) {
@NonNull
@Override
public Path resolvePathToBlock(final long blockNumber) {
return resolvePathToBlock(blockNumber, "");
}

@NonNull
@Override
public Path resolvePathToBlock(final long blockNumber, @NonNull final String compressionExtension) {
Preconditions.requireWhole(blockNumber);
return liveRootPath.resolve(String.valueOf(blockNumber));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

import com.hedera.block.common.utils.FileUtilities;
import com.hedera.block.common.utils.Preconditions;
import com.hedera.block.server.Constants;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -56,19 +55,11 @@ public static BlockAsLocalFilePathResolver of(@NonNull final Path liveRootPath)
@NonNull
@Override
public Path resolvePathToBlock(final long blockNumber) {
return resolvePathToBlock(blockNumber, "");
}

@NonNull
@Override
public Path resolvePathToBlock(final long blockNumber, @NonNull final String compressionExtension) {
Preconditions.requireWhole(blockNumber);
Objects.requireNonNull(compressionExtension);
final String rawBlockNumber = String.format("%0" + MAX_LONG_DIGITS + "d", blockNumber);
final String[] blockPath = rawBlockNumber.split("");
final String blockFileName = rawBlockNumber.concat(Constants.BLOCK_FILE_EXTENSION);
blockPath[blockPath.length - 1] = blockFileName;
final Path rawPath = Path.of(liveRootPath.toString(), blockPath);
return FileUtilities.appendExtension(rawPath, compressionExtension);
return Path.of(liveRootPath.toString(), blockPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,4 @@ public interface BlockPathResolver {
*/
@NonNull
Path resolvePathToBlock(final long blockNumber);

/**
* This method resolves the fs {@link Path} to a Block by a given input
* number. This method does not guarantee that the returned {@link Path}
* exists! This method is guaranteed to return a {@code non-null}
* {@link Path}. The compressionExtension is appended to the file name.
*
* @param blockNumber to be resolved the path for
* @param compressionExtension the extension to be appended based on
* compression algorithm
* @return the resolved path to the given Block by a number
* @throws IllegalArgumentException if the blockNumber IS NOT a whole number
*/
@NonNull
Path resolvePathToBlock(final long blockNumber, @NonNull final String compressionExtension);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.file.Path;
import java.util.Objects;

/**
* A no-op path resolver.
Expand All @@ -43,31 +42,11 @@ public static NoOpBlockPathResolver newInstance() {
/**
* No-op resolver. Does nothing and always returns a path under '/tmp' that
* resolves to 'blockNumber.tmp.blk'. No preconditions check also.
*
* @param blockNumber to be resolved the path for
* @return the resolved path to a given block by a number
*/
@NonNull
@Override
public Path resolvePathToBlock(final long blockNumber) {
return resolvePathToBlock(blockNumber, "");
}

/**
* No-op resolver. Does nothing and always returns a path under '/tmp' that
* resolves to 'blockNumber.tmp.blk' appended with compressionExtension.
* No preconditions check also.
*
* @param blockNumber to be resolved the path for
* @param compressionExtension the extension to be appended based on
* compression algorithm
* @return the resolved path to a given block by a number
*/
@NonNull
@Override
public Path resolvePathToBlock(final long blockNumber, @NonNull final String compressionExtension) {
Objects.requireNonNull(compressionExtension);
final String blockName = String.format("%d.tmp.blk%s", blockNumber, compressionExtension);
final String blockName = String.format("%d.tmp.blk", blockNumber);
return Path.of("/tmp/hashgraph/blocknode/data/").resolve(blockName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ public Optional<List<BlockItemUnparsed>> write(@NonNull final List<BlockItemUnpa

private List<BlockItemUnparsed> writeToFs() throws IOException {
final Path rawBlockPath = blockPathResolver.resolvePathToBlock(currentBlockNumber);
final Path blockToWritePathResolved =
final Path resolvedBlockPath =
FileUtilities.appendExtension(rawBlockPath, compression.getCompressionFileExtension());
FileUtilities.createFile(blockToWritePathResolved);
try (final OutputStream out = compression.wrap(Files.newOutputStream(blockToWritePathResolved))) {
FileUtilities.createFile(resolvedBlockPath);
try (final OutputStream out = compression.wrap(Files.newOutputStream(resolvedBlockPath))) {
final BlockUnparsed blockToWrite =
BlockUnparsed.newBuilder().blockItems(currentBlockItems).build();
BlockUnparsed.PROTOBUF.toBytes(blockToWrite).writeTo(out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Tests for the {@link ZstdCompression} class.
*/
@SuppressWarnings("FieldCanBeLocal")
class ZstdCompressionTest {
@TempDir
private Path testTempDir = Path.of("src/test/resources/tempDir");
private Path testTempDir;

private BlockNodeContext blockNodeContext;
private PersistenceStorageConfig testConfig;
Expand Down

0 comments on commit e61a459

Please sign in to comment.