From 0effb4bf450dc7dab7198614b57007d2d2aa953b Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sat, 20 Jul 2024 12:42:05 +0530 Subject: [PATCH] Use var for Immutable builders --- .../java/org/gaul/s3proxy/AliasBlobStore.java | 3 +- .../org/gaul/s3proxy/EncryptedBlobStore.java | 2 +- src/main/java/org/gaul/s3proxy/Main.java | 14 ++--- .../java/org/gaul/s3proxy/NullBlobStore.java | 4 +- .../java/org/gaul/s3proxy/S3ProxyHandler.java | 9 +-- .../org/gaul/s3proxy/ShardedBlobStore.java | 28 +++------ .../org/gaul/s3proxy/AliasBlobStoreTest.java | 6 +- .../java/org/gaul/s3proxy/AwsSdkTest.java | 16 ++--- .../s3proxy/GlobBlobStoreLocatorTest.java | 61 ++++++++----------- .../org/gaul/s3proxy/RegexBlobStoreTest.java | 9 ++- .../gaul/s3proxy/ShardedBlobStoreTest.java | 7 +-- 11 files changed, 64 insertions(+), 95 deletions(-) diff --git a/src/main/java/org/gaul/s3proxy/AliasBlobStore.java b/src/main/java/org/gaul/s3proxy/AliasBlobStore.java index 4ae125b5..6e99be23 100644 --- a/src/main/java/org/gaul/s3proxy/AliasBlobStore.java +++ b/src/main/java/org/gaul/s3proxy/AliasBlobStore.java @@ -135,8 +135,7 @@ public void setContainerAccess(String container, @Override public PageSet list() { PageSet upstream = this.delegate().list(); - ImmutableList.Builder results = - new ImmutableList.Builder<>(); + var results = new ImmutableList.Builder(); for (StorageMetadata sm : upstream) { if (aliases.containsValue(sm.getName())) { MutableStorageMetadata bucketAlias = diff --git a/src/main/java/org/gaul/s3proxy/EncryptedBlobStore.java b/src/main/java/org/gaul/s3proxy/EncryptedBlobStore.java index 0c8147e7..7d1b7102 100644 --- a/src/main/java/org/gaul/s3proxy/EncryptedBlobStore.java +++ b/src/main/java/org/gaul/s3proxy/EncryptedBlobStore.java @@ -233,7 +233,7 @@ private Blob decryptBlob(Decryption decryption, String container, // filter the list by showing the unencrypted blob size private PageSet filteredList( PageSet pageSet) { - ImmutableSet.Builder builder = ImmutableSet.builder(); + var builder = ImmutableSet.builder(); for (StorageMetadata sm : pageSet) { if (sm instanceof BlobMetadata) { MutableBlobMetadata mbm = diff --git a/src/main/java/org/gaul/s3proxy/Main.java b/src/main/java/org/gaul/s3proxy/Main.java index b21d0683..7fea714b 100644 --- a/src/main/java/org/gaul/s3proxy/Main.java +++ b/src/main/java/org/gaul/s3proxy/Main.java @@ -106,10 +106,10 @@ public static void main(String[] args) throws Exception { .build(); ExecutorService executorService = DynamicExecutors.newScalingThreadPool( 1, 20, 60 * 1000, factory); - ImmutableMap.Builder> locators = - ImmutableMap.builder(); - ImmutableMap.Builder> - globLocators = ImmutableMap.builder(); + var locators = ImmutableMap + .>builder(); + var globLocators = ImmutableMap + .>builder(); Set locatorGlobs = new HashSet<>(); Set parsedIdentities = new HashSet<>(); for (File propertiesFile : options.propertiesFiles) { @@ -177,10 +177,8 @@ public static void main(String[] args) throws Exception { throw e; } - final Map> locator = - locators.build(); - final Map> - globLocator = globLocators.build(); + var locator = locators.build(); + var globLocator = globLocators.build(); if (!locator.isEmpty() || !globLocator.isEmpty()) { s3Proxy.setBlobStoreLocator( new GlobBlobStoreLocator(locator, globLocator)); diff --git a/src/main/java/org/gaul/s3proxy/NullBlobStore.java b/src/main/java/org/gaul/s3proxy/NullBlobStore.java index 14f23fd3..14d91766 100644 --- a/src/main/java/org/gaul/s3proxy/NullBlobStore.java +++ b/src/main/java/org/gaul/s3proxy/NullBlobStore.java @@ -100,7 +100,7 @@ public Blob getBlob(String container, String name, GetOptions options) { @Override public PageSet list(String container) { - ImmutableSet.Builder builder = ImmutableSet.builder(); + var builder = ImmutableSet.builder(); PageSet pageSet = super.list(container); for (StorageMetadata sm : pageSet) { MutableStorageMetadata msm = new MutableStorageMetadataImpl(sm); @@ -203,7 +203,7 @@ public MultipartPart uploadMultipartPart(MultipartUpload mpu, @Override public List listMultipartUpload(MultipartUpload mpu) { - ImmutableList.Builder builder = ImmutableList.builder(); + var builder = ImmutableList.builder(); for (MultipartPart part : super.listMultipartUpload(mpu)) { // get real blob size from stub blob Blob blob = getBlob(mpu.containerName(), diff --git a/src/main/java/org/gaul/s3proxy/S3ProxyHandler.java b/src/main/java/org/gaul/s3proxy/S3ProxyHandler.java index 2e3679bb..b1ad3d3a 100644 --- a/src/main/java/org/gaul/s3proxy/S3ProxyHandler.java +++ b/src/main/java/org/gaul/s3proxy/S3ProxyHandler.java @@ -1845,8 +1845,7 @@ private void handleCopyBlob(HttpServletRequest request, if (replaceMetadata) { ContentMetadataBuilder contentMetadata = ContentMetadataBuilder.create(); - ImmutableMap.Builder userMetadata = - ImmutableMap.builder(); + var userMetadata = ImmutableMap.builder(); for (String headerName : Collections.list( request.getHeaderNames())) { String headerValue = Strings.nullToEmpty(request.getHeader( @@ -2268,8 +2267,7 @@ private void handleCompleteMultipartUpload(HttpServletRequest request, // List parts to get part sizes and to map multiple Azure parts // into single parts. - ImmutableMap.Builder builder = - ImmutableMap.builder(); + var builder = ImmutableMap.builder(); for (MultipartPart part : blobStore.listMultipartUpload(mpu)) { builder.put(part.partNumber(), part); } @@ -3026,8 +3024,7 @@ private void addCorsResponseHeader(HttpServletRequest request, private static void addContentMetdataFromHttpRequest( BlobBuilder.PayloadBlobBuilder builder, HttpServletRequest request) { - ImmutableMap.Builder userMetadata = - ImmutableMap.builder(); + var userMetadata = ImmutableMap.builder(); for (String headerName : Collections.list(request.getHeaderNames())) { if (startsWithIgnoreCase(headerName, USER_METADATA_PREFIX)) { userMetadata.put( diff --git a/src/main/java/org/gaul/s3proxy/ShardedBlobStore.java b/src/main/java/org/gaul/s3proxy/ShardedBlobStore.java index 71f92c7f..3a887464 100644 --- a/src/main/java/org/gaul/s3proxy/ShardedBlobStore.java +++ b/src/main/java/org/gaul/s3proxy/ShardedBlobStore.java @@ -126,8 +126,7 @@ private ShardedBlobStore(BlobStore blobStore, "Number of shards unset for sharded buckets: %s", allMissingShards)); } - ImmutableMap.Builder bucketsBuilder = - new ImmutableMap.Builder<>(); + var bucketsBuilder = new ImmutableMap.Builder(); for (String bucket : shards.keySet()) { String prefix = prefixes.get(bucket); if (prefix == null) { @@ -138,8 +137,7 @@ private ShardedBlobStore(BlobStore blobStore, } this.buckets = bucketsBuilder.build(); - ImmutableMap.Builder prefixMapBuilder = - new ImmutableMap.Builder<>(); + var prefixMapBuilder = new ImmutableMap.Builder(); for (String virtualBucket : buckets.keySet()) { String prefix = buckets.get(virtualBucket).prefix; prefixMapBuilder.put(prefix, virtualBucket); @@ -149,8 +147,7 @@ private ShardedBlobStore(BlobStore blobStore, public static ImmutableMap parseBucketShards( Properties properties) { - ImmutableMap.Builder shardsMap = - new ImmutableMap.Builder<>(); + var shardsMap = new ImmutableMap.Builder(); for (String key : properties.stringPropertyNames()) { Matcher matcher = PROPERTIES_SHARDS_RE.matcher(key); if (!matcher.matches()) { @@ -168,8 +165,7 @@ public static ImmutableMap parseBucketShards( public static ImmutableMap parsePrefixes( Properties properties) { - ImmutableMap.Builder prefixesMap = - new ImmutableMap.Builder<>(); + var prefixesMap = new ImmutableMap.Builder(); for (String key : properties.stringPropertyNames()) { Matcher matcher = PROPERTIES_PREFIX_RE.matcher(key); if (!matcher.matches()) { @@ -189,8 +185,7 @@ static ShardedBlobStore newShardedBlobStore( } private Map createSuperblockMeta(ShardedBucket bucket) { - ImmutableMap.Builder meta = - new ImmutableMap.Builder<>(); + var meta = new ImmutableMap.Builder(); meta.put("s3proxy-sharded-superblock-version", SUPERBLOCK_VERSION); meta.put("s3proxy-sharded-superblock-prefix", bucket.prefix); meta.put("s3proxy-sharded-superblock-shards", @@ -229,8 +224,7 @@ private void checkSuperBlock(Blob blob, Map expectedMeta, private boolean createShards(ShardedBucket bucket, Location location, CreateContainerOptions options) { - ImmutableList.Builder> futuresBuilder = - new ImmutableList.Builder<>(); + var futuresBuilder = new ImmutableList.Builder>(); ExecutorService executor = Executors.newFixedThreadPool( Math.min(bucket.shards, MAX_SHARD_THREADS)); BlobStore blobStore = this.delegate(); @@ -241,7 +235,7 @@ private boolean createShards(ShardedBucket bucket, Location location, () -> blobStore.createContainerInLocation( location, shardContainer, options))); } - ImmutableList> futures = futuresBuilder.build(); + var futures = futuresBuilder.build(); executor.shutdown(); boolean ret = true; for (Future future : futures) { @@ -304,8 +298,7 @@ public boolean createContainerInLocation( @Override public PageSet list() { PageSet upstream = this.delegate().list(); - ImmutableList.Builder results = - new ImmutableList.Builder<>(); + var results = new ImmutableList.Builder(); Set virtualBuckets = new HashSet<>(); for (StorageMetadata sm : upstream) { Matcher matcher = SHARD_RE.matcher(sm.getName()); @@ -403,8 +396,7 @@ public void deleteContainer(String container) { } private boolean deleteShards(ShardedBucket bucket) { - ImmutableList.Builder> futuresBuilder = - new ImmutableList.Builder<>(); + var futuresBuilder = new ImmutableList.Builder>(); ExecutorService executor = Executors.newFixedThreadPool( Math.min(bucket.shards, MAX_SHARD_THREADS)); for (int n = 0; n < bucket.shards; ++n) { @@ -413,7 +405,7 @@ private boolean deleteShards(ShardedBucket bucket) { () -> this.delegate().deleteContainerIfEmpty(shard))); } executor.shutdown(); - ImmutableList> futures = futuresBuilder.build(); + var futures = futuresBuilder.build(); boolean ret = true; for (Future future : futures) { try { diff --git a/src/test/java/org/gaul/s3proxy/AliasBlobStoreTest.java b/src/test/java/org/gaul/s3proxy/AliasBlobStoreTest.java index f156d4e4..65b68903 100644 --- a/src/test/java/org/gaul/s3proxy/AliasBlobStoreTest.java +++ b/src/test/java/org/gaul/s3proxy/AliasBlobStoreTest.java @@ -66,8 +66,7 @@ public void setUp() { .modules(ImmutableList.of(new SLF4JLoggingModule())) .build(BlobStoreContext.class); blobStore = context.getBlobStore(); - ImmutableBiMap.Builder aliasesBuilder = - new ImmutableBiMap.Builder<>(); + var aliasesBuilder = new ImmutableBiMap.Builder(); aliasesBuilder.put(aliasContainerName, containerName); aliasBlobStore = AliasBlobStore.newAliasBlobStore( blobStore, aliasesBuilder.build()); @@ -152,8 +151,7 @@ public void testAliasMultipartUpload() throws IOException { MultipartPart part = aliasBlobStore.uploadMultipartPart( mpu, 1, Payloads.newPayload(content)); assertThat(part.partETag()).isEqualTo(contentHash.toString()); - ImmutableList.Builder parts = - new ImmutableList.Builder<>(); + var parts = new ImmutableList.Builder(); parts.add(part); String mpuETag = aliasBlobStore.completeMultipartUpload(mpu, parts.build()); diff --git a/src/test/java/org/gaul/s3proxy/AwsSdkTest.java b/src/test/java/org/gaul/s3proxy/AwsSdkTest.java index dbc358f3..cbe7ac9f 100644 --- a/src/test/java/org/gaul/s3proxy/AwsSdkTest.java +++ b/src/test/java/org/gaul/s3proxy/AwsSdkTest.java @@ -755,7 +755,7 @@ public void testHttpClient() throws Exception { @Test public void testListBuckets() throws Exception { - ImmutableList.Builder builder = ImmutableList.builder(); + var builder = ImmutableList.builder(); for (Bucket bucket : client.listBuckets()) { builder.add(bucket.getName()); } @@ -841,7 +841,7 @@ public void testBlobList() throws Exception { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(BYTE_SOURCE.size()); - ImmutableList.Builder builder = ImmutableList.builder(); + var builder = ImmutableList.builder(); client.putObject(containerName, "blob1", BYTE_SOURCE.openStream(), metadata); listing = client.listObjects(containerName); @@ -872,7 +872,7 @@ public void testBlobListRecursive() throws Exception { client.putObject(containerName, "prefix/blob2", BYTE_SOURCE.openStream(), metadata); - ImmutableList.Builder builder = ImmutableList.builder(); + var builder = ImmutableList.builder(); listing = client.listObjects(new ListObjectsRequest() .withBucketName(containerName) .withDelimiter("/")); @@ -1030,7 +1030,7 @@ public void testSinglepartUpload() throws Exception { String contentEncoding = "gzip"; String contentLanguage = "fr"; String contentType = "audio/mp4"; - Map userMetadata = ImmutableMap.of( + var userMetadata = ImmutableMap.of( "key1", "value1", "key2", "value2"); ObjectMetadata metadata = new ObjectMetadata(); @@ -1092,7 +1092,7 @@ public void testMultipartUpload() throws Exception { String contentEncoding = "gzip"; String contentLanguage = "fr"; String contentType = "audio/mp4"; - Map userMetadata = ImmutableMap.of( + var userMetadata = ImmutableMap.of( "key1", "value1", "key2", "value2"); ObjectMetadata metadata = new ObjectMetadata(); @@ -1185,7 +1185,7 @@ public void testMaximumMultipartUpload() throws Exception { InitiateMultipartUploadResult result = client.initiateMultipartUpload( new InitiateMultipartUploadRequest(containerName, blobName)); - ImmutableList.Builder parts = ImmutableList.builder(); + var parts = ImmutableList.builder(); for (int i = 0; i < numParts; ++i) { ByteSource partByteSource = byteSource.slice( @@ -1286,7 +1286,7 @@ public void testCopyObjectPreserveMetadata() throws Exception { String contentEncoding = "gzip"; String contentLanguage = "en"; String contentType = "audio/ogg"; - Map userMetadata = ImmutableMap.of( + var userMetadata = ImmutableMap.of( "key1", "value1", "key2", "value2"); ObjectMetadata metadata = new ObjectMetadata(); @@ -1390,7 +1390,7 @@ public void testCopyObjectReplaceMetadata() throws Exception { } contentMetadata.setContentType(contentType); // TODO: expires - Map userMetadata = ImmutableMap.of( + var userMetadata = ImmutableMap.of( "key3", "value3", "key4", "value4"); contentMetadata.setUserMetadata(userMetadata); diff --git a/src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java b/src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java index e4bb2f2c..7ddcc625 100644 --- a/src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java +++ b/src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java @@ -56,10 +56,9 @@ public void setUp() { @Test public void testLocateIdentity() { - ImmutableMap> credsMap = - ImmutableSortedMap.of( - "id1", Maps.immutableEntry("one", blobStoreOne), - "id2", Maps.immutableEntry("two", blobStoreTwo)); + var credsMap = ImmutableSortedMap.of( + "id1", Maps.immutableEntry("one", blobStoreOne), + "id2", Maps.immutableEntry("two", blobStoreTwo)); GlobBlobStoreLocator locator = new GlobBlobStoreLocator( credsMap, ImmutableMap.of()); assertThat(locator.locateBlobStore("id2", null, null).getKey()) @@ -71,19 +70,14 @@ public void testLocateIdentity() { @Test public void testLocateContainer() { - ImmutableMap> credsMap = - ImmutableMap.of( - "id1", Maps.immutableEntry("one", blobStoreOne), - "id2", Maps.immutableEntry("two", blobStoreTwo)); - ImmutableMap> globMap = - ImmutableMap.of( - FileSystems.getDefault().getPathMatcher( - "glob:container1"), - Maps.immutableEntry("id1", blobStoreOne), - FileSystems.getDefault().getPathMatcher( - "glob:container2"), - Maps.immutableEntry("id2", blobStoreTwo) - ); + var credsMap = ImmutableMap.of( + "id1", Maps.immutableEntry("one", blobStoreOne), + "id2", Maps.immutableEntry("two", blobStoreTwo)); + var globMap = ImmutableMap.of( + FileSystems.getDefault().getPathMatcher("glob:container1"), + Maps.immutableEntry("id1", blobStoreOne), + FileSystems.getDefault().getPathMatcher("glob:container2"), + Maps.immutableEntry("id2", blobStoreTwo)); GlobBlobStoreLocator locator = new GlobBlobStoreLocator(credsMap, globMap); @@ -103,20 +97,18 @@ public void testLocateContainer() { @Test public void testLocateGlob() { - ImmutableMap> credsMap = - ImmutableSortedMap.of( - "id0", Maps.immutableEntry("zero", null), - "id1", Maps.immutableEntry("one", blobStoreOne), - "id2", Maps.immutableEntry("two", blobStoreTwo)); - ImmutableMap> globMap = - ImmutableMap.of( + var credsMap = + ImmutableSortedMap.>of( + "id0", Maps.immutableEntry("zero", null), + "id1", Maps.immutableEntry("one", blobStoreOne), + "id2", Maps.immutableEntry("two", blobStoreTwo)); + var globMap = + ImmutableMap.>of( FileSystems.getDefault().getPathMatcher( "glob:{one,two}"), Maps.immutableEntry("id1", blobStoreOne), - FileSystems.getDefault().getPathMatcher( - "glob:cont?X*"), - Maps.immutableEntry("id2", blobStoreTwo) - ); + FileSystems.getDefault().getPathMatcher("glob:cont?X*"), + Maps.immutableEntry("id2", blobStoreTwo)); GlobBlobStoreLocator locator = new GlobBlobStoreLocator(credsMap, globMap); @@ -130,15 +122,12 @@ public void testLocateGlob() { @Test public void testGlobLocatorAnonymous() { - ImmutableMap> globMap = - ImmutableMap.of( - FileSystems.getDefault().getPathMatcher( - "glob:one"), + var globMap = + ImmutableMap.>of( + FileSystems.getDefault().getPathMatcher("glob:one"), Maps.immutableEntry(null, blobStoreOne), - FileSystems.getDefault().getPathMatcher( - "glob:two"), - Maps.immutableEntry(null, blobStoreTwo) - ); + FileSystems.getDefault().getPathMatcher("glob:two"), + Maps.immutableEntry(null, blobStoreTwo)); GlobBlobStoreLocator locator = new GlobBlobStoreLocator( ImmutableMap.of(), globMap); diff --git a/src/test/java/org/gaul/s3proxy/RegexBlobStoreTest.java b/src/test/java/org/gaul/s3proxy/RegexBlobStoreTest.java index edb57281..6f2dc073 100644 --- a/src/test/java/org/gaul/s3proxy/RegexBlobStoreTest.java +++ b/src/test/java/org/gaul/s3proxy/RegexBlobStoreTest.java @@ -71,12 +71,11 @@ public void tearDown() throws Exception { @Test public void testRemoveSomeCharsFromName() throws IOException { - ImmutableList.Builder> regexBuilder = - new ImmutableList.Builder<>(); - regexBuilder.add(new SimpleEntry(Pattern.compile( - "[^a-zA-Z0-9/_.]"), "_")); + var regexes = ImmutableList.>of( + new SimpleEntry( + Pattern.compile("[^a-zA-Z0-9/_.]"), "_")); BlobStore regexBlobStore = RegexBlobStore.newRegexBlobStore(delegate, - regexBuilder.build()); + regexes); String initialBlobName = "test/remove:badchars-folder/blob.txt"; String targetBlobName = "test/remove_badchars_folder/blob.txt"; diff --git a/src/test/java/org/gaul/s3proxy/ShardedBlobStoreTest.java b/src/test/java/org/gaul/s3proxy/ShardedBlobStoreTest.java index 942c890b..65569858 100644 --- a/src/test/java/org/gaul/s3proxy/ShardedBlobStoreTest.java +++ b/src/test/java/org/gaul/s3proxy/ShardedBlobStoreTest.java @@ -60,11 +60,8 @@ public void setUp() { .modules(ImmutableList.of(new SLF4JLoggingModule())) .build(BlobStoreContext.class); blobStore = context.getBlobStore(); - ImmutableMap shardsMap = - new ImmutableMap.Builder() - .put(containerName, shards).build(); - prefixesMap = new ImmutableMap.Builder() - .put(containerName, prefix).build(); + var shardsMap = ImmutableMap.of(containerName, shards); + prefixesMap = ImmutableMap.of(containerName, prefix); shardedBlobStore = ShardedBlobStore.newShardedBlobStore( blobStore, shardsMap, prefixesMap); createdContainers = new ArrayList<>();