Skip to content

Commit e7f15f4

Browse files
authored
Merge pull request #342 from wayne-pq/feat/optimized-code-wayne
fix: Avoid Optional in fields/params - intended solely for return types
2 parents 0fd1e8b + 3a6cdec commit e7f15f4

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

databend-client/src/main/java/com/databend/client/DatabendClientV1.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.time.Duration;
2525
import java.util.List;
2626
import java.util.Map;
27-
import java.util.OptionalLong;
2827
import java.util.concurrent.atomic.AtomicReference;
2928
import java.util.function.Consumer;
3029
import java.util.logging.Level;
@@ -52,7 +51,7 @@ public class DatabendClientV1
5251

5352
public static final String QUERY_PATH = "/v1/query";
5453
public static final String DISCOVERY_PATH = "/v1/discovery_nodes";
55-
private static final long MAX_MATERIALIZED_JSON_RESPONSE_SIZE = 128 * 1024;
54+
private static final Long MAX_MATERIALIZED_JSON_RESPONSE_SIZE = 128 * 1024L;
5655
private final OkHttpClient httpClient;
5756
private final String query;
5857
private final String host;
@@ -103,7 +102,7 @@ public static List<DiscoveryNode> discoverNodes(OkHttpClient httpClient, ClientS
103102
requireNonNull(settings, "settings is null");
104103
requireNonNull(settings.getHost(), "settings.host is null");
105104
Request request = buildDiscoveryRequest(settings);
106-
DiscoveryResponseCodec.DiscoveryResponse response = getDiscoveryResponse(httpClient, request, OptionalLong.empty(), settings.getQueryTimeoutSecs());
105+
DiscoveryResponseCodec.DiscoveryResponse response = getDiscoveryResponse(httpClient, request, null, settings.getQueryTimeoutSecs());
107106
return response.getNodes();
108107
}
109108

@@ -159,7 +158,7 @@ public String getQuery() {
159158
return query;
160159
}
161160

162-
private static DiscoveryResponseCodec.DiscoveryResponse getDiscoveryResponse(OkHttpClient httpClient, Request request, OptionalLong materializedJsonSizeLimit, int requestTimeoutSecs) {
161+
private static DiscoveryResponseCodec.DiscoveryResponse getDiscoveryResponse(OkHttpClient httpClient, Request request, Long materializedJsonSizeLimit, int requestTimeoutSecs) {
163162
requireNonNull(request, "request is null");
164163

165164
long start = System.nanoTime();
@@ -227,7 +226,7 @@ private static DiscoveryResponseCodec.DiscoveryResponse getDiscoveryResponse(OkH
227226
}
228227
}
229228

230-
private boolean executeInternal(Request request, OptionalLong materializedJsonSizeLimit) {
229+
private boolean executeInternal(Request request, Long materializedJsonSizeLimit) {
231230
requireNonNull(request, "request is null");
232231

233232
long start = System.nanoTime();
@@ -305,7 +304,7 @@ private boolean executeInternal(Request request, OptionalLong materializedJsonSi
305304

306305
@Override
307306
public boolean execute(Request request) {
308-
return executeInternal(request, OptionalLong.empty());
307+
return executeInternal(request, null);
309308
}
310309

311310
private void processResponse(Headers headers, QueryResults results) {
@@ -348,7 +347,7 @@ public boolean advance() {
348347
Request.Builder builder = prepareRequest(url, this.additionalHeaders);
349348
builder.addHeader(ClientSettings.X_DATABEND_STICKY_NODE, this.nodeID);
350349
Request request = builder.get().build();
351-
return executeInternal(request, OptionalLong.of(MAX_MATERIALIZED_JSON_RESPONSE_SIZE));
350+
return executeInternal(request, MAX_MATERIALIZED_JSON_RESPONSE_SIZE);
352351
}
353352

354353
@Override

databend-client/src/main/java/com/databend/client/JsonResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import javax.annotation.Nullable;
2121
import java.io.IOException;
2222
import java.io.UncheckedIOException;
23+
import java.util.Objects;
2324
import java.util.Optional;
24-
import java.util.OptionalLong;
2525

2626
import static com.google.common.base.MoreObjects.toStringHelper;
2727
import static com.google.common.net.HttpHeaders.LOCATION;
@@ -59,7 +59,7 @@ private JsonResponse(int statusCode, String statusMessage, Headers headers, @Nul
5959
this.hasValue = (exception == null);
6060
}
6161

62-
public static <T> JsonResponse<T> execute(JsonCodec<T> codec, OkHttpClient client, Request request, OptionalLong materializedJsonSizeLimit) throws RuntimeException {
62+
public static <T> JsonResponse<T> execute(JsonCodec<T> codec, OkHttpClient client, Request request, Long materializedJsonSizeLimit) throws RuntimeException {
6363
try (Response response = client.newCall(request).execute()) {
6464
// TODO: fix in OkHttp: https://github.com/square/okhttp/issues/3111
6565
if ((response.code() == 307) || (response.code() == 308)) {
@@ -76,7 +76,7 @@ public static <T> JsonResponse<T> execute(JsonCodec<T> codec, OkHttpClient clien
7676
T value = null;
7777
IllegalArgumentException exception = null;
7878
try {
79-
if (materializedJsonSizeLimit.isPresent() && (responseBody.contentLength() < 0 || responseBody.contentLength() > materializedJsonSizeLimit.getAsLong())) {
79+
if (!Objects.isNull(materializedJsonSizeLimit) && (responseBody.contentLength() < 0 || responseBody.contentLength() > materializedJsonSizeLimit)) {
8080
// Parse from input stream, response is either of unknown size or too large to materialize. Raw response body
8181
// will not be available if parsing fails
8282
value = codec.fromJson(responseBody.byteStream());

0 commit comments

Comments
 (0)