From f7e84b56a034a9501cad584918385bab16407927 Mon Sep 17 00:00:00 2001 From: mcilwain Date: Thu, 12 Jan 2017 15:51:02 -0800 Subject: [PATCH] Relax uniqueness constraint on ROID suffixes The command still enforces uniqueness (which is fine), but by changing the cache from a BiMap to a Map, we can support non-unique suffixes if they happen to be configured as data. The only reason the cache was ever a BiMap in the first place was to support the Registry 2.0 migration, which was finished a year and a half ago. It's only being read one way now, so a Map is fine. See https://github.com/google/nomulus/pull/53 for context. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=144381946 --- java/google/registry/model/RoidSuffixes.java | 30 +++++++++---------- .../registry/model/RoidSuffixesTest.java | 8 +++++ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/java/google/registry/model/RoidSuffixes.java b/java/google/registry/model/RoidSuffixes.java index 2b19d9872..9e432c1f0 100644 --- a/java/google/registry/model/RoidSuffixes.java +++ b/java/google/registry/model/RoidSuffixes.java @@ -20,30 +20,29 @@ import static google.registry.model.common.EntityGroupRoot.getCrossTldKey; import static google.registry.model.ofy.ObjectifyService.ofy; import com.google.common.base.Supplier; -import com.google.common.collect.HashBiMap; +import com.google.common.collect.ImmutableMap; import com.googlecode.objectify.Work; import google.registry.model.registry.Registry; /** Utility class for dealing with EPP ROID suffixes. */ public final class RoidSuffixes { - private static Supplier> roidSuffixMapCache = - memoizeWithShortExpiration(new Supplier>() { + /** A cached map of TLD strings to ROID suffixes. */ + private static final Supplier> ROID_SUFFIX_MAP_CACHE = + memoizeWithShortExpiration(new Supplier>() { @Override - public HashBiMap get() { - return ofy().doTransactionless(new Work>() { + public ImmutableMap get() { + return ofy().doTransactionless(new Work>() { @Override - public HashBiMap run() { - HashBiMap bimap = HashBiMap.create(); + public ImmutableMap run() { + ImmutableMap.Builder builder = new ImmutableMap.Builder<>(); for (Registry registry : ofy().load().type(Registry.class).ancestor(getCrossTldKey()).list()) { - bimap.put(registry.getTldStr(), registry.getRoidSuffix()); + builder.put(registry.getTldStr(), registry.getRoidSuffix()); } - return bimap; - } - }); - } - }); + return builder.build(); + }}); + }}); /** * Returns the roid suffix corresponding to the given tld using the per-tld roidSuffix field. @@ -52,13 +51,12 @@ public final class RoidSuffixes { * configured on it */ public static String getRoidSuffixForTld(String tld) { - String roidSuffix = roidSuffixMapCache.get().get(tld); + String roidSuffix = ROID_SUFFIX_MAP_CACHE.get().get(tld); checkState(roidSuffix != null, "Could not find ROID suffix for TLD %s", tld); return roidSuffix; } public static boolean isRoidSuffixUsed(String roidSuffix) { - return roidSuffixMapCache.get().containsValue(roidSuffix); + return ROID_SUFFIX_MAP_CACHE.get().containsValue(roidSuffix); } - } diff --git a/javatests/google/registry/model/RoidSuffixesTest.java b/javatests/google/registry/model/RoidSuffixesTest.java index df4a01e76..18c530c76 100644 --- a/javatests/google/registry/model/RoidSuffixesTest.java +++ b/javatests/google/registry/model/RoidSuffixesTest.java @@ -39,4 +39,12 @@ public class RoidSuffixesTest { persistResource(newRegistry("tld", "MEOW")); assertThat(getRoidSuffixForTld("tld")).isEqualTo("MEOW"); } + + @Test + public void test_allowDupeRoidSuffixes() { + persistResource(newRegistry("tld", "MEOW")); + persistResource(newRegistry("example", "MEOW")); + assertThat(getRoidSuffixForTld("tld")).isEqualTo("MEOW"); + assertThat(getRoidSuffixForTld("example")).isEqualTo("MEOW"); + } }