Skip to content

Commit 411128b

Browse files
refactor(datastore): remove redundant null check for executionOptions in DatastoreImpl
1 parent d3fd1cc commit 411128b

16 files changed

Lines changed: 1087 additions & 143 deletions

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreExecutionOptions.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@
1818

1919
import com.google.api.core.BetaApi;
2020
import com.google.cloud.datastore.models.ExplainOptions;
21+
import com.google.cloud.datastore.models.RequestOptions;
2122
import com.google.common.base.Objects;
2223
import com.google.common.base.Preconditions;
2324
import com.google.common.collect.ImmutableList;
24-
import com.google.datastore.v1.RequestOptions;
2525
import java.util.Collections;
2626
import java.util.List;
27-
import javax.annotation.Nonnull;
28-
import javax.annotation.Nullable;
27+
import org.jspecify.annotations.NullMarked;
28+
import org.jspecify.annotations.Nullable;
2929

3030
/**
3131
* Class representing options for query execution in Google Cloud Datastore. Combines {@link
3232
* ExplainOptions}, {@link RequestOptions}, and {@link ReadOption}s.
3333
*/
3434
@BetaApi
35+
@NullMarked
3536
public class DatastoreExecutionOptions {
3637

37-
private final ExplainOptions explainOptions;
38+
private final @Nullable ExplainOptions explainOptions;
3839
private final RequestOptions requestOptions;
3940
private final List<ReadOption> readOptions;
4041

@@ -44,17 +45,14 @@ private DatastoreExecutionOptions(Builder builder) {
4445
this.readOptions = ImmutableList.copyOf(builder.readOptions);
4546
}
4647

47-
@Nullable
48-
public ExplainOptions getExplainOptions() {
48+
public @Nullable ExplainOptions getExplainOptions() {
4949
return explainOptions;
5050
}
5151

52-
@Nullable
5352
public RequestOptions getRequestOptions() {
5453
return requestOptions;
5554
}
5655

57-
@Nonnull
5856
public List<ReadOption> getReadOptions() {
5957
return readOptions;
6058
}
@@ -89,8 +87,8 @@ public static DatastoreExecutionOptions getDefaultInstance() {
8987

9088
/** Builder for {@link DatastoreExecutionOptions}. */
9189
public static class Builder {
92-
private ExplainOptions explainOptions;
93-
private RequestOptions requestOptions;
90+
private @Nullable ExplainOptions explainOptions;
91+
private RequestOptions requestOptions = RequestOptions.getDefaultInstance();
9492
private List<ReadOption> readOptions = Collections.emptyList();
9593

9694
private Builder() {}
@@ -106,12 +104,13 @@ public Builder setExplainOptions(@Nullable ExplainOptions explainOptions) {
106104
return this;
107105
}
108106

109-
public Builder setRequestOptions(@Nullable RequestOptions requestOptions) {
107+
public Builder setRequestOptions(RequestOptions requestOptions) {
108+
Preconditions.checkNotNull(requestOptions, "requestOptions cannot be null");
110109
this.requestOptions = requestOptions;
111110
return this;
112111
}
113112

114-
public Builder setReadOptions(@Nonnull List<ReadOption> readOptions) {
113+
public Builder setReadOptions(List<ReadOption> readOptions) {
115114
Preconditions.checkNotNull(readOptions, "readOptions cannot be null");
116115
this.readOptions = readOptions;
117116
return this;

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreImpl.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.datastore;
1818

19+
import static com.google.cloud.datastore.RequestOptionsHelper.createRequestOptions;
1920
import static com.google.cloud.datastore.telemetry.TelemetryConstants.ATTRIBUTES_KEY_DEFERRED;
2021
import static com.google.cloud.datastore.telemetry.TelemetryConstants.ATTRIBUTES_KEY_DOCUMENT_COUNT;
2122
import static com.google.cloud.datastore.telemetry.TelemetryConstants.ATTRIBUTES_KEY_MISSING;
@@ -111,7 +112,7 @@ final class DatastoreImpl extends BaseService<DatastoreOptions> implements Datas
111112
private static final ExceptionHandler TRANSACTION_OPERATION_EXCEPTION_HANDLER =
112113
TransactionOperationExceptionHandler.build();
113114

114-
private final com.google.cloud.datastore.telemetry.TraceUtil otelTraceUtil =
115+
private final TraceUtil otelTraceUtil =
115116
getOptions().getTraceUtil();
116117
private final DatastoreMetricsRecorder metricsRecorder;
117118
private final OpenTelemetry builtInOpenTelemetry;
@@ -180,7 +181,7 @@ static class TracedReadWriteTransactionCallable<T> implements Callable<T> {
180181

181182
TracedReadWriteTransactionCallable(
182183
ReadWriteTransactionCallable<T> delegate,
183-
@Nullable com.google.cloud.datastore.telemetry.TraceUtil.Span parentSpan) {
184+
@Nullable TraceUtil.Span parentSpan) {
184185
this.delegate = delegate;
185186
this.parentSpan = parentSpan;
186187
}
@@ -362,13 +363,16 @@ public <T> QueryResults<T> run(
362363
@Override
363364
@BetaApi
364365
public <T> QueryResults<T> run(Query<T> query, DatastoreExecutionOptions executionOptions) {
366+
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
365367
com.google.cloud.datastore.models.ExplainOptions explainOptions =
366368
executionOptions.getExplainOptions();
367369
return run(
368370
toReadOptionsPb(executionOptions.getReadOptions().toArray(new ReadOption[0])),
369371
query,
370372
explainOptions != null ? explainOptions.toPb() : null,
371-
executionOptions.getRequestOptions());
373+
executionOptions.getRequestOptions() != null
374+
? executionOptions.getRequestOptions().toPb()
375+
: null);
372376
}
373377

374378
@SuppressWarnings("unchecked")
@@ -435,7 +439,7 @@ RunQueryResponse runQuery(final RunQueryRequest requestPb) {
435439
return runWithObservability(
436440
() -> {
437441
RunQueryResponse response = datastoreRpc.runQuery(requestPb);
438-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
442+
TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
439443
if (span != null) {
440444
span.addEvent(
441445
spanName + " complete.",
@@ -488,7 +492,6 @@ public List<Key> allocateId(IncompleteKey... keys) {
488492
@Override
489493
@BetaApi
490494
public List<Key> allocateId(List<IncompleteKey> keys, DatastoreExecutionOptions executionOptions) {
491-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
492495
Preconditions.checkArgument(
493496
verifyIncompleteKeyType(keys), "keys must be IncompleteKey instances");
494497
if (keys.isEmpty()) {
@@ -501,7 +504,7 @@ public List<Key> allocateId(List<IncompleteKey> keys, DatastoreExecutionOptions
501504
requestPb.setProjectId(getOptions().getProjectId());
502505
requestPb.setDatabaseId(getOptions().getDatabaseId());
503506
requestPb.setRequestOptions(
504-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
507+
createRequestOptions(getOptions(), executionOptions));
505508
AllocateIdsResponse responsePb = allocateIds(requestPb.build());
506509
ImmutableList.Builder<Key> keyList = ImmutableList.builder();
507510
for (com.google.datastore.v1.Key keyPb : responsePb.getKeysList()) {
@@ -546,7 +549,6 @@ public List<Entity> add(FullEntity<?>... entities) {
546549
@Override
547550
@BetaApi
548551
public List<Entity> add(List<FullEntity<?>> entities, DatastoreExecutionOptions executionOptions) {
549-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
550552
if (entities.isEmpty()) {
551553
return Collections.emptyList();
552554
}
@@ -663,7 +665,7 @@ Iterator<Entity> get(
663665
requestPb.setProjectId(getOptions().getProjectId());
664666
requestPb.setDatabaseId(getOptions().getDatabaseId());
665667
requestPb.setRequestOptions(
666-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
668+
createRequestOptions(getOptions(), executionOptions));
667669
return new ResultsIterator(requestPb);
668670
}
669671

@@ -707,7 +709,7 @@ LookupResponse lookup(final LookupRequest requestPb) {
707709
return runWithObservability(
708710
() -> {
709711
LookupResponse response = datastoreRpc.lookup(requestPb);
710-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
712+
TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
711713
if (span != null) {
712714
span.addEvent(
713715
spanName + " complete.",
@@ -738,15 +740,14 @@ public List<Key> reserveIds(Key... keys) {
738740
@Override
739741
@BetaApi
740742
public List<Key> reserveIds(List<Key> keys, DatastoreExecutionOptions executionOptions) {
741-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
742743
ReserveIdsRequest.Builder requestPb = ReserveIdsRequest.newBuilder();
743744
for (Key key : keys) {
744745
requestPb.addKeys(key.toPb());
745746
}
746747
requestPb.setProjectId(getOptions().getProjectId());
747748
requestPb.setDatabaseId(getOptions().getDatabaseId());
748749
requestPb.setRequestOptions(
749-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
750+
createRequestOptions(getOptions(), executionOptions));
750751
ReserveIdsResponse responsePb = reserveIds(requestPb.build());
751752
ImmutableList.Builder<Key> keyList = ImmutableList.builder();
752753
if (responsePb.isInitialized()) {
@@ -773,7 +774,6 @@ public void update(Entity... entities) {
773774
@Override
774775
@BetaApi
775776
public void update(List<Entity> entities, DatastoreExecutionOptions executionOptions) {
776-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
777777
if (!entities.isEmpty()) {
778778
ImmutableList.Builder<Mutation> mutationsPb = ImmutableList.builder();
779779
Map<Key, Entity> dedupEntities = new LinkedHashMap<>();
@@ -801,7 +801,6 @@ public List<Entity> put(FullEntity<?>... entities) {
801801
@Override
802802
@BetaApi
803803
public List<Entity> put(List<FullEntity<?>> entities, DatastoreExecutionOptions executionOptions) {
804-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
805804
if (entities.isEmpty()) {
806805
return Collections.emptyList();
807806
}
@@ -842,7 +841,6 @@ public void delete(Key... keys) {
842841
@Override
843842
@BetaApi
844843
public void delete(List<Key> keys, DatastoreExecutionOptions executionOptions) {
845-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
846844
if (!keys.isEmpty()) {
847845
ImmutableList.Builder<Mutation> mutationsPb = ImmutableList.builder();
848846
Set<Key> dedupKeys = new LinkedHashSet<>(keys);
@@ -858,20 +856,15 @@ public KeyFactory newKeyFactory() {
858856
return DatastoreHelper.newKeyFactory(getOptions());
859857
}
860858

861-
private CommitResponse commitMutation(ImmutableList<Mutation> mutationsPb) {
862-
return commitMutation(mutationsPb, DatastoreExecutionOptions.getDefaultInstance());
863-
}
864-
865859
private CommitResponse commitMutation(
866860
ImmutableList<Mutation> mutationsPb, DatastoreExecutionOptions executionOptions) {
867861
CommitRequest.Builder requestPb =
868862
CommitRequest.newBuilder()
869863
.setMode(CommitRequest.Mode.NON_TRANSACTIONAL)
870864
.setProjectId(getOptions().getProjectId())
871865
.setDatabaseId(getOptions().getDatabaseId())
872-
.addAllMutations(mutationsPb);
873-
requestPb.setRequestOptions(
874-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
866+
.addAllMutations(mutationsPb)
867+
.setRequestOptions(createRequestOptions(getOptions(), executionOptions));
875868
return commit(requestPb.build());
876869
}
877870

@@ -883,7 +876,7 @@ CommitResponse commit(final CommitRequest requestPb) {
883876
return runWithObservability(
884877
() -> {
885878
CommitResponse response = datastoreRpc.commit(requestPb);
886-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
879+
TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
887880
if (span != null) {
888881
span.addEvent(
889882
spanName + " complete.",
@@ -925,16 +918,15 @@ void rollbackTransaction(ByteString transaction, DatastoreExecutionOptions execu
925918
requestPb.setTransaction(transaction);
926919
requestPb.setProjectId(getOptions().getProjectId());
927920
requestPb.setDatabaseId(getOptions().getDatabaseId());
928-
requestPb.setRequestOptions(
929-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
921+
requestPb.setRequestOptions(createRequestOptions(getOptions(), executionOptions));
930922
rollback(requestPb.build());
931923
}
932924

933925
void rollback(final RollbackRequest requestPb) {
934926
runWithObservability(
935927
() -> {
936928
datastoreRpc.rollback(requestPb);
937-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
929+
TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
938930
if (span != null) {
939931
span.addEvent(
940932
SPAN_NAME_ROLLBACK,
@@ -954,7 +946,7 @@ private <T> T runWithObservability(
954946
String methodName,
955947
String spanName,
956948
ResultRetryAlgorithm<?> exceptionHandler) {
957-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.startSpan(spanName);
949+
TraceUtil.Span span = otelTraceUtil.startSpan(spanName);
958950

959951
Stopwatch operationStopwatch = Stopwatch.createStarted();
960952
String operationStatus = StatusCode.Code.OK.toString();

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.datastore;
1818

1919
import static com.google.cloud.datastore.Validator.validateNamespace;
20+
import static com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST;
2021

2122
import com.google.api.core.BetaApi;
2223
import com.google.api.gax.grpc.ChannelPoolSettings;
@@ -240,7 +241,7 @@ public DatastoreOptions build() {
240241
if (this.transportOptions instanceof GrpcTransportOptions) {
241242
this.setHost(DatastoreSettings.getDefaultEndpoint());
242243
} else if (this.transportOptions instanceof HttpTransportOptions) {
243-
this.setHost(com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST);
244+
this.setHost(DEFAULT_HOST);
244245
}
245246
}
246247
return new DatastoreOptions(this);
@@ -260,6 +261,9 @@ public Builder setDatabaseId(String databaseId) {
260261
/**
261262
* Sets the request tags to be associated with all requests sent by this client.
262263
*
264+
* <p>These instance-level tags will be merged with any request-level options or tags specified
265+
* via {@link DatastoreExecutionOptions} before sending a request.
266+
*
263267
* @param requestTags the list of request tags to set
264268
* @return the builder object
265269
*/
@@ -340,7 +344,7 @@ public TransportChannelProvider getTransportChannelProvider() {
340344
@Override
341345
protected String getDefaultHost() {
342346
String host = System.getProperty(LOCAL_HOST_ENV_VAR, System.getenv(LOCAL_HOST_ENV_VAR));
343-
return host != null ? host : com.google.datastore.v1.client.DatastoreFactory.DEFAULT_HOST;
347+
return host != null ? host : DEFAULT_HOST;
344348
}
345349

346350
@Override
@@ -393,6 +397,9 @@ public String getDatabaseId() {
393397
/**
394398
* Returns the request tags to be associated with all requests sent by this client.
395399
*
400+
* <p>These instance-level tags are merged with any request-level options or tags passed via {@link
401+
* DatastoreExecutionOptions} before sending a request.
402+
*
396403
* @return the request tags
397404
*/
398405
public List<String> getRequestTags() {

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/QueryResultsImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.datastore;
1818

19+
import static com.google.cloud.datastore.RequestOptionsHelper.createRequestOptions;
1920
import com.google.api.core.BetaApi;
2021
import com.google.cloud.datastore.Query.ResultType;
2122
import com.google.cloud.datastore.models.ExplainMetrics;
@@ -130,10 +131,8 @@ private void sendRequest() {
130131
if (explainOptions != null) {
131132
requestPb.setExplainOptions(explainOptions);
132133
}
133-
if (requestOptions != null) {
134-
requestPb.setRequestOptions(
135-
RequestOptionsHelper.createRequestOptions(datastore.getOptions(), requestOptions));
136-
}
134+
requestPb.setRequestOptions(
135+
createRequestOptions(datastore.getOptions(), requestOptions));
137136
query.populatePb(requestPb);
138137
runQueryResponsePb = datastore.runQuery(requestPb.build());
139138
mostRecentQueryPb = requestPb.getQuery();

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/ReadOption.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.datastore.v1.RequestOptions;
2626
import com.google.protobuf.ByteString;
2727
import java.io.Serializable;
28-
import java.util.Collections;
2928
import java.util.List;
3029
import java.util.Map;
3130

@@ -183,18 +182,20 @@ public RequestOptions getRequestOptions() {
183182
public static <Q extends Query<?>> QueryConfig<Q> createWithDatastoreExecutionOptions(
184183
Q query, DatastoreExecutionOptions executionOptions) {
185184
Preconditions.checkNotNull(query, "query cannot be null");
186-
if (executionOptions != null) {
187-
ExplainOptions explainOptions =
188-
executionOptions.getExplainOptions() != null
189-
? executionOptions.getExplainOptions().toPb()
190-
: null;
191-
return new QueryConfig<>(
192-
query,
193-
explainOptions,
194-
executionOptions.getReadOptions(),
195-
executionOptions.getRequestOptions());
196-
}
197-
return new QueryConfig<>(query, null, Collections.emptyList(), null);
185+
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
186+
ExplainOptions explainOptions =
187+
executionOptions.getExplainOptions() != null
188+
? executionOptions.getExplainOptions().toPb()
189+
: null;
190+
RequestOptions requestOptions =
191+
executionOptions.getRequestOptions() != null
192+
? executionOptions.getRequestOptions().toPb()
193+
: null;
194+
return new QueryConfig<>(
195+
query,
196+
explainOptions,
197+
executionOptions.getReadOptions(),
198+
requestOptions);
198199
}
199200
}
200201
}

0 commit comments

Comments
 (0)