From dad3fcca9a81fede9e04baca18dd31d7f81ce8a8 Mon Sep 17 00:00:00 2001 From: Michael Muller Date: Tue, 8 Jun 2021 15:23:25 -0400 Subject: [PATCH] Fix Datastore "count" queries (#1201) * Fix Datastore "count" queries The objectify "count()" method doesn't work for result sets larger than 1000 elements, use the original trick from "count domains" that fetches the keys and counts them. * Added an SO link --- .../registry/model/ofy/DatastoreTransactionManager.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/google/registry/model/ofy/DatastoreTransactionManager.java b/core/src/main/java/google/registry/model/ofy/DatastoreTransactionManager.java index d91911f68..eb5703ded 100644 --- a/core/src/main/java/google/registry/model/ofy/DatastoreTransactionManager.java +++ b/core/src/main/java/google/registry/model/ofy/DatastoreTransactionManager.java @@ -480,7 +480,13 @@ public class DatastoreTransactionManager implements TransactionManager { @Override public long count() { - return buildQuery().count(); + // Objectify provides a count() function, but unfortunately that doesn't work if there are + // more than 1000 (the default response page size?) entries in the result set. We also use + // chunkAll() here as it provides a nice performance boost. + // + // There is some information on this issue on SO, see: + // https://stackoverflow.com/questions/751124/how-does-one-get-a-count-of-rows-in-a-datastore-model-in-google-app-engine + return Iterables.size(buildQuery().chunkAll().keys()); } @Override