Skip to content

Commit

Permalink
fix: reduce overhead for listDocuments()/listCollections() (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Dec 27, 2019
1 parent 3cd93c8 commit 5c870e6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dev/src/reference.ts
Expand Up @@ -286,7 +286,13 @@ export class DocumentReference implements Serializable {
listCollections(): Promise<CollectionReference[]> {
const tag = requestTag();
return this.firestore.initializeIfNeeded(tag).then(() => {
const request = {parent: this.formattedName};
const request: api.IListCollectionIdsRequest = {
parent: this.formattedName,
// Setting `pageSize` to an arbitrarily large value lets the backend cap
// the page size (currently to 300). Note that the backend rejects
// MAX_INT32 (b/146883794).
pageSize: Math.pow(2, 16) - 1,
};
return this._firestore
.request<string[]>(
'listCollectionIds',
Expand Down Expand Up @@ -2051,6 +2057,9 @@ export class CollectionReference extends Query {
parent: parentPath.formattedName,
collectionId: this.id,
showMissing: true,
// Setting `pageSize` to the maximum allowed value lets the backend cap
// the page size (currently to 300).
pageSize: Math.pow(2, 32) - 1,
mask: {fieldPaths: []},
};

Expand Down
1 change: 1 addition & 0 deletions dev/test/collection.ts
Expand Up @@ -142,6 +142,7 @@ describe('Collection interface', () => {
parent: `${DATABASE_ROOT}/documents/a/b`,
collectionId: 'c',
showMissing: true,
pageSize: 4294967295,
mask: {fieldPaths: []},
});

Expand Down
1 change: 1 addition & 0 deletions dev/test/document.ts
Expand Up @@ -2003,6 +2003,7 @@ describe('listCollections() method', () => {
listCollectionIds: (request, options, callback) => {
expect(request).to.deep.eq({
parent: `projects/${PROJECT_ID}/databases/(default)/documents/coll/doc`,
pageSize: 65535,
});

callback(null, ['second', 'first']);
Expand Down
1 change: 1 addition & 0 deletions dev/test/index.ts
Expand Up @@ -871,6 +871,7 @@ describe('listCollections() method', () => {
listCollectionIds: (request, options, callback) => {
expect(request).to.deep.eq({
parent: `projects/${PROJECT_ID}/databases/(default)/documents`,
pageSize: 65535,
});

callback(null, ['first', 'second']);
Expand Down

0 comments on commit 5c870e6

Please sign in to comment.