Skip to content

Commit 46036ae

Browse files
committed
Merge pull request #297 from stephenplusplus/spp--datastore-stream-limit
datastore: runQuery(stream mode) - use limit(n) logically - fixes #296
2 parents 1cc2031 + b091d03 commit 46036ae

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

lib/datastore/request.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ DatastoreRequest.prototype.delete = function(keys, callback) {
336336
DatastoreRequest.prototype.runQuery = function(q, callback) {
337337
var that = this;
338338
var stream;
339+
var resultsToSend = q.limitVal;
339340

340341
var req = {
341342
read_options: {},
@@ -375,21 +376,29 @@ DatastoreRequest.prototype.runQuery = function(q, callback) {
375376
cursor = resp.batch.end_cursor.toBase64();
376377
}
377378

378-
if (stream) {
379-
if (cursor && entities.length > 0) {
380-
entities.forEach(function (entity) {
381-
stream.push(entity);
382-
});
379+
if (!stream) {
380+
callback(null, entities, cursor);
381+
return;
382+
}
383383

384-
req.query = entity.queryToQueryProto(q.start(cursor).offset(0));
384+
if (!cursor || entities.length === 0) {
385+
stream.end();
386+
return;
387+
}
385388

386-
runQuery();
387-
} else {
388-
stream.end();
389-
}
390-
} else {
391-
callback(null, entities, cursor);
389+
var result;
390+
while ((result = entities.shift()) && resultsToSend !== 0) {
391+
stream.push(result);
392+
resultsToSend--;
392393
}
394+
395+
if (resultsToSend === 0) {
396+
stream.end();
397+
return;
398+
}
399+
400+
req.query = entity.queryToQueryProto(q.start(cursor).offset(0));
401+
runQuery();
393402
});
394403
}
395404
};

regression/datastore.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ describe('datastore', function() {
249249
});
250250

251251
it('should run a query as a stream', function(done) {
252-
var q = ds.createQuery('Character').hasAncestor(ancestor)
253-
.limit(5);
252+
var q = ds.createQuery('Character').hasAncestor(ancestor);
254253

255254
var resultsReturned = 0;
256255

@@ -263,6 +262,21 @@ describe('datastore', function() {
263262
});
264263
});
265264

265+
it('should not go over a limit with a stream', function(done) {
266+
var limit = 3;
267+
var q = ds.createQuery('Character').hasAncestor(ancestor).limit(limit);
268+
269+
var resultsReturned = 0;
270+
271+
ds.runQuery(q)
272+
.on('error', done)
273+
.on('data', function() { resultsReturned++; })
274+
.on('end', function() {
275+
assert.equal(resultsReturned, limit);
276+
done();
277+
});
278+
});
279+
266280
it('should filter queries with simple indexes', function(done) {
267281
var q = ds.createQuery('Character').hasAncestor(ancestor)
268282
.filter('appearances >=', 20);

test/datastore/request.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,25 @@ describe('Request', function() {
387387
});
388388
});
389389

390+
it('should only emit the limited number of results', function(done) {
391+
var limit = 2;
392+
393+
query.limitVal = limit;
394+
395+
request.makeReq_ = function(method, req, callback) {
396+
callback(null, mockResponse.withResultsAndEndCursor);
397+
};
398+
399+
var resultsReturned = 0;
400+
401+
request.runQuery(query)
402+
.on('data', function() { resultsReturned++; })
403+
.on('end', function() {
404+
assert.equal(resultsReturned, limit);
405+
done();
406+
});
407+
});
408+
390409
it('should emit an error', function(done) {
391410
var error = new Error('Error.');
392411

0 commit comments

Comments
 (0)