Use the max remaining results to limit query
If the API server always uses the batch size, the results of the
final query may bump the total requests above the query's limit.
@@ -496,9 +496,15 @@ class DatastoreDistributed(apiproxy_stub.APIProxyStub):
del self.__queries[cursor_handle]
return
- count = _BATCH_SIZE
+ if query.limit():
+ max_remaining_results = query.limit() - internal_cursor.get_offset()
+ else:
+ max_remaining_results = sys.maxint
+
if next_request.has_count():
- count = next_request.count()
+ count = min(next_request.count(), max_remaining_results)
+ else:
+ count = min(_BATCH_SIZE, max_remaining_results)
query.set_count(count)
if next_request.has_offset():
