From 20a0e4ce3ffebcb9541f3912fd865572e5bc1bc7 Mon Sep 17 00:00:00 2001 From: gbrodman Date: Mon, 24 May 2021 13:12:40 -0400 Subject: [PATCH] Remove a couple additional ofy() calls (#1171) * Remove a couple additional ofy() calls --- .../java/google/registry/model/Buildable.java | 12 +++++-- .../google/registry/model/common/Cursor.java | 8 +---- .../poll/PollMessageExternalKeyConverter.java | 31 +++++++++---------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/google/registry/model/Buildable.java b/core/src/main/java/google/registry/model/Buildable.java index 2a9a20187..5d61f32dc 100644 --- a/core/src/main/java/google/registry/model/Buildable.java +++ b/core/src/main/java/google/registry/model/Buildable.java @@ -16,7 +16,7 @@ package google.registry.model; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; -import static google.registry.model.ofy.ObjectifyService.ofy; +import static google.registry.model.ofy.ObjectifyService.auditedOfy; import google.registry.model.ofy.ObjectifyService; import google.registry.util.TypeUtils.TypeInstantiator; @@ -57,8 +57,14 @@ public interface Buildable { // If this object has a Long or long Objectify @Id field that is not set, set it now. Field idField = null; try { - idField = ModelUtils.getAllFields(instance.getClass()).get( - ofy().factory().getMetadata(instance.getClass()).getKeyMetadata().getIdFieldName()); + idField = + ModelUtils.getAllFields(instance.getClass()) + .get( + auditedOfy() + .factory() + .getMetadata(instance.getClass()) + .getKeyMetadata() + .getIdFieldName()); } catch (Exception e) { // Expected if the class is not registered with Objectify. } diff --git a/core/src/main/java/google/registry/model/common/Cursor.java b/core/src/main/java/google/registry/model/common/Cursor.java index 13c9b20d5..493c105cb 100644 --- a/core/src/main/java/google/registry/model/common/Cursor.java +++ b/core/src/main/java/google/registry/model/common/Cursor.java @@ -17,7 +17,6 @@ package google.registry.model.common; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static google.registry.model.common.EntityGroupRoot.getCrossTldKey; -import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.util.DateTimeUtils.START_OF_TIME; import com.google.common.base.Splitter; @@ -210,12 +209,7 @@ public class Cursor extends ImmutableObject implements DatastoreAndSqlEntity { private static void checkValidCursorTypeForScope( CursorType cursorType, Key scope) { checkArgument( - cursorType - .getScopeClass() - .equals( - scope.equals(getCrossTldKey()) - ? EntityGroupRoot.class - : ofy().factory().getMetadata(scope).getEntityClass()), + cursorType.getScopeClass().getSimpleName().equals(scope.getKind()), "Class required for cursor does not match scope class"); } diff --git a/core/src/main/java/google/registry/model/poll/PollMessageExternalKeyConverter.java b/core/src/main/java/google/registry/model/poll/PollMessageExternalKeyConverter.java index 0eccac613..2fe91275a 100644 --- a/core/src/main/java/google/registry/model/poll/PollMessageExternalKeyConverter.java +++ b/core/src/main/java/google/registry/model/poll/PollMessageExternalKeyConverter.java @@ -14,10 +14,10 @@ package google.registry.model.poll; -import static google.registry.model.ofy.ObjectifyService.ofy; +import static com.google.common.collect.ImmutableMap.toImmutableMap; import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableBiMap; +import com.google.common.collect.ImmutableMap; import com.googlecode.objectify.Key; import google.registry.model.EppResource; import google.registry.model.contact.ContactResource; @@ -26,6 +26,7 @@ import google.registry.model.host.HostResource; import google.registry.model.reporting.HistoryEntry; import google.registry.persistence.VKey; import java.util.List; +import java.util.Map; /** * A converter between external key strings for {@link PollMessage}s (i.e. what registrars use to @@ -49,24 +50,23 @@ public class PollMessageExternalKeyConverter { /** An exception thrown when an external key cannot be parsed. */ public static class PollMessageExternalKeyParseException extends RuntimeException {} - /** - * A map of IDs used in external keys corresponding to which EppResource class the poll message - * belongs to. - */ - public static final ImmutableBiMap, Long> EXTERNAL_KEY_CLASS_ID_MAP = - ImmutableBiMap.of( - DomainBase.class, 1L, - ContactResource.class, 2L, - HostResource.class, 3L); + /** Maps that detail the correspondence between EppResource classes and external IDs. */ + private static final ImmutableMap> ID_TO_CLASS_MAP = + ImmutableMap.of( + 1L, DomainBase.class, + 2L, ContactResource.class, + 3L, HostResource.class); + + private static final ImmutableMap KEY_KIND_TO_ID_MAP = + ID_TO_CLASS_MAP.entrySet().stream() + .collect(toImmutableMap(entry -> entry.getValue().getSimpleName(), Map.Entry::getKey)); /** Returns an external poll message ID for the given poll message. */ public static String makePollMessageExternalId(PollMessage pollMessage) { @SuppressWarnings("unchecked") Key ancestorResource = (Key) (Key) pollMessage.getParentKey().getParent(); - long externalKeyClassId = - EXTERNAL_KEY_CLASS_ID_MAP.get( - ofy().factory().getMetadata(ancestorResource.getKind()).getEntityClass()); + long externalKeyClassId = KEY_KIND_TO_ID_MAP.get(ancestorResource.getKind()); return String.format( "%d-%s-%d-%d-%d", externalKeyClassId, @@ -92,8 +92,7 @@ public class PollMessageExternalKeyConverter { throw new PollMessageExternalKeyParseException(); } try { - Class resourceClazz = - EXTERNAL_KEY_CLASS_ID_MAP.inverse().get(Long.parseLong(idComponents.get(0))); + Class resourceClazz = ID_TO_CLASS_MAP.get(Long.parseLong(idComponents.get(0))); if (resourceClazz == null) { throw new PollMessageExternalKeyParseException(); }