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

fix: return results from getPartitions() in order #1521

Merged
merged 3 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 18 additions & 14 deletions dev/src/collection-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {validateInteger} from './validate';

import api = protos.google.firestore.v1;
import {defaultConverter} from './types';
import {compareArrays} from './order';

/**
* A `CollectionGroup` refers to all documents that are contained in a
Expand Down Expand Up @@ -79,8 +80,7 @@ export class CollectionGroup<T = firestore.DocumentData>
const tag = requestTag();
await this.firestore.initializeIfNeeded(tag);

let lastValues: api.IValue[] | undefined = undefined;
let partitionCount = 0;
const partitions: Array<api.IValue>[] = [];

if (desiredPartitionCount > 1) {
// Partition queries require explicit ordering by __name__.
Expand All @@ -100,32 +100,36 @@ export class CollectionGroup<T = firestore.DocumentData>
stream.resume();

for await (const currentCursor of stream) {
++partitionCount;
const currentValues = currentCursor.values ?? [];
yield new QueryPartition(
this._firestore,
this._queryOptions.collectionId,
this._queryOptions.converter,
lastValues,
currentValues
);
lastValues = currentValues;
partitions.push(currentCursor.values ?? []);
}
}

logger(
'Firestore.getPartitions',
tag,
'Received %d partitions',
partitionCount
partitions.length
);

// Sort the partitions as they may not be ordered if responses are paged.
partitions.sort((l, r) => compareArrays(l, r));

for (let i = 0; i < partitions.length; ++i) {
yield new QueryPartition(
this._firestore,
this._queryOptions.collectionId,
this._queryOptions.converter,
i > 0 ? partitions[i - 1] : undefined,
partitions[i]
);
}

// Return the extra partition with the empty cursor.
yield new QueryPartition(
this._firestore,
this._queryOptions.collectionId,
this._queryOptions.converter,
lastValues,
partitions.pop(),
undefined
);
}
Expand Down
2 changes: 1 addition & 1 deletion dev/src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function compareGeoPoints(
/*!
* @private
*/
function compareArrays(left: api.IValue[], right: api.IValue[]): number {
export function compareArrays(left: api.IValue[], right: api.IValue[]): number {
for (let i = 0; i < left.length && i < right.length; i++) {
const valueComparison = compare(left[i], right[i]);
if (valueComparison !== 0) {
Expand Down
58 changes: 56 additions & 2 deletions dev/test/partition-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
QueryPartition,
} from '@google-cloud/firestore';

import {describe, it, beforeEach, afterEach} from 'mocha';
import {afterEach, beforeEach, describe, it} from 'mocha';
import {expect, use} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as extend from 'extend';

import {google} from '../protos/firestore_v1_proto_api';
import {Firestore} from '../src';
import {DocumentReference, Firestore} from '../src';
import {setTimeoutHandler} from '../src/backoff';
import {
ApiOverride,
Expand Down Expand Up @@ -102,6 +102,28 @@ describe('Partition Query', () => {
return partitions;
}

function verifyPartition(
partition: FirebaseFirestore.QueryPartition,
startAt: string | null,
endBefore: string | null
) {
if (startAt) {
expect(
partition.startAt?.map(value => (value as DocumentReference).path)
).to.have.members([startAt]);
} else {
expect(partition.startAt).to.be.undefined;
}

if (endBefore) {
expect(
partition.endBefore?.map(value => (value as DocumentReference).path)
).to.have.members([endBefore]);
} else {
expect(partition.endBefore).to.be.undefined;
}
}

it('requests one less than desired partitions', () => {
const desiredPartitionsCount = 2;
const cursorValue = {
Expand Down Expand Up @@ -255,4 +277,36 @@ describe('Partition Query', () => {
return result[0].toQuery().get();
});
});

it('sorts partitions', () => {
const desiredPartitionsCount = 3;

const overrides: ApiOverride = {
partitionQueryStream: () => {
return stream<api.ICursor>(
{
values: [
{referenceValue: 'projects/p1/databases/d1/documents/coll/doc2'},
],
},
{
values: [
{referenceValue: 'projects/p1/databases/d1/documents/coll/doc1'},
],
}
);
},
};

return createInstance(overrides).then(async firestore => {
const query = firestore.collectionGroup('collectionId');

const partitions = await getPartitions(query, desiredPartitionsCount);
expect(partitions.length).to.equal(3);

verifyPartition(partitions[0], null, 'coll/doc1');
verifyPartition(partitions[1], 'coll/doc1', 'coll/doc2');
verifyPartition(partitions[2], 'coll/doc2', null);
});
});
});