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
This commit is contained in:
mcilwain 2017-01-12 15:51:02 -08:00 committed by Ben McIlwain
parent 66a02856c4
commit f7e84b56a0
2 changed files with 22 additions and 16 deletions

View file

@ -20,30 +20,29 @@ import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.HashBiMap; import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Work; import com.googlecode.objectify.Work;
import google.registry.model.registry.Registry; import google.registry.model.registry.Registry;
/** Utility class for dealing with EPP ROID suffixes. */ /** Utility class for dealing with EPP ROID suffixes. */
public final class RoidSuffixes { public final class RoidSuffixes {
private static Supplier<HashBiMap<String, String>> roidSuffixMapCache = /** A cached map of TLD strings to ROID suffixes. */
memoizeWithShortExpiration(new Supplier<HashBiMap<String, String>>() { private static final Supplier<ImmutableMap<String, String>> ROID_SUFFIX_MAP_CACHE =
memoizeWithShortExpiration(new Supplier<ImmutableMap<String, String>>() {
@Override @Override
public HashBiMap<String, String> get() { public ImmutableMap<String, String> get() {
return ofy().doTransactionless(new Work<HashBiMap<String, String>>() { return ofy().doTransactionless(new Work<ImmutableMap<String, String>>() {
@Override @Override
public HashBiMap<String, String> run() { public ImmutableMap<String, String> run() {
HashBiMap<String, String> bimap = HashBiMap.create(); ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
for (Registry registry : for (Registry registry :
ofy().load().type(Registry.class).ancestor(getCrossTldKey()).list()) { 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. * 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 * configured on it
*/ */
public static String getRoidSuffixForTld(String tld) { 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); checkState(roidSuffix != null, "Could not find ROID suffix for TLD %s", tld);
return roidSuffix; return roidSuffix;
} }
public static boolean isRoidSuffixUsed(String roidSuffix) { public static boolean isRoidSuffixUsed(String roidSuffix) {
return roidSuffixMapCache.get().containsValue(roidSuffix); return ROID_SUFFIX_MAP_CACHE.get().containsValue(roidSuffix);
} }
} }

View file

@ -39,4 +39,12 @@ public class RoidSuffixesTest {
persistResource(newRegistry("tld", "MEOW")); persistResource(newRegistry("tld", "MEOW"));
assertThat(getRoidSuffixForTld("tld")).isEqualTo("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");
}
} }