DeReference the codebase

This change replaces all Ref objects in the code with Key objects. These are
stored in datastore as the same object (raw datastore keys), so this is not
a model change.

Our best practices doc says to use Keys not Refs because:
 * The .get() method obscures what's actually going on
   - Much harder to visually audit the code for datastore loads
   - Hard to distinguish Ref<T> get()'s from Optional get()'s and Supplier get()'s
 * Implicit ofy().load() offers much less control
   - Antipattern for ultimate goal of making Ofy injectable
   - Can't control cache use or batch loading without making ofy() explicit anyway
 * Serialization behavior is surprising and could be quite dangerous/incorrect
   - Can lead to serialization errors. If it actually worked "as intended",
     it would lead to a Ref<> on a serialized object being replaced upon
     deserialization with a stale copy of the old value, which could potentially
     break all kinds of transactional expectations
 * Having both Ref<T> and Key<T> introduces extra boilerplate everywhere
   - E.g. helper methods all need to have Ref and Key overloads, or you need to
     call .key() to get the Key<T> for every Ref<T> you want to pass in
   - Creating a Ref<T> is more cumbersome, since it doesn't have all the create()
     overloads that Key<T> has, only create(Key<T>) and create(Entity) - no way to
     create directly from kind+ID/name, raw Key, websafe key string, etc.

(Note that Refs are treated specially by Objectify's @Load method and Keys are not;
we don't use that feature, but it is the one advantage Refs have over Keys.)

The direct impetus for this change is that I am trying to audit our use of memcache,
and the implicit .get() calls to datastore were making that very hard.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131965491
This commit is contained in:
cgoldfeder 2016-09-01 10:44:23 -07:00 committed by Ben McIlwain
parent 1a60073b24
commit 5098b03af4
119 changed files with 772 additions and 817 deletions

View file

@ -36,7 +36,7 @@ import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Ordering;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.IgnoreSave;
import com.googlecode.objectify.annotation.Index;
@ -78,7 +78,7 @@ public abstract class DomainBase extends EppResource {
/** References to hosts that are the nameservers for the domain. */
@XmlTransient
//TODO(b/28713909): Make this a Set<Ref<HostResource>>.
//TODO(b/28713909): Make this a Set<Key<HostResource>>.
Set<ReferenceUnion<HostResource>> nameservers;
/**
@ -149,7 +149,7 @@ public abstract class DomainBase extends EppResource {
public ForeignKeyedDesignatedContact apply(DesignatedContact designated) {
return ForeignKeyedDesignatedContact.create(
designated.getType(),
designated.getContactRef().get().getContactId());
ofy().load().key(designated.getContactKey()).now().getContactId());
}})
.toSet();
}
@ -158,7 +158,7 @@ public abstract class DomainBase extends EppResource {
@XmlElement(name = "registrant")
private String getMarshalledRegistrant() {
preMarshal();
return getRegistrant().get().getContactId();
return ofy().load().key(getRegistrant()).now().getContactId();
}
public String getFullyQualifiedDomainName() {
@ -177,8 +177,8 @@ public abstract class DomainBase extends EppResource {
return idnTableName;
}
public ImmutableSet<Ref<HostResource>> getNameservers() {
ImmutableSet.Builder<Ref<HostResource>> builder = new ImmutableSet.Builder<>();
public ImmutableSet<Key<HostResource>> getNameservers() {
ImmutableSet.Builder<Key<HostResource>> builder = new ImmutableSet.Builder<>();
for (ReferenceUnion<HostResource> union : nullToEmptyImmutableCopy(nameservers)) {
builder.add(union.getLinked());
}
@ -187,7 +187,7 @@ public abstract class DomainBase extends EppResource {
/** Loads and returns the fully qualified host names of all linked nameservers. */
public ImmutableSortedSet<String> loadNameserverFullyQualifiedHostNames() {
return FluentIterable.from(ofy().load().refs(getNameservers()).values())
return FluentIterable.from(ofy().load().keys(getNameservers()).values())
.transform(
new Function<HostResource, String>() {
@Override
@ -198,14 +198,14 @@ public abstract class DomainBase extends EppResource {
.toSortedSet(Ordering.natural());
}
/** A reference to the registrant who registered this domain. */
public Ref<ContactResource> getRegistrant() {
/** A key to the registrant who registered this domain. */
public Key<ContactResource> getRegistrant() {
return FluentIterable
.from(nullToEmpty(allContacts))
.filter(IS_REGISTRANT)
.first()
.get()
.getContactRef();
.getContactKey();
}
/** Associated contacts for the domain (other than registrant). */
@ -221,11 +221,11 @@ public abstract class DomainBase extends EppResource {
}
/** Returns all referenced contacts from this domain or application. */
public ImmutableSet<Ref<ContactResource>> getReferencedContacts() {
ImmutableSet.Builder<Ref<ContactResource>> contactsBuilder =
public ImmutableSet<Key<ContactResource>> getReferencedContacts() {
ImmutableSet.Builder<Key<ContactResource>> contactsBuilder =
new ImmutableSet.Builder<>();
for (DesignatedContact designated : nullToEmptyImmutableCopy(allContacts)) {
contactsBuilder.add(designated.getContactRef());
contactsBuilder.add(designated.getContactKey());
}
return contactsBuilder.build();
}
@ -276,7 +276,7 @@ public abstract class DomainBase extends EppResource {
return thisCastToDerived();
}
public B setRegistrant(Ref<ContactResource> registrant) {
public B setRegistrant(Key<ContactResource> registrant) {
// Replace the registrant contact inside allContacts.
getInstance().allContacts = union(
getInstance().getContacts(),
@ -289,21 +289,21 @@ public abstract class DomainBase extends EppResource {
return thisCastToDerived();
}
public B setNameservers(ImmutableSet<Ref<HostResource>> nameservers) {
public B setNameservers(ImmutableSet<Key<HostResource>> nameservers) {
ImmutableSet.Builder<ReferenceUnion<HostResource>> builder = new ImmutableSet.Builder<>();
for (Ref<HostResource> ref : nullToEmpty(nameservers)) {
builder.add(ReferenceUnion.create(ref));
for (Key<HostResource> key : nullToEmpty(nameservers)) {
builder.add(ReferenceUnion.create(key));
}
getInstance().nameservers = builder.build();
return thisCastToDerived();
}
public B addNameservers(ImmutableSet<Ref<HostResource>> nameservers) {
public B addNameservers(ImmutableSet<Key<HostResource>> nameservers) {
return setNameservers(
ImmutableSet.copyOf(union(getInstance().getNameservers(), nameservers)));
}
public B removeNameservers(ImmutableSet<Ref<HostResource>> nameservers) {
public B removeNameservers(ImmutableSet<Key<HostResource>> nameservers) {
return setNameservers(
ImmutableSet.copyOf(difference(getInstance().getNameservers(), nameservers)));
}