-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add compression mode support for BlockAsFileReader (#449)
Signed-off-by: Atanas Atanasov <a.v.atanasov98@gmail.com>
- Loading branch information
Showing
26 changed files
with
717 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 41 additions & 12 deletions
53
...n/java/com/hedera/block/server/persistence/storage/path/BlockAsLocalFilePathResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,79 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
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 com.hedera.block.server.persistence.storage.PersistenceStorageConfig; | ||
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig.CompressionType; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* A Block path resolver for block-as-file. | ||
*/ | ||
public final class BlockAsLocalFilePathResolver implements BlockPathResolver { | ||
private static final int MAX_LONG_DIGITS = 19; | ||
private final Path liveRootPath; | ||
private final CompressionType compressionType; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param liveRootPath valid, {@code non-null} instance of {@link Path} | ||
* that points to the live root of the block storage | ||
* @param config valid, {@code non-null} instance of | ||
* {@link PersistenceStorageConfig} used for initializing the resolver | ||
*/ | ||
private BlockAsLocalFilePathResolver(@NonNull final Path liveRootPath) { | ||
this.liveRootPath = Objects.requireNonNull(liveRootPath); | ||
private BlockAsLocalFilePathResolver(@NonNull final PersistenceStorageConfig config) { | ||
this.liveRootPath = Path.of(config.liveRootPath()); | ||
this.compressionType = config.compression(); | ||
} | ||
|
||
/** | ||
* This method creates and returns a new instance of | ||
* {@link BlockAsLocalFilePathResolver}. | ||
* | ||
* @param liveRootPath valid, {@code non-null} instance of {@link Path} | ||
* that points to the live root of the block storage | ||
* @return a new, fully initialized instance of | ||
* {@link BlockAsLocalFilePathResolver} | ||
* @param config valid, {@code non-null} instance of | ||
* {@link PersistenceStorageConfig} used for initializing the resolver | ||
* @return a new, fully initialized instance of {@link BlockAsLocalFilePathResolver} | ||
*/ | ||
public static BlockAsLocalFilePathResolver of(@NonNull final Path liveRootPath) { | ||
return new BlockAsLocalFilePathResolver(liveRootPath); | ||
public static BlockAsLocalFilePathResolver of(@NonNull final PersistenceStorageConfig config) { | ||
return new BlockAsLocalFilePathResolver(config); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Path resolvePathToBlock(final long blockNumber) { | ||
public Path resolveLiveRawPathToBlock(final long blockNumber) { | ||
Preconditions.requireWhole(blockNumber); | ||
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; | ||
return Path.of(liveRootPath.toString(), blockPath); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Path resolveArchiveRawPathToBlock(final long blockNumber) { | ||
Preconditions.requireWhole(blockNumber); | ||
throw new UnsupportedOperationException("Not implemented yet"); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Optional<Path> findBlock(final long blockNumber) { | ||
Preconditions.requireWhole(blockNumber); | ||
final Path liveRawRootPath = resolveLiveRawPathToBlock(blockNumber); | ||
final Path compressionExtendedLiveRawRootPath = | ||
FileUtilities.appendExtension(liveRawRootPath, compressionType.getFileExtension()); | ||
if (Files.exists(compressionExtendedLiveRawRootPath)) { | ||
return Optional.of(compressionExtendedLiveRawRootPath); | ||
} else if (Files.exists(liveRawRootPath)) { | ||
return Optional.of(liveRawRootPath); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} // todo consider to add additional handling here, like test for other compression types (currently not existing) | ||
// and also look for archived blocks (will be implemented in a follow-up PR) | ||
} |
Oops, something went wrong.