Clean up tx manager insert() signature and add convenience helper method (#1325)

* Clean up tx manager insert() signature and add convenience helper method

This is the first of a series of PRs to clean up the type signatures on the
TransactionManager methods (which are way too generic), along with creating some
helper methods for use in tests only that don't require creating transactions
all over the place, thus reducing visual noise at callsites. This first method
is DatabaseHelper.insertInDb(), but there will be plenty of others. Note that
this is only for the Cloud SQL transaction manager -- I'm not bothering to
migrate any Datastore-only code, as that will be going away soon enough.
This commit is contained in:
Ben McIlwain
2021-09-17 14:45:07 -04:00
committed by GitHub
parent 12dac76dc8
commit d91ca0eb8a
40 changed files with 173 additions and 110 deletions
@@ -32,6 +32,7 @@ import com.google.common.collect.Streams;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result;
import com.googlecode.objectify.cmd.Query;
import google.registry.model.ImmutableObject;
import google.registry.model.annotations.InCrossTld;
import google.registry.model.contact.ContactHistory;
import google.registry.model.domain.DomainHistory;
@@ -127,6 +128,11 @@ public class DatastoreTransactionManager implements TransactionManager {
putAll(entities);
}
@Override
public void insertAll(ImmutableObject... entities) {
putAll(entities);
}
@Override
public void insertWithoutBackup(Object entity) {
putWithoutBackup(entity);
@@ -143,7 +149,7 @@ public class DatastoreTransactionManager implements TransactionManager {
}
@Override
public void putAll(Object... entities) {
public void putAll(ImmutableObject... entities) {
syncIfTransactionless(
getOfy().save().entities(toDatastoreEntities(ImmutableList.copyOf(entities))));
}
@@ -312,6 +312,11 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
entities.forEach(this::insert);
}
@Override
public void insertAll(ImmutableObject... entities) {
insertAll(ImmutableSet.copyOf(entities));
}
@Override
public void insertWithoutBackup(Object entity) {
insert(entity);
@@ -335,7 +340,7 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
}
@Override
public void putAll(Object... entities) {
public void putAll(ImmutableObject... entities) {
checkArgumentNotNull(entities, "entities must be specified");
assertInTransaction();
for (Object entity : entities) {
@@ -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.ImmutableObject;
import google.registry.model.annotations.InCrossTld;
import google.registry.persistence.VKey;
import java.util.NoSuchElementException;
@@ -97,6 +98,9 @@ public interface TransactionManager {
/** Persists all new entities in the database, throws exception if any entity already exists. */
void insertAll(ImmutableCollection<?> entities);
/** Persists all new entities in the database, throws exception if any entity already exists. */
void insertAll(ImmutableObject... entities);
/**
* Persists a new entity in the database without writing any backup if the underlying database is
* Datastore.
@@ -125,7 +129,7 @@ public interface TransactionManager {
void put(Object entity);
/** Persists all new entities or updates the existing entities in the database. */
void putAll(Object... entities);
void putAll(ImmutableObject... entities);
/** Persists all new entities or updates the existing entities in the database. */
void putAll(ImmutableCollection<?> entities);