Search in sources :

Example 1 with AllocateIdsRequest

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

the class AsyncDatastoreServiceImpl method allocateIds.

@Override
public Future<KeyRange> allocateIds(final Key parent, final String kind, long num) {
    if (num <= 0) {
        throw new IllegalArgumentException("num must be > 0");
    }
    if (num > 1000000000) {
        throw new IllegalArgumentException("num must be < 1 billion");
    }
    // kind validation taken care of by the next call
    final AppIdNamespace appIdNamespace = datastoreServiceConfig.getAppIdNamespace();
    Reference allocateIdsRef = buildAllocateIdsRef(parent, kind, appIdNamespace);
    AllocateIdsRequest req = new AllocateIdsRequest().setSize(num).setModelKey(allocateIdsRef);
    AllocateIdsResponse resp = new AllocateIdsResponse();
    Future<AllocateIdsResponse> future = makeAsyncCall(apiConfig, DatastoreService_3.Method.AllocateIds, req, resp);
    return new FutureWrapper<AllocateIdsResponse, KeyRange>(future) {

        @Override
        protected KeyRange wrap(AllocateIdsResponse resp) throws Exception {
            return new KeyRange(parent, kind, resp.getStart(), resp.getEnd(), appIdNamespace);
        }

        @Override
        protected Throwable convertException(Throwable cause) {
            return cause;
        }
    };
}
Also used : Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) FutureWrapper(com.google.appengine.api.utils.FutureWrapper) AllocateIdsResponse(com.google.apphosting.datastore.DatastoreV3Pb.AllocateIdsResponse) AllocateIdsRequest(com.google.apphosting.datastore.DatastoreV3Pb.AllocateIdsRequest)

Example 2 with AllocateIdsRequest

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

the class AsyncDatastoreServiceImpl method allocateIdRange.

@Override
public Future<KeyRangeState> allocateIdRange(final KeyRange range) {
    Key parent = range.getParent();
    final String kind = range.getKind();
    final long start = range.getStart().getId();
    long end = range.getEnd().getId();
    AllocateIdsRequest req = new AllocateIdsRequest().setModelKey(AsyncDatastoreServiceImpl.buildAllocateIdsRef(parent, kind, null)).setMax(end);
    AllocateIdsResponse resp = new AllocateIdsResponse();
    Future<AllocateIdsResponse> future = makeAsyncCall(apiConfig, DatastoreService_3.Method.AllocateIds, req, resp);
    return new FutureWrapper<AllocateIdsResponse, KeyRangeState>(future) {

        @SuppressWarnings("deprecation")
        @Override
        protected KeyRangeState wrap(AllocateIdsResponse resp) throws Exception {
            // Check for collisions, i.e. existing entities with ids in this range.
            // 
            // We could do this before the allocation, but we'd still have to do it
            // afterward as well to catch the race condition where an entity is inserted
            // after that initial check but before the allocation. So, skip the up front
            // check and just do it once, here.
            Query query = new Query(kind).setKeysOnly();
            query.addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN_OR_EQUAL, range.getStart());
            query.addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, range.getEnd());
            List<Entity> collision = prepare(query).asList(withLimit(1));
            if (!collision.isEmpty()) {
                return KeyRangeState.COLLISION;
            }
            // Check for a race condition, i.e. cases where the datastore may have
            // cached id batches that contain ids in this range.
            boolean raceCondition = start < resp.getStart();
            return raceCondition ? KeyRangeState.CONTENTION : KeyRangeState.EMPTY;
        }

        @Override
        protected Throwable convertException(Throwable cause) {
            return cause;
        }
    };
}
Also used : FutureWrapper(com.google.appengine.api.utils.FutureWrapper) AllocateIdsResponse(com.google.apphosting.datastore.DatastoreV3Pb.AllocateIdsResponse) AllocateIdsRequest(com.google.apphosting.datastore.DatastoreV3Pb.AllocateIdsRequest)

Aggregations

FutureWrapper (com.google.appengine.api.utils.FutureWrapper)2 AllocateIdsRequest (com.google.apphosting.datastore.DatastoreV3Pb.AllocateIdsRequest)2 AllocateIdsResponse (com.google.apphosting.datastore.DatastoreV3Pb.AllocateIdsResponse)2 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)1