Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,10 @@ public void delete(BlobKey... blobKeys) {
try {
ApiProxy.makeSyncCall(PACKAGE, "DeleteBlob", request.build().toByteArray());
} catch (ApiProxy.ApplicationException ex) {
switch (BlobstoreServiceError.ErrorCode.forNumber(ex.getApplicationError())) {
case INTERNAL_ERROR:
throw new BlobstoreFailureException("An internal blobstore error occurred.");
default:
throw new BlobstoreFailureException("An unexpected error occurred.", ex);
}
throw switch (BlobstoreServiceError.ErrorCode.forNumber(ex.getApplicationError())) {
case INTERNAL_ERROR -> new BlobstoreFailureException("An internal blobstore error occurred.");
default -> new BlobstoreFailureException("An unexpected error occurred.", ex);
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,11 @@ protected TransactionImpl.InternalTransaction doBeginTransaction(TransactionOpti
}
}
if (options.transactionMode() != null) {
switch (options.transactionMode()) {
case READ_ONLY:
request.setMode(TransactionMode.READ_ONLY);
break;
case READ_WRITE:
request.setMode(TransactionMode.READ_WRITE);
break;
default:
throw new AssertionError("Unrecognized transaction mode: " + options.transactionMode());
}
request.setMode(switch (options.transactionMode()) {
case READ_ONLY -> TransactionMode.READ_ONLY;
case READ_WRITE -> TransactionMode.READ_WRITE;
default -> throw new AssertionError("Unrecognized transaction mode: " + options.transactionMode());
});
}

Future<DatastoreV3Pb.Transaction> future =
Expand Down Expand Up @@ -620,21 +615,11 @@ protected Map<Index, IndexState> wrap(CompositeIndices indices) throws Exception
for (CompositeIndex ci : indices.getIndexList()) {
Index index = IndexTranslator.convertFromPb(ci);
switch (ci.getState()) {
case DELETED:
answer.put(index, IndexState.DELETING);
break;
case ERROR:
answer.put(index, IndexState.ERROR);
break;
case READ_WRITE:
answer.put(index, IndexState.SERVING);
break;
case WRITE_ONLY:
answer.put(index, IndexState.BUILDING);
break;
default:
logger.log(Level.WARNING, "Unrecognized index state for " + index);
break;
case DELETED -> answer.put(index, IndexState.DELETING);
case ERROR -> answer.put(index, IndexState.ERROR);
case READ_WRITE -> answer.put(index, IndexState.SERVING);
case WRITE_ONLY -> answer.put(index, IndexState.BUILDING);
default -> logger.log(Level.WARNING, "Unrecognized index state for {0}", index);
}
}
return answer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,7 @@ public BaseQueryResultsSource(
this.offset = fetchOptions.getOffset() != null ? fetchOptions.getOffset() : 0;
this.txn = txn;
this.query = query;
this.currentTransactionProvider =
new CurrentTransactionProvider() {
@Override
public Transaction getCurrentTransaction(Transaction defaultValue) {
return txn;
}
};
this.currentTransactionProvider = defaultValue -> txn;
this.initialQueryResultFuture = initialQueryResultFuture;
this.skippedResults = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public int hashCode() {
/** Two {@code Blob} objects are considered equal if their contained bytes match exactly. */
@Override
public boolean equals(@Nullable Object object) {
if (object instanceof Blob) {
Blob key = (Blob) object;
if (object instanceof Blob key) {
return Arrays.equals(bytes, key.bytes);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.compute.ComputeCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.ExponentialBackOff;
import com.google.auto.value.AutoValue;
Expand Down Expand Up @@ -59,6 +57,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -126,67 +125,31 @@ static synchronized CloudDatastoreV1ClientImpl create(DatastoreServiceConfig con

@Override
public Future<BeginTransactionResponse> beginTransaction(final BeginTransactionRequest req) {
return makeCall(
new Callable<BeginTransactionResponse>() {
@Override
public BeginTransactionResponse call() throws DatastoreException {
return datastore.beginTransaction(req);
}
});
return makeCall(() -> datastore.beginTransaction(req));
}

@Override
public Future<RollbackResponse> rollback(final RollbackRequest req) {
return makeCall(
new Callable<RollbackResponse>() {
@Override
public RollbackResponse call() throws DatastoreException {
return datastore.rollback(req);
}
});
return makeCall(() -> datastore.rollback(req));
}

@Override
public Future<RunQueryResponse> runQuery(final RunQueryRequest req) {
return makeCall(
new Callable<RunQueryResponse>() {
@Override
public RunQueryResponse call() throws DatastoreException {
return datastore.runQuery(req);
}
});
return makeCall(() -> datastore.runQuery(req));
}

@Override
public Future<LookupResponse> lookup(final LookupRequest req) {
return makeCall(
new Callable<LookupResponse>() {
@Override
public LookupResponse call() throws DatastoreException {
return datastore.lookup(req);
}
});
return makeCall(() -> datastore.lookup(req));
}

@Override
public Future<AllocateIdsResponse> allocateIds(final AllocateIdsRequest req) {
return makeCall(
new Callable<AllocateIdsResponse>() {
@Override
public AllocateIdsResponse call() throws DatastoreException {
return datastore.allocateIds(req);
}
});
return makeCall(() -> datastore.allocateIds(req));
}

private Future<CommitResponse> commit(final CommitRequest req) {
return makeCall(
new Callable<CommitResponse>() {
@Override
public CommitResponse call() throws DatastoreException {
return datastore.commit(req);
}
});
return makeCall(() -> datastore.commit(req));
}

@Override
Expand Down Expand Up @@ -222,10 +185,11 @@ public T call() throws Exception {
return callable.call();
} catch (Exception e) {
if (isRetryable(e) && remainingTries > 0) {
logger.log(
Level.FINE,
String.format("Caught retryable exception; %d tries remaining", remainingTries),
e);
LogRecord lr =
new LogRecord(Level.FINE, "Caught retryable exception; {0} tries remaining");
lr.setParameters(new Object[] {remainingTries});
lr.setThrown(e);
logger.log(lr);
Thread.sleep(backoff.nextBackOffMillis());
} else {
throw e;
Expand All @@ -249,21 +213,17 @@ private <T extends Message> Future<T> makeCall(final Callable<T> oneAttempt) {
? new Exception()
: null;
return executor.submit(
new Callable<T>() {
@Override
public T call() throws Exception {
try {
return new RetryingCallable<>(oneAttempt, maxRetries).call();
} catch (DatastoreException e) {
String message =
stackTraceCapturer != null
? String.format(
"%s%nstack trace when async call was initiated: <%n%s>",
e.getMessage(), Throwables.getStackTraceAsString(stackTraceCapturer))
: String.format(
"%s%n(stack trace capture for async call is disabled)", e.getMessage());
throw DatastoreApiHelper.createV1Exception(e.getCode(), message, e);
}
() -> {
try {
return new RetryingCallable<>(oneAttempt, maxRetries).call();
} catch (DatastoreException e) {
String message =
stackTraceCapturer != null
? e.getMessage()
+ "\nstack trace when async call was initiated: <\n"
+ Throwables.getStackTraceAsString(stackTraceCapturer)
: e.getMessage() + "\n(stack trace capture for async call is disabled)";
throw DatastoreApiHelper.createV1Exception(e.getCode(), message, e);
}
});
}
Expand All @@ -275,13 +235,10 @@ private static DatastoreOptions createDatastoreOptions(
setProjectEndpoint(projectId, options);
options.credential(getCredential());
options.initializer(
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
request.setConnectTimeout(httpConnectTimeoutMillis);
if (config.getDeadline() != null) {
request.setReadTimeout((int) (config.getDeadline() * 1000));
}
request -> {
request.setConnectTimeout(httpConnectTimeoutMillis);
if (config.getDeadline() != null) {
request.setReadTimeout((int) (config.getDeadline() * 1000));
}
});
return options.build();
Expand All @@ -298,8 +255,7 @@ private static Credential getCredential() throws GeneralSecurityException, IOExc
if (privateKeyFile != null) {
logger.log(
Level.INFO,
"Service account and private key file were provided. "
+ "Using service account credential.");
"Service account and private key file were provided. Using service account credential.");
return getServiceAccountCredentialBuilder(serviceAccount)
.setServiceAccountPrivateKeyFromP12File(new File(privateKeyFile))
.build();
Expand All @@ -308,8 +264,7 @@ private static Credential getCredential() throws GeneralSecurityException, IOExc
if (privateKey != null) {
logger.log(
Level.INFO,
"Service account and private key were provided. "
+ "Using service account credential.");
"Service account and private key were provided. Using service account credential.");
return getServiceAccountCredentialBuilder(serviceAccount)
.setServiceAccountPrivateKey(privateKey)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,7 @@ protected String generateXmlForIndex(Index index, IndexSource source) {

/** We compare {@link Property Properties} by comparing their names. */
private static final Comparator<Property> PROPERTY_NAME_COMPARATOR =
new Comparator<Property>() {
@Override
public int compare(Property o1, Property o2) {
return o1.getName().compareTo(o2.getName());
}
};
(o1, o2) -> o1.getName().compareTo(o2.getName());

private List<Property> getRecommendedIndexProps(IndexComponentsOnlyQuery query) {
// Construct the list of index properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,6 @@ public RawValue getValue(Value propertyValue) {
}

@Override
@ SuppressWarnings("PatternMatchingInstanceof")
public @Nullable Comparable<?> asComparable(Object value) {
Object value2 = ((RawValue) value).getValue();
// All possible values except byte[] are already comparable.
Expand Down Expand Up @@ -2027,10 +2026,7 @@ public int compareTo(ComparableByteArray other) {

@Override
public boolean equals(@Nullable Object obj) {
if (obj == null) {
return false;
}
return Arrays.equals(bytes, ((ComparableByteArray) obj).bytes);
return obj instanceof ComparableByteArray other && Arrays.equals(bytes, other.bytes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,59 +63,41 @@ public static RuntimeException translateError(ApiProxy.ApplicationException exce
if (errorCode == null) {
return new DatastoreFailureException(exception.getErrorDetail());
}
switch (errorCode) {
case BAD_REQUEST:
return new IllegalArgumentException(exception.getErrorDetail());

case CONCURRENT_TRANSACTION:
return new ConcurrentModificationException(exception.getErrorDetail());

case NEED_INDEX:
return new DatastoreNeedIndexException(exception.getErrorDetail());

case TIMEOUT:
case BIGTABLE_ERROR:
return new DatastoreTimeoutException(exception.getErrorDetail());

case COMMITTED_BUT_STILL_APPLYING:
return new CommittedButStillApplyingException(exception.getErrorDetail());

case RESOURCE_EXHAUSTED:
return new ApiProxy.OverQuotaException(exception.getErrorDetail(), (Throwable) null);

case INTERNAL_ERROR:
default:
return new DatastoreFailureException(exception.getErrorDetail());
}
return switch (errorCode) {
case BAD_REQUEST -> new IllegalArgumentException(exception.getErrorDetail());
case CONCURRENT_TRANSACTION -> new ConcurrentModificationException(exception.getErrorDetail());
case NEED_INDEX -> new DatastoreNeedIndexException(exception.getErrorDetail());
case TIMEOUT, BIGTABLE_ERROR -> new DatastoreTimeoutException(exception.getErrorDetail());
case COMMITTED_BUT_STILL_APPLYING -> new CommittedButStillApplyingException(
exception.getErrorDetail());
case RESOURCE_EXHAUSTED -> new ApiProxy.OverQuotaException(
exception.getErrorDetail(), (Throwable) null);
default -> new DatastoreFailureException(exception.getErrorDetail());
};
}

static RuntimeException createV1Exception(Code code, String message, Throwable cause) {
if (code == null) {
return new DatastoreFailureException(message, cause);
}
switch (code) {
case ABORTED:
return new ConcurrentModificationException(message, cause);
case FAILED_PRECONDITION:
return switch (code) {
case ABORTED -> new ConcurrentModificationException(message, cause);
case FAILED_PRECONDITION -> {
if (message.contains("The Cloud Datastore API is not enabled for the project")) {
return new DatastoreFailureException(message, cause);
yield new DatastoreFailureException(message, cause);
}
// Could also indicate ErrorCode.SAFE_TIME_TOO_OLD.
return new DatastoreNeedIndexException(message, cause);
case DEADLINE_EXCEEDED:
return new DatastoreTimeoutException(message, cause);
case INVALID_ARGUMENT:
case PERMISSION_DENIED:
return new IllegalArgumentException(message, cause);
case UNAVAILABLE:
return new ApiProxy.RPCFailedException(message, cause);
case RESOURCE_EXHAUSTED:
return new ApiProxy.OverQuotaException(message, cause);
case INTERNAL:
// Could also indicate ErrorCode.COMMITTED_BUT_STILL_APPLYING.
default:
return new DatastoreFailureException(message, cause);
}
yield new DatastoreNeedIndexException(message, cause);
}
case DEADLINE_EXCEEDED -> new DatastoreTimeoutException(message, cause);
case INVALID_ARGUMENT, PERMISSION_DENIED -> new IllegalArgumentException(message, cause);
case UNAVAILABLE -> new ApiProxy.RPCFailedException(message, cause);
case RESOURCE_EXHAUSTED -> new ApiProxy.OverQuotaException(message, cause);
case INTERNAL ->
// Could also indicate ErrorCode.COMMITTED_BUT_STILL_APPLYING.
new DatastoreFailureException(message, cause);
default -> new DatastoreFailureException(message, cause);
};
}

static <T extends Message, S extends Message.Builder> Future<T> makeAsyncCall(
Expand Down Expand Up @@ -162,8 +144,8 @@ protected T wrap(byte[] responseBytes) throws InvalidProtocolBufferException {

@Override
protected Throwable convertException(Throwable cause) {
if (cause instanceof ApiProxy.ApplicationException) {
return translateError((ApiProxy.ApplicationException) cause);
if (cause instanceof ApiProxy.ApplicationException applicationException) {
return translateError(applicationException);
}
return cause;
}
Expand Down
Loading
Loading