mirror of
https://github.com/google/nomulus.git
synced 2025-05-16 17:37:13 +02:00
Convert domain label list code to use Java 8 streams features
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=172774927
This commit is contained in:
parent
f1c76d035f
commit
4828417c73
4 changed files with 69 additions and 95 deletions
|
@ -16,6 +16,7 @@ package google.registry.model.registry.label;
|
|||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Strings.isNullOrEmpty;
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
|
||||
import static google.registry.model.registry.Registries.getTlds;
|
||||
|
||||
|
@ -136,14 +137,11 @@ public abstract class BaseDomainLabelList<T extends Comparable<?>, R extends Dom
|
|||
|
||||
/** Gets the names of the tlds that reference this list. */
|
||||
public final ImmutableSet<String> getReferencingTlds() {
|
||||
ImmutableSet.Builder<String> builder = new ImmutableSet.Builder<>();
|
||||
Key<? extends BaseDomainLabelList<?, ?>> key = Key.create(this);
|
||||
for (String tld : getTlds()) {
|
||||
if (refersToKey(Registry.get(tld), key)) {
|
||||
builder.add(tld);
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
return getTlds()
|
||||
.stream()
|
||||
.filter((tld) -> refersToKey(Registry.get(tld), key))
|
||||
.collect(toImmutableSet());
|
||||
}
|
||||
|
||||
protected abstract boolean refersToKey(
|
||||
|
|
|
@ -33,7 +33,6 @@ import com.google.common.cache.LoadingCache;
|
|||
import com.google.common.hash.BloomFilter;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.Work;
|
||||
import com.googlecode.objectify.annotation.Entity;
|
||||
import com.googlecode.objectify.annotation.Id;
|
||||
import com.googlecode.objectify.annotation.Parent;
|
||||
|
@ -113,13 +112,12 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
|
|||
// encoding on them.
|
||||
revision.probablePremiumLabels =
|
||||
BloomFilter.create(unencodedCharsFunnel(), premiumLabels.size());
|
||||
for (String label : premiumLabels) {
|
||||
revision.probablePremiumLabels.put(label);
|
||||
}
|
||||
premiumLabels.forEach(revision.probablePremiumLabels::put);
|
||||
try {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
revision.probablePremiumLabels.writeTo(bos);
|
||||
checkArgument(bos.size() <= MAX_BLOOM_FILTER_BYTES,
|
||||
checkArgument(
|
||||
bos.size() <= MAX_BLOOM_FILTER_BYTES,
|
||||
"Too many premium labels were specified; Bloom filter exceeds max entity size");
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Could not serialize premium labels Bloom filter", e);
|
||||
|
@ -137,26 +135,28 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
|
|||
static final LoadingCache<String, PremiumList> cachePremiumLists =
|
||||
CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(getDomainLabelListCacheDuration().getMillis(), MILLISECONDS)
|
||||
.build(new CacheLoader<String, PremiumList>() {
|
||||
@Override
|
||||
public PremiumList load(final String listName) {
|
||||
return ofy().doTransactionless(new Work<PremiumList>() {
|
||||
.build(
|
||||
new CacheLoader<String, PremiumList>() {
|
||||
@Override
|
||||
public PremiumList run() {
|
||||
return ofy().load()
|
||||
.type(PremiumList.class)
|
||||
.parent(getCrossTldKey())
|
||||
.id(listName)
|
||||
.now();
|
||||
}});
|
||||
}});
|
||||
public PremiumList load(final String listName) {
|
||||
return ofy()
|
||||
.doTransactionless(
|
||||
() ->
|
||||
ofy()
|
||||
.load()
|
||||
.type(PremiumList.class)
|
||||
.parent(getCrossTldKey())
|
||||
.id(listName)
|
||||
.now());
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* In-memory cache for {@link PremiumListRevision}s, used for retrieving Bloom filters quickly.
|
||||
*
|
||||
* <p>This is cached for a long duration (essentially indefinitely) because a given
|
||||
* {@link PremiumListRevision} is immutable and cannot ever be changed once created, so its cache
|
||||
* need not ever expire.
|
||||
* <p>This is cached for a long duration (essentially indefinitely) because a given {@link
|
||||
* PremiumListRevision} is immutable and cannot ever be changed once created, so its cache need
|
||||
* not ever expire.
|
||||
*/
|
||||
static final LoadingCache<Key<PremiumListRevision>, PremiumListRevision>
|
||||
cachePremiumListRevisions =
|
||||
|
@ -166,14 +166,9 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
|
|||
new CacheLoader<Key<PremiumListRevision>, PremiumListRevision>() {
|
||||
@Override
|
||||
public PremiumListRevision load(final Key<PremiumListRevision> revisionKey) {
|
||||
return ofy()
|
||||
.doTransactionless(
|
||||
new Work<PremiumListRevision>() {
|
||||
@Override
|
||||
public PremiumListRevision run() {
|
||||
return ofy().load().key(revisionKey).now();
|
||||
}});
|
||||
}});
|
||||
return ofy().doTransactionless(() -> ofy().load().key(revisionKey).now());
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* In-memory cache for {@link PremiumListEntry}s for a given label and {@link PremiumListRevision}
|
||||
|
@ -206,10 +201,7 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
|
|||
@Override
|
||||
public Optional<PremiumListEntry> load(final Key<PremiumListEntry> entryKey) {
|
||||
return ofy()
|
||||
.doTransactionless(
|
||||
() -> {
|
||||
return Optional.ofNullable(ofy().load().key(entryKey).now());
|
||||
});
|
||||
.doTransactionless(() -> Optional.ofNullable(ofy().load().key(entryKey).now()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ import com.google.common.cache.CacheLoader;
|
|||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.common.net.InternetDomainName;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
import com.googlecode.objectify.Key;
|
||||
|
@ -45,7 +46,6 @@ import com.googlecode.objectify.annotation.Mapify;
|
|||
import com.googlecode.objectify.mapper.Mapper;
|
||||
import google.registry.model.registry.Registry;
|
||||
import google.registry.model.registry.label.DomainLabelMetrics.MetricsReservedListMatch;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
@ -96,6 +96,7 @@ public final class ReservedList
|
|||
|
||||
/** Mapper for use with @Mapify */
|
||||
static class LabelMapper implements Mapper<String, ReservedListEntry> {
|
||||
|
||||
@Override
|
||||
public String getKey(ReservedListEntry entry) {
|
||||
return entry.getLabel();
|
||||
|
@ -130,7 +131,8 @@ public final class ReservedList
|
|||
entry.allowedNameservers = Joiner.on(',').join(allowedNameservers);
|
||||
}
|
||||
} else {
|
||||
checkArgument(reservationType != RESERVED_FOR_ANCHOR_TENANT,
|
||||
checkArgument(
|
||||
reservationType != RESERVED_FOR_ANCHOR_TENANT,
|
||||
"Anchor tenant reservations must have an auth code configured");
|
||||
checkArgument(
|
||||
reservationType != NAMESERVER_RESTRICTED,
|
||||
|
@ -143,13 +145,13 @@ public final class ReservedList
|
|||
}
|
||||
|
||||
private static void checkNameserversAreValid(Set<String> nameservers) {
|
||||
for (String nameserver : nameservers) {
|
||||
// A domain name with fewer than two parts cannot be a hostname, as a nameserver should be.
|
||||
checkArgument(
|
||||
InternetDomainName.from(nameserver).parts().size() >= 3,
|
||||
"%s is not a valid nameserver hostname",
|
||||
nameserver);
|
||||
}
|
||||
// A domain name with fewer than two parts cannot be a hostname, as a nameserver should be.
|
||||
nameservers.forEach(
|
||||
(ns) ->
|
||||
checkArgument(
|
||||
InternetDomainName.from(ns).parts().size() >= 3,
|
||||
"%s is not a valid nameserver hostname",
|
||||
ns));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -218,7 +220,7 @@ public final class ReservedList
|
|||
}
|
||||
return getReservedListEntries(label, tld)
|
||||
.stream()
|
||||
.map((ReservedListEntry reservedListEntry) -> reservedListEntry.reservationType)
|
||||
.map(ReservedListEntry::getValue)
|
||||
.collect(toImmutableSet());
|
||||
}
|
||||
|
||||
|
@ -230,15 +232,13 @@ public final class ReservedList
|
|||
*/
|
||||
public static boolean matchesAnchorTenantReservation(
|
||||
InternetDomainName domainName, String authCode) {
|
||||
ImmutableSet<ReservedListEntry> entries =
|
||||
getReservedListEntries(domainName.parts().get(0), domainName.parent().toString());
|
||||
|
||||
Set<String> domainAuthCodes = new HashSet<>();
|
||||
for (ReservedListEntry entry : entries) {
|
||||
if (entry.reservationType == RESERVED_FOR_ANCHOR_TENANT) {
|
||||
domainAuthCodes.add(entry.getAuthCode());
|
||||
}
|
||||
}
|
||||
ImmutableSet<String> domainAuthCodes =
|
||||
getReservedListEntries(domainName.parts().get(0), domainName.parent().toString())
|
||||
.stream()
|
||||
.filter((entry) -> entry.reservationType == RESERVED_FOR_ANCHOR_TENANT)
|
||||
.map(ReservedListEntry::getAuthCode)
|
||||
.collect(toImmutableSet());
|
||||
checkState(
|
||||
domainAuthCodes.size() <= 1, "There are conflicting auth codes for domain: %s", domainName);
|
||||
|
||||
|
@ -253,38 +253,27 @@ public final class ReservedList
|
|||
* domain is not set with {@code NAMESERVER_RESTRICTED} reservation type.
|
||||
*/
|
||||
public static ImmutableSet<String> getAllowedNameservers(InternetDomainName domainName) {
|
||||
HashSet<String> allowedNameservers = new HashSet<>();
|
||||
boolean foundFirstNameserverRestricted = false;
|
||||
for (ReservedListEntry entry :
|
||||
getReservedListEntries(domainName.parts().get(0), domainName.parent().toString())) {
|
||||
if (entry.reservationType == NAMESERVER_RESTRICTED) {
|
||||
if (foundFirstNameserverRestricted) {
|
||||
allowedNameservers.retainAll(entry.getAllowedNameservers());
|
||||
} else {
|
||||
allowedNameservers = new HashSet<String>(entry.getAllowedNameservers());
|
||||
foundFirstNameserverRestricted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ImmutableSet.copyOf(allowedNameservers);
|
||||
return getReservedListEntries(domainName.parts().get(0), domainName.parent().toString())
|
||||
.stream()
|
||||
.filter((entry) -> entry.reservationType == NAMESERVER_RESTRICTED)
|
||||
.map(ReservedListEntry::getAllowedNameservers)
|
||||
.reduce((types1, types2) -> Sets.intersection(types1, types2).immutableCopy())
|
||||
.orElse(ImmutableSet.of());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to retrieve the entries associated with this label and TLD, or an empty set if
|
||||
* no such entry exists.
|
||||
*/
|
||||
private static ImmutableSet<ReservedListEntry> getReservedListEntries(String label, String tld) {
|
||||
DateTime startTime = DateTime.now(UTC);
|
||||
Registry registry = Registry.get(checkNotNull(tld, "tld"));
|
||||
ImmutableSet<Key<ReservedList>> reservedLists = registry.getReservedLists();
|
||||
ImmutableSet<ReservedList> lists = loadReservedLists(reservedLists);
|
||||
Registry registry = Registry.get(checkNotNull(tld, "tld must not be null"));
|
||||
ImmutableSet.Builder<ReservedListEntry> entriesBuilder = new ImmutableSet.Builder<>();
|
||||
ImmutableSet.Builder<MetricsReservedListMatch> metricMatchesBuilder =
|
||||
new ImmutableSet.Builder<>();
|
||||
|
||||
// Loop through all reservation lists and add each of them.
|
||||
for (ReservedList rl : lists) {
|
||||
for (ReservedList rl : loadReservedLists(registry.getReservedLists())) {
|
||||
if (rl.getReservedListEntries().containsKey(label)) {
|
||||
ReservedListEntry entry = rl.getReservedListEntries().get(label);
|
||||
entriesBuilder.add(entry);
|
||||
|
@ -300,17 +289,20 @@ public final class ReservedList
|
|||
|
||||
private static ImmutableSet<ReservedList> loadReservedLists(
|
||||
ImmutableSet<Key<ReservedList>> reservedListKeys) {
|
||||
ImmutableSet.Builder<ReservedList> builder = new ImmutableSet.Builder<>();
|
||||
for (Key<ReservedList> listKey : reservedListKeys) {
|
||||
try {
|
||||
builder.add(cache.get(listKey.getName()));
|
||||
} catch (ExecutionException e) {
|
||||
throw new UncheckedExecutionException(String.format(
|
||||
"Could not load the reserved list '%s' from the cache", listKey.getName()), e);
|
||||
}
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
return reservedListKeys
|
||||
.stream()
|
||||
.map(
|
||||
(listKey) -> {
|
||||
try {
|
||||
return cache.get(listKey.getName());
|
||||
} catch (ExecutionException e) {
|
||||
throw new UncheckedExecutionException(
|
||||
String.format(
|
||||
"Could not load the reserved list '%s' from the cache", listKey.getName()),
|
||||
e);
|
||||
}
|
||||
})
|
||||
.collect(toImmutableSet());
|
||||
}
|
||||
|
||||
private static LoadingCache<String, ReservedList> cache =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue