Skip to content

Commit 00db490

Browse files
datastore: tests: refactor pb/transaction.
1 parent daf247c commit 00db490

File tree

4 files changed

+238
-142
lines changed

4 files changed

+238
-142
lines changed

lib/datastore/request.js

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,10 @@ DatastoreRequest.prototype.get = function(keys, callback) {
106106
var isMultipleRequest = Array.isArray(keys);
107107
keys = isMultipleRequest ? keys : [keys];
108108
callback = callback || util.noop;
109-
var req = new pb.LookupRequest({
109+
var req = {
110110
key: keys.map(entity.keyToKeyProto)
111-
});
112-
var res = pb.LookupResponse;
113-
if (this.id) {
114-
req.transaction = this.id;
115-
}
116-
this.createRequest_('lookup', req, res, function(err, resp) {
111+
};
112+
this.createRequest_('lookup', req, function(err, resp) {
117113
if (err) {
118114
callback(err);
119115
return;
@@ -217,7 +213,6 @@ DatastoreRequest.prototype.save = function(entities, callback) {
217213
return entityObject.key;
218214
});
219215
var req = {
220-
mode: MODE_NON_TRANSACTIONAL,
221216
mutation: entities.reduce(function(acc, entityObject, index) {
222217
var ent = {};
223218
if (Array.isArray(entityObject.data)) {
@@ -242,13 +237,7 @@ DatastoreRequest.prototype.save = function(entities, callback) {
242237
return acc;
243238
}.bind(this), { upsert: [], insert_auto_id: [] })
244239
};
245-
if (this.id) {
246-
req.transaction = this.id;
247-
req.mode = MODE_TRANSACTIONAL;
248-
}
249-
req = new pb.CommitRequest(req);
250-
var res = pb.CommitResponse;
251-
this.createRequest_('commit', req, res, function(err, resp) {
240+
this.createRequest_('commit', req, function(err, resp) {
252241
if (err || !resp) {
253242
callback(err);
254243
return;
@@ -290,20 +279,12 @@ DatastoreRequest.prototype.delete = function(keys, callback) {
290279
var isMultipleRequest = Array.isArray(keys);
291280
keys = isMultipleRequest ? keys : [keys];
292281
callback = callback || util.noop;
293-
294282
var req = {
295-
mode: MODE_NON_TRANSACTIONAL,
296283
mutation: {
297284
delete: keys.map(entity.keyToKeyProto)
298285
}
299286
};
300-
if (this.id) {
301-
req.transaction = this.id;
302-
req.mode = MODE_TRANSACTIONAL;
303-
}
304-
req = new pb.CommitRequest(req);
305-
var res = pb.CommitResponse;
306-
this.createRequest_('commit', req, res, function(err) {
287+
this.createRequest_('commit', req, function(err) {
307288
if (!err && this.id) {
308289
this.isFinalized = true;
309290
}
@@ -334,28 +315,20 @@ DatastoreRequest.prototype.delete = function(keys, callback) {
334315
* }
335316
* });
336317
*/
337-
338318
DatastoreRequest.prototype.runQuery = function(q, callback) {
339319
callback = callback || util.noop;
340320
var req = {
341321
read_options: {},
342322
query: entity.queryToQueryProto(q)
343323
};
344324

345-
if (this.id) {
346-
req.read_options.transaction = this.id;
347-
}
348-
349325
if (q.namespace) {
350326
req.partition_id = {
351327
namespace: q.namespace
352328
};
353329
}
354330

355-
req = new pb.RunQueryRequest(req);
356-
var res = pb.RunQueryResponse;
357-
358-
this.createRequest_('runQuery', req, res, function(err, resp) {
331+
this.createRequest_('runQuery', req, function(err, resp) {
359332
if (err || !resp.batch || !resp.batch.entity_result) {
360333
callback(err);
361334
return;
@@ -406,11 +379,10 @@ DatastoreRequest.prototype.allocateIds = function(incompleteKey, n, callback) {
406379
for (var i = 0; i < n; i++) {
407380
incompleteKeys.push(entity.keyToKeyProto(incompleteKey));
408381
}
409-
410-
this.createRequest_(
411-
'allocateIds',
412-
new pb.AllocateIdsRequest({ key: incompleteKeys }),
413-
pb.AllocateIdsResponse, function(err, resp) {
382+
var req = {
383+
key: incompleteKeys
384+
};
385+
this.createRequest_('allocateIds', req, function(err, resp) {
414386
if (err) {
415387
callback(err);
416388
return;
@@ -424,28 +396,50 @@ DatastoreRequest.prototype.allocateIds = function(incompleteKey, n, callback) {
424396
};
425397

426398
/**
427-
* Make a request to the API endpoint.
399+
* Make a request to the API endpoint. Properties to indicate a transactional or
400+
* non-transactional operation are added automatically.
428401
*
429-
* @param {string} method - Transaction action (allocateIds, commit, etc.).
430-
* @param {object} req - Request configuration object.
431-
* @param {object} respType - Response type.
432-
* @param {function} cb - The callback function.
402+
* @param {string} method - Datastore action (allocateIds, commit, etc.).
403+
* @param {object=} req - Request configuration object.
404+
* @param {function} callback - The callback function.
433405
*
434406
* @private
435407
*
436408
* @example
437409
* var deleteRequest = {
438-
* MODE: 'NON_TRANSACTIONAL',
439410
* mutation: {
440411
* delete: [] // datastore key objects.
441412
* }
442413
* };
443414
* transaction.makeReq('commit', deleteRequest, function(err) {});
444415
*/
445-
DatastoreRequest.prototype.createRequest_ =
446-
function(method, req, respType, cb) {
416+
DatastoreRequest.prototype.createRequest_ = function(method, req, callback) {
447417
// TODO: Handle non-HTTP 200 cases.
448-
cb = cb || util.noop;
418+
if (!callback) {
419+
callback = req;
420+
req = {};
421+
}
422+
callback = callback || util.noop;
423+
424+
// Set properties to indicate if we're in a transaction or not.
425+
if (method === 'commit') {
426+
if (this.id) {
427+
req.mode = MODE_TRANSACTIONAL;
428+
req.transaction = this.id;
429+
} else {
430+
req.mode = MODE_NON_TRANSACTIONAL;
431+
}
432+
}
433+
434+
if (method === 'lookup' && this.id) {
435+
req.read_options = req.read_options || {};
436+
req.read_options.transaction = this.id;
437+
}
438+
439+
var pbKey = method[0].toUpperCase() + method.substr(1);
440+
var pbRequest = new pb[pbKey + 'Request'](req).toBuffer();
441+
var pbResponse = pb[pbKey + 'Response'];
442+
449443
this.connection.createAuthorizedReq({
450444
method: 'POST',
451445
host: GOOGLE_APIS_HOST,
@@ -455,7 +449,7 @@ DatastoreRequest.prototype.createRequest_ =
455449
}
456450
}, function(err, request) {
457451
if (err) {
458-
cb(err);
452+
callback(err);
459453
return;
460454
}
461455
var remoteStream = https.request(request, function(resp) {
@@ -466,15 +460,15 @@ DatastoreRequest.prototype.createRequest_ =
466460
resp.on('end', function() {
467461
util.handleResp(null, resp, buffer.toString(), function(err) {
468462
if (err) {
469-
cb(err);
463+
callback(err);
470464
return;
471465
}
472-
cb(null, respType.decode(buffer));
466+
callback(null, pbResponse.decode(buffer));
473467
});
474468
});
475469
});
476-
remoteStream.on('error', cb);
477-
remoteStream.write(req.toBuffer());
470+
remoteStream.on('error', callback);
471+
remoteStream.write(pbRequest);
478472
remoteStream.end();
479473
});
480474
};

lib/datastore/transaction.js

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222

2323
var nodeutil = require('util');
2424

25-
/**
26-
* @type module:datastore/pb
27-
* @private
28-
*/
29-
var pb = require('./pb.js');
30-
3125
/**
3226
* @type module:common/util
3327
* @private
@@ -66,11 +60,9 @@ var DatastoreRequest = require('./request.js');
6660
*/
6761
function Transaction(connection, projectId) {
6862
this.connection = connection;
69-
this.projectId = projectId;
70-
// the default transaction has no id.
71-
// if id is not set, run operations non-transactional.
7263
this.id = null;
7364
this.isFinalized = false;
65+
this.projectId = projectId;
7466
}
7567

7668
nodeutil.inherits(Transaction, DatastoreRequest);
@@ -96,17 +88,14 @@ nodeutil.inherits(Transaction, DatastoreRequest);
9688
*/
9789
Transaction.prototype.begin = function(callback) {
9890
callback = callback || util.noop;
99-
var that = this;
100-
var req = new pb.BeginTransactionRequest();
101-
var res = pb.BeginTransactionResponse;
102-
this.createRequest_('beginTransaction', req, res, function(err, resp) {
91+
this.createRequest_('beginTransaction', function(err, resp) {
10392
if (err) {
10493
callback(err);
10594
return;
10695
}
107-
that.id = resp.transaction;
96+
this.id = resp.transaction;
10897
callback(null);
109-
});
98+
}.bind(this));
11099
};
111100

112101
/**
@@ -125,13 +114,10 @@ Transaction.prototype.begin = function(callback) {
125114
*/
126115
Transaction.prototype.rollback = function(callback) {
127116
callback = callback || util.noop;
128-
var that = this;
129-
var req = new pb.RollbackRequest({ transaction: this.id });
130-
var res = pb.RollbackResponse;
131-
this.createRequest_('rollback', req, res, function(err) {
132-
that.isFinalized = true;
117+
this.createRequest_('rollback', function(err) {
118+
this.isFinalized = true;
133119
callback(err || null);
134-
});
120+
}.bind(this));
135121
};
136122

137123
/**
@@ -150,17 +136,14 @@ Transaction.prototype.rollback = function(callback) {
150136
*/
151137
Transaction.prototype.commit = function(callback) {
152138
callback = callback || util.noop;
153-
var that = this;
154-
var req = new pb.CommitRequest({ transaction: this.id });
155-
var res = pb.CommitResponse;
156-
this.createRequest_('commit', req, res, function(err) {
139+
this.createRequest_('commit', function(err) {
157140
if (err) {
158141
callback(err);
159142
return;
160143
}
161-
that.isFinalized = true;
144+
this.isFinalized = true;
162145
callback(null);
163-
});
146+
}.bind(this));
164147
};
165148

166149
/**

0 commit comments

Comments
 (0)