Search in sources :

Example 1 with DeleteResponse

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

Example 2 with DeleteResponse

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

the class LocalDatastoreService method deleteImpl.

// status
@SuppressWarnings("unused")
public DeleteResponse deleteImpl(Status status, DeleteRequest request) {
    DeleteResponse response = new DeleteResponse();
    if (request.keySize() == 0) {
        return response;
    }
    Cost totalCost = response.getMutableCost();
    // We don't support requests that span apps, so the app for the first key
    // is the app for all keys.
    String app = request.keys().get(0).getApp();
    final Profile profile = getOrCreateProfile(app);
    LiveTxn liveTxn = null;
    // Maintain a mapping of keys by entity group so that we can apply one job
    // per entity group.
    Map<Path, List<Reference>> keysByEntityGroup = new LinkedHashMap<>();
    Map<Reference, Long> writtenVersions = new HashMap<>();
    synchronized (profile) {
        for (final Reference key : request.keys()) {
            validatePathComplete(key);
            Path group = getGroup(key);
            if (request.hasTransaction()) {
                if (liveTxn == null) {
                    liveTxn = profile.getTxn(request.getTransaction().getHandle());
                }
                checkRequest(!liveTxn.isReadOnly(), "Cannot modify entities in a read-only transaction.");
                Profile.EntityGroup eg = profile.getGroup(group);
                // this will throw an exception if we attempt to modify
                // the wrong entity group
                eg.addTransaction(liveTxn).addDeletedEntity(key);
            } else {
                List<Reference> keysToDelete = keysByEntityGroup.get(group);
                if (keysToDelete == null) {
                    keysToDelete = new ArrayList<>();
                    keysByEntityGroup.put(group, keysToDelete);
                }
                keysToDelete.add(key);
            }
        }
        // does all the work for each entity group.
        for (final Map.Entry<Path, List<Reference>> entry : keysByEntityGroup.entrySet()) {
            Profile.EntityGroup eg = profile.getGroup(entry.getKey());
            eg.incrementVersion();
            LocalDatastoreJob job = new WriteJob(highRepJobPolicy, eg, profile, Collections.<EntityProto>emptyList(), entry.getValue());
            addTo(totalCost, job.calculateJobCost());
            eg.addJob(job);
            for (Reference deletedKey : entry.getValue()) {
                writtenVersions.put(deletedKey, job.getMutationTimestamp(deletedKey));
            }
        }
    }
    if (!request.hasTransaction()) {
        for (Reference key : request.keys()) {
            response.addVersion(writtenVersions.get(key));
        }
    }
    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) ByteString(com.google.protobuf.ByteString) Cost(com.google.apphosting.datastore.DatastoreV3Pb.Cost) LinkedHashMap(java.util.LinkedHashMap) DeleteResponse(com.google.apphosting.datastore.DatastoreV3Pb.DeleteResponse) 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)

Aggregations

DeleteResponse (com.google.apphosting.datastore.DatastoreV3Pb.DeleteResponse)2 ReorderingMultiFuture (com.google.appengine.api.datastore.Batcher.ReorderingMultiFuture)1 MultiFuture (com.google.appengine.api.datastore.FutureHelper.MultiFuture)1 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)1 DeleteRequest (com.google.apphosting.datastore.DatastoreV3Pb.DeleteRequest)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteString (com.google.protobuf.ByteString)1 Path (com.google.storage.onestore.v3.OnestoreEntity.Path)1 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1