Skip to content

Commit 3c379df

Browse files
streamrouter: default autoPaginate: true
1 parent 88fd2fa commit 3c379df

File tree

20 files changed

+439
-428
lines changed

20 files changed

+439
-428
lines changed

lib/bigquery/index.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -163,31 +163,33 @@ BigQuery.prototype.dataset = function(id) {
163163
* @param {object=} query - Configuration object.
164164
* @param {boolean} query.all - List all datasets, including hidden ones.
165165
* @param {boolean} query.autoPaginate - Have pagination handled automatically.
166-
* Default: false.
166+
* Default: true.
167167
* @param {number} query.maxResults - Maximum number of results to return.
168168
* @param {string} query.pageToken - Token returned from a previous call, to
169169
* request the next page of results.
170170
* @param {function} callback - The callback function.
171171
*
172172
* @example
173+
* bigquery.getDatasets(function(err, datasets) {
174+
* if (!err) {
175+
* // datasets is an array of Dataset objects.
176+
* }
177+
* });
178+
*
179+
* //-
180+
* // To control how many API requests are made and page through the results
181+
* // manually, set `autoPaginate` to `false`.
182+
* //-
173183
* var callback = function(err, datasets, nextQuery, apiResponse) {
174184
* if (nextQuery) {
175185
* // More results exist.
176186
* bigquery.getDatasets(nextQuery, callback);
177187
* }
178188
* };
179189
*
180-
* bigquery.getDatasets(callback);
181-
*
182-
* //-
183-
* // To have pagination handled for you, set `autoPaginate`. Note the changed
184-
* // callback parameters.
185-
* //-
186190
* bigquery.getDatasets({
187-
* autoPaginate: true
188-
* }, function(err, datasets) {
189-
* // Called after all datasets have been retrieved.
190-
* });
191+
* autoPaginate: false
192+
* }, callback);
191193
*
192194
* //-
193195
* // Get the datasets from your project as a readable object stream.
@@ -251,7 +253,7 @@ BigQuery.prototype.getDatasets = function(query, callback) {
251253
* @param {boolean=} options.allUsers - Display jobs owned by all users in the
252254
* project.
253255
* @param {boolean} options.autoPaginate - Have pagination handled
254-
* automatically. Default: false.
256+
* automatically. Default: true.
255257
* @param {number=} options.maxResults - Maximum number of results to return.
256258
* @param {string=} options.pageToken - Token returned from a previous call, to
257259
* request the next page of results.
@@ -263,19 +265,27 @@ BigQuery.prototype.getDatasets = function(query, callback) {
263265
* @param {function} callback - The callback function.
264266
*
265267
* @example
266-
* bigquery.getJobs(function(err, jobs, nextQuery, apiResponse) {
267-
* // If `nextQuery` is non-null, there are more results to fetch.
268+
* bigquery.getJobs(function(err, jobs) {
269+
* if (!err) {
270+
* // jobs is an array of Job objects.
271+
* }
268272
* });
269273
*
270274
* //-
271-
* // To have pagination handled for you, set `autoPaginate`. Note the changed
272-
* // callback parameters.
275+
* // To control how many API requests are made and page through the results
276+
* // manually, set `autoPaginate` to `false`.
273277
* //-
278+
* var callback = function(err, jobs, nextQuery, apiRespose) {
279+
* if (nextQuery) {
280+
* // More results exist.
281+
* bigquery.getJobs(nextQuery, callback);
282+
* }
283+
* };
284+
*
274285
* bigquery.getJobs({
275-
* autoPaginate: true
276-
* }, function(err, jobs) {
277-
* // Called after all jobs have been retrieved.
278-
* });
286+
* autoPaginate: false
287+
* }, callback);
288+
*
279289
* //-
280290
* // Get the jobs from your project as a readable object stream.
281291
* //-
@@ -360,7 +370,7 @@ BigQuery.prototype.job = function(id) {
360370
*
361371
* @param {string|object} options - A string SQL query or configuration object.
362372
* @param {boolean} options.autoPaginate - Have pagination handled
363-
* automatically. Default: false.
373+
* automatically. Default: true.
364374
* @param {number} options.maxResults - Maximum number of results to read.
365375
* @param {string} options.query - A query string, following the BigQuery query
366376
* syntax, of the query to execute.
@@ -373,29 +383,26 @@ BigQuery.prototype.job = function(id) {
373383
* @example
374384
* var query = 'SELECT url FROM [publicdata:samples.github_nested] LIMIT 100';
375385
*
386+
* bigquery.query(query, function(err, rows) {
387+
* if (!err) {
388+
* // Handle results here.
389+
* }
390+
* });
391+
*
376392
* //-
377-
* // You can run a query against your data in a serial manner.
393+
* // To control how many API requests are made and page through the results
394+
* // manually, set `autoPaginate` to `false`.
378395
* //-
379396
* var callback = function(err, rows, nextQuery, apiResponse) {
380-
* // Handle results here.
381-
*
382397
* if (nextQuery) {
383398
* bigquery.query(nextQuery, callback);
384399
* }
385400
* };
386401
*
387-
* bigquery.query(query, callback);
388-
*
389-
* //-
390-
* // To have pagination handled for you, set `autoPaginate`. Note the changed
391-
* // callback parameters.
392-
* //-
393402
* bigquery.query({
394403
* query: query,
395-
* autoPaginate: true
396-
* }, function(err, rows) {
397-
* // Called after all rows have been retrieved.
398-
* });
404+
* autoPaginate: false
405+
* }, callback);
399406
*
400407
* //-
401408
* // You can also use the `query` method as a readable object stream by

lib/bigquery/job.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Job.prototype.getMetadata = function(callback) {
9292
*
9393
* @param {object=} options - Configuration object.
9494
* @param {boolean} options.autoPaginate - Have pagination handled
95-
* automatically. Default: false.
95+
* automatically. Default: true.
9696
* @param {number} options.maxResults - Maximum number of results to read.
9797
* @param {string} options.pageToken - Page token, returned by a previous call,
9898
* to request the next page of results. Note: This is automatically added to

lib/bigquery/table.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -484,33 +484,31 @@ Table.prototype.getMetadata = function(callback) {
484484
*
485485
* @param {object=} options - The configuration object.
486486
* @param {boolean} options.autoPaginate - Have pagination handled
487-
* automatically. Default: false.
487+
* automatically. Default: true.
488488
* @param {number} options.maxResults - Maximum number of results to return.
489489
* @param {function} callback - The callback function.
490490
*
491491
* @example
492-
* var options = {
493-
* maxResults: 100
494-
* };
492+
* table.getRows(function(err, rows) {
493+
* if (!err) {
494+
* // Handle results here.
495+
* }
496+
* });
495497
*
498+
* //-
499+
* // To control how many API requests are made and page through the results
500+
* // manually, set `autoPaginate` to `false`.
501+
* //-
496502
* var callback = function(err, rows, nextQuery, apiResponse) {
497503
* if (nextQuery) {
498504
* // More results exist.
499505
* table.getRows(nextQuery, callback);
500506
* }
501507
* };
502508
*
503-
* table.getRows(options, callback);
504-
*
505-
* //-
506-
* // To have pagination handled for you, set `autoPaginate`. Note the changed
507-
* // callback parameters.
508-
* //-
509509
* table.getRows({
510-
* autoPaginate: true
511-
* }, function(err, rows) {
512-
* // Called after all rows have been retrieved.
513-
* });
510+
* autoPaginate: false
511+
* }, callback);
514512
*
515513
* //-
516514
* // Get the rows as a readable object stream.

lib/common/stream-router.js

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,51 @@ streamRouter.extend = function(Class, methodNames) {
7474
* method received.
7575
*/
7676
streamRouter.parseArguments_ = function(args) {
77-
var parsedArguments = {};
77+
var query;
78+
var callback;
79+
var maxResults = -1;
80+
var autoPaginate = true;
7881

7982
var firstArgument = args[0];
8083
var lastArgument = args[args.length - 1];
8184

8285
if (util.is(firstArgument, 'function')) {
83-
parsedArguments.callback = firstArgument;
86+
callback = firstArgument;
8487
} else {
85-
parsedArguments.query = firstArgument;
88+
query = firstArgument;
8689
}
8790

8891
if (util.is(lastArgument, 'function')) {
89-
parsedArguments.callback = lastArgument;
92+
callback = lastArgument;
9093
}
9194

92-
return parsedArguments;
95+
if (util.is(query, 'object')) {
96+
// Check if the user only asked for a certain amount of results.
97+
if (util.is(query.maxResults, 'number')) {
98+
// `maxResults` is used API-wide.
99+
maxResults = query.maxResults;
100+
} else if (util.is(query.limitVal, 'number')) {
101+
// `limitVal` is part of a Datastore query.
102+
maxResults = query.limitVal;
103+
} else if (util.is(query.pageSize, 'number')) {
104+
// `pageSize` is Pub/Sub's `maxResults`.
105+
maxResults = query.pageSize;
106+
}
107+
108+
if (callback &&
109+
(maxResults !== -1 || // The user specified a limit.
110+
query.autoPaginate === false ||
111+
query.autoPaginateVal === false)) {
112+
autoPaginate = false;
113+
}
114+
}
115+
116+
return {
117+
query: query || {},
118+
callback: callback,
119+
maxResults: maxResults,
120+
autoPaginate: autoPaginate
121+
};
93122
};
94123

95124
/**
@@ -103,20 +132,20 @@ streamRouter.parseArguments_ = function(args) {
103132
* commonly an object, but to make the API more simple, it can also be a
104133
* string in some places.
105134
* @param {function=} parsedArguments.callback - Callback function.
135+
* @param {boolean} parsedArguments.autoPaginate - Auto-pagination enabled.
136+
* @param {number} parsedArguments.maxResults - Maximum results to return.
106137
* @param {function} originalMethod - The cached method that accepts a callback
107138
* and returns `nextQuery` to receive more results.
108139
* @return {undefined|stream}
109140
*/
110141
streamRouter.router_ = function(parsedArguments, originalMethod) {
111-
var query = parsedArguments.query || {};
142+
var query = parsedArguments.query;
112143
var callback = parsedArguments.callback;
144+
var autoPaginate = parsedArguments.autoPaginate;
113145

114146
if (callback) {
115-
if (query.autoPaginate === true || query.autoPaginateVal === true) {
116-
delete query.autoPaginate;
117-
delete query.autoPaginateVal;
118-
119-
this.runAsStream_(query, originalMethod)
147+
if (autoPaginate) {
148+
this.runAsStream_(parsedArguments, originalMethod)
120149
.on('error', callback)
121150
.pipe(concat(function(results) {
122151
callback(null, results);
@@ -125,7 +154,7 @@ streamRouter.router_ = function(parsedArguments, originalMethod) {
125154
originalMethod(query, callback);
126155
}
127156
} else {
128-
return this.runAsStream_(query, originalMethod);
157+
return this.runAsStream_(parsedArguments, originalMethod);
129158
}
130159
};
131160

@@ -136,26 +165,19 @@ streamRouter.router_ = function(parsedArguments, originalMethod) {
136165
* `maxResults` and `limitVal` (from Datastore) will act as a cap for how many
137166
* results are fetched and emitted to the stream.
138167
*
139-
* @param {object=|string=} query - Query object. This is most
168+
* @param {object=|string=} parsedArguments.query - Query object. This is most
140169
* commonly an object, but to make the API more simple, it can also be a
141170
* string in some places.
171+
* @param {function=} parsedArguments.callback - Callback function.
172+
* @param {boolean} parsedArguments.autoPaginate - Auto-pagination enabled.
173+
* @param {number} parsedArguments.maxResults - Maximum results to return.
142174
* @param {function} originalMethod - The cached method that accepts a callback
143175
* and returns `nextQuery` to receive more results.
144176
* @return {stream} - Readable object stream.
145177
*/
146-
streamRouter.runAsStream_ = function(query, originalMethod) {
147-
query = query || {};
148-
149-
var resultsToSend = -1;
150-
151-
// Check if the user only asked for a certain amount of results.
152-
if (util.is(query.maxResults, 'number')) {
153-
// `maxResults` is used API-wide.
154-
resultsToSend = query.maxResults;
155-
} else if (util.is(query.limitVal, 'number')) {
156-
// `limitVal` is part of a Datastore query.
157-
resultsToSend = query.limitVal;
158-
}
178+
streamRouter.runAsStream_ = function(parsedArguments, originalMethod) {
179+
var query = parsedArguments.query;
180+
var resultsToSend = parsedArguments.maxResults;
159181

160182
var stream = streamEvents(through.obj());
161183

@@ -201,7 +223,7 @@ streamRouter.runAsStream_ = function(query, originalMethod) {
201223
}
202224

203225
stream.once('reading', function() {
204-
originalMethod.call(null, query, onResultSet);
226+
originalMethod(query, onResultSet);
205227
});
206228

207229
return stream;

lib/common/util.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,13 +674,12 @@ function makeRequest(reqOpts, config, callback) {
674674

675675
config = config || {};
676676

677+
reqOpts = util.decorateRequest(reqOpts);
678+
677679
var MAX_RETRIES = config.maxRetries || 3;
678680
var autoRetry = config.autoRetry !== false ? true : false;
679681
var attemptedRetries = 0;
680682

681-
reqOpts.headers = reqOpts.headers || {};
682-
reqOpts.headers['User-Agent'] = USER_AGENT;
683-
684683
function shouldRetry(err) {
685684
return autoRetry &&
686685
MAX_RETRIES > attemptedRetries &&
@@ -704,3 +703,28 @@ function makeRequest(reqOpts, config, callback) {
704703
}
705704

706705
util.makeRequest = makeRequest;
706+
707+
/**
708+
* Decorate the options about to be made in a request.
709+
*
710+
* @param {object} reqOpts - The options to be passed to `request`.
711+
* @return {object} reqOpts - The decorated reqOpts.
712+
*/
713+
function decorateRequest(reqOpts) {
714+
reqOpts.headers = reqOpts.headers || {};
715+
reqOpts.headers['User-Agent'] = USER_AGENT;
716+
717+
if (util.is(reqOpts.qs, 'object')) {
718+
delete reqOpts.qs.autoPaginate;
719+
delete reqOpts.qs.autoPaginateVal;
720+
}
721+
722+
if (util.is(reqOpts.json, 'object')) {
723+
delete reqOpts.json.autoPaginate;
724+
delete reqOpts.json.autoPaginateVal;
725+
}
726+
727+
return reqOpts;
728+
}
729+
730+
util.decorateRequest = decorateRequest;

lib/datastore/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function Query(namespace, kinds) {
7373
this.selectVal = [];
7474

7575
// pagination
76-
this.autoPaginateVal = false;
76+
this.autoPaginateVal = true;
7777
this.startVal = null;
7878
this.endVal = null;
7979
this.limitVal = -1;

0 commit comments

Comments
 (0)