Skip to content

Commit f1db1a6

Browse files
committed
Migrate indexIterator to key-only datastore query
As long as we need only the keys of received entities, it's much cheaper to query for them directly instead of doing multiple entity reads.
1 parent bf41329 commit f1db1a6

File tree

1 file changed

+9
-9
lines changed
  • datastore/src/main/java/io/spine/server/storage/datastore

1 file changed

+9
-9
lines changed

datastore/src/main/java/io/spine/server/storage/datastore/Indexes.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
package io.spine.server.storage.datastore;
2222

23-
import com.google.cloud.datastore.Entity;
24-
import com.google.cloud.datastore.EntityQuery;
2523
import com.google.cloud.datastore.Key;
2624
import com.google.cloud.datastore.Query;
25+
import com.google.cloud.datastore.StructuredQuery;
2726
import com.google.common.collect.Streams;
2827
import io.spine.string.Stringifiers;
2928
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -63,19 +62,20 @@ static <I> Iterator<I> indexIterator(DatastoreWrapper datastore, Kind kind, Clas
6362
checkNotNull(kind);
6463
checkNotNull(idType);
6564

66-
EntityQuery.Builder query = Query.newEntityQueryBuilder()
67-
.setKind(kind.getValue());
68-
Iterator<Entity> allEntities = datastore.read(query.build());
65+
StructuredQuery<Key> query = Query.newKeyQueryBuilder()
66+
.setKind(kind.getValue())
67+
.build();
68+
Iterator<Key> allEntities = datastore.read(query);
6969
Iterator<I> idIterator = Streams.stream(allEntities)
7070
.map(idExtractor(idType))
7171
.iterator();
7272
return idIterator;
7373
}
7474

75-
private static <I> Function<Entity, @Nullable I> idExtractor(Class<I> idType) {
76-
return input -> {
77-
checkNotNull(input);
78-
Key key = input.getKey();
75+
private static <I> Function<Key, @Nullable I>
76+
idExtractor(Class<I> idType) {
77+
return key -> {
78+
checkNotNull(key);
7979
String stringId = key.getName();
8080
I id = Stringifiers.fromString(stringId, idType);
8181
return id;

0 commit comments

Comments
 (0)