1
0
mirror of https://github.com/google/nomulus synced 2026-02-13 00:02:04 +00:00

Add deleteAll method to TransactionManager (#604)

* Add deleteAll method to TransactionManager

* Rename deleteAll to delete

* Add bucket.getLastWrittenTime() before second mutation
This commit is contained in:
Shicong Huang
2020-06-03 10:02:48 -04:00
committed by GitHub
parent ed64dd3548
commit 472503541b
4 changed files with 116 additions and 82 deletions

View File

@@ -14,6 +14,7 @@
package google.registry.model.ofy;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
@@ -157,7 +158,7 @@ public class DatastoreTransactionManager implements TransactionManager {
@Override
public <T> ImmutableList<T> load(Iterable<VKey<T>> keys) {
Iterator<Key<T>> iter =
StreamSupport.stream(keys.spliterator(), false).map(key -> key.getOfyKey()).iterator();
StreamSupport.stream(keys.spliterator(), false).map(VKey::getOfyKey).iterator();
// The lambda argument to keys() effectively converts Iterator -> Iterable.
return ImmutableList.copyOf(getOfy().load().keys(() -> iter).values());
@@ -170,7 +171,18 @@ public class DatastoreTransactionManager implements TransactionManager {
}
@Override
public <T> void delete(VKey<T> key) {
public void delete(VKey<?> key) {
getOfy().delete().key(key.getOfyKey()).now();
}
@Override
public void delete(Iterable<? extends VKey<?>> vKeys) {
// We have to create a list to work around the wildcard capture issue here.
// See https://docs.oracle.com/javase/tutorial/java/generics/capture.html
ImmutableList<Key<?>> list =
StreamSupport.stream(vKeys.spliterator(), false)
.map(VKey::getOfyKey)
.collect(toImmutableList());
getOfy().delete().keys(list).now();
}
}

View File

@@ -278,7 +278,7 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
.getResultList());
}
private <T> int internalDelete(VKey<T> key) {
private int internalDelete(VKey<?> key) {
checkArgumentNotNull(key, "key must be specified");
assertInTransaction();
EntityType<?> entityType = getEntityType(key.getKind());
@@ -291,10 +291,16 @@ public class JpaTransactionManagerImpl implements JpaTransactionManager {
}
@Override
public <T> void delete(VKey<T> key) {
public void delete(VKey<?> key) {
internalDelete(key);
}
@Override
public void delete(Iterable<? extends VKey<?>> vKeys) {
checkArgumentNotNull(vKeys, "vKeys must be specified");
vKeys.forEach(this::internalDelete);
}
@Override
public <T> void assertDelete(VKey<T> key) {
if (internalDelete(key) != 1) {

View File

@@ -115,7 +115,7 @@ public interface TransactionManager {
<T> T load(VKey<T> key);
/**
* Leads the set of entities by their key id.
* Loads the set of entities by their key id.
*
* @throws NoSuchElementException if any of the keys are not found.
*/
@@ -125,5 +125,8 @@ public interface TransactionManager {
<T> ImmutableList<T> loadAll(Class<T> clazz);
/** Deletes the entity by its id. */
<T> void delete(VKey<T> key);
void delete(VKey<?> key);
/** Deletes the set of entities by their key id. */
void delete(Iterable<? extends VKey<?>> keys);
}