mirror of
https://github.com/google/nomulus
synced 2026-09-06 08:07:03 +00:00
Replace ofyOrJpaTm with tm().isOfy() conditionals (#870)
* Replace ofyOrJpaTm with tm().isOfy() conditionals Replace existing ofyOrJpaTm() calls with conditionals (either "if" statements or ternary expressions) gated on tm().isOfy().
This commit is contained in:
@@ -25,7 +25,6 @@ import static google.registry.model.CacheUtils.memoizeWithShortExpiration;
|
||||
import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static google.registry.persistence.transaction.TransactionManagerUtil.ofyOrJpaTm;
|
||||
import static google.registry.util.CollectionUtils.entriesToImmutableMap;
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
|
||||
|
||||
@@ -60,21 +59,19 @@ public final class Registries {
|
||||
tm().doTransactionless(
|
||||
() -> {
|
||||
ImmutableSet<String> tlds =
|
||||
ofyOrJpaTm(
|
||||
() ->
|
||||
ofy()
|
||||
.load()
|
||||
.type(Registry.class)
|
||||
.ancestor(getCrossTldKey())
|
||||
.keys()
|
||||
.list()
|
||||
.stream()
|
||||
.map(Key::getName)
|
||||
.collect(toImmutableSet()),
|
||||
() ->
|
||||
tm().loadAll(Registry.class).stream()
|
||||
.map(Registry::getTldStr)
|
||||
.collect(toImmutableSet()));
|
||||
tm().isOfy()
|
||||
? ofy()
|
||||
.load()
|
||||
.type(Registry.class)
|
||||
.ancestor(getCrossTldKey())
|
||||
.keys()
|
||||
.list()
|
||||
.stream()
|
||||
.map(Key::getName)
|
||||
.collect(toImmutableSet())
|
||||
: tm().loadAll(Registry.class).stream()
|
||||
.map(Registry::getTldStr)
|
||||
.collect(toImmutableSet());
|
||||
return Registry.getAll(tlds).stream()
|
||||
.map(e -> Maps.immutableEntry(e.getTldStr(), e.getTldType()))
|
||||
.collect(entriesToImmutableMap());
|
||||
|
||||
@@ -25,7 +25,6 @@ import static google.registry.model.common.EntityGroupRoot.getCrossTldKey;
|
||||
import static google.registry.model.ofy.ObjectifyService.allocateId;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static google.registry.persistence.transaction.TransactionManagerUtil.ofyOrJpaTm;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Splitter;
|
||||
@@ -209,9 +208,9 @@ public final class PremiumList extends BaseDomainLabelList<Money, PremiumList.Pr
|
||||
}
|
||||
|
||||
private static PremiumList loadPremiumList(String name) {
|
||||
return ofyOrJpaTm(
|
||||
() -> ofy().load().type(PremiumList.class).parent(getCrossTldKey()).id(name).now(),
|
||||
() -> PremiumListDao.getLatestRevision(name).orElseThrow(NoSuchElementException::new));
|
||||
return tm().isOfy()
|
||||
? ofy().load().type(PremiumList.class).parent(getCrossTldKey()).id(name).now()
|
||||
: PremiumListDao.getLatestRevision(name).orElseThrow(NoSuchElementException::new);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static google.registry.config.RegistryConfig.getDomainLabelListCacheDuration;
|
||||
import static google.registry.model.registry.label.ReservationType.FULLY_BLOCKED;
|
||||
import static google.registry.persistence.transaction.TransactionManagerUtil.ofyOrJpaTm;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static google.registry.util.CollectionUtils.nullToEmpty;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
@@ -247,9 +247,9 @@ public final class ReservedList
|
||||
new CacheLoader<String, ReservedList>() {
|
||||
@Override
|
||||
public ReservedList load(String listName) {
|
||||
return ofyOrJpaTm(
|
||||
() -> ReservedListDualWriteDao.getLatestRevision(listName).orElse(null),
|
||||
() -> ReservedListSqlDao.getLatestRevision(listName).orElse(null));
|
||||
return tm().isOfy()
|
||||
? ReservedListDualWriteDao.getLatestRevision(listName).orElse(null)
|
||||
: ReservedListSqlDao.getLatestRevision(listName).orElse(null);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package google.registry.persistence.transaction;
|
||||
import com.google.common.collect.ImmutableCollection;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import google.registry.model.ofy.DatastoreTransactionManager;
|
||||
import google.registry.persistence.VKey;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
@@ -236,4 +237,9 @@ public interface TransactionManager {
|
||||
|
||||
/** Clears the session cache if the underlying database is Datastore, otherwise it is a no-op. */
|
||||
void clearSessionCache();
|
||||
|
||||
/** Returns true if the transaction manager is DatastoreTransactionManager. */
|
||||
default boolean isOfy() {
|
||||
return this instanceof DatastoreTransactionManager;
|
||||
}
|
||||
}
|
||||
|
||||
-34
@@ -50,40 +50,6 @@ public class TransactionManagerUtil {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes either {@code ofyRunnable} if {@link TransactionManagerFactory#tm()} returns a {@link
|
||||
* JpaTransactionManager} instance, or {@code jpaRunnable} if {@link
|
||||
* TransactionManagerFactory#tm()} returns a {@link DatastoreTransactionManager} instance.
|
||||
*/
|
||||
public static void ofyOrJpaTm(Runnable ofyRunnable, Runnable jpaRunnable) {
|
||||
ofyOrJpaTm(
|
||||
() -> {
|
||||
ofyRunnable.run();
|
||||
return null;
|
||||
},
|
||||
() -> {
|
||||
jpaRunnable.run();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result from either {@code ofySupplier} if {@link TransactionManagerFactory#tm()}
|
||||
* returns a {@link JpaTransactionManager} instance, or {@code jpaSupplier} if {@link
|
||||
* TransactionManagerFactory#tm()} returns a {@link DatastoreTransactionManager} instance.
|
||||
*/
|
||||
public static <T> T ofyOrJpaTm(Supplier<T> ofySupplier, Supplier<T> jpaSupplier) {
|
||||
if (tm() instanceof DatastoreTransactionManager) {
|
||||
return ofySupplier.get();
|
||||
} else if (tm() instanceof JpaTransactionManager) {
|
||||
return jpaSupplier.get();
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"Expected tm() to be DatastoreTransactionManager or JpaTransactionManager, but got "
|
||||
+ tm().getClass());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the given {@link Runnable} if {@link TransactionManagerFactory#tm()} returns a {@link
|
||||
* DatastoreTransactionManager} instance, otherwise does nothing.
|
||||
|
||||
Reference in New Issue
Block a user