/** * 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(); }
/** * 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; }
/** * 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(); }
@Override public <V> Set<Key<V>> fetchParentKeys() { Set<Key<V>> parentKeys = new LinkedHashSet<Key<V>>(); for (Key<T> key: this.fetchKeys()) { if (key.getParent() == null) throw new IllegalStateException("Tried to fetch parent from a key that has no parent: " + key); parentKeys.add(key.<V>getParent()); } return parentKeys; }
@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())); }