Skip to content

Commit 5849f57

Browse files
Merging Master into Multi-Tab (#1078)
* Catch invalid provider id error (#1064) * RxFire: Api Change and documentation (#1066) * api changes and doc updates * fixes * Refactor PersistentStream (no behavior changes). (#1041) This breaks out a number of changes I made as prep for b/80402781 (Continue retrying streams for 1 minute (idle delay)). PersistentStream changes: * Rather than providing a stream event listener to every call of start(), the stream listener is now provided once to the constructor and cannot be changed. * Streams can now be restarted indefinitely, even after a call to stop(). * PersistentStreamState.Stopped was removed and we just return to 'Initial' after a stop() call. * Added `closeCount` member to PersistentStream in order to avoid bleedthrough issues with auth and stream events once stop() has been called. * Calling stop() now triggers the onClose() event listener, which simplifies stream cleanup. * PersistentStreamState.Auth renamed to 'Starting' to better reflect that it encompasses both authentication and opening the stream. RemoteStore changes: * Creates streams once and just stop() / start()s them as necessary, never recreating them completely. * Added networkEnabled flag to track whether the network is enabled or not, since we no longer null out the streams. * Refactored disableNetwork() / enableNetwork() to remove stream re-creation. Misc: * Comment improvements including a state diagram on PersistentStream. * Fixed spec test shutdown to schedule via the AsyncQueue to fix sequencing order I ran into. * Merging Persistent Stream refactor (#1069) * Merging PersistentStream refactor * [AUTOMATED]: Prettier Code Styling * Typo * Remove canUseNetwork state. (#1076) * Merging the latest merge into the previous merge (#1077) * Implement global resume token (#1052) * Add a spec test that shows correct global resume token handling * Minimum implementation to handle global resume tokens * Remove unused QueryView.resumeToken * Avoid persisting the resume token unless required * Persist the resume token on unlisten * Add a type parameter to Persistence (#1047) * Cherry pick sequence number starting point * Working on typed transactions * Start plumbing in sequence number * Back out sequence number changes * [AUTOMATED]: Prettier Code Styling * Fix tests * [AUTOMATED]: Prettier Code Styling * Fix lint * [AUTOMATED]: Prettier Code Styling * Uncomment line * MemoryPersistenceTransaction -> MemoryTransaction * [AUTOMATED]: Prettier Code Styling * Review updates * Style * Lint and style * Review feedback * [AUTOMATED]: Prettier Code Styling * Revert some unintentional import churn * Line 44 should definitely be empty * Checkpoint before adding helper function for stores * Use a helper for casting PersistenceTransaction to IndexedDbTransaction * [AUTOMATED]: Prettier Code Styling * Remove errant generic type * Lint * Fix typo * Port optimizations to LocalDocumentsView from iOS (#1055) * add a method to find batches affecting a set of keys (port of [1479](firebase/firebase-ios-sdk#1479)); * use the newly-added method to avoid rereading batches when getting documents in `LocalDocumentsView` (port of [1505](firebase/firebase-ios-sdk#1505)); * avoid rereading batches when searching for documents in a collection (port of [1533](firebase/firebase-ios-sdk#1533)). Speedup was measured by running tests in browser and checking time spent writing 10 batches of 500 mutations each, and then querying the resulting 5K docs collection from cache in offline mode. For this case, the writing speedup is about 3x, and querying speedup is about 6x (see PR for more details). * Add a CHANGELOG entry for #1052 (#1071) * Add a CHANGELOG entry for #1052 * Add notes for #1055 * Rename idleTimer and fix comments. (#1068) * Merge (#1073)
1 parent 40dea6a commit 5849f57

34 files changed

+1193
-687
lines changed

packages/auth/src/error_auth.js

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ fireauth.authenum.Error = {
147147
INVALID_PASSWORD: 'wrong-password',
148148
INVALID_PERSISTENCE: 'invalid-persistence-type',
149149
INVALID_PHONE_NUMBER: 'invalid-phone-number',
150+
INVALID_PROVIDER_ID: 'invalid-provider-id',
150151
INVALID_RECIPIENT_EMAIL: 'invalid-recipient-email',
151152
INVALID_SENDER: 'invalid-sender',
152153
INVALID_SESSION_INFO: 'invalid-verification-id',
@@ -294,6 +295,8 @@ fireauth.AuthError.MESSAGES_[fireauth.authenum.Error.INVALID_PHONE_NUMBER] =
294295
'phone number in a format that can be parsed into E.164 format. E.164 ' +
295296
'phone numbers are written in the format [+][country code][subscriber ' +
296297
'number including area code].';
298+
fireauth.AuthError.MESSAGES_[fireauth.authenum.Error.INVALID_PROVIDER_ID] =
299+
'The specified provider ID is invalid.';
297300
fireauth.AuthError.MESSAGES_[fireauth.authenum.Error.INVALID_RECIPIENT_EMAIL] =
298301
'The email corresponding to this action failed to send as the provided ' +
299302
'recipient email address is invalid.';

packages/auth/src/rpchandler.js

+5
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ fireauth.RpcHandler.ServerError = {
207207
INVALID_OOB_CODE: 'INVALID_OOB_CODE',
208208
INVALID_PASSWORD: 'INVALID_PASSWORD',
209209
INVALID_PHONE_NUMBER: 'INVALID_PHONE_NUMBER',
210+
INVALID_PROVIDER_ID: 'INVALID_PROVIDER_ID',
210211
INVALID_RECIPIENT_EMAIL: 'INVALID_RECIPIENT_EMAIL',
211212
INVALID_SENDER: 'INVALID_SENDER',
212213
INVALID_SESSION_INFO: 'INVALID_SESSION_INFO',
@@ -2244,6 +2245,10 @@ fireauth.RpcHandler.getDeveloperError_ =
22442245
errorMap[fireauth.RpcHandler.ServerError.MISSING_OOB_CODE] =
22452246
fireauth.authenum.Error.INTERNAL_ERROR;
22462247

2248+
// Get Auth URI errors:
2249+
errorMap[fireauth.RpcHandler.ServerError.INVALID_PROVIDER_ID] =
2250+
fireauth.authenum.Error.INVALID_PROVIDER_ID;
2251+
22472252
// Operations that require ID token in request:
22482253
errorMap[fireauth.RpcHandler.ServerError.CREDENTIAL_TOO_OLD_LOGIN_AGAIN] =
22492254
fireauth.authenum.Error.CREDENTIAL_TOO_OLD_LOGIN_AGAIN;

packages/auth/test/rpchandler_test.js

+24
Original file line numberDiff line numberDiff line change
@@ -5268,6 +5268,30 @@ function testGetAuthUri_success() {
52685268
}
52695269

52705270

5271+
/**
5272+
* Tests server side getAuthUri error.
5273+
*/
5274+
function testGetAuthUri_caughtServerError() {
5275+
var expectedUrl = 'https://www.googleapis.com/identitytoolkit/v3/relyin' +
5276+
'gparty/createAuthUri?key=apiKey';
5277+
var requestBody = {
5278+
'providerId': 'abc.com',
5279+
'continueUri': 'http://localhost/widget',
5280+
'customParameter': {}
5281+
};
5282+
var errorMap = {};
5283+
// All related server errors for getAuthUri.
5284+
errorMap[fireauth.RpcHandler.ServerError.INVALID_PROVIDER_ID] =
5285+
fireauth.authenum.Error.INVALID_PROVIDER_ID;
5286+
5287+
assertServerErrorsAreHandled(function() {
5288+
return rpcHandler.getAuthUri(
5289+
'abc.com',
5290+
'http://localhost/widget');
5291+
}, errorMap, expectedUrl, requestBody);
5292+
}
5293+
5294+
52715295
/**
52725296
* Tests successful getAuthUri request with Google provider and sessionId.
52735297
*/

packages/firestore/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Unreleased
2+
- [changed] Improved how Firestore handles idle queries to reduce the cost of
3+
re-listening within 30 minutes.
4+
- [changed] Improved offline performance with many outstanding writes.
5+
6+
# 0.6.0
27
- [fixed] Fixed an issue where queries returned fewer results than they should,
38
caused by documents that were cached as deleted when they should not have
49
been (firebase/firebase-ios-sdk#1548). Because some cache data is cleared,

packages/firestore/src/core/sync_engine.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import {
5050
MutationBatchState,
5151
OnlineState,
5252
OnlineStateSource,
53-
ProtoByteString,
5453
TargetId
5554
} from './types';
5655
import {
@@ -88,12 +87,6 @@ class QueryView {
8887
* stream to identify this query.
8988
*/
9089
public targetId: TargetId,
91-
/**
92-
* An identifier from the datastore backend that indicates the last state
93-
* of the results that was received. This can be used to indicate where
94-
* to continue receiving new doc changes for the query.
95-
*/
96-
public resumeToken: ProtoByteString,
9790
/**
9891
* The view is responsible for computing the final merged truth of what
9992
* docs are in the query. It gets notified of local and remote changes,
@@ -274,12 +267,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
274267
'applyChanges for new view should always return a snapshot'
275268
);
276269

277-
const data = new QueryView(
278-
query,
279-
queryData.targetId,
280-
queryData.resumeToken,
281-
view
282-
);
270+
const data = new QueryView(query, queryData.targetId, view);
283271
this.queryViewsByQuery.set(query, data);
284272
this.queryViewsByTarget[queryData.targetId] = data;
285273
return viewChange.snapshot!;

packages/firestore/src/local/indexeddb_mutation_queue.ts

+86-36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Timestamp } from '../api/timestamp';
1818
import { User } from '../auth/user';
1919
import { Query } from '../core/query';
2020
import { BatchId, ProtoByteString } from '../core/types';
21+
import { DocumentKeySet } from '../model/collections';
2122
import { DocumentKey } from '../model/document_key';
2223
import { Mutation } from '../model/mutation';
2324
import { BATCHID_UNKNOWN, MutationBatch } from '../model/mutation_batch';
@@ -40,8 +41,8 @@ import { LocalSerializer } from './local_serializer';
4041
import { MutationQueue } from './mutation_queue';
4142
import { PersistenceTransaction } from './persistence';
4243
import { PersistencePromise } from './persistence_promise';
43-
import { SimpleDb, SimpleDbStore } from './simple_db';
44-
import { DocumentKeySet } from '../model/collections';
44+
import { SimpleDbStore } from './simple_db';
45+
import { IndexedDbPersistence } from './indexeddb_persistence';
4546

4647
/** A mutation queue for a specific user, backed by IndexedDB. */
4748
export class IndexedDbMutationQueue implements MutationQueue {
@@ -342,6 +343,50 @@ export class IndexedDbMutationQueue implements MutationQueue {
342343
.next(() => results);
343344
}
344345

346+
getAllMutationBatchesAffectingDocumentKeys(
347+
transaction: PersistenceTransaction,
348+
documentKeys: DocumentKeySet
349+
): PersistencePromise<MutationBatch[]> {
350+
let uniqueBatchIDs = new SortedSet<BatchId>(primitiveComparator);
351+
352+
const promises: Array<PersistencePromise<void>> = [];
353+
documentKeys.forEach(documentKey => {
354+
const indexStart = DbDocumentMutation.prefixForPath(
355+
this.userId,
356+
documentKey.path
357+
);
358+
const range = IDBKeyRange.lowerBound(indexStart);
359+
360+
const promise = documentMutationsStore(transaction).iterate(
361+
{ range },
362+
(indexKey, _, control) => {
363+
const [userID, encodedPath, batchID] = indexKey;
364+
365+
// Only consider rows matching exactly the specific key of
366+
// interest. Note that because we order by path first, and we
367+
// order terminators before path separators, we'll encounter all
368+
// the index rows for documentKey contiguously. In particular, all
369+
// the rows for documentKey will occur before any rows for
370+
// documents nested in a subcollection beneath documentKey so we
371+
// can stop as soon as we hit any such row.
372+
const path = EncodedResourcePath.decode(encodedPath);
373+
if (userID !== this.userId || !documentKey.path.isEqual(path)) {
374+
control.done();
375+
return;
376+
}
377+
378+
uniqueBatchIDs = uniqueBatchIDs.add(batchID);
379+
}
380+
);
381+
382+
promises.push(promise);
383+
});
384+
385+
return PersistencePromise.waitFor(promises).next(() =>
386+
this.lookupMutationBatches(transaction, uniqueBatchIDs)
387+
);
388+
}
389+
345390
getAllMutationBatchesAffectingQuery(
346391
transaction: PersistenceTransaction,
347392
query: Query
@@ -393,34 +438,39 @@ export class IndexedDbMutationQueue implements MutationQueue {
393438
}
394439
uniqueBatchIDs = uniqueBatchIDs.add(batchID);
395440
})
396-
.next(() => {
397-
const results: MutationBatch[] = [];
398-
const promises: Array<PersistencePromise<void>> = [];
399-
// TODO(rockwood): Implement this using iterate.
400-
uniqueBatchIDs.forEach(batchId => {
401-
promises.push(
402-
mutationsStore(transaction)
403-
.get(batchId)
404-
.next(mutation => {
405-
if (!mutation) {
406-
fail(
407-
'Dangling document-mutation reference found, ' +
408-
'which points to ' +
409-
batchId
410-
);
411-
}
412-
assert(
413-
mutation.userId === this.userId,
414-
`Unexpected user '${
415-
mutation.userId
416-
}' for mutation batch ${batchId}`
417-
);
418-
results.push(this.serializer.fromDbMutationBatch(mutation!));
419-
})
420-
);
421-
});
422-
return PersistencePromise.waitFor(promises).next(() => results);
423-
});
441+
.next(() => this.lookupMutationBatches(transaction, uniqueBatchIDs));
442+
}
443+
444+
private lookupMutationBatches(
445+
transaction: PersistenceTransaction,
446+
batchIDs: SortedSet<BatchId>
447+
): PersistencePromise<MutationBatch[]> {
448+
const results: MutationBatch[] = [];
449+
const promises: Array<PersistencePromise<void>> = [];
450+
// TODO(rockwood): Implement this using iterate.
451+
batchIDs.forEach(batchId => {
452+
promises.push(
453+
mutationsStore(transaction)
454+
.get(batchId)
455+
.next(mutation => {
456+
if (mutation === null) {
457+
fail(
458+
'Dangling document-mutation reference found, ' +
459+
'which points to ' +
460+
batchId
461+
);
462+
}
463+
assert(
464+
mutation.userId === this.userId,
465+
`Unexpected user '${
466+
mutation.userId
467+
}' for mutation batch ${batchId}`
468+
);
469+
results.push(this.serializer.fromDbMutationBatch(mutation!));
470+
})
471+
);
472+
});
473+
return PersistencePromise.waitFor(promises).next(() => results);
424474
}
425475

426476
removeMutationBatches(
@@ -567,7 +617,7 @@ function convertStreamToken(token: ProtoByteString): string {
567617
function mutationsStore(
568618
txn: PersistenceTransaction
569619
): SimpleDbStore<DbMutationBatchKey, DbMutationBatch> {
570-
return SimpleDb.getStore<DbMutationBatchKey, DbMutationBatch>(
620+
return IndexedDbPersistence.getStore<DbMutationBatchKey, DbMutationBatch>(
571621
txn,
572622
DbMutationBatch.store
573623
);
@@ -579,10 +629,10 @@ function mutationsStore(
579629
function documentMutationsStore(
580630
txn: PersistenceTransaction
581631
): SimpleDbStore<DbDocumentMutationKey, DbDocumentMutation> {
582-
return SimpleDb.getStore<DbDocumentMutationKey, DbDocumentMutation>(
583-
txn,
584-
DbDocumentMutation.store
585-
);
632+
return IndexedDbPersistence.getStore<
633+
DbDocumentMutationKey,
634+
DbDocumentMutation
635+
>(txn, DbDocumentMutation.store);
586636
}
587637

588638
/**
@@ -591,7 +641,7 @@ function documentMutationsStore(
591641
function mutationQueuesStore(
592642
txn: PersistenceTransaction
593643
): SimpleDbStore<DbMutationQueueKey, DbMutationQueue> {
594-
return SimpleDb.getStore<DbMutationQueueKey, DbMutationQueue>(
644+
return IndexedDbPersistence.getStore<DbMutationQueueKey, DbMutationQueue>(
595645
txn,
596646
DbMutationQueue.store
597647
);

0 commit comments

Comments
 (0)