Skip to content

Commit

Permalink
datastore: support deferred results. fixes googleapis#77.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 23, 2014
1 parent 00553cc commit 53db768
Show file tree
Hide file tree
Showing 3 changed files with 59 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 @@ -95,7 +95,10 @@ var SIGN_TO_ORDER = {
*
* @example
* ```js
* var key = new Key('Company', 123);
* var key = new Key({
* namespace: 'ns',
* path: ['Company', 123]
* });
* ```
*/
function Key(options) {
Expand Down
24 changes: 20 additions & 4 deletions lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,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
35 changes: 35 additions & 0 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ describe('Dataset', function() {
});
});

it('should continue looking for deferred results', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, typ, callback) {
assert.equal(method, 'lookup');
if (mockRespGet.deferred.length) {
// Revert deferred to original state.
mockRespGet.deferred = [];
} else {
mockRespGet.deferred = [
{
partition_id: {
dataset_id: 's~bamboo-shift-504',
namespace: null
},
path_element: [
{
kind: 'Kind',
id: 5732568548769792,
name: null
}
]
}
];
}
callback(null, mockRespGet);
};
var key = ds.key('Kind', 5732568548769792);
ds.get([key], function(err, entities) {
assert.equal(entities.length, 2);
assert.equal(entities[0].key.path_[1], 5732568548769792);
assert.equal(entities[1].key.path_[1], 5732568548769792);
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 53db768

Please sign in to comment.