Make EppResource.loadCached() use batched fetch (#652)

* Make EppResource.loadCached() use batched fetch

Use a batched fetch (ofy().load().keys(...)) from datastore in
EppResource.loadCached().

To support this, convert TransactionManager.load(Iterable<VKey>) to accept the
more flexible generic parameters and return a map.

* Simplify datastore key streaming

* Changes requested in review.
This commit is contained in:
Michael Muller 2020-06-26 13:50:02 -04:00 committed by GitHub
parent 5c5b6b20ce
commit 07ff6279bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 68 additions and 39 deletions

View file

@ -21,6 +21,7 @@ import static google.registry.persistence.transaction.TransactionManagerFactory.
import static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
@ -35,6 +36,7 @@ import google.registry.testing.InjectRule;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.RegisterExtension;
@ -272,6 +274,40 @@ public class TransactionManagerTest {
assertAllEntitiesNotExist(moreEntities);
}
@TestTemplate
void load_multi() {
assertAllEntitiesNotExist(moreEntities);
tm().transact(() -> tm().saveAllNew(moreEntities));
List<VKey<TestEntity>> keys =
moreEntities.stream().map(TestEntity::key).collect(toImmutableList());
assertThat(tm().transact(() -> tm().load(keys)))
.isEqualTo(Maps.uniqueIndex(moreEntities, TestEntity::key));
}
@TestTemplate
void load_multiWithDuplicateKeys() {
assertAllEntitiesNotExist(moreEntities);
tm().transact(() -> tm().saveAllNew(moreEntities));
ImmutableList<VKey<TestEntity>> keys =
moreEntities.stream().map(TestEntity::key).collect(toImmutableList());
ImmutableList<VKey<TestEntity>> doubleKeys =
Stream.concat(keys.stream(), keys.stream()).collect(toImmutableList());
assertThat(tm().transact(() -> tm().load(doubleKeys)))
.isEqualTo(Maps.uniqueIndex(moreEntities, TestEntity::key));
}
@TestTemplate
void load_multiMissingKeys() {
assertAllEntitiesNotExist(moreEntities);
tm().transact(() -> tm().saveAllNew(moreEntities));
List<VKey<TestEntity>> keys =
Stream.concat(moreEntities.stream(), Stream.of(new TestEntity("dark", "matter")))
.map(TestEntity::key)
.collect(toImmutableList());
assertThat(tm().transact(() -> tm().load(keys)))
.isEqualTo(Maps.uniqueIndex(moreEntities, TestEntity::key));
}
private static void assertEntityExists(TestEntity entity) {
assertThat(tm().transact(() -> tm().checkExists(entity))).isTrue();
}