-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathbatch-transaction.js
348 lines (321 loc) · 10.3 KB
/
batch-transaction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*!
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var common = require('@google-cloud/common');
var extend = require('extend');
var is = require('is');
var util = require('util');
var codec = require('./codec.js');
var Transaction = require('./transaction.js');
/**
* Use a BatchTransaction object to create partitions and read/query against
* your Cloud Spanner database.
*
* @class
* @extends Transaction
*
* @param {TransactionOptions} [options] [Transaction options](https://cloud.google.com/spanner/docs/timestamp-bounds).
*/
function BatchTransaction(session) {
Transaction.call(this, session, {readOnly: true});
}
util.inherits(BatchTransaction, Transaction);
/**
* Closes all open resources.
*
* When the transaction is no longer needed, you should call this method to free
* up resources allocated by the Batch client.
*
* Calling this method would render the transaction unusable everywhere. In
* particular if this transaction object was being used across multiple
* machines, calling this method on any of the machine would make the
* transaction unusable on all the machines. This should only be called when the
* transaction is no longer needed anywhere
*
* @param {BasicCallback} [callback] Callback function.
* @returns {Promise<BasicResponse>}
*
* @example
* const Spanner = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* const instance = spanner.instance('my-instance');
* const database = instance.database('my-database');
*
* database.createBatchTransaction(function(err, transaction) {
* if (err) {
* // Error handling omitted.
* }
*
* transaction.close(function(err, apiResponse) {});
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* database.createBatchTransaction().then(function(data) {
* var transaction = data[0];
* return transaction.close();
* });
*/
BatchTransaction.prototype.close = function(callback) {
this.session.delete(callback);
};
/**
* @see [`ExecuteSqlRequest`](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)
* @typedef {object} QueryPartition
* @property {string} partitionToken The partition token.
*/
/**
* @typedef {array} CreateQueryPartitionsResponse
* @property {QueryPartition[]} 0 List of query partitions.
* @property {object} 1 The full API response.
*/
/**
* @callback CreateQueryPartitionsCallback
* @param {?Error} err Request error, if any.
* @param {QueryPartition[]} partitions List of query partitions.
* @param {object} apiResponse The full API response.
*/
/**
* Creates a set of query partitions that can be used to execute a query
* operation in parallel. Partitions become invalid when the transaction used
* to create them is closed.
*
* @param {string|object} query A SQL query or
* [`ExecuteSqlRequest`](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)
* object.
* @param {object} [query.gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {object} [query.params] A map of parameter name to values.
* @param {object} [query.partitionOptions] A map of partition options.
* @param {object} [query.types] A map of parameter types.
* @param {CreateQueryPartitionsCallback} [callback] Callback callback function.
* @returns {Promise<CreateQueryPartitionsResponse>}
*
* @example <caption>include:samples/batch.js</caption>
* region_tag:spanner_batch_client
*/
BatchTransaction.prototype.createQueryPartitions = function(query, callback) {
if (is.string(query)) {
query = {
sql: query,
};
}
var reqOpts = codec.encodeQuery(query);
var gaxOpts = query.gaxOptions;
if (gaxOpts) {
delete reqOpts.gaxOptions;
}
this.createPartitions_(
{
client: 'SpannerClient',
method: 'partitionQuery',
reqOpts,
gaxOpts,
},
callback
);
};
/**
* Generic create partition method. Handles common parameters used in both
* {@link BatchTransaction#createQueryPartitions} and {@link BatchTransaction#createReadPartitions}
*
* @private
*
* @param {object} config The request config.
* @param {function} Callback function.
*/
BatchTransaction.prototype.createPartitions_ = function(config, callback) {
var self = this;
var query = extend({}, config.reqOpts, {
session: this.session.formattedName_,
transaction: {id: this.id},
});
config.reqOpts = extend({}, query);
delete query.partitionOptions;
this.request(config, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}
var partitions = resp.partitions.map(function(partition) {
return extend({}, query, partition);
});
if (resp.transaction) {
self.id = resp.transaction.id;
self.readTimestamp = resp.transaction.readTimestamp;
}
callback(null, partitions, resp);
});
};
/**
* @typedef {object} ReadPartition
* @mixes ReadRequestOptions
* @property {string} partitionToken The partition token.
* @property {object} [gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
*/
/**
* @typedef {array} CreateReadPartitionsResponse
* @property {ReadPartition[]} 0 List of read partitions.
* @property {object} 1 The full API response.
*/
/**
* @callback CreateReadPartitionsCallback
* @param {?Error} err Request error, if any.
* @param {ReadPartition[]} partitions List of read partitions.
* @param {object} apiResponse The full API response.
*/
/**
* Creates a set of read partitions that can be used to execute a read
* operation in parallel. Partitions become invalid when the transaction used
* to create them is closed.
*
* @param {ReadRequestOptions} options Configuration object, describing what to
* read from.
* @param {CreateReadPartitionsCallback} [callback] Callback function.
* @returns {Promise<CreateReadPartitionsResponse>}
*/
BatchTransaction.prototype.createReadPartitions = function(options, callback) {
var reqOpts = codec.encodeRead(options);
var gaxOpts = options.gaxOptions;
if (gaxOpts) {
delete reqOpts.gaxOptions;
}
this.createPartitions_(
{
client: 'SpannerClient',
method: 'partitionRead',
reqOpts,
gaxOpts,
},
callback
);
};
/**
* Executes partition.
*
* @see {@link Transaction#read} when using {@link ReadPartition}.
* @see {@link Transaction#run} when using {@link QueryParition}.
*
* @param {ReadPartition|QueryParition} partition The partition object.
* @param {object} [partition.gaxOptions] Request configuration options,
* outlined here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {TransactionRequestReadCallback|RunCallback} callback Callback
* function.
* @returns {Promise<RunResponse>|Promise<TransactionRequestReadResponse>}
*
* @example <caption>include:samples/batch.js</caption>
* region_tag:spanner_batch_execute_partitions
*/
BatchTransaction.prototype.execute = function(partition, callback) {
if (is.string(partition.table)) {
this.read(partition.table, partition, callback);
return;
}
this.run(partition, callback);
};
/**
* Executes partition in streaming mode.
*
* @see {@link Transaction#createReadStream} when using {@link ReadPartition}.
* @see {@link Transaction#runStream} when using {@link QueryPartition}.
*
* @param {ReadPartition|QueryPartition} partition The partition object.
* @returns {ReadableStream} A readable stream that emits rows.
*
* @example
* const Spanner = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* const instance = spanner.instance('my-instance');
* const database = instance.database('my-database');
*
* database.createBatchTransaction(function(err, transaction) {
* if (err) {
* // Error handling omitted.
* }
*
* transaction.createReadPartitions(options, function(err, partitions) {
* const partition = partitions[0];
*
* transaction
* .executeStream(partition)
* .on('error', function(err) {})
* .on('data', function(row) {
* // row = [
* // {
* // name: 'SingerId',
* // value: '1'
* // },
* // {
* // name: 'Name',
* // value: 'Eddie Wilson'
* // }
* // ]
* })
* .on('end', function() {
* // All results retrieved
* });
* });
* });
*/
BatchTransaction.prototype.executeStream = function(partition) {
if (is.string(partition.table)) {
return this.createReadStream(partition.table, partition);
}
return this.runStream(partition);
};
/**
* @typedef {object} TransactionIdentifier
* @property {string} session The full session name.
* @property {string} transaction The transaction ID.
* @property {string|Date} readTimestamp The transaction read timestamp.
*/
/**
* Creates a transaction identifier used to reference the transaction in
* workers.
*
* @returns {TransactionIdentifier}
*
* @example
* const Spanner = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* const instance = spanner.instance('my-instance');
* const database = instance.database('my-database');
*
* database.createBatchTransaction(function(err, transaction) {
* const identifier = transaction.identifier();
* });
*/
BatchTransaction.prototype.identifier = function() {
return {
transaction: this.id.toString('base64'),
session: this.session.id,
timestamp: this.readTimestamp,
};
};
/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(BatchTransaction, {
exclude: ['identifier'],
});
module.exports = BatchTransaction;