use of com.google.apphosting.utils.remoteapi.RemoteApiPb.TransactionRequest in project appengine-java-standard by GoogleCloudPlatform.
the class RemoteApiServlet method executeTx.
private byte[] executeTx(Request request) {
TransactionRequest txRequest = new TransactionRequest();
parseFromBytes(txRequest, request.getRequestAsBytes());
byte[] tx = beginTransaction(txRequest.isAllowMultipleEg());
List<Precondition> preconditions = txRequest.preconditions();
// Check transaction preconditions
if (!preconditions.isEmpty()) {
GetRequest getRequest = new GetRequest();
for (Precondition precondition : preconditions) {
OnestoreEntity.Reference key = precondition.getKey();
OnestoreEntity.Reference requestKey = getRequest.addKey();
requestKey.mergeFrom(key);
}
GetResponse getResponse = txGet(tx, getRequest);
List<GetResponse.Entity> entities = getResponse.entitys();
// TODO: Consider supporting deferred gets here.
assert (entities.size() == preconditions.size());
for (int i = 0; i < entities.size(); i++) {
// Throw an exception if any of the Entities don't match the Precondition specification.
assertEntityResultMatchesPrecondition(entities.get(i), preconditions.get(i));
}
}
// Preconditions OK.
// Perform puts.
// a serialized VoidProto
byte[] res = new byte[0];
if (txRequest.hasPuts()) {
PutRequest putRequest = txRequest.getPuts();
parseFromBytes(putRequest.getMutableTransaction(), tx);
res = ApiProxy.makeSyncCall("datastore_v3", "Put", putRequest.toByteArray());
}
// Perform deletes.
if (txRequest.hasDeletes()) {
DeleteRequest deleteRequest = txRequest.getDeletes();
parseFromBytes(deleteRequest.getMutableTransaction(), tx);
ApiProxy.makeSyncCall("datastore_v3", "Delete", deleteRequest.toByteArray());
}
// Commit transaction.
ApiProxy.makeSyncCall("datastore_v3", "Commit", tx);
return res;
}
Aggregations