/** Allocate num copies of the incompleteKey */ private <T> KeyRange<T> allocate(final IncompleteKey incompleteKey, final int num) { final IncompleteKey[] allocations = new IncompleteKey[num]; Arrays.fill(allocations, incompleteKey); final List<Key<T>> typedKeys = datastore().allocateId(allocations).stream() .map(Key::<T>create) .collect(Collectors.toList()); return new KeyRange<>(typedKeys); }
/** Obtain it from the global ObjectifyFactory */ private KeyFactory newKeyFactory() { return ObjectifyService.factory().datastore().newKeyFactory(); }
/** * <p>Preallocate multiple unique ids within the namespace of the * specified entity class. These ids can be used in concert with the normal * automatic allocation of ids when save()ing entities with null Long id fields.</p> * * <p>The {@code KeyRange<?>} class is deprecated; when using this method, * treat the return value as {@code List<Key<T>>}.</p> * * @param clazz must be a registered entity class with a Long or long id field. * @param num must be >= 1 and small enough we can fit a set of keys in RAM. */ public <T> KeyRange<T> allocateIds(final Class<T> clazz, final int num) { final String kind = Key.getKind(clazz); final IncompleteKey incompleteKey = datastore().newKeyFactory().setKind(kind).newKey(); return allocate(incompleteKey, num); }