Skip to content

Commit

Permalink
datastore: re-use query object for continuation queries (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored and callmehiphop committed Sep 28, 2016
1 parent 6c1ebc0 commit 96b845b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
8 changes: 5 additions & 3 deletions packages/datastore/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
});
}

Expand Down
20 changes: 16 additions & 4 deletions packages/datastore/test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit 96b845b

Please sign in to comment.