Skip to content

Commit

Permalink
Use var for Immutable builders
Browse files Browse the repository at this point in the history
  • Loading branch information
gaul committed Jul 20, 2024
1 parent 5a129e5 commit 0effb4b
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 95 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/gaul/s3proxy/AliasBlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ public void setContainerAccess(String container,
@Override
public PageSet<? extends StorageMetadata> list() {
PageSet<? extends StorageMetadata> upstream = this.delegate().list();
ImmutableList.Builder<StorageMetadata> results =
new ImmutableList.Builder<>();
var results = new ImmutableList.Builder<StorageMetadata>();
for (StorageMetadata sm : upstream) {
if (aliases.containsValue(sm.getName())) {
MutableStorageMetadata bucketAlias =
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/gaul/s3proxy/EncryptedBlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private Blob decryptBlob(Decryption decryption, String container,
// filter the list by showing the unencrypted blob size
private PageSet<? extends StorageMetadata> filteredList(
PageSet<? extends StorageMetadata> pageSet) {
ImmutableSet.Builder<StorageMetadata> builder = ImmutableSet.builder();
var builder = ImmutableSet.<StorageMetadata>builder();
for (StorageMetadata sm : pageSet) {
if (sm instanceof BlobMetadata) {
MutableBlobMetadata mbm =
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/gaul/s3proxy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public static void main(String[] args) throws Exception {
.build();
ExecutorService executorService = DynamicExecutors.newScalingThreadPool(
1, 20, 60 * 1000, factory);
ImmutableMap.Builder<String, Map.Entry<String, BlobStore>> locators =
ImmutableMap.builder();
ImmutableMap.Builder<PathMatcher, Map.Entry<String, BlobStore>>
globLocators = ImmutableMap.builder();
var locators = ImmutableMap
.<String, Map.Entry<String, BlobStore>>builder();
var globLocators = ImmutableMap
.<PathMatcher, Map.Entry<String, BlobStore>>builder();
Set<String> locatorGlobs = new HashSet<>();
Set<String> parsedIdentities = new HashSet<>();
for (File propertiesFile : options.propertiesFiles) {
Expand Down Expand Up @@ -177,10 +177,8 @@ public static void main(String[] args) throws Exception {
throw e;
}

final Map<String, Map.Entry<String, BlobStore>> locator =
locators.build();
final Map<PathMatcher, Map.Entry<String, BlobStore>>
globLocator = globLocators.build();
var locator = locators.build();
var globLocator = globLocators.build();
if (!locator.isEmpty() || !globLocator.isEmpty()) {
s3Proxy.setBlobStoreLocator(
new GlobBlobStoreLocator(locator, globLocator));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/gaul/s3proxy/NullBlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Blob getBlob(String container, String name, GetOptions options) {

@Override
public PageSet<? extends StorageMetadata> list(String container) {
ImmutableSet.Builder<StorageMetadata> builder = ImmutableSet.builder();
var builder = ImmutableSet.<StorageMetadata>builder();
PageSet<? extends StorageMetadata> pageSet = super.list(container);
for (StorageMetadata sm : pageSet) {
MutableStorageMetadata msm = new MutableStorageMetadataImpl(sm);
Expand Down Expand Up @@ -203,7 +203,7 @@ public MultipartPart uploadMultipartPart(MultipartUpload mpu,

@Override
public List<MultipartPart> listMultipartUpload(MultipartUpload mpu) {
ImmutableList.Builder<MultipartPart> builder = ImmutableList.builder();
var builder = ImmutableList.<MultipartPart>builder();
for (MultipartPart part : super.listMultipartUpload(mpu)) {
// get real blob size from stub blob
Blob blob = getBlob(mpu.containerName(),
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/gaul/s3proxy/S3ProxyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1845,8 +1845,7 @@ private void handleCopyBlob(HttpServletRequest request,
if (replaceMetadata) {
ContentMetadataBuilder contentMetadata =
ContentMetadataBuilder.create();
ImmutableMap.Builder<String, String> userMetadata =
ImmutableMap.builder();
var userMetadata = ImmutableMap.<String, String>builder();
for (String headerName : Collections.list(
request.getHeaderNames())) {
String headerValue = Strings.nullToEmpty(request.getHeader(
Expand Down Expand Up @@ -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<Integer, MultipartPart> builder =
ImmutableMap.builder();
var builder = ImmutableMap.<Integer, MultipartPart>builder();
for (MultipartPart part : blobStore.listMultipartUpload(mpu)) {
builder.put(part.partNumber(), part);
}
Expand Down Expand Up @@ -3026,8 +3024,7 @@ private void addCorsResponseHeader(HttpServletRequest request,
private static void addContentMetdataFromHttpRequest(
BlobBuilder.PayloadBlobBuilder builder,
HttpServletRequest request) {
ImmutableMap.Builder<String, String> userMetadata =
ImmutableMap.builder();
var userMetadata = ImmutableMap.<String, String>builder();
for (String headerName : Collections.list(request.getHeaderNames())) {
if (startsWithIgnoreCase(headerName, USER_METADATA_PREFIX)) {
userMetadata.put(
Expand Down
28 changes: 10 additions & 18 deletions src/main/java/org/gaul/s3proxy/ShardedBlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ private ShardedBlobStore(BlobStore blobStore,
"Number of shards unset for sharded buckets: %s",
allMissingShards));
}
ImmutableMap.Builder<String, ShardedBucket> bucketsBuilder =
new ImmutableMap.Builder<>();
var bucketsBuilder = new ImmutableMap.Builder<String, ShardedBucket>();
for (String bucket : shards.keySet()) {
String prefix = prefixes.get(bucket);
if (prefix == null) {
Expand All @@ -138,8 +137,7 @@ private ShardedBlobStore(BlobStore blobStore,
}
this.buckets = bucketsBuilder.build();

ImmutableMap.Builder<String, String> prefixMapBuilder =
new ImmutableMap.Builder<>();
var prefixMapBuilder = new ImmutableMap.Builder<String, String>();
for (String virtualBucket : buckets.keySet()) {
String prefix = buckets.get(virtualBucket).prefix;
prefixMapBuilder.put(prefix, virtualBucket);
Expand All @@ -149,8 +147,7 @@ private ShardedBlobStore(BlobStore blobStore,

public static ImmutableMap<String, Integer> parseBucketShards(
Properties properties) {
ImmutableMap.Builder<String, Integer> shardsMap =
new ImmutableMap.Builder<>();
var shardsMap = new ImmutableMap.Builder<String, Integer>();
for (String key : properties.stringPropertyNames()) {
Matcher matcher = PROPERTIES_SHARDS_RE.matcher(key);
if (!matcher.matches()) {
Expand All @@ -168,8 +165,7 @@ public static ImmutableMap<String, Integer> parseBucketShards(

public static ImmutableMap<String, String> parsePrefixes(
Properties properties) {
ImmutableMap.Builder<String, String> prefixesMap =
new ImmutableMap.Builder<>();
var prefixesMap = new ImmutableMap.Builder<String, String>();
for (String key : properties.stringPropertyNames()) {
Matcher matcher = PROPERTIES_PREFIX_RE.matcher(key);
if (!matcher.matches()) {
Expand All @@ -189,8 +185,7 @@ static ShardedBlobStore newShardedBlobStore(
}

private Map<String, String> createSuperblockMeta(ShardedBucket bucket) {
ImmutableMap.Builder<String, String> meta =
new ImmutableMap.Builder<>();
var meta = new ImmutableMap.Builder<String, String>();
meta.put("s3proxy-sharded-superblock-version", SUPERBLOCK_VERSION);
meta.put("s3proxy-sharded-superblock-prefix", bucket.prefix);
meta.put("s3proxy-sharded-superblock-shards",
Expand Down Expand Up @@ -229,8 +224,7 @@ private void checkSuperBlock(Blob blob, Map<String, String> expectedMeta,

private boolean createShards(ShardedBucket bucket, Location location,
CreateContainerOptions options) {
ImmutableList.Builder<Future<Boolean>> futuresBuilder =
new ImmutableList.Builder<>();
var futuresBuilder = new ImmutableList.Builder<Future<Boolean>>();
ExecutorService executor = Executors.newFixedThreadPool(
Math.min(bucket.shards, MAX_SHARD_THREADS));
BlobStore blobStore = this.delegate();
Expand All @@ -241,7 +235,7 @@ private boolean createShards(ShardedBucket bucket, Location location,
() -> blobStore.createContainerInLocation(
location, shardContainer, options)));
}
ImmutableList<Future<Boolean>> futures = futuresBuilder.build();
var futures = futuresBuilder.build();
executor.shutdown();
boolean ret = true;
for (Future<Boolean> future : futures) {
Expand Down Expand Up @@ -304,8 +298,7 @@ public boolean createContainerInLocation(
@Override
public PageSet<? extends StorageMetadata> list() {
PageSet<? extends StorageMetadata> upstream = this.delegate().list();
ImmutableList.Builder<StorageMetadata> results =
new ImmutableList.Builder<>();
var results = new ImmutableList.Builder<StorageMetadata>();
Set<String> virtualBuckets = new HashSet<>();
for (StorageMetadata sm : upstream) {
Matcher matcher = SHARD_RE.matcher(sm.getName());
Expand Down Expand Up @@ -403,8 +396,7 @@ public void deleteContainer(String container) {
}

private boolean deleteShards(ShardedBucket bucket) {
ImmutableList.Builder<Future<Boolean>> futuresBuilder =
new ImmutableList.Builder<>();
var futuresBuilder = new ImmutableList.Builder<Future<Boolean>>();
ExecutorService executor = Executors.newFixedThreadPool(
Math.min(bucket.shards, MAX_SHARD_THREADS));
for (int n = 0; n < bucket.shards; ++n) {
Expand All @@ -413,7 +405,7 @@ private boolean deleteShards(ShardedBucket bucket) {
() -> this.delegate().deleteContainerIfEmpty(shard)));
}
executor.shutdown();
ImmutableList<Future<Boolean>> futures = futuresBuilder.build();
var futures = futuresBuilder.build();
boolean ret = true;
for (Future<Boolean> future : futures) {
try {
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/org/gaul/s3proxy/AliasBlobStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public void setUp() {
.modules(ImmutableList.<Module>of(new SLF4JLoggingModule()))
.build(BlobStoreContext.class);
blobStore = context.getBlobStore();
ImmutableBiMap.Builder<String, String> aliasesBuilder =
new ImmutableBiMap.Builder<>();
var aliasesBuilder = new ImmutableBiMap.Builder<String, String>();
aliasesBuilder.put(aliasContainerName, containerName);
aliasBlobStore = AliasBlobStore.newAliasBlobStore(
blobStore, aliasesBuilder.build());
Expand Down Expand Up @@ -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<MultipartPart> parts =
new ImmutableList.Builder<>();
var parts = new ImmutableList.Builder<MultipartPart>();
parts.add(part);
String mpuETag = aliasBlobStore.completeMultipartUpload(mpu,
parts.build());
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/org/gaul/s3proxy/AwsSdkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public void testHttpClient() throws Exception {

@Test
public void testListBuckets() throws Exception {
ImmutableList.Builder<String> builder = ImmutableList.builder();
var builder = ImmutableList.<String>builder();
for (Bucket bucket : client.listBuckets()) {
builder.add(bucket.getName());
}
Expand Down Expand Up @@ -841,7 +841,7 @@ public void testBlobList() throws Exception {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(BYTE_SOURCE.size());

ImmutableList.Builder<String> builder = ImmutableList.builder();
var builder = ImmutableList.<String>builder();
client.putObject(containerName, "blob1", BYTE_SOURCE.openStream(),
metadata);
listing = client.listObjects(containerName);
Expand Down Expand Up @@ -872,7 +872,7 @@ public void testBlobListRecursive() throws Exception {
client.putObject(containerName, "prefix/blob2",
BYTE_SOURCE.openStream(), metadata);

ImmutableList.Builder<String> builder = ImmutableList.builder();
var builder = ImmutableList.<String>builder();
listing = client.listObjects(new ListObjectsRequest()
.withBucketName(containerName)
.withDelimiter("/"));
Expand Down Expand Up @@ -1030,7 +1030,7 @@ public void testSinglepartUpload() throws Exception {
String contentEncoding = "gzip";
String contentLanguage = "fr";
String contentType = "audio/mp4";
Map<String, String> userMetadata = ImmutableMap.of(
var userMetadata = ImmutableMap.of(
"key1", "value1",
"key2", "value2");
ObjectMetadata metadata = new ObjectMetadata();
Expand Down Expand Up @@ -1092,7 +1092,7 @@ public void testMultipartUpload() throws Exception {
String contentEncoding = "gzip";
String contentLanguage = "fr";
String contentType = "audio/mp4";
Map<String, String> userMetadata = ImmutableMap.of(
var userMetadata = ImmutableMap.of(
"key1", "value1",
"key2", "value2");
ObjectMetadata metadata = new ObjectMetadata();
Expand Down Expand Up @@ -1185,7 +1185,7 @@ public void testMaximumMultipartUpload() throws Exception {

InitiateMultipartUploadResult result = client.initiateMultipartUpload(
new InitiateMultipartUploadRequest(containerName, blobName));
ImmutableList.Builder<PartETag> parts = ImmutableList.builder();
var parts = ImmutableList.<PartETag>builder();

for (int i = 0; i < numParts; ++i) {
ByteSource partByteSource = byteSource.slice(
Expand Down Expand Up @@ -1286,7 +1286,7 @@ public void testCopyObjectPreserveMetadata() throws Exception {
String contentEncoding = "gzip";
String contentLanguage = "en";
String contentType = "audio/ogg";
Map<String, String> userMetadata = ImmutableMap.of(
var userMetadata = ImmutableMap.of(
"key1", "value1",
"key2", "value2");
ObjectMetadata metadata = new ObjectMetadata();
Expand Down Expand Up @@ -1390,7 +1390,7 @@ public void testCopyObjectReplaceMetadata() throws Exception {
}
contentMetadata.setContentType(contentType);
// TODO: expires
Map<String, String> userMetadata = ImmutableMap.of(
var userMetadata = ImmutableMap.of(
"key3", "value3",
"key4", "value4");
contentMetadata.setUserMetadata(userMetadata);
Expand Down
61 changes: 25 additions & 36 deletions src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ public void setUp() {

@Test
public void testLocateIdentity() {
ImmutableMap<String, Map.Entry<String, BlobStore>> 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())
Expand All @@ -71,19 +70,14 @@ public void testLocateIdentity() {

@Test
public void testLocateContainer() {
ImmutableMap<String, Map.Entry<String, BlobStore>> credsMap =
ImmutableMap.of(
"id1", Maps.immutableEntry("one", blobStoreOne),
"id2", Maps.immutableEntry("two", blobStoreTwo));
ImmutableMap<PathMatcher, Map.Entry<String, BlobStore>> 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);

Expand All @@ -103,20 +97,18 @@ public void testLocateContainer() {

@Test
public void testLocateGlob() {
ImmutableMap<String, Map.Entry<String, BlobStore>> credsMap =
ImmutableSortedMap.of(
"id0", Maps.immutableEntry("zero", null),
"id1", Maps.immutableEntry("one", blobStoreOne),
"id2", Maps.immutableEntry("two", blobStoreTwo));
ImmutableMap<PathMatcher, Map.Entry<String, BlobStore>> globMap =
ImmutableMap.of(
var credsMap =
ImmutableSortedMap.<String, Map.Entry<String, BlobStore>>of(
"id0", Maps.immutableEntry("zero", null),
"id1", Maps.immutableEntry("one", blobStoreOne),
"id2", Maps.immutableEntry("two", blobStoreTwo));
var globMap =
ImmutableMap.<PathMatcher, Map.Entry<String, BlobStore>>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);

Expand All @@ -130,15 +122,12 @@ public void testLocateGlob() {

@Test
public void testGlobLocatorAnonymous() {
ImmutableMap<PathMatcher, Map.Entry<String, BlobStore>> globMap =
ImmutableMap.of(
FileSystems.getDefault().getPathMatcher(
"glob:one"),
var globMap =
ImmutableMap.<PathMatcher, Map.Entry<String, BlobStore>>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);

Expand Down
Loading

0 comments on commit 0effb4b

Please sign in to comment.