Skip to content

Commit

Permalink
datastore: support deferred results. fixes #77.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 27, 2014
1 parent 8921c73 commit 473f55d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ var SIGN_TO_ORDER = {
* @param {string=} options.namespace - Optional namespace.
*
* @example
* var key = new Key('Company', 123);
* var key = new Key({
* namespace: 'ns',
* path: ['Company', 123]
* });
*/
function Key(options) {
this.namespace_ = options.namespace;
Expand Down
24 changes: 20 additions & 4 deletions lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,27 @@ Transaction.prototype.get = function(keys, callback) {
}
this.makeReq('lookup', req, res, function(err, resp) {
if (err) {
return callback(err);
callback(err);
return;
}
var response = entity.formatArray(resp.found);
callback(null, isMultipleRequest ? response : response[0]);
});
var found = entity.formatArray(resp.found);
if (isMultipleRequest && resp.deferred && resp.deferred.length) {
// There may be more results. Call `.get` again, and append the results.
this.get(
resp.deferred.map(entity.keyFromKeyProto), function (err, entities) {
if (err) {
callback(err);
return;
}
if (resp) {
found = (found || []).concat(entities);
}
callback(null, found);
});
return;
}
callback(null, isMultipleRequest ? found : found[0]);
}.bind(this));
};

/**
Expand Down
24 changes: 24 additions & 0 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
var assert = require('assert');
var ByteBuffer = require('bytebuffer');
var datastore = require('../../lib').datastore;
var entity = require('../../lib/datastore/entity.js');
var mockRespGet = require('../testdata/response_get.json');
var Transaction = require('../../lib/datastore/transaction.js');

Expand Down Expand Up @@ -78,6 +79,29 @@ describe('Dataset', function() {
});
});

it('should continue looking for deferred results', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
var key = ds.key('Kind', 5732568548769792);
var key2 = ds.key('Kind', 5732568548769792);
var lookupCount = 0;
ds.transaction.makeReq = function(method, proto, typ, callback) {
lookupCount++;
assert.equal(method, 'lookup');
if (mockRespGet.deferred.length) {
// Revert deferred to original state.
mockRespGet.deferred = [];
} else {
mockRespGet.deferred = [ entity.keyToKeyProto(key2) ];
}
callback(null, mockRespGet);
};
ds.get([key, key2], function(err, entities) {
assert.equal(entities.length, 2);
assert.equal(lookupCount, 2);
done();
});
});

it('should delete by key', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
Expand Down

0 comments on commit 473f55d

Please sign in to comment.