Search in sources :

Example 1 with PutResponse

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

Example 2 with PutResponse

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

the class LocalDatastoreService method putImpl.

// status
@SuppressWarnings("unused")
public PutResponse putImpl(Status status, PutRequest request) {
    PutResponse response = new PutResponse();
    if (request.entitySize() == 0) {
        return response;
    }
    Cost totalCost = response.getMutableCost();
    String app = request.entitys().get(0).getKey().getApp();
    List<EntityProto> clones = new ArrayList<>();
    for (EntityProto entity : request.entitys()) {
        validateAndProcessEntityProto(entity);
        EntityProto clone = entity.clone();
        clones.add(clone);
        checkArgument(clone.hasKey());
        Reference key = clone.getKey();
        checkArgument(key.getPath().elementSize() > 0);
        clone.getMutableKey().setApp(app);
        Element lastPath = getLastElement(key);
        if (lastPath.getId() == 0 && !lastPath.hasName()) {
            if (autoIdAllocationPolicy == AutoIdAllocationPolicy.SEQUENTIAL) {
                lastPath.setId(entityIdSequential.getAndIncrement());
            } else {
                lastPath.setId(toScatteredId(entityIdScattered.getAndIncrement()));
            }
        }
        preprocessEntity(clone);
        if (clone.getEntityGroup().elementSize() == 0) {
            // The entity needs its entity group set.
            Path group = clone.getMutableEntityGroup();
            Element root = key.getPath().elements().get(0);
            Element pathElement = group.addElement();
            pathElement.setType(root.getType());
            if (root.hasName()) {
                pathElement.setName(root.getName());
            } else {
                pathElement.setId(root.getId());
            }
        } else {
            // update an existing entity
            checkState(clone.hasEntityGroup() && clone.getEntityGroup().elementSize() > 0);
        }
    }
    Map<Path, List<EntityProto>> entitiesByEntityGroup = new LinkedHashMap<>();
    Map<Reference, Long> writtenVersions = new HashMap<>();
    final Profile profile = getOrCreateProfile(app);
    synchronized (profile) {
        LiveTxn liveTxn = null;
        for (EntityProto clone : clones) {
            Profile.EntityGroup eg = profile.getGroup(clone.getEntityGroup());
            if (request.hasTransaction()) {
                // the transaction is committed.
                if (liveTxn == null) {
                    liveTxn = profile.getTxn(request.getTransaction().getHandle());
                }
                checkRequest(!liveTxn.isReadOnly(), "Cannot modify entities in a read-only transaction.");
                // this will throw an exception if we attempt to
                // modify the wrong entity group
                eg.addTransaction(liveTxn).addWrittenEntity(clone);
            } else {
                List<EntityProto> entities = entitiesByEntityGroup.get(clone.getEntityGroup());
                if (entities == null) {
                    entities = new ArrayList<>();
                    entitiesByEntityGroup.put(clone.getEntityGroup(), entities);
                }
                entities.add(clone);
            }
            response.mutableKeys().add(clone.getKey());
        }
        for (final Map.Entry<Path, List<EntityProto>> entry : entitiesByEntityGroup.entrySet()) {
            Profile.EntityGroup eg = profile.getGroup(entry.getKey());
            eg.incrementVersion();
            LocalDatastoreJob job = new WriteJob(highRepJobPolicy, eg, profile, entry.getValue(), Collections.<Reference>emptyList());
            addTo(totalCost, job.calculateJobCost());
            eg.addJob(job);
            for (EntityProto entity : entry.getValue()) {
                writtenVersions.put(entity.getKey(), job.getMutationTimestamp(entity.getKey()));
            }
        }
    }
    if (!request.hasTransaction()) {
        logger.fine("put: " + request.entitySize() + " entities");
        // Fill the version numbers, in the same order
        for (Reference key : response.keys()) {
            response.addVersion(writtenVersions.get(key));
        }
    }
    response.setCost(totalCost);
    return response;
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element) Utils.getLastElement(com.google.appengine.api.datastore.dev.Utils.getLastElement) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) PutResponse(com.google.apphosting.datastore.DatastoreV3Pb.PutResponse) Cost(com.google.apphosting.datastore.DatastoreV3Pb.Cost) LinkedHashMap(java.util.LinkedHashMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Aggregations

PutResponse (com.google.apphosting.datastore.DatastoreV3Pb.PutResponse)2 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ReorderingMultiFuture (com.google.appengine.api.datastore.Batcher.ReorderingMultiFuture)1 MultiFuture (com.google.appengine.api.datastore.FutureHelper.MultiFuture)1 Utils.getLastElement (com.google.appengine.api.datastore.dev.Utils.getLastElement)1 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)1 PutRequest (com.google.apphosting.datastore.DatastoreV3Pb.PutRequest)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteString (com.google.protobuf.ByteString)1 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)1 Path (com.google.storage.onestore.v3.OnestoreEntity.Path)1 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1