From 96b845b7d104815de86083489ed857aad4c931cf Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 28 Sep 2016 11:03:14 -0400 Subject: [PATCH] datastore: re-use query object for continuation queries (#1635) --- packages/datastore/src/request.js | 8 +++++--- packages/datastore/test/request.js | 20 ++++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/datastore/src/request.js b/packages/datastore/src/request.js index 37ce304a9f5..28744cb68d1 100644 --- a/packages/datastore/src/request.js +++ b/packages/datastore/src/request.js @@ -518,6 +518,8 @@ DatastoreRequest.prototype.runQuery = function(query, options, callback) { return; } + query = extend(true, new Query(), query); + var limiter = common.util.createLimiter(makeRequest, options); var stream = limiter.stream; @@ -586,16 +588,16 @@ DatastoreRequest.prototype.runQuery = function(query, options, callback) { // The query is "NOT_FINISHED". Get the rest of the results. var offset = query.offsetVal === -1 ? 0 : query.offsetVal; - var continuationQuery = extend(true, new Query(), query) + query .start(info.endCursor) .offset(offset - resp.batch.skippedResults); var limit = query.limitVal; if (limit && limit > -1) { - continuationQuery.limit(limit - resp.batch.entityResults.length); + query.limit(limit - resp.batch.entityResults.length); } - limiter.makeRequest(continuationQuery); + limiter.makeRequest(query); }); } diff --git a/packages/datastore/test/request.js b/packages/datastore/test/request.js index 5d27873c491..6e28f4ba95d 100644 --- a/packages/datastore/test/request.js +++ b/packages/datastore/test/request.js @@ -589,12 +589,25 @@ describe('Request', function() { request.runQuery({}, options, assert.ifError); }); + it('should clone the query', function(done) { + var query = new FakeQuery(); + query.namespace = 'namespace'; + query = extend(true, new FakeQuery(), query); + + overrides.entity.queryToQueryProto = function(query_) { + assert.notStrictEqual(query_, query); + assert.deepEqual(query_, query); + done(); + }; + + request.runQuery(query, assert.ifError); + }); + it('should make correct request', function(done) { var query = { namespace: 'namespace' }; var queryProto = {}; - overrides.entity.queryToQueryProto = function(query_) { - assert.strictEqual(query_, query); + overrides.entity.queryToQueryProto = function() { return queryProto; }; @@ -693,7 +706,6 @@ describe('Request', function() { }); it('should re-run query if not finished', function(done) { - var continuationQuery; var query = { limitVal: 1, offsetVal: 8 @@ -769,7 +781,7 @@ describe('Request', function() { overrides.entity.queryToQueryProto = function(query_) { if (timesRequestCalled > 1) { - assert.strictEqual(query_, continuationQuery); + assert.strictEqual(query_, query); } return queryProto; };