@Override public Key<?> fromString(String str) { return Key.create(str); } }
/** * Easy null-safe conversion of the typed key. */ public static com.google.cloud.datastore.Key key(final Key<?> typed) { if (typed == null) return null; else return typed.getRaw(); }
/** * Gets the root of a parent graph of keys. If a Key has no parent, it is the root. * * @return the topmost parent key, or this object itself if it is the root. * Note that the root key could potentially have any type. */ @SuppressWarnings("unchecked") public <V> Key<V> getRoot() { if (this.getParent() == null) return (Key<V>)this; else return this.getParent().getRoot(); }
/** Create a key with a parent and a long id */ private Key(final Key<?> parent, final Class<? extends T> kindClass, final long id) { final String kind = getKind(kindClass); if (parent == null) { this.raw = newKeyFactory().setKind(kind).newKey(id); } else { this.raw = com.google.cloud.datastore.Key.newBuilder(key(parent), kind, id).build(); } this.parent = parent; }
@Override public String from(Key<E> from) { return from.getName(); } };
/** * Create a {@link Key Key<CounterShardData>}. Keys for this entity are not "parented" so that they can be added * under high volume load in a given application. Note that CounterData will be in a namespace specific. * * @param counterDataKey * @param shardNumber * @return */ public static Key<CounterShardData> key(final Key<CounterData> counterDataKey, final int shardNumber) { // Preconditions checked by #constructCounterShardIdentifier return Key.create(CounterShardData.class, constructCounterShardIdentifier(counterDataKey.getName(), shardNumber)); }
/** * Preallocate a contiguous range of unique ids within the namespace of the * specified entity class and the parent key. These ids can be used in concert with the normal * automatic allocation of ids when put()ing entities with null Long id fields. * * @param parentKeyOrEntity must be a legitimate parent for the class type. It need not * point to an existent entity, but it must be the correct type for clazz. * @param clazz must be a registered entity class with a Long or long id field, and * a parent key of the correct type. * @param num must be >= 1 and <= 1 billion */ public <T> KeyRange<T> allocateIds(final Object parentKeyOrEntity, final Class<T> clazz, final int num) { final Key<?> parent = keys().anythingToKey(parentKeyOrEntity); final String kind = Key.getKind(clazz); final IncompleteKey incompleteKey = com.google.cloud.datastore.Key.newBuilder(parent.getRaw(), kind).build(); return allocate(incompleteKey, num); }
/** * @return the metadata for the specified class, or null if there was nothing appropriate registered */ @SuppressWarnings("unchecked") public <T> EntityMetadata<T> getMetadata(Class<T> clazz) { return getMetadata(Key.getKind(clazz)); }
final int result = this.compareAncestors(other); if (result != 0) return result; final int result = this.getRaw().getKind().compareTo(other.getRaw().getKind()); if (result != 0) { return result; return compareToWithIdentityHash(this.raw, other.raw);
@Override protected Map<S, T> wrap(Map<Key<T>, T> base) throws Exception { Map<S, T> result = new LinkedHashMap<S, T>(base.size() * 2); for (Map.Entry<Key<T>, T> entry: base.entrySet()) { Object mapKey = entry.getKey().getName() != null ? entry.getKey().getName() : entry.getKey().getId(); result.put((S)mapKey, entry.getValue()); } return result; } };
@Test public void getterTest() throws Exception { final Key<CounterData> counterDataKey = CounterData.key(TEST_COUNTER1); final Key<CounterShardData> counterShardDataKey = CounterShardData.key(counterDataKey, 0); final UUID uuid = UUID.randomUUID(); final CounterShardOperationData counterShardOperationData = new CounterShardOperationData(counterShardDataKey, uuid, CounterOperationType.INCREMENT, 1L); assertThat(counterShardOperationData.getMutationAmount(), is(1L)); assertThat(counterShardOperationData.getCounterShardDataKey(), is(counterShardDataKey)); assertThat(counterShardOperationData.getId(), is(uuid.toString())); assertThat( counterShardOperationData.getCreationDateTime().isBefore(DateTime.now(DateTimeZone.UTC).plusSeconds(10)), is(true)); // TypedKey assertThat(counterShardOperationData.getTypedKey().getName(), is(uuid.toString())); assertThat(counterShardOperationData.getTypedKey().getParent().getName(), is(counterShardDataKey.getName())); // RawKey assertThat(counterShardOperationData.getKey().getName(), is(uuid.toString())); }
/** * Easy null-safe conversion of the raw key. */ public static <V> Key<V> key(final com.google.cloud.datastore.Key raw) { if (raw == null) return null; else return new Key<>(raw); }
/** * Gets the result, possibly from the session, putting it in the session if necessary. * Also will recursively prepare the session with @Load parents as appropriate. * @throws NullPointerException if key is null */ public <T> Result<T> load(final Key<T> key) { if (key == null) throw new NullPointerException("You tried to load a null key!"); final Result<T> result = round.get(key); // If we are running a transaction, enlist the result so that it gets processed on commit even // if the client never materializes the result. if (ofy.getTransaction() != null) ((PrivateAsyncTransaction)ofy.getTransaction()).enlist(result); // Now check to see if we need to recurse and add our parent(s) to the round if (key.getParent() != null) { final KeyMetadata<?> meta = ofy.factory().keys().getMetadata(key); // Is it really possible for this to be null? if (meta != null) { if (meta.shouldLoadParent(loadArrangement)) { load(key.getParent()); } } } return result; }
/** Comparison is based on key */ @Override public int compareTo(Ref<T> o) { return this.key().compareTo(o.key()); }
@Override public String toString() { return index.getName() + "->" + name; }
/** * Preallocate a contiguous range of unique ids within the namespace of the * specified entity class and the parent key. These ids can be used in concert with the normal * automatic allocation of ids when put()ing entities with null Long id fields. * * @param parentKeyOrEntity must be a legitimate parent for the class type. It need not * point to an existent entity, but it must be the correct type for clazz. * @param clazz must be a registered entity class with a Long or long id field, and * a parent key of the correct type. * @param num must be >= 1 and <= 1 billion */ public <T> KeyRange<T> allocateIds(Object parentKeyOrEntity, Class<T> clazz, long num) { Key<?> parent = this.getKey(parentKeyOrEntity); String kind = Key.getKind(clazz); // Feels a little weird going directly to the DatastoreServiceFactory but the // allocateIds() method really is optionless. return new KeyRange<T>(DatastoreServiceFactory.getDatastoreService().allocateIds(parent.getRaw(), kind, num)); }
/** * @return the metadata for a kind of entity based on its key * @throws IllegalArgumentException if the kind has not been registered */ public <T> EntityMetadata<T> getMetadata(final Key<T> key) throws IllegalArgumentException { return this.registrar.getMetadataSafe(key.getKind()); }
/** Key.create(parent, Blah.class, id) is easier to type than new Key<Blah>(parent, Blah.class, id) */ public static <T> Key<T> create(final Key<?> parent, final Class<? extends T> kindClass, final long id) { return new Key<>(parent, kindClass, id); }