Skip to content

Commit 573aa32

Browse files
committed
ae/datastore: Remove unnecessary getKeys() calls.
Also, update most queries to be `setKeysOnly()`. This is a good practice when we don't care about the property values (more efficient queries). And it also demonstrates that the equality matching for `Entity` objects only checks the key values, not the property values.
1 parent 224123b commit 573aa32

File tree

2 files changed

+64
-99
lines changed

2 files changed

+64
-99
lines changed

appengine/datastore/src/test/java/com/example/appengine/IndexesTest.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.google.appengine.api.datastore.Query.FilterPredicate;
3131
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
3232
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
33-
import com.google.common.collect.ImmutableList;
3433
import org.junit.After;
3534
import org.junit.Before;
3635
import org.junit.Test;
@@ -90,14 +89,6 @@ public void propertyFilterExample_returnsMatchingEntities() throws Exception {
9089
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
9190
// [END unindexed_properties_1]
9291

93-
assertThat(getKeys(results)).named("query result keys").containsExactly(tom.getKey());
94-
}
95-
96-
private ImmutableList<Key> getKeys(List<Entity> entities) {
97-
ImmutableList.Builder<Key> keys = ImmutableList.builder();
98-
for (Entity entity : entities) {
99-
keys.add(entity.getKey());
100-
}
101-
return keys.build();
92+
assertThat(results).named("query results").containsExactly(tom);
10293
}
10394
}

appengine/datastore/src/test/java/com/example/appengine/QueriesTest.java

Lines changed: 63 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ public void propertyFilterExample_returnsMatchingEntities() throws Exception {
9393
// [END property_filter_example]
9494

9595
// Assert
96-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
97-
assertThat(getKeys(results))
98-
.named("query result keys")
99-
.containsExactly(p2.getKey(), p3.getKey());
96+
List<Entity> results =
97+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
98+
assertThat(results).named("query results").containsExactly(p2, p3);
10099
}
101100

102101
@Test
@@ -120,16 +119,14 @@ public void keyFilterExample_returnsMatchingEntities() throws Exception {
120119
// [END key_filter_example]
121120

122121
// Assert
123-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
124-
assertThat(getKeys(results))
125-
.named("query result keys")
122+
List<Entity> results =
123+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
124+
assertThat(results)
125+
.named("query results")
126126
.containsExactly(
127-
// Ancestor path "b/bb/aaa" is greater than "b/bb".
128-
aaa.getKey(),
129-
// Ancestor path "b/bb/bbb" is greater than "b/bb".
130-
bbb.getKey(),
131-
// Key name identifier "c" is greater than b.
132-
c.getKey());
127+
aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb".
128+
bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb".
129+
c); // Key name identifier "c" is greater than b.
133130
}
134131

135132
@Test
@@ -155,18 +152,15 @@ public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception
155152
// [END kindless_query_example]
156153

157154
// Assert
158-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
159-
assertThat(getKeys(results))
160-
.named("query result keys")
155+
List<Entity> results =
156+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
157+
assertThat(results)
158+
.named("query results")
161159
.containsExactly(
162-
// Ancestor path "b/bb/aaa" is greater than "b/bb".
163-
aaa.getKey(),
164-
// Ancestor path "b/bb/bbb" is greater than "b/bb".
165-
bbb.getKey(),
166-
// Kind "ZooAnimal" is greater than "Child"
167-
zooAnimal.getKey(),
168-
// Key name identifier "c" is greater than b.
169-
c.getKey());
160+
aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb".
161+
bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb".
162+
zooAnimal, // Kind "ZooAnimal" is greater than "Child"
163+
c); // Key name identifier "c" is greater than b.
170164
}
171165

172166
@Test
@@ -184,10 +178,9 @@ public void ancestorFilterExample_returnsMatchingEntities() throws Exception {
184178
// [END ancestor_filter_example]
185179

186180
// Assert
187-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
188-
assertThat(getKeys(results))
189-
.named("query result keys")
190-
.containsExactly(a.getKey(), aa.getKey(), ab.getKey());
181+
List<Entity> results =
182+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
183+
assertThat(results).named("query results").containsExactly(a, aa, ab);
191184
}
192185

193186
@Test
@@ -222,9 +215,7 @@ public void ancestorQueryExample_returnsMatchingEntities() throws Exception {
222215
datastore.prepare(photoQuery).asList(FetchOptions.Builder.withDefaults());
223216
// [END ancestor_query_example]
224217

225-
assertThat(getKeys(results))
226-
.named("query result keys")
227-
.containsExactly(weddingPhoto.getKey(), babyPhoto.getKey(), dancePhoto.getKey());
218+
assertThat(results).named("query results").containsExactly(weddingPhoto, babyPhoto, dancePhoto);
228219
}
229220

230221
@Test
@@ -252,10 +243,9 @@ public void ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities() thr
252243
// [END kindless_ancestor_key_query_example]
253244

254245
// Assert
255-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
256-
assertThat(getKeys(results))
257-
.named("query result keys")
258-
.containsExactly(bc.getKey(), bbb.getKey());
246+
List<Entity> results =
247+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
248+
assertThat(results).named("query results").containsExactly(bc, bbb);
259249
}
260250

261251
@Test
@@ -290,9 +280,7 @@ public void ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities()
290280
datastore.prepare(mediaQuery).asList(FetchOptions.Builder.withDefaults());
291281
// [END kindless_ancestor_query_example]
292282

293-
assertThat(getKeys(results))
294-
.named("query result keys")
295-
.containsExactly(weddingPhoto.getKey(), weddingVideo.getKey());
283+
assertThat(results).named("query result keys").containsExactly(weddingPhoto, weddingVideo);
296284
}
297285

298286
@Test
@@ -309,7 +297,7 @@ public void keysOnlyExample_returnsMatchingEntities() throws Exception {
309297

310298
// Assert
311299
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
312-
assertThat(getKeys(results)).named("query result keys").containsExactly(a.getKey(), c.getKey());
300+
assertThat(results).named("query results").containsExactly(a, c);
313301
}
314302

315303
@Test
@@ -337,16 +325,11 @@ public void sortOrderExample_returnsSortedEntities() throws Exception {
337325

338326
// Assert
339327
List<Entity> lastNameResults =
340-
datastore.prepare(q1).asList(FetchOptions.Builder.withDefaults());
341-
assertThat(getKeys(lastNameResults))
342-
.named("last name query result keys")
343-
.containsExactly(a.getKey(), b.getKey(), c.getKey())
344-
.inOrder();
345-
List<Entity> heightResults = datastore.prepare(q2).asList(FetchOptions.Builder.withDefaults());
346-
assertThat(getKeys(heightResults))
347-
.named("height query result keys")
348-
.containsExactly(c.getKey(), b.getKey(), a.getKey())
349-
.inOrder();
328+
datastore.prepare(q1.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
329+
assertThat(lastNameResults).named("last name query results").containsExactly(a, b, c).inOrder();
330+
List<Entity> heightResults =
331+
datastore.prepare(q2.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
332+
assertThat(heightResults).named("height query results").containsExactly(c, b, a).inOrder();
350333
}
351334

352335
@Test
@@ -375,11 +358,9 @@ public void sortOrderExample_multipleSortOrders_returnsSortedEntities() throws E
375358
// [END multiple_sort_orders_example]
376359

377360
// Assert
378-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
379-
assertThat(getKeys(results))
380-
.named("query result keys")
381-
.containsExactly(a.getKey(), b2.getKey(), b1.getKey(), c.getKey())
382-
.inOrder();
361+
List<Entity> results =
362+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
363+
assertThat(results).named("query results").containsExactly(a, b2, b1, c).inOrder();
383364
}
384365

385366
@Test
@@ -460,8 +441,9 @@ public void queryInterface_singleFilter_returnsMatchedEntities() throws Exceptio
460441
// [END interface_2]
461442

462443
// Assert
463-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
464-
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey(), c.getKey());
444+
List<Entity> results =
445+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
446+
assertThat(results).named("query results").containsExactly(b, c);
465447
}
466448

467449
@Test
@@ -492,8 +474,9 @@ public void queryInterface_orFilter_printsMatchedEntities() throws Exception {
492474
// [END interface_3]
493475

494476
// Assert
495-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
496-
assertThat(getKeys(results)).named("query result keys").containsExactly(a.getKey(), c.getKey());
477+
List<Entity> results =
478+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
479+
assertThat(results).named("query results").containsExactly(a, c);
497480
}
498481

499482
@Test
@@ -524,8 +507,9 @@ public void queryRestrictions_compositeFilter_returnsMatchedEntities() throws Ex
524507
// [END inequality_filters_one_property_valid_example_1]
525508

526509
// Assert
527-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
528-
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey());
510+
List<Entity> results =
511+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
512+
assertThat(results).named("query results").containsExactly(b);
529513
}
530514

531515
@Test
@@ -598,8 +582,9 @@ public void queryRestrictions_compositeEqualFilter_returnsMatchedEntities() thro
598582
// [END inequality_filters_one_property_valid_example_2]
599583

600584
// Assert
601-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
602-
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey());
585+
List<Entity> results =
586+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
587+
assertThat(results).named("query results").containsExactly(b);
603588
}
604589

605590
@Test
@@ -632,11 +617,9 @@ public void queryRestrictions_inequalitySortedFirst_returnsMatchedEntities() thr
632617
// [END inequality_filters_sort_orders_valid_example]
633618

634619
// Assert
635-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
636-
assertThat(getKeys(results))
637-
.named("query result keys")
638-
.containsExactly(c.getKey(), d.getKey(), b.getKey())
639-
.inOrder();
620+
List<Entity> results =
621+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
622+
assertThat(results).named("query results").containsExactly(c, d, b).inOrder();
640623
}
641624

642625
@Test
@@ -700,7 +683,8 @@ public void queryRestrictions_surprisingMultipleValuesAllMustMatch_returnsNoEnti
700683
// Entity "a" will not match because no individual value matches all filters.
701684
// See the documentation for more details:
702685
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
703-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
686+
List<Entity> results =
687+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
704688
assertThat(results).named("query results").isEmpty();
705689
}
706690

@@ -731,8 +715,9 @@ public void queryRestrictions_surprisingMultipleValuesEquals_returnsMatchedEntit
731715
// Only "a" and "e" have both 1 and 2 in the "x" array-valued property.
732716
// See the documentation for more details:
733717
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
734-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
735-
assertThat(getKeys(results)).named("query result keys").containsExactly(a.getKey(), e.getKey());
718+
List<Entity> results =
719+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
720+
assertThat(results).named("query results").containsExactly(a, e);
736721
}
737722

738723
@Test
@@ -757,10 +742,9 @@ public void queryRestrictions_surprisingMultipleValuesNotEquals_returnsMatchedEn
757742
// The query matches any entity that has a some value other than 1. Only
758743
// entity "e" is not matched. See the documentation for more details:
759744
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
760-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
761-
assertThat(getKeys(results))
762-
.named("query result keys")
763-
.containsExactly(a.getKey(), b.getKey(), c.getKey(), d.getKey());
745+
List<Entity> results =
746+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
747+
assertThat(results).named("query results").containsExactly(a, b, c, d);
764748
}
765749

766750
@Test
@@ -788,8 +772,9 @@ public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatche
788772
//
789773
// See the documentation for more details:
790774
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
791-
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
792-
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey());
775+
List<Entity> results =
776+
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
777+
assertThat(results).named("query results").containsExactly(b);
793778
}
794779

795780
private Entity retrievePersonWithLastName(String targetLastName) {
@@ -814,7 +799,7 @@ public void singleRetrievalExample_singleEntity_returnsEntity() throws Exception
814799

815800
Entity result = retrievePersonWithLastName("Johnson");
816801

817-
assertThat(result.getKey()).named("result key").isEqualTo(a.getKey());
802+
assertThat(result).named("result").isEqualTo(a); // Note: Entity.equals() only checks the Key.
818803
}
819804

820805
@Test
@@ -866,17 +851,6 @@ public void queryLimitExample_returnsLimitedEntities() throws Exception {
866851

867852
List<Entity> results = getTallestPeople();
868853

869-
assertThat(getKeys(results))
870-
.named("result keys")
871-
.containsExactly(g.getKey(), e.getKey(), c.getKey(), a.getKey(), b.getKey())
872-
.inOrder();
873-
}
874-
875-
private ImmutableList<Key> getKeys(List<Entity> entities) {
876-
ImmutableList.Builder<Key> keys = ImmutableList.builder();
877-
for (Entity entity : entities) {
878-
keys.add(entity.getKey());
879-
}
880-
return keys.build();
854+
assertThat(results).named("results").containsExactly(g, e, c, a, b).inOrder();
881855
}
882856
}

0 commit comments

Comments
 (0)