Skip to content

Commit c2d9004

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

11 files changed

Lines changed: 99 additions & 111 deletions

File tree

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@
2424
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 RequestOptions requestOptions;
38+
private final @Nullable ExplainOptions explainOptions;
39+
private final @Nullable RequestOptions requestOptions;
3940
private final List<ReadOption> readOptions;
4041

4142
private DatastoreExecutionOptions(Builder builder) {
@@ -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
53-
public RequestOptions getRequestOptions() {
52+
public @Nullable 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 @Nullable RequestOptions requestOptions;
9492
private List<ReadOption> readOptions = Collections.emptyList();
9593

9694
private Builder() {}
@@ -111,7 +109,7 @@ public Builder setRequestOptions(@Nullable RequestOptions requestOptions) {
111109
return this;
112110
}
113111

114-
public Builder setReadOptions(@Nonnull List<ReadOption> readOptions) {
112+
public Builder setReadOptions(List<ReadOption> readOptions) {
115113
Preconditions.checkNotNull(readOptions, "readOptions cannot be null");
116114
this.readOptions = readOptions;
117115
return this;

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

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_LOOKUP;
3636
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_RUN;
3737
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_RUN_QUERY;
38+
import static com.google.cloud.datastore.RequestOptionsHelper.createRequestOptions;
3839

3940
import com.google.api.core.BetaApi;
4041
import com.google.api.gax.retrying.ResultRetryAlgorithm;
@@ -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
}
@@ -435,7 +436,7 @@ RunQueryResponse runQuery(final RunQueryRequest requestPb) {
435436
return runWithObservability(
436437
() -> {
437438
RunQueryResponse response = datastoreRpc.runQuery(requestPb);
438-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
439+
TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
439440
if (span != null) {
440441
span.addEvent(
441442
spanName + " complete.",
@@ -488,7 +489,6 @@ public List<Key> allocateId(IncompleteKey... keys) {
488489
@Override
489490
@BetaApi
490491
public List<Key> allocateId(List<IncompleteKey> keys, DatastoreExecutionOptions executionOptions) {
491-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
492492
Preconditions.checkArgument(
493493
verifyIncompleteKeyType(keys), "keys must be IncompleteKey instances");
494494
if (keys.isEmpty()) {
@@ -501,7 +501,7 @@ public List<Key> allocateId(List<IncompleteKey> keys, DatastoreExecutionOptions
501501
requestPb.setProjectId(getOptions().getProjectId());
502502
requestPb.setDatabaseId(getOptions().getDatabaseId());
503503
requestPb.setRequestOptions(
504-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
504+
createRequestOptions(getOptions(), executionOptions));
505505
AllocateIdsResponse responsePb = allocateIds(requestPb.build());
506506
ImmutableList.Builder<Key> keyList = ImmutableList.builder();
507507
for (com.google.datastore.v1.Key keyPb : responsePb.getKeysList()) {
@@ -546,7 +546,6 @@ public List<Entity> add(FullEntity<?>... entities) {
546546
@Override
547547
@BetaApi
548548
public List<Entity> add(List<FullEntity<?>> entities, DatastoreExecutionOptions executionOptions) {
549-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
550549
if (entities.isEmpty()) {
551550
return Collections.emptyList();
552551
}
@@ -567,7 +566,7 @@ public List<Entity> add(List<FullEntity<?>> entities, DatastoreExecutionOptions
567566
}
568567
mutationsPb.add(Mutation.newBuilder().setInsert(entity.toPb()).build());
569568
}
570-
CommitResponse commitResponse = commitMutation(mutationsPb.build(), executionOptions);
569+
CommitResponse commitResponse = commitMutation(mutationsPb.build());
571570
Iterator<MutationResult> mutationResults = commitResponse.getMutationResultsList().iterator();
572571
ImmutableList.Builder<Entity> responseBuilder = ImmutableList.builder();
573572
for (FullEntity<?> entity : entities) {
@@ -663,7 +662,7 @@ Iterator<Entity> get(
663662
requestPb.setProjectId(getOptions().getProjectId());
664663
requestPb.setDatabaseId(getOptions().getDatabaseId());
665664
requestPb.setRequestOptions(
666-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
665+
createRequestOptions(getOptions(), executionOptions));
667666
return new ResultsIterator(requestPb);
668667
}
669668

@@ -707,7 +706,7 @@ LookupResponse lookup(final LookupRequest requestPb) {
707706
return runWithObservability(
708707
() -> {
709708
LookupResponse response = datastoreRpc.lookup(requestPb);
710-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
709+
TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
711710
if (span != null) {
712711
span.addEvent(
713712
spanName + " complete.",
@@ -738,15 +737,14 @@ public List<Key> reserveIds(Key... keys) {
738737
@Override
739738
@BetaApi
740739
public List<Key> reserveIds(List<Key> keys, DatastoreExecutionOptions executionOptions) {
741-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
742740
ReserveIdsRequest.Builder requestPb = ReserveIdsRequest.newBuilder();
743741
for (Key key : keys) {
744742
requestPb.addKeys(key.toPb());
745743
}
746744
requestPb.setProjectId(getOptions().getProjectId());
747745
requestPb.setDatabaseId(getOptions().getDatabaseId());
748746
requestPb.setRequestOptions(
749-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
747+
createRequestOptions(getOptions(), executionOptions));
750748
ReserveIdsResponse responsePb = reserveIds(requestPb.build());
751749
ImmutableList.Builder<Key> keyList = ImmutableList.builder();
752750
if (responsePb.isInitialized()) {
@@ -773,7 +771,6 @@ public void update(Entity... entities) {
773771
@Override
774772
@BetaApi
775773
public void update(List<Entity> entities, DatastoreExecutionOptions executionOptions) {
776-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
777774
if (!entities.isEmpty()) {
778775
ImmutableList.Builder<Mutation> mutationsPb = ImmutableList.builder();
779776
Map<Key, Entity> dedupEntities = new LinkedHashMap<>();
@@ -783,7 +780,7 @@ public void update(List<Entity> entities, DatastoreExecutionOptions executionOpt
783780
for (Entity entity : dedupEntities.values()) {
784781
mutationsPb.add(Mutation.newBuilder().setUpdate(entity.toPb()).build());
785782
}
786-
commitMutation(mutationsPb.build(), executionOptions);
783+
commitMutation(mutationsPb.build());
787784
}
788785
}
789786

@@ -801,7 +798,6 @@ public List<Entity> put(FullEntity<?>... entities) {
801798
@Override
802799
@BetaApi
803800
public List<Entity> put(List<FullEntity<?>> entities, DatastoreExecutionOptions executionOptions) {
804-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
805801
if (entities.isEmpty()) {
806802
return Collections.emptyList();
807803
}
@@ -819,7 +815,7 @@ public List<Entity> put(List<FullEntity<?>> entities, DatastoreExecutionOptions
819815
for (Entity entity : dedupEntities.values()) {
820816
mutationsPb.add(Mutation.newBuilder().setUpsert(entity.toPb()).build());
821817
}
822-
CommitResponse commitResponse = commitMutation(mutationsPb.build(), executionOptions);
818+
CommitResponse commitResponse = commitMutation(mutationsPb.build());
823819
Iterator<MutationResult> mutationResults = commitResponse.getMutationResultsList().iterator();
824820
ImmutableList.Builder<Entity> responseBuilder = ImmutableList.builder();
825821
for (FullEntity<?> entity : entities) {
@@ -842,14 +838,13 @@ public void delete(Key... keys) {
842838
@Override
843839
@BetaApi
844840
public void delete(List<Key> keys, DatastoreExecutionOptions executionOptions) {
845-
Preconditions.checkNotNull(executionOptions, "executionOptions cannot be null");
846841
if (!keys.isEmpty()) {
847842
ImmutableList.Builder<Mutation> mutationsPb = ImmutableList.builder();
848843
Set<Key> dedupKeys = new LinkedHashSet<>(keys);
849844
for (Key key : dedupKeys) {
850845
mutationsPb.add(Mutation.newBuilder().setDelete(key.toPb()).build());
851846
}
852-
commitMutation(mutationsPb.build(), executionOptions);
847+
commitMutation(mutationsPb.build());
853848
}
854849
}
855850

@@ -859,19 +854,14 @@ public KeyFactory newKeyFactory() {
859854
}
860855

861856
private CommitResponse commitMutation(ImmutableList<Mutation> mutationsPb) {
862-
return commitMutation(mutationsPb, DatastoreExecutionOptions.getDefaultInstance());
863-
}
864-
865-
private CommitResponse commitMutation(
866-
ImmutableList<Mutation> mutationsPb, DatastoreExecutionOptions executionOptions) {
867857
CommitRequest.Builder requestPb =
868858
CommitRequest.newBuilder()
869859
.setMode(CommitRequest.Mode.NON_TRANSACTIONAL)
870860
.setProjectId(getOptions().getProjectId())
871861
.setDatabaseId(getOptions().getDatabaseId())
872862
.addAllMutations(mutationsPb);
873863
requestPb.setRequestOptions(
874-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
864+
createRequestOptions(getOptions(), null));
875865
return commit(requestPb.build());
876866
}
877867

@@ -883,7 +873,7 @@ CommitResponse commit(final CommitRequest requestPb) {
883873
return runWithObservability(
884874
() -> {
885875
CommitResponse response = datastoreRpc.commit(requestPb);
886-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
876+
TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
887877
if (span != null) {
888878
span.addEvent(
889879
spanName + " complete.",
@@ -917,24 +907,20 @@ BeginTransactionResponse beginTransaction(final BeginTransactionRequest requestP
917907
}
918908

919909
void rollbackTransaction(ByteString transaction) {
920-
rollbackTransaction(transaction, DatastoreExecutionOptions.getDefaultInstance());
921-
}
922-
923-
void rollbackTransaction(ByteString transaction, DatastoreExecutionOptions executionOptions) {
924910
RollbackRequest.Builder requestPb = RollbackRequest.newBuilder();
925911
requestPb.setTransaction(transaction);
926912
requestPb.setProjectId(getOptions().getProjectId());
927913
requestPb.setDatabaseId(getOptions().getDatabaseId());
928914
requestPb.setRequestOptions(
929-
RequestOptionsHelper.createRequestOptions(getOptions(), executionOptions));
915+
createRequestOptions(getOptions(), null));
930916
rollback(requestPb.build());
931917
}
932918

933919
void rollback(final RollbackRequest requestPb) {
934920
runWithObservability(
935921
() -> {
936922
datastoreRpc.rollback(requestPb);
937-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
923+
TraceUtil.Span span = otelTraceUtil.getCurrentSpan();
938924
if (span != null) {
939925
span.addEvent(
940926
SPAN_NAME_ROLLBACK,
@@ -954,7 +940,7 @@ private <T> T runWithObservability(
954940
String methodName,
955941
String spanName,
956942
ResultRetryAlgorithm<?> exceptionHandler) {
957-
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.startSpan(spanName);
943+
TraceUtil.Span span = otelTraceUtil.startSpan(spanName);
958944

959945
Stopwatch operationStopwatch = Stopwatch.createStarted();
960946
String operationStatus = StatusCode.Code.OK.toString();

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

Lines changed: 3 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);
@@ -340,7 +341,7 @@ public TransportChannelProvider getTransportChannelProvider() {
340341
@Override
341342
protected String getDefaultHost() {
342343
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;
344+
return host != null ? host : DEFAULT_HOST;
344345
}
345346

346347
@Override

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

Lines changed: 2 additions & 1 deletion
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;
@@ -132,7 +133,7 @@ private void sendRequest() {
132133
}
133134
if (requestOptions != null) {
134135
requestPb.setRequestOptions(
135-
RequestOptionsHelper.createRequestOptions(datastore.getOptions(), requestOptions));
136+
createRequestOptions(datastore.getOptions(), requestOptions));
136137
}
137138
query.populatePb(requestPb);
138139
runQueryResponsePb = datastore.runQuery(requestPb.build());

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

Lines changed: 10 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,16 @@ 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+
return new QueryConfig<>(
191+
query,
192+
explainOptions,
193+
executionOptions.getReadOptions(),
194+
executionOptions.getRequestOptions());
198195
}
199196
}
200197
}

0 commit comments

Comments
 (0)