Search in sources :

Example 1 with DeleteRequest

use of com.google.apphosting.datastore.DatastoreV3Pb.DeleteRequest 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;
}
Also used : OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity) Precondition(com.google.apphosting.utils.remoteapi.RemoteApiPb.TransactionRequest.Precondition) PutRequest(com.google.apphosting.datastore.DatastoreV3Pb.PutRequest) GetResponse(com.google.apphosting.datastore.DatastoreV3Pb.GetResponse) BeginTransactionRequest(com.google.apphosting.datastore.DatastoreV3Pb.BeginTransactionRequest) TransactionRequest(com.google.apphosting.utils.remoteapi.RemoteApiPb.TransactionRequest) GetRequest(com.google.apphosting.datastore.DatastoreV3Pb.GetRequest) OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity) DeleteRequest(com.google.apphosting.datastore.DatastoreV3Pb.DeleteRequest)

Example 2 with DeleteRequest

use of com.google.apphosting.datastore.DatastoreV3Pb.DeleteRequest in project appengine-java-standard by GoogleCloudPlatform.

the class AsyncDatastoreServiceImpl method doBatchDelete.

@Override
protected Future<Void> doBatchDelete(@Nullable Transaction txn, Collection<Key> keys) {
    DeleteRequest baseReq = new DeleteRequest();
    if (txn != null) {
        TransactionImpl.ensureTxnActive(txn);
        baseReq.setTransaction(InternalTransactionV3.toProto(txn));
    }
    // Do not group inside a transaction.
    boolean group = !baseReq.hasTransaction();
    Iterator<DeleteRequest> batches = deleteBatcher.getBatches(keys, baseReq, baseReq.getSerializedSize(), group);
    List<Future<DeleteResponse>> futures = deleteBatcher.makeCalls(batches);
    return registerInTransaction(txn, new MultiFuture<DeleteResponse, Void>(futures) {

        @Override
        public Void get() throws InterruptedException, ExecutionException {
            for (Future<DeleteResponse> future : futures) {
                future.get();
            }
            return null;
        }

        @Override
        public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            for (Future<DeleteResponse> future : futures) {
                future.get(timeout, unit);
            }
            return null;
        }
    });
}
Also used : DeleteResponse(com.google.apphosting.datastore.DatastoreV3Pb.DeleteResponse) Future(java.util.concurrent.Future) MultiFuture(com.google.appengine.api.datastore.FutureHelper.MultiFuture) ReorderingMultiFuture(com.google.appengine.api.datastore.Batcher.ReorderingMultiFuture) TimeUnit(java.util.concurrent.TimeUnit) ExecutionException(java.util.concurrent.ExecutionException) DeleteRequest(com.google.apphosting.datastore.DatastoreV3Pb.DeleteRequest) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

DeleteRequest (com.google.apphosting.datastore.DatastoreV3Pb.DeleteRequest)2 ReorderingMultiFuture (com.google.appengine.api.datastore.Batcher.ReorderingMultiFuture)1 MultiFuture (com.google.appengine.api.datastore.FutureHelper.MultiFuture)1 BeginTransactionRequest (com.google.apphosting.datastore.DatastoreV3Pb.BeginTransactionRequest)1 DeleteResponse (com.google.apphosting.datastore.DatastoreV3Pb.DeleteResponse)1 GetRequest (com.google.apphosting.datastore.DatastoreV3Pb.GetRequest)1 GetResponse (com.google.apphosting.datastore.DatastoreV3Pb.GetResponse)1 PutRequest (com.google.apphosting.datastore.DatastoreV3Pb.PutRequest)1 TransactionRequest (com.google.apphosting.utils.remoteapi.RemoteApiPb.TransactionRequest)1 Precondition (com.google.apphosting.utils.remoteapi.RemoteApiPb.TransactionRequest.Precondition)1 OnestoreEntity (com.google.storage.onestore.v3.OnestoreEntity)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1