Convert to tm() some low-hanging ofy fruit (#1029)

* Convert to tm() some low-hanging ofy fruit
This commit is contained in:
gbrodman
2021-03-25 20:01:53 -04:00
committed by GitHub
parent 2649a9362a
commit 1e650bd0a1
10 changed files with 118 additions and 60 deletions
@@ -16,6 +16,7 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkState;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import com.beust.jcommander.Parameter;
@@ -61,13 +62,7 @@ final class DeleteTldCommand extends ConfirmingCommand implements CommandWithRem
"Cannot delete TLD because registrar %s lists it as an allowed TLD",
registrar.getClientId());
}
int count = ofy().load()
.type(DomainBase.class)
.filter("tld", tld)
.limit(1)
.count();
checkState(count == 0, "Cannot delete TLD because a domain is defined on it");
checkState(!tldContainsDomains(tld), "Cannot delete TLD because a domain is defined on it");
}
@Override
@@ -77,8 +72,25 @@ final class DeleteTldCommand extends ConfirmingCommand implements CommandWithRem
@Override
protected String execute() {
tm().transactNew(() -> ofy().delete().entity(registry).now());
tm().transactNew(() -> tm().delete(registry));
registry.invalidateInCache();
return String.format("Deleted TLD '%s'.\n", tld);
}
private boolean tldContainsDomains(String tld) {
if (tm().isOfy()) {
return ofy().load().type(DomainBase.class).filter("tld", tld).limit(1).count() > 0;
} else {
return jpaTm()
.transact(
() ->
jpaTm()
.query("FROM Domain WHERE tld = :tld", DomainBase.class)
.setParameter("tld", tld)
.setMaxResults(1)
.getResultStream()
.findFirst()
.isPresent());
}
}
}
@@ -15,17 +15,19 @@
package google.registry.tools;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Strings;
import com.googlecode.objectify.Key;
import com.google.common.collect.ImmutableMap;
import google.registry.model.common.Cursor;
import google.registry.model.common.Cursor.CursorType;
import google.registry.model.registry.Registries;
import google.registry.model.registry.Registry;
import google.registry.model.registry.Registry.TldType;
import google.registry.persistence.VKey;
import java.util.Map;
import java.util.Optional;
@@ -50,20 +52,18 @@ final class ListCursorsCommand implements CommandWithRemoteApi {
@Override
public void run() {
Map<Registry, Key<Cursor>> registries =
Registries.getTlds()
.stream()
Map<Registry, VKey<Cursor>> registries =
Registries.getTlds().stream()
.map(Registry::get)
.filter(r -> r.getTldType() == filterTldType)
.filter(r -> !filterEscrowEnabled || r.getEscrowEnabled())
.collect(toImmutableMap(r -> r, r -> Cursor.createKey(cursorType, r)));
Map<Key<Cursor>, Cursor> cursors = ofy().load().keys(registries.values());
.collect(toImmutableMap(r -> r, r -> Cursor.createVKey(cursorType, r.getTldStr())));
ImmutableMap<VKey<? extends Cursor>, Cursor> cursors =
transactIfJpaTm(() -> tm().loadByKeysIfPresent(registries.values()));
if (!registries.isEmpty()) {
String header = String.format(OUTPUT_FMT, "TLD", "Cursor Time", "Last Update Time");
System.out.printf("%s\n%s\n", header, Strings.repeat("-", header.length()));
registries
.entrySet()
.stream()
registries.entrySet().stream()
.map(
e ->
renderLine(
@@ -15,13 +15,13 @@
package google.registry.tools.server;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.POST;
import static java.util.Comparator.comparing;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import google.registry.model.EppResourceUtils;
import google.registry.model.host.HostResource;
import google.registry.request.Action;
@@ -51,7 +51,7 @@ public final class ListHostsAction extends ListObjectsAction<HostResource> {
@Override
public ImmutableSet<HostResource> loadObjects() {
final DateTime now = clock.nowUtc();
return Streams.stream(ofy().load().type(HostResource.class))
return transactIfJpaTm(() -> tm().loadAllOf(HostResource.class)).stream()
.filter(host -> EppResourceUtils.isActive(host, now))
.collect(toImmutableSortedSet(comparing(HostResource::getHostName)));
}
@@ -14,16 +14,21 @@
package google.registry.tools.server;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.POST;
import com.google.common.collect.ImmutableSet;
import google.registry.model.registry.label.PremiumList;
import google.registry.model.registry.label.PremiumListDualDao;
import google.registry.request.Action;
import google.registry.request.auth.Auth;
import java.util.Comparator;
import java.util.Optional;
import javax.inject.Inject;
import org.hibernate.Hibernate;
/**
* An action that lists premium lists, for use by the {@code nomulus list_premium_lists} command.
@@ -46,7 +51,14 @@ public final class ListPremiumListsAction extends ListObjectsAction<PremiumList>
@Override
public ImmutableSet<PremiumList> loadObjects() {
return ImmutableSet.copyOf(
ofy().load().type(PremiumList.class).ancestor(getCrossTldKey()).list());
return transactIfJpaTm(
() ->
tm().loadAllOf(PremiumList.class).stream()
.map(PremiumList::getName)
.map(PremiumListDualDao::getLatestRevision)
.filter(Optional::isPresent)
.map(Optional::get)
.peek(list -> Hibernate.initialize(list.getLabelsToPrices()))
.collect(toImmutableSortedSet(Comparator.comparing(PremiumList::getName))));
}
}
@@ -14,15 +14,19 @@
package google.registry.tools.server;
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.persistence.transaction.TransactionManagerUtil.transactIfJpaTm;
import static google.registry.request.Action.Method.GET;
import static google.registry.request.Action.Method.POST;
import com.google.common.collect.ImmutableSet;
import google.registry.model.registry.label.ReservedList;
import google.registry.model.registry.label.ReservedListDualDatabaseDao;
import google.registry.request.Action;
import google.registry.request.auth.Auth;
import java.util.Comparator;
import java.util.Optional;
import javax.inject.Inject;
/** A that lists reserved lists, for use by the {@code nomulus list_reserved_lists} command. */
@@ -44,7 +48,13 @@ public final class ListReservedListsAction extends ListObjectsAction<ReservedLis
@Override
public ImmutableSet<ReservedList> loadObjects() {
return ImmutableSet.copyOf(
ofy().load().type(ReservedList.class).ancestor(getCrossTldKey()).list());
return transactIfJpaTm(
() ->
tm().loadAllOf(ReservedList.class).stream()
.map(ReservedList::getName)
.map(ReservedListDualDatabaseDao::getLatestRevision)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toImmutableSortedSet(Comparator.comparing(ReservedList::getName))));
}
}