Improve ListDomainsCommand to list domains on multiple TLDs

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=140053423
This commit is contained in:
mcilwain 2016-11-23 11:23:14 -08:00 committed by Ben McIlwain
parent 35d88a9f8c
commit 5eb9702f05
11 changed files with 120 additions and 88 deletions

View file

@ -14,28 +14,38 @@
package google.registry.tools.server;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.model.EppResourceUtils.queryNotDeleted;
import static google.registry.model.registry.Registries.assertTldExists;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.POST;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Lists;
import google.registry.model.domain.DomainResource;
import google.registry.request.Action;
import google.registry.request.Parameter;
import google.registry.util.Clock;
import java.util.Comparator;
import java.util.List;
import javax.inject.Inject;
/** An action that lists domains, for use by the {@code nomulus list_domains} command. */
@Action(path = ListDomainsAction.PATH, method = {GET, POST})
public final class ListDomainsAction extends ListObjectsAction<DomainResource> {
/** An App Engine limitation on how many subqueries can be used in a single query. */
private static final int MAX_NUM_SUBQUERIES = 30;
private static final Comparator<DomainResource> COMPARATOR =
new Comparator<DomainResource>() {
@Override
public int compare(DomainResource a, DomainResource b) {
return a.getFullyQualifiedDomainName().compareTo(b.getFullyQualifiedDomainName());
}};
public static final String PATH = "/_dr/admin/list/domains";
public static final String TLD_PARAM = "tld";
@Inject @Parameter("tld") String tld;
@Inject @Parameter("tlds") ImmutableSet<String> tlds;
@Inject Clock clock;
@Inject ListDomainsAction() {}
@ -46,12 +56,15 @@ public final class ListDomainsAction extends ListObjectsAction<DomainResource> {
@Override
public ImmutableSet<DomainResource> loadObjects() {
return FluentIterable
.from(queryNotDeleted(DomainResource.class, clock.nowUtc(), "tld", assertTldExists(tld)))
.toSortedSet(new Comparator<DomainResource>() {
@Override
public int compare(DomainResource a, DomainResource b) {
return a.getFullyQualifiedDomainName().compareTo(b.getFullyQualifiedDomainName());
}});
checkArgument(!tlds.isEmpty(), "Must specify TLDs to query");
for (String tld : tlds) {
assertTldExists(tld);
}
ImmutableSortedSet.Builder<DomainResource> builder =
new ImmutableSortedSet.Builder<DomainResource>(COMPARATOR);
for (List<String> batch : Lists.partition(tlds.asList(), MAX_NUM_SUBQUERIES)) {
builder.addAll(queryNotDeleted(DomainResource.class, clock.nowUtc(), "tld in", batch));
}
return builder.build();
}
}