Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: support deferred results. fixes #77. #133

Merged
merged 1 commit into from
Aug 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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