Skip to content

Commit

Permalink
remove NONE singleton in CompressorRegistry and remove static block init
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Aug 14, 2023
1 parent ec326ae commit f637245
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,19 @@
*/
@InternalApi
public final class CompressorRegistry {
/** No compression singleton - we still register so users can specify NONE in the API*/
public static final Compressor NONE;

// the backing registry map
private static final Map<String, Compressor> registeredCompressors;
private static final Map<String, Compressor> registeredCompressors = ServiceLoader.load(
CompressorProvider.class,
CompressorProvider.class.getClassLoader()
)
.stream()
.flatMap(p -> p.get().getCompressors().stream())
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));

// no instance:
private CompressorRegistry() {}

static {
registeredCompressors = ServiceLoader.load(CompressorProvider.class, CompressorProvider.class.getClassLoader())
.stream()
.flatMap(p -> p.get().getCompressors().stream())
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
NONE = registeredCompressors.get(NoneCompressor.NAME);
}

/**
* Returns the default compressor
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ protected BlobStoreRepository(
cacheRepositoryData = CACHE_REPOSITORY_DATA.get(metadata.settings());
bufferSize = Math.toIntExact(BUFFER_SIZE_SETTING.get(metadata.settings()).getBytes());
maxShardBlobDeleteBatch = MAX_SNAPSHOT_SHARD_BLOB_DELETE_BATCH_SIZE.get(metadata.settings());
this.compressor = compress ? COMPRESSION_TYPE_SETTING.get(metadata.settings()) : CompressorRegistry.NONE;
this.compressor = compress ? COMPRESSION_TYPE_SETTING.get(metadata.settings()) : CompressorRegistry.none();
}

@Override
Expand Down Expand Up @@ -770,7 +770,7 @@ public BlobStore blobStore() {
* @return true if compression is needed
*/
protected final boolean isCompress() {
return compressor != CompressorRegistry.NONE;
return compressor != CompressorRegistry.none();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void testBlobStoreOperations() throws IOException {
ChecksumBlobStoreFormat<BlobObj> checksumSMILE = new ChecksumBlobStoreFormat<>(BLOB_CODEC, "%s", BlobObj::fromXContent);

// Write blobs in different formats
checksumSMILE.write(new BlobObj("checksum smile"), blobContainer, "check-smile", CompressorRegistry.NONE);
checksumSMILE.write(new BlobObj("checksum smile"), blobContainer, "check-smile", CompressorRegistry.none());
checksumSMILE.write(
new BlobObj("checksum smile compressed"),
blobContainer,
Expand All @@ -142,7 +142,7 @@ public void testCompressionIsApplied() throws IOException {
ChecksumBlobStoreFormat<BlobObj> checksumFormat = new ChecksumBlobStoreFormat<>(BLOB_CODEC, "%s", BlobObj::fromXContent);
BlobObj blobObj = new BlobObj(veryRedundantText.toString());
checksumFormat.write(blobObj, blobContainer, "blob-comp", CompressorRegistry.getCompressor(DeflateCompressor.NAME));
checksumFormat.write(blobObj, blobContainer, "blob-not-comp", CompressorRegistry.NONE);
checksumFormat.write(blobObj, blobContainer, "blob-not-comp", CompressorRegistry.none());
Map<String, BlobMetadata> blobs = blobContainer.listBlobsByPrefix("blob-");
assertEquals(blobs.size(), 2);
assertThat(blobs.get("blob-not-comp").length(), greaterThan(blobs.get("blob-comp").length()));
Expand Down

0 comments on commit f637245

Please sign in to comment.