Migrate away from VoidWorks

This is one last hanging piece of work left over from last year's Java 8
migration. There's no functionality changes in this CL, just refactoring.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=201947600
This commit is contained in:
mcilwain 2018-06-25 07:04:26 -07:00 committed by Ben McIlwain
parent 7d3cb3d426
commit d3364b0387
18 changed files with 349 additions and 502 deletions

View file

@ -30,7 +30,6 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.VoidWork;
import google.registry.model.ImmutableObject;
import google.registry.tools.Command.RemoteApiCommand;
import java.util.ArrayList;
@ -142,39 +141,33 @@ public abstract class MutatingCommand extends ConfirmingCommand implements Remot
@Override
protected String execute() throws Exception {
for (final List<EntityChange> batch : getCollatedEntityChangeBatches()) {
ofy().transact(new VoidWork() {
@Override
public void vrun() {
for (EntityChange change : batch) {
// Load the key of the entity to mutate and double-check that it hasn't been
// modified from the version that existed when the change was prepared.
ImmutableObject existingEntity = ofy().load().key(change.key).now();
checkState(
Objects.equals(change.oldEntity, existingEntity),
"Entity changed since init() was called.\n%s",
prettyPrintEntityDeepDiff(
change.oldEntity == null ? ImmutableMap.of()
: change.oldEntity.toDiffableFieldMap(),
existingEntity == null ? ImmutableMap.of()
: existingEntity.toDiffableFieldMap()));
switch (change.type) {
case CREATE: // Fall through.
case UPDATE:
ofy().save().entity(change.newEntity).now();
break;
case DELETE:
ofy().delete().key(change.key).now();
break;
default:
throw new UnsupportedOperationException(
"Unknown entity change type: " + change.type);
}
}
}});
ofy().transact(() -> batch.forEach(this::executeChange));
}
return String.format("Updated %d entities.\n", changedEntitiesMap.size());
}
private void executeChange(EntityChange change) {
// Load the key of the entity to mutate and double-check that it hasn't been
// modified from the version that existed when the change was prepared.
ImmutableObject existingEntity = ofy().load().key(change.key).now();
checkState(
Objects.equals(change.oldEntity, existingEntity),
"Entity changed since init() was called.\n%s",
prettyPrintEntityDeepDiff(
(change.oldEntity == null) ? ImmutableMap.of() : change.oldEntity.toDiffableFieldMap(),
(existingEntity == null) ? ImmutableMap.of() : existingEntity.toDiffableFieldMap()));
switch (change.type) {
case CREATE: // Fall through.
case UPDATE:
ofy().save().entity(change.newEntity).now();
return;
case DELETE:
ofy().delete().key(change.key).now();
return;
}
throw new UnsupportedOperationException("Unknown entity change type: " + change.type);
}
/**
* Returns a set of lists of EntityChange actions to commit. Each list should be executed in
* order inside a single transaction.