Search in sources :

Example 1 with PutRequest

use of com.google.apphosting.datastore.DatastoreV3Pb.PutRequest 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 PutRequest

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

the class RemoteApiServlet method executeGetIDs.

private byte[] executeGetIDs(Request request, boolean isXG) {
    PutRequest putRequest = new PutRequest();
    parseFromBytes(putRequest, request.getRequestAsBytes());
    for (EntityProto entity : putRequest.entitys()) {
        assert (entity.propertySize() == 0);
        assert (entity.rawPropertySize() == 0);
        assert (entity.getEntityGroup().elementSize() == 0);
        List<Element> elementList = entity.getKey().getPath().elements();
        Element lastPart = elementList.get(elementList.size() - 1);
        assert (lastPart.getId() == 0);
        assert (!lastPart.hasName());
    }
    // Start a Transaction.
    // TODO: Shouldn't this use allocateIds instead?
    byte[] tx = beginTransaction(isXG);
    parseFromBytes(putRequest.getMutableTransaction(), tx);
    // Make a put request for a bunch of empty entities with the requisite
    // paths.
    byte[] res = ApiProxy.makeSyncCall("datastore_v3", "Put", putRequest.toByteArray());
    // Roll back the transaction so we don't actually insert anything.
    rollback(tx);
    return res;
}
Also used : Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element) PutRequest(com.google.apphosting.datastore.DatastoreV3Pb.PutRequest) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 3 with PutRequest

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

the class AsyncDatastoreServiceImpl method doBatchPut.

@Override
protected Future<List<Key>> doBatchPut(@Nullable Transaction txn, final List<Entity> entities) {
    PutRequest baseReq = new PutRequest();
    if (txn != null) {
        TransactionImpl.ensureTxnActive(txn);
        baseReq.setTransaction(InternalTransactionV3.toProto(txn));
    }
    // Do not group when inside a transaction.
    boolean group = !baseReq.hasTransaction();
    final List<Integer> order = Lists.newArrayListWithCapacity(entities.size());
    Iterator<PutRequest> batches = putBatcher.getBatches(entities, baseReq, baseReq.getSerializedSize(), group, order);
    List<Future<PutResponse>> futures = putBatcher.makeCalls(batches);
    return registerInTransaction(txn, new ReorderingMultiFuture<PutResponse, List<Key>>(futures, order) {

        @Override
        protected List<Key> aggregate(PutResponse intermediateResult, Iterator<Integer> indexItr, List<Key> result) {
            for (Reference reference : intermediateResult.keys()) {
                int index = indexItr.next();
                Key key = entities.get(index).getKey();
                KeyTranslator.updateKey(reference, key);
                result.set(index, key);
            }
            return result;
        }

        @Override
        protected List<Key> initResult() {
            // Create an array pre-populated with null values (twice :-))
            List<Key> result = new ArrayList<Key>(Collections.<Key>nCopies(order.size(), null));
            return result;
        }
    });
}
Also used : Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) PutRequest(com.google.apphosting.datastore.DatastoreV3Pb.PutRequest) PutResponse(com.google.apphosting.datastore.DatastoreV3Pb.PutResponse) Future(java.util.concurrent.Future) MultiFuture(com.google.appengine.api.datastore.FutureHelper.MultiFuture) ReorderingMultiFuture(com.google.appengine.api.datastore.Batcher.ReorderingMultiFuture) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

PutRequest (com.google.apphosting.datastore.DatastoreV3Pb.PutRequest)3 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 DeleteRequest (com.google.apphosting.datastore.DatastoreV3Pb.DeleteRequest)1 GetRequest (com.google.apphosting.datastore.DatastoreV3Pb.GetRequest)1 GetResponse (com.google.apphosting.datastore.DatastoreV3Pb.GetResponse)1 PutResponse (com.google.apphosting.datastore.DatastoreV3Pb.PutResponse)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 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)1 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)1 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Future (java.util.concurrent.Future)1