Simplify the logic for checking for finding linked contacts/hosts.

This is preparatory refactoring for the next CL, which gets rid
of cloneWithLinkedStatus

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=145424322
This commit is contained in:
cgoldfeder 2017-01-24 08:56:26 -08:00 committed by Ben McIlwain
parent b5cf58bf2c
commit 2bb61b82f4
2 changed files with 25 additions and 18 deletions

View file

@ -20,7 +20,7 @@ import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.tryFind; import static com.google.common.collect.Iterables.tryFind;
import static com.google.common.collect.Sets.intersection; import static com.google.common.collect.Sets.intersection;
import static google.registry.model.EppResourceUtils.loadByForeignKey; import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.model.EppResourceUtils.queryDomainsUsingResource; import static google.registry.model.EppResourceUtils.queryForLinkedDomains;
import static google.registry.model.domain.DomainResource.extendRegistrationWithCap; import static google.registry.model.domain.DomainResource.extendRegistrationWithCap;
import static google.registry.model.index.ForeignKeyIndex.loadAndGetKey; import static google.registry.model.index.ForeignKeyIndex.loadAndGetKey;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
@ -189,8 +189,8 @@ public final class ResourceFlowUtils {
// eventually consistent and so might be very stale, but the direct load will not be stale, // eventually consistent and so might be very stale, but the direct load will not be stale,
// just non-transactional. If we find at least one actual reference then we can reliably // just non-transactional. If we find at least one actual reference then we can reliably
// fail. If we don't find any, we can't trust the query and need to do the full mapreduce. // fail. If we don't find any, we can't trust the query and need to do the full mapreduce.
List<Key<DomainBase>> keys = queryDomainsUsingResource( Iterable<Key<DomainBase>> keys =
resourceClass, fki.getResourceKey(), now, FAILFAST_CHECK_COUNT); queryForLinkedDomains(fki.getResourceKey(), now).limit(FAILFAST_CHECK_COUNT).keys();
Predicate<DomainBase> predicate = new Predicate<DomainBase>() { Predicate<DomainBase> predicate = new Predicate<DomainBase>() {
@Override @Override
public boolean apply(DomainBase domain) { public boolean apply(DomainBase domain) {

View file

@ -25,6 +25,7 @@ import static google.registry.util.DateTimeUtils.latestOf;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result; import com.googlecode.objectify.Result;
import com.googlecode.objectify.cmd.Query;
import com.googlecode.objectify.util.ResultNow; import com.googlecode.objectify.util.ResultNow;
import google.registry.model.EppResource.Builder; import google.registry.model.EppResource.Builder;
import google.registry.model.EppResource.BuilderWithTransferData; import google.registry.model.EppResource.BuilderWithTransferData;
@ -34,7 +35,6 @@ import google.registry.model.contact.ContactResource;
import google.registry.model.domain.DomainApplication; import google.registry.model.domain.DomainApplication;
import google.registry.model.domain.DomainBase; import google.registry.model.domain.DomainBase;
import google.registry.model.eppcommon.StatusValue; import google.registry.model.eppcommon.StatusValue;
import google.registry.model.host.HostResource;
import google.registry.model.index.ForeignKeyIndex; import google.registry.model.index.ForeignKeyIndex;
import google.registry.model.ofy.CommitLogManifest; import google.registry.model.ofy.CommitLogManifest;
import google.registry.model.ofy.CommitLogMutation; import google.registry.model.ofy.CommitLogMutation;
@ -331,35 +331,42 @@ public final class EppResourceUtils {
} }
/** /**
* Find keys of domains or applications that reference a specified contact or host. * Returns a query for domains or applications that reference a specified contact or host.
* *
* <p>This is an eventually consistent query. * <p>This is an eventually consistent query.
* *
* @param clazz the referent type (contact or host)
* @param key the referent key * @param key the referent key
* @param now the logical time of the check * @param now the logical time of the check
* @param limit max number of keys to return
*/ */
public static List<Key<DomainBase>> queryDomainsUsingResource( public static Query<DomainBase> queryForLinkedDomains(
Class<? extends EppResource> clazz, Key<? extends EppResource> key, DateTime now, int limit) { Key<? extends EppResource> key, DateTime now) {
checkArgument(ContactResource.class.equals(clazz) || HostResource.class.equals(clazz)); boolean isContactKey = key.getKind().equals(Key.getKind(ContactResource.class));
return ofy() return ofy()
.load() .load()
.type(DomainBase.class) .type(DomainBase.class)
.filter(clazz.equals(ContactResource.class) ? "allContacts.contact" : "nsHosts", key) .filter(isContactKey ? "allContacts.contact" : "nsHosts", key)
.filter("deletionTime >", now) .filter("deletionTime >", now);
.limit(limit) }
.keys()
.list(); /**
* Returns whether the given contact or host is linked to (that is, referenced by) a domain.
*
* <p>This is an eventually consistent query.
*
* @param key the referent key
* @param now the logical time of the check
*/
public static boolean isLinked(Key<? extends EppResource> key, DateTime now) {
return queryForLinkedDomains(key, now).limit(1).count() > 0;
} }
/** Clone a contact or host with an eventually-consistent notion of LINKED. */ /** Clone a contact or host with an eventually-consistent notion of LINKED. */
public static EppResource cloneResourceWithLinkedStatus(EppResource resource, DateTime now) { public static EppResource cloneResourceWithLinkedStatus(EppResource resource, DateTime now) {
Builder<?, ?> builder = resource.asBuilder(); Builder<?, ?> builder = resource.asBuilder();
if (queryDomainsUsingResource(resource.getClass(), Key.create(resource), now, 1).isEmpty()) { if (isLinked(Key.create(resource), now)) {
builder.removeStatusValue(StatusValue.LINKED);
} else {
builder.addStatusValue(StatusValue.LINKED); builder.addStatusValue(StatusValue.LINKED);
} else {
builder.removeStatusValue(StatusValue.LINKED);
} }
return builder.build(); return builder.build();
} }