Temporarily revert phase two of migration away from ReferenceUnions

*** Reason for rollback ***

This code is fine, and I will be resurrecting it unaltered next week.  It's only being rolled back for operational reasons related to the timing of our internal pushes.

*** Original change description ***

Switch over to non-ReferenceUnion fields on DomainBase

This is the second phase of a three phase migration to remove
ReferenceUnions. As of the end of this phase, ReferenceUnions are no longer read
from in any active code paths, but are still written to in case a rollback to
the previous version is necessary. The third and final phase will remove the
ReferenceUnions entirely.

***

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=136642759
This commit is contained in:
mcilwain 2016-10-19 14:30:38 -07:00 committed by Ben McIlwain
parent 0f8e398c00
commit 0b4c3e56d6
6 changed files with 34 additions and 25 deletions

View file

@ -347,10 +347,12 @@ public final class EppResourceUtils {
public static List<Key<DomainBase>> queryDomainsUsingResource(
Class<? extends EppResource> clazz, Key<? extends EppResource> key, DateTime now, int limit) {
checkArgument(ContactResource.class.equals(clazz) || HostResource.class.equals(clazz));
return ofy()
.load()
.type(DomainBase.class)
.filter(clazz.equals(ContactResource.class) ? "allContacts.contact" : "nsHosts", key)
return ofy().load().type(DomainBase.class)
.filter(
clazz.equals(ContactResource.class)
? "allContacts.contactId.linked"
: "nameservers.linked",
key)
.filter("deletionTime >", now)
.limit(limit)
.keys()

View file

@ -72,6 +72,6 @@ public class DesignatedContact extends ImmutableObject {
}
public Key<ContactResource> getContactKey() {
return contact;
return contactId.getLinked();
}
}

View file

@ -183,7 +183,11 @@ public abstract class DomainBase extends EppResource {
}
public ImmutableSet<Key<HostResource>> getNameservers() {
return nullToEmptyImmutableCopy(nsHosts);
ImmutableSet.Builder<Key<HostResource>> builder = new ImmutableSet.Builder<>();
for (ReferenceUnion<HostResource> union : nullToEmptyImmutableCopy(nameservers)) {
builder.add(union.getLinked());
}
return builder.build();
}
/** Loads and returns the fully qualified host names of all linked nameservers. */
@ -238,13 +242,13 @@ public abstract class DomainBase extends EppResource {
@OnSave
void dualSaveReferenceUnions() {
for (DesignatedContact contact : nullToEmptyImmutableCopy(allContacts)) {
contact.contactId = ReferenceUnion.create(contact.contact);
contact.contact = contact.contactId.getLinked();
}
ImmutableSet.Builder<ReferenceUnion<HostResource>> hosts = new ImmutableSet.Builder<>();
for (Key<HostResource> hostKey : nullToEmptyImmutableCopy(nsHosts)) {
hosts.add(ReferenceUnion.create(hostKey));
ImmutableSet.Builder<Key<HostResource>> hostKeys = new ImmutableSet.Builder<>();
for (ReferenceUnion<HostResource> refUnion : nullToEmptyImmutableCopy(nameservers)) {
hostKeys.add(refUnion.getLinked());
}
nameservers = hosts.build();
nsHosts = hostKeys.build();
}
/** Predicate to determine if a given {@link DesignatedContact} is the registrant. */
@ -303,7 +307,11 @@ public abstract class DomainBase extends EppResource {
}
public B setNameservers(ImmutableSet<Key<HostResource>> nameservers) {
getInstance().nsHosts = nameservers;
ImmutableSet.Builder<ReferenceUnion<HostResource>> builder = new ImmutableSet.Builder<>();
for (Key<HostResource> key : nullToEmpty(nameservers)) {
builder.add(ReferenceUnion.create(key));
}
getInstance().nameservers = builder.build();
return thisCastToDerived();
}

View file

@ -269,7 +269,7 @@ public class RdapDomainSearchAction extends RdapActionBase {
for (List<Key<HostResource>> chunk : Iterables.partition(hostKeys, 30)) {
for (DomainResource domain : ofy().load()
.type(DomainResource.class)
.filter("nsHosts in", chunk)
.filter("nameservers.linked in", chunk)
.filter("deletionTime >", now)
.limit(rdapResultSetMaxSize + 1)) {
if (!domains.contains(domain)) {

View file

@ -145,16 +145,16 @@ public class DomainApplicationTest extends EntityTestCase {
@Test
public void testEmptySetsAndArraysBecomeNull() {
assertThat(emptyBuilder().setNameservers(null).build().nsHosts).isNull();
assertThat(emptyBuilder().setNameservers(null).build().nameservers).isNull();
assertThat(emptyBuilder()
.setNameservers(ImmutableSet.<Key<HostResource>>of())
.build()
.nsHosts)
.nameservers)
.isNull();
assertThat(emptyBuilder()
.setNameservers(ImmutableSet.of(Key.create(newHostResource("foo.example.tld"))))
.build()
.nsHosts)
.nameservers)
.isNotNull();
// This behavior should also hold true for ImmutableObjects nested in collections.
assertThat(emptyBuilder()

View file

@ -207,13 +207,13 @@ public class DomainResourceTest extends EntityTestCase {
@Test
public void testEmptySetsAndArraysBecomeNull() {
assertThat(newDomainResource("example.com").asBuilder()
.setNameservers(null).build().nsHosts).isNull();
.setNameservers(null).build().nameservers).isNull();
assertThat(newDomainResource("example.com").asBuilder()
.setNameservers(ImmutableSet.<Key<HostResource>>of()).build().nsHosts)
.setNameservers(ImmutableSet.<Key<HostResource>>of()).build().nameservers)
.isNull();
assertThat(newDomainResource("example.com").asBuilder()
.setNameservers(ImmutableSet.of(Key.create(newHostResource("foo.example.tld"))))
.build().nsHosts)
.build().nameservers)
.isNotNull();
// This behavior should also hold true for ImmutableObjects nested in collections.
assertThat(newDomainResource("example.com").asBuilder()
@ -462,22 +462,21 @@ public class DomainResourceTest extends EntityTestCase {
public void testDualSavingOfDesignatedContact() {
ContactResource contact = persistActiveContact("time1006");
DesignatedContact designatedContact = new DesignatedContact();
designatedContact.contact = Key.create(contact);
designatedContact.contactId = ReferenceUnion.create(Key.create(contact));
designatedContact.type = Type.ADMIN;
DomainResource domainWithContact =
domain.asBuilder().setContacts(ImmutableSet.of(designatedContact)).build();
assertThat(getOnlyElement(domainWithContact.getContacts()).contactId).isNull();
assertThat(getOnlyElement(domainWithContact.getContacts()).contact).isNull();
DomainResource reloadedDomain = persistResource(domainWithContact);
assertThat(getOnlyElement(reloadedDomain.getContacts()).contactId)
.isEqualTo(ReferenceUnion.create(Key.create(contact)));
assertThat(getOnlyElement(reloadedDomain.getContacts()).contact).isEqualTo(Key.create(contact));
}
@Test
public void testDualSavingOfNameservers() {
HostResource host = persistActiveHost("zzz.xxx.yyy");
DomainResource domain = newDomainResource("python-django-unchained.com", host);
assertThat(domain.nameservers).isNull();
assertThat(domain.nsHosts).isNull();
DomainResource djangoReloaded = persistResource(domain);
assertThat(djangoReloaded.nameservers).containsExactly(ReferenceUnion.create(Key.create(host)));
assertThat(djangoReloaded.nsHosts).containsExactly(Key.create(host));
}
}