Skip to content

Commit 3287592

Browse files
authored
Minor Performance Improvements in Storage (Azure#24763)
1 parent 26e7c0c commit 3287592

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

sdk/storage/azure-storage-blob-nio/src/main/java/com/azure/storage/blob/nio/AzureFileSystemProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,9 @@ private String extractAccountEndpoint(URI uri) {
11431143

11441144
String endpoint = Flux.fromArray(uri.getQuery().split("&"))
11451145
.filter(s -> s.startsWith(ENDPOINT_QUERY_KEY + "="))
1146-
.switchIfEmpty(Mono.error(LoggingUtility.logError(this.logger, new IllegalArgumentException(
1147-
"URI does not contain an \"" + ENDPOINT_QUERY_KEY + "=\" parameter. FileSystems require a URI "
1148-
+ "of the format \"azb://?endpoint=<endpoint>\""))))
1146+
.switchIfEmpty(Mono.defer(() -> Mono.error(LoggingUtility.logError(this.logger,
1147+
new IllegalArgumentException("URI does not contain an \"" + ENDPOINT_QUERY_KEY + "=\" parameter. "
1148+
+ "FileSystems require a URI of the format \"azb://?endpoint=<endpoint>\"")))))
11491149
.map(s -> s.substring(ENDPOINT_QUERY_KEY.length() + 1)) // Trim the query key and =
11501150
.blockLast();
11511151

sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobAsyncClientBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ Mono<BlobDownloadAsyncResponse> downloadStreamWithResponse(BlobRange range, Down
11221122
},
11231123
finalOptions.getMaxRetryRequests(),
11241124
finalRange.getOffset()
1125-
).switchIfEmpty(Flux.just(ByteBuffer.wrap(new byte[0])));
1125+
).switchIfEmpty(Flux.defer(() -> Flux.just(ByteBuffer.wrap(new byte[0]))));
11261126

11271127
return new BlobDownloadAsyncResponse(response.getRequest(), response.getStatusCode(),
11281128
response.getHeaders(), bufferFlux, blobDownloadHeaders);

sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/ReliableDownload.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ retries as we have not actually retried yet, only made the initial try. Because
9393
? rawResponse.getValue().timeout(TIMEOUT_VALUE)
9494
: applyReliableDownload(rawResponse.getValue(), -1, options);
9595

96-
return value.switchIfEmpty(Flux.just(ByteBuffer.wrap(new byte[0])));
96+
return value.switchIfEmpty(Flux.defer(() -> Flux.just(ByteBuffer.wrap(new byte[0]))));
9797
}
9898

9999
private Flux<ByteBuffer> tryContinueFlux(Throwable t, int retryCount, DownloadRetryOptions options) {

sdk/storage/azure-storage-common/src/main/java/com/azure/storage/common/Utility.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.time.ZoneOffset;
2222
import java.time.format.DateTimeFormatter;
2323
import java.util.Locale;
24+
import java.util.regex.Pattern;
2425

2526
/**
2627
* Utility methods for storage client libraries.
@@ -51,6 +52,11 @@ public final class Utility {
5152
*/
5253
private static final int MAX_PRECISION_DATESTRING_LENGTH = MAX_PRECISION_PATTERN.replaceAll("'", "")
5354
.length();
55+
/**
56+
* A compiled Pattern that finds 'Z'. This is used as Java 8's String.replace method uses Pattern.compile
57+
* internally without simple case opt-outs.
58+
*/
59+
private static final Pattern Z_PATTERN = Pattern.compile("Z");
5460

5561

5662
/**
@@ -193,11 +199,11 @@ public static OffsetDateTime parseDate(String dateString) {
193199
break;
194200
case 23: // "yyyy-MM-dd'T'HH:mm:ss.SS'Z'"-> [2012-01-04T23:21:59.12Z] length = 23
195201
// SS is assumed to be milliseconds, so a trailing 0 is necessary
196-
dateString = dateString.replace("Z", "0");
202+
dateString = Z_PATTERN.matcher(dateString).replaceAll("0");
197203
break;
198204
case 22: // "yyyy-MM-dd'T'HH:mm:ss.S'Z'"-> [2012-01-04T23:21:59.1Z] length = 22
199205
// S is assumed to be milliseconds, so trailing 0's are necessary
200-
dateString = dateString.replace("Z", "00");
206+
dateString = Z_PATTERN.matcher(dateString).replaceAll("00");
201207
break;
202208
case 20: // "yyyy-MM-dd'T'HH:mm:ss'Z'"-> [2012-01-04T23:21:59Z] length = 20
203209
pattern = Utility.ISO8601_PATTERN;

sdk/storage/azure-storage-common/src/main/java/com/azure/storage/common/policy/ScrubEtagPolicy.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
import com.azure.core.http.HttpRequest;
1111
import com.azure.core.http.HttpResponse;
1212
import com.azure.core.http.policy.HttpPipelinePolicy;
13+
import reactor.core.publisher.Flux;
14+
import reactor.core.publisher.Mono;
1315

1416
import java.nio.ByteBuffer;
1517
import java.nio.charset.Charset;
16-
17-
import reactor.core.publisher.Flux;
18-
import reactor.core.publisher.Mono;
18+
import java.util.regex.Pattern;
1919

2020
/**
2121
* Wraps any potential error responses from the service and applies post processing of the response's eTag header to
2222
* standardize the value.
2323
*/
2424
public class ScrubEtagPolicy implements HttpPipelinePolicy {
25+
private static final Pattern QUOTE_PATTERN = Pattern.compile("\"");
2526
private static final String ETAG = "eTag";
2627

2728
/**
@@ -49,9 +50,9 @@ private HttpResponse scrubETagHeader(HttpResponse unprocessedResponse) {
4950
}
5051
String eTag = eTagHeader.getValue();
5152

52-
eTag = eTag.replace("\"", "");
53+
eTag = QUOTE_PATTERN.matcher(eTag).replaceAll("");
5354
HttpHeaders headers = unprocessedResponse.getHeaders();
54-
headers.put(eTagHeader.getName(), eTag);
55+
headers.set(eTagHeader.getName(), eTag);
5556
return new InnerHttpResponse(unprocessedResponse, headers, unprocessedResponse.getRequest());
5657
}
5758

sdk/storage/azure-storage-file-share/src/main/java/com/azure/storage/file/share/ShareFileAsyncClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,8 @@ Mono<Response<ShareFileProperties>> downloadToFileWithResponse(String downloadFi
728728
private Mono<Response<ShareFileProperties>> downloadResponseInChunk(Response<ShareFileProperties> response,
729729
AsynchronousFileChannel channel, ShareFileRange range, ShareRequestConditions requestConditions,
730730
Context context) {
731-
return Mono.justOrEmpty(range).switchIfEmpty(Mono.just(new ShareFileRange(0, response.getValue()
732-
.getContentLength())))
731+
return Mono.justOrEmpty(range).switchIfEmpty(Mono.defer(() -> Mono.just(new ShareFileRange(0, response.getValue()
732+
.getContentLength()))))
733733
.map(currentRange -> {
734734
List<ShareFileRange> chunks = new ArrayList<>();
735735
for (long pos = currentRange.getStart(); pos < currentRange.getEnd(); pos += FILE_DEFAULT_BLOCK_SIZE) {
@@ -925,7 +925,7 @@ Mono<ShareFileDownloadAsyncResponse> downloadWithResponse(ShareFileDownloadOptio
925925
},
926926
retryOptions.getMaxRetryRequests(),
927927
range.getStart()
928-
).switchIfEmpty(Flux.just(ByteBuffer.wrap(new byte[0])));
928+
).switchIfEmpty(Flux.defer(() -> Flux.just(ByteBuffer.wrap(new byte[0]))));
929929

930930
return new ShareFileDownloadAsyncResponse(response.getRequest(), response.getStatusCode(),
931931
response.getHeaders(), bufferFlux, headers);

0 commit comments

Comments
 (0)