@firebase/firestore 3.4.4-canary.0482aa5b7 → 3.4.4-canary.3fe2c4a03

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/dist/firestore/src/local/indexeddb_index_manager.d.ts +10 -2
  2. package/dist/firestore/src/local/indexeddb_persistence.d.ts +4 -4
  3. package/dist/firestore/src/local/indexeddb_remote_document_cache.d.ts +2 -8
  4. package/dist/firestore/src/local/indexeddb_schema.d.ts +35 -7
  5. package/dist/firestore/src/local/local_documents_view.d.ts +0 -1
  6. package/dist/firestore/src/local/local_serializer.d.ts +7 -2
  7. package/dist/firestore/src/local/memory_persistence.d.ts +3 -2
  8. package/dist/firestore/src/local/memory_remote_document_cache.d.ts +1 -3
  9. package/dist/firestore/src/local/persistence.d.ts +2 -2
  10. package/dist/firestore/src/local/remote_document_cache.d.ts +7 -9
  11. package/dist/firestore/src/local/simple_db.d.ts +1 -0
  12. package/dist/firestore/src/model/field_index.d.ts +2 -2
  13. package/dist/firestore/test/unit/local/counting_query_engine.d.ts +3 -3
  14. package/dist/firestore/test/unit/local/persistence_test_helpers.d.ts +1 -0
  15. package/dist/firestore/test/unit/local/test_index_manager.d.ts +11 -0
  16. package/dist/firestore/test/unit/local/test_remote_document_cache.d.ts +4 -2
  17. package/dist/firestore/test/unit/local/test_remote_document_change_buffer.d.ts +1 -1
  18. package/dist/index.esm2017.js +2619 -2547
  19. package/dist/index.esm2017.js.map +1 -1
  20. package/dist/index.esm5.js +2929 -2822
  21. package/dist/index.esm5.js.map +1 -1
  22. package/dist/index.node.cjs.js +234 -143
  23. package/dist/index.node.cjs.js.map +1 -1
  24. package/dist/index.node.mjs +234 -143
  25. package/dist/index.node.mjs.map +1 -1
  26. package/dist/index.rn.js +2614 -2546
  27. package/dist/index.rn.js.map +1 -1
  28. package/dist/internal.d.ts +9 -12
  29. package/dist/lite/firestore/src/local/indexeddb_index_manager.d.ts +10 -2
  30. package/dist/lite/firestore/src/local/indexeddb_persistence.d.ts +4 -4
  31. package/dist/lite/firestore/src/local/indexeddb_remote_document_cache.d.ts +2 -8
  32. package/dist/lite/firestore/src/local/indexeddb_schema.d.ts +35 -7
  33. package/dist/lite/firestore/src/local/local_documents_view.d.ts +0 -1
  34. package/dist/lite/firestore/src/local/local_serializer.d.ts +7 -2
  35. package/dist/lite/firestore/src/local/memory_persistence.d.ts +3 -2
  36. package/dist/lite/firestore/src/local/memory_remote_document_cache.d.ts +1 -3
  37. package/dist/lite/firestore/src/local/persistence.d.ts +2 -2
  38. package/dist/lite/firestore/src/local/remote_document_cache.d.ts +7 -9
  39. package/dist/lite/firestore/src/local/simple_db.d.ts +1 -0
  40. package/dist/lite/firestore/src/model/field_index.d.ts +2 -2
  41. package/dist/lite/firestore/test/unit/local/counting_query_engine.d.ts +3 -3
  42. package/dist/lite/firestore/test/unit/local/persistence_test_helpers.d.ts +1 -0
  43. package/dist/lite/firestore/test/unit/local/test_index_manager.d.ts +11 -0
  44. package/dist/lite/firestore/test/unit/local/test_remote_document_cache.d.ts +4 -2
  45. package/dist/lite/firestore/test/unit/local/test_remote_document_change_buffer.d.ts +1 -1
  46. package/dist/lite/index.browser.esm2017.js +1112 -1112
  47. package/dist/lite/index.browser.esm2017.js.map +1 -1
  48. package/dist/lite/index.browser.esm5.js +14 -14
  49. package/dist/lite/index.browser.esm5.js.map +1 -1
  50. package/dist/lite/index.node.cjs.js +2 -2
  51. package/dist/lite/index.node.cjs.js.map +1 -1
  52. package/dist/lite/index.node.mjs +2 -2
  53. package/dist/lite/index.node.mjs.map +1 -1
  54. package/dist/lite/index.rn.esm2017.js +2 -2
  55. package/dist/lite/index.rn.esm2017.js.map +1 -1
  56. package/dist/lite/packages/firestore/dist/lite/index.browser.esm2017.d.ts +112 -112
  57. package/dist/lite/packages/firestore/src/local/indexeddb_index_manager.d.ts +10 -2
  58. package/dist/lite/packages/firestore/src/local/indexeddb_persistence.d.ts +4 -4
  59. package/dist/lite/packages/firestore/src/local/indexeddb_remote_document_cache.d.ts +2 -8
  60. package/dist/lite/packages/firestore/src/local/indexeddb_schema.d.ts +35 -7
  61. package/dist/lite/packages/firestore/src/local/local_documents_view.d.ts +0 -1
  62. package/dist/lite/packages/firestore/src/local/local_serializer.d.ts +7 -2
  63. package/dist/lite/packages/firestore/src/local/memory_persistence.d.ts +3 -2
  64. package/dist/lite/packages/firestore/src/local/memory_remote_document_cache.d.ts +1 -3
  65. package/dist/lite/packages/firestore/src/local/persistence.d.ts +2 -2
  66. package/dist/lite/packages/firestore/src/local/remote_document_cache.d.ts +7 -9
  67. package/dist/lite/packages/firestore/src/local/simple_db.d.ts +1 -0
  68. package/dist/lite/packages/firestore/src/model/field_index.d.ts +2 -2
  69. package/dist/lite/packages/firestore/test/unit/local/counting_query_engine.d.ts +3 -3
  70. package/dist/lite/packages/firestore/test/unit/local/persistence_test_helpers.d.ts +1 -0
  71. package/dist/lite/packages/firestore/test/unit/local/test_index_manager.d.ts +11 -0
  72. package/dist/lite/packages/firestore/test/unit/local/test_remote_document_cache.d.ts +4 -2
  73. package/dist/lite/packages/firestore/test/unit/local/test_remote_document_change_buffer.d.ts +1 -1
  74. package/dist/packages/firestore/dist/index.esm2017.d.ts +174 -174
  75. package/dist/packages/firestore/src/local/indexeddb_index_manager.d.ts +10 -2
  76. package/dist/packages/firestore/src/local/indexeddb_persistence.d.ts +4 -4
  77. package/dist/packages/firestore/src/local/indexeddb_remote_document_cache.d.ts +2 -8
  78. package/dist/packages/firestore/src/local/indexeddb_schema.d.ts +35 -7
  79. package/dist/packages/firestore/src/local/local_documents_view.d.ts +0 -1
  80. package/dist/packages/firestore/src/local/local_serializer.d.ts +7 -2
  81. package/dist/packages/firestore/src/local/memory_persistence.d.ts +3 -2
  82. package/dist/packages/firestore/src/local/memory_remote_document_cache.d.ts +1 -3
  83. package/dist/packages/firestore/src/local/persistence.d.ts +2 -2
  84. package/dist/packages/firestore/src/local/remote_document_cache.d.ts +7 -9
  85. package/dist/packages/firestore/src/local/simple_db.d.ts +1 -0
  86. package/dist/packages/firestore/src/model/field_index.d.ts +2 -2
  87. package/dist/packages/firestore/test/unit/local/counting_query_engine.d.ts +3 -3
  88. package/dist/packages/firestore/test/unit/local/persistence_test_helpers.d.ts +1 -0
  89. package/dist/packages/firestore/test/unit/local/test_index_manager.d.ts +11 -0
  90. package/dist/packages/firestore/test/unit/local/test_remote_document_cache.d.ts +4 -2
  91. package/dist/packages/firestore/test/unit/local/test_remote_document_change_buffer.d.ts +1 -1
  92. package/dist/private.d.ts +9 -12
  93. package/package.json +9 -9
@@ -8,7 +8,7 @@ import { credentials, loadPackageDefinition, Metadata } from '@grpc/grpc-js';
8
8
  import { fromJSON } from '@grpc/proto-loader';
9
9
 
10
10
  const name = "@firebase/firestore";
11
- const version$1 = "3.4.4-canary.0482aa5b7";
11
+ const version$1 = "3.4.4-canary.3fe2c4a03";
12
12
 
13
13
  /**
14
14
  * @license
@@ -61,7 +61,7 @@ User.GOOGLE_CREDENTIALS = new User('google-credentials-uid');
61
61
  User.FIRST_PARTY = new User('first-party-uid');
62
62
  User.MOCK_USER = new User('mock-user');
63
63
 
64
- const version = "9.6.5-canary.0482aa5b7";
64
+ const version = "9.6.6-canary.3fe2c4a03";
65
65
 
66
66
  /**
67
67
  * @license
@@ -1774,7 +1774,10 @@ DbNamedQuery.keyPath = 'name';
1774
1774
  /** An object representing the global configuration for a field index. */
1775
1775
  class DbIndexConfiguration {
1776
1776
  constructor(
1777
- /** The index id for this entry. */
1777
+ /**
1778
+ * The index id for this entry. Undefined for indexes that are not yet
1779
+ * persisted.
1780
+ */
1778
1781
  indexId,
1779
1782
  /** The collection group this index belongs to. */
1780
1783
  collectionGroup,
@@ -1788,6 +1791,16 @@ class DbIndexConfiguration {
1788
1791
  /** Name of the IndexedDb object store. */
1789
1792
  DbIndexConfiguration.store = 'indexConfiguration';
1790
1793
  DbIndexConfiguration.keyPath = 'indexId';
1794
+ /**
1795
+ * An index that provides access to the index configurations by collection
1796
+ * group.
1797
+ *
1798
+ * PORTING NOTE: iOS and Android maintain this index in-memory, but this is
1799
+ * not possible here as the Web client supports concurrent access to
1800
+ * persistence via multi-tab.
1801
+ */
1802
+ DbIndexConfiguration.collectionGroupIndex = 'collectionGroupIndex';
1803
+ DbIndexConfiguration.collectionGroupIndexPath = 'collectionGroup';
1791
1804
  /**
1792
1805
  * An object describing how up-to-date the index backfill is for each user and
1793
1806
  * index.
@@ -1829,8 +1842,18 @@ class DbIndexState {
1829
1842
  /** Name of the IndexedDb object store. */
1830
1843
  DbIndexState.store = 'indexState';
1831
1844
  DbIndexState.keyPath = ['indexId', 'uid'];
1845
+ /**
1846
+ * An index that provides access to documents in a collection sorted by last
1847
+ * update time. Used by the backfiller.
1848
+ *
1849
+ * PORTING NOTE: iOS and Android maintain this index in-memory, but this is
1850
+ * not possible here as the Web client supports concurrent access to
1851
+ * persistence via multi-tab.
1852
+ */
1853
+ DbIndexState.sequenceNumberIndex = 'sequenceNumberIndex';
1854
+ DbIndexState.sequenceNumberIndexPath = ['uid', 'sequenceNumber'];
1832
1855
  /** An object that stores the encoded entries for all documents and fields. */
1833
- class DbIndexEntries {
1856
+ class DbIndexEntry {
1834
1857
  constructor(
1835
1858
  /** The index id for this entry. */
1836
1859
  indexId,
@@ -1850,8 +1873,8 @@ class DbIndexEntries {
1850
1873
  }
1851
1874
  }
1852
1875
  /** Name of the IndexedDb object store. */
1853
- DbIndexEntries.store = 'indexEntries';
1854
- DbIndexEntries.keyPath = [
1876
+ DbIndexEntry.store = 'indexEntries';
1877
+ DbIndexEntry.keyPath = [
1855
1878
  'indexId',
1856
1879
  'uid',
1857
1880
  'arrayValue',
@@ -1881,18 +1904,24 @@ const V8_STORES = [...V6_STORES, DbCollectionParent.store];
1881
1904
  // V9 does not change the set of stores.
1882
1905
  // V10 does not change the set of stores.
1883
1906
  const V11_STORES = [...V8_STORES, DbBundle.store, DbNamedQuery.store];
1884
- [
1907
+ const V12_STORES = [
1885
1908
  ...V11_STORES,
1886
1909
  DbIndexConfiguration.store,
1887
1910
  DbIndexState.store,
1888
- DbIndexEntries.store
1911
+ DbIndexEntry.store
1889
1912
  ];
1890
- /**
1891
- * The list of all default IndexedDB stores used throughout the SDK. This is
1892
- * used when creating transactions so that access across all stores is done
1893
- * atomically.
1894
- */
1895
- const ALL_STORES = V11_STORES;
1913
+ /** Returns the object stores for the provided schema. */
1914
+ function getObjectStores(schemaVersion) {
1915
+ if (schemaVersion === 12) {
1916
+ return V12_STORES;
1917
+ }
1918
+ else if (schemaVersion === 11) {
1919
+ return V11_STORES;
1920
+ }
1921
+ else {
1922
+ fail();
1923
+ }
1924
+ }
1896
1925
 
1897
1926
  /**
1898
1927
  * @license
@@ -2189,6 +2218,15 @@ class SimpleDbTransaction {
2189
2218
  this.transaction.abort();
2190
2219
  }
2191
2220
  }
2221
+ maybeCommit() {
2222
+ // If the browser supports V3 IndexedDB, we invoke commit() explicitly to
2223
+ // speed up index DB processing if the event loop remains blocks.
2224
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2225
+ const maybeV3IndexedDb = this.transaction;
2226
+ if (!this.aborted && typeof maybeV3IndexedDb.commit === 'function') {
2227
+ maybeV3IndexedDb.commit();
2228
+ }
2229
+ }
2192
2230
  /**
2193
2231
  * Returns a SimpleDbStore<KeyType, ValueType> for the specified store. All
2194
2232
  * operations performed on the SimpleDbStore happen within the context of this
@@ -2385,6 +2423,10 @@ class SimpleDb {
2385
2423
  this.db = await this.ensureDb(action);
2386
2424
  const transaction = SimpleDbTransaction.open(this.db, action, readonly ? 'readonly' : 'readwrite', objectStores);
2387
2425
  const transactionFnResult = transactionFn(transaction)
2426
+ .next(result => {
2427
+ transaction.maybeCommit();
2428
+ return result;
2429
+ })
2388
2430
  .catch(error => {
2389
2431
  // Abort the transaction if there was an error.
2390
2432
  transaction.abort(error);
@@ -4417,12 +4459,12 @@ class FieldIndex {
4417
4459
  /** The collection ID this index applies to. */
4418
4460
  collectionGroup,
4419
4461
  /** The field segments for this index. */
4420
- segments,
4462
+ fields,
4421
4463
  /** Shows how up-to-date the index is for the current user. */
4422
4464
  indexState) {
4423
4465
  this.indexId = indexId;
4424
4466
  this.collectionGroup = collectionGroup;
4425
- this.segments = segments;
4467
+ this.fields = fields;
4426
4468
  this.indexState = indexState;
4427
4469
  }
4428
4470
  }
@@ -8683,6 +8725,19 @@ function fromBundleMetadata(metadata) {
8683
8725
  version: metadata.version,
8684
8726
  createTime: fromVersion(metadata.createTime)
8685
8727
  };
8728
+ }
8729
+ function toDbIndexConfiguration(index) {
8730
+ return new DbIndexConfiguration(index.indexId, index.collectionGroup, index.fields.map(s => [s.fieldPath.canonicalString(), s.kind]));
8731
+ }
8732
+ function fromDbIndexConfiguration(index, state) {
8733
+ const decodedState = state
8734
+ ? new IndexState(state.sequenceNumber, new IndexOffset(fromDbTimestamp(state.readTime), new DocumentKey(decodeResourcePath(state.documentKey)), state.largestBatchId))
8735
+ : IndexState.empty();
8736
+ const decodedSegments = index.fields.map(([fieldPath, kind]) => new IndexSegment(FieldPath$1.fromServerFormat(fieldPath), kind));
8737
+ return new FieldIndex(index.indexId, index.collectionGroup, decodedSegments, decodedState);
8738
+ }
8739
+ function toDbIndexState(indexId, user, sequenceNumber, offset) {
8740
+ return new DbIndexState(indexId, user.uid || '', sequenceNumber, toDbTimestamp(offset.readTime), encodeResourcePath(offset.documentKey.path), offset.largestBatchId);
8686
8741
  }
8687
8742
 
8688
8743
  /**
@@ -8855,17 +8910,22 @@ class MemoryCollectionParentIndex {
8855
8910
  */
8856
8911
  /**
8857
8912
  * A persisted implementation of IndexManager.
8913
+ *
8914
+ * PORTING NOTE: Unlike iOS and Android, the Web SDK does not memoize index
8915
+ * data as it supports multi-tab access.
8858
8916
  */
8859
8917
  class IndexedDbIndexManager {
8860
- constructor() {
8918
+ constructor(user) {
8919
+ this.user = user;
8861
8920
  /**
8862
8921
  * An in-memory copy of the index entries we've already written since the SDK
8863
8922
  * launched. Used to avoid re-writing the same entry repeatedly.
8864
8923
  *
8865
- * This is *NOT* a complete cache of what's in persistence and so can never be used to
8866
- * satisfy reads.
8924
+ * This is *NOT* a complete cache of what's in persistence and so can never be
8925
+ * used to satisfy reads.
8867
8926
  */
8868
8927
  this.collectionParentsCache = new MemoryCollectionParentIndex();
8928
+ this.uid = user.uid || '';
8869
8929
  }
8870
8930
  /**
8871
8931
  * Adds a new entry to the collection parent index.
@@ -8913,12 +8973,25 @@ class IndexedDbIndexManager {
8913
8973
  });
8914
8974
  }
8915
8975
  addFieldIndex(transaction, index) {
8916
- // TODO(indexing): Implement
8917
- return PersistencePromise.resolve();
8976
+ // TODO(indexing): Verify that the auto-incrementing index ID works in
8977
+ // Safari & Firefox.
8978
+ const indexes = indexConfigurationStore(transaction);
8979
+ const dbIndex = toDbIndexConfiguration(index);
8980
+ delete dbIndex.indexId; // `indexId` is auto-populated by IndexedDb
8981
+ return indexes.add(dbIndex).next();
8918
8982
  }
8919
8983
  deleteFieldIndex(transaction, index) {
8920
- // TODO(indexing): Implement
8921
- return PersistencePromise.resolve();
8984
+ const indexes = indexConfigurationStore(transaction);
8985
+ const states = indexStateStore(transaction);
8986
+ const entries = indexEntriesStore(transaction);
8987
+ return indexes
8988
+ .delete(index.indexId)
8989
+ .next(() => states.delete(IDBKeyRange.bound([index.indexId], [index.indexId + 1],
8990
+ /*lowerOpen=*/ false,
8991
+ /*upperOpen=*/ true)))
8992
+ .next(() => entries.delete(IDBKeyRange.bound([index.indexId], [index.indexId + 1],
8993
+ /*lowerOpen=*/ false,
8994
+ /*upperOpen=*/ true)));
8922
8995
  }
8923
8996
  getDocumentsMatchingTarget(transaction, fieldIndex, target) {
8924
8997
  // TODO(indexing): Implement
@@ -8929,21 +9002,55 @@ class IndexedDbIndexManager {
8929
9002
  return PersistencePromise.resolve(null);
8930
9003
  }
8931
9004
  getFieldIndexes(transaction, collectionGroup) {
8932
- // TODO(indexing): Implement
8933
- return PersistencePromise.resolve([]);
9005
+ const indexes = indexConfigurationStore(transaction);
9006
+ const states = indexStateStore(transaction);
9007
+ return (collectionGroup
9008
+ ? indexes.loadAll(DbIndexConfiguration.collectionGroupIndex, IDBKeyRange.bound(collectionGroup, collectionGroup))
9009
+ : indexes.loadAll()).next(indexConfigs => {
9010
+ const result = [];
9011
+ return PersistencePromise.forEach(indexConfigs, (indexConfig) => {
9012
+ return states
9013
+ .get([indexConfig.indexId, this.uid])
9014
+ .next(indexState => {
9015
+ result.push(fromDbIndexConfiguration(indexConfig, indexState));
9016
+ });
9017
+ }).next(() => result);
9018
+ });
8934
9019
  }
8935
9020
  getNextCollectionGroupToUpdate(transaction) {
8936
- // TODO(indexing): Implement
8937
- return PersistencePromise.resolve(null);
9021
+ return this.getFieldIndexes(transaction).next(indexes => {
9022
+ if (indexes.length === 0) {
9023
+ return null;
9024
+ }
9025
+ indexes.sort((l, r) => l.indexState.sequenceNumber - r.indexState.sequenceNumber);
9026
+ return indexes[0].collectionGroup;
9027
+ });
8938
9028
  }
8939
9029
  updateCollectionGroup(transaction, collectionGroup, offset) {
8940
- // TODO(indexing): Implement
8941
- return PersistencePromise.resolve();
9030
+ const indexes = indexConfigurationStore(transaction);
9031
+ const states = indexStateStore(transaction);
9032
+ return this.getNextSequenceNumber(transaction).next(nextSequenceNumber => indexes
9033
+ .loadAll(DbIndexConfiguration.collectionGroupIndex, IDBKeyRange.bound(collectionGroup, collectionGroup))
9034
+ .next(configs => PersistencePromise.forEach(configs, (config) => states.put(toDbIndexState(config.indexId, this.user, nextSequenceNumber, offset)))));
8942
9035
  }
8943
9036
  updateIndexEntries(transaction, documents) {
8944
9037
  // TODO(indexing): Implement
8945
9038
  return PersistencePromise.resolve();
8946
9039
  }
9040
+ getNextSequenceNumber(transaction) {
9041
+ let nextSequenceNumber = 1;
9042
+ const states = indexStateStore(transaction);
9043
+ return states
9044
+ .iterate({
9045
+ index: DbIndexState.sequenceNumberIndex,
9046
+ reverse: true,
9047
+ range: IDBKeyRange.upperBound([this.uid, Number.MAX_SAFE_INTEGER])
9048
+ }, (_, state, controller) => {
9049
+ controller.done();
9050
+ nextSequenceNumber = state.sequenceNumber + 1;
9051
+ })
9052
+ .next(() => nextSequenceNumber);
9053
+ }
8947
9054
  }
8948
9055
  /**
8949
9056
  * Helper to get a typed SimpleDbStore for the collectionParents
@@ -8951,6 +9058,24 @@ class IndexedDbIndexManager {
8951
9058
  */
8952
9059
  function collectionParentsStore(txn) {
8953
9060
  return getStore(txn, DbCollectionParent.store);
9061
+ }
9062
+ /**
9063
+ * Helper to get a typed SimpleDbStore for the index entry object store.
9064
+ */
9065
+ function indexEntriesStore(txn) {
9066
+ return getStore(txn, DbIndexEntry.store);
9067
+ }
9068
+ /**
9069
+ * Helper to get a typed SimpleDbStore for the index configuration object store.
9070
+ */
9071
+ function indexConfigurationStore(txn) {
9072
+ return getStore(txn, DbIndexConfiguration.store);
9073
+ }
9074
+ /**
9075
+ * Helper to get a typed SimpleDbStore for the index state object store.
9076
+ */
9077
+ function indexStateStore(txn) {
9078
+ return getStore(txn, DbIndexState.store);
8954
9079
  }
8955
9080
 
8956
9081
  /**
@@ -10420,12 +10545,10 @@ class RemoteDocumentChangeBuffer {
10420
10545
  * `newIndexedDbRemoteDocumentCache()`.
10421
10546
  */
10422
10547
  class IndexedDbRemoteDocumentCacheImpl {
10423
- /**
10424
- * @param serializer - The document serializer.
10425
- * @param indexManager - The query indexes that need to be maintained.
10426
- */
10427
- constructor(serializer, indexManager) {
10548
+ constructor(serializer) {
10428
10549
  this.serializer = serializer;
10550
+ }
10551
+ setIndexManager(indexManager) {
10429
10552
  this.indexManager = indexManager;
10430
10553
  }
10431
10554
  /**
@@ -10547,21 +10670,21 @@ class IndexedDbRemoteDocumentCacheImpl {
10547
10670
  }
10548
10671
  });
10549
10672
  }
10550
- getDocumentsMatchingQuery(transaction, query, sinceReadTime) {
10673
+ getAll(transaction, collection, sinceReadTime) {
10551
10674
  let results = mutableDocumentMap();
10552
- const immediateChildrenPathLength = query.path.length + 1;
10675
+ const immediateChildrenPathLength = collection.length + 1;
10553
10676
  const iterationOptions = {};
10554
10677
  if (sinceReadTime.isEqual(SnapshotVersion.min())) {
10555
10678
  // Documents are ordered by key, so we can use a prefix scan to narrow
10556
10679
  // down the documents we need to match the query against.
10557
- const startKey = query.path.toArray();
10680
+ const startKey = collection.toArray();
10558
10681
  iterationOptions.range = IDBKeyRange.lowerBound(startKey);
10559
10682
  }
10560
10683
  else {
10561
10684
  // Execute an index-free query and filter by read time. This is safe
10562
10685
  // since all document changes to queries that have a
10563
10686
  // lastLimboFreeSnapshotVersion (`sinceReadTime`) have a read time set.
10564
- const collectionKey = query.path.toArray();
10687
+ const collectionKey = collection.toArray();
10565
10688
  const readTimeKey = toDbTimestampKey(sinceReadTime);
10566
10689
  iterationOptions.range = IDBKeyRange.lowerBound([collectionKey, readTimeKey],
10567
10690
  /* open= */ true);
@@ -10578,12 +10701,12 @@ class IndexedDbRemoteDocumentCacheImpl {
10578
10701
  return;
10579
10702
  }
10580
10703
  const document = this.maybeDecodeDocument(DocumentKey.fromSegments(key), dbRemoteDoc);
10581
- if (!query.path.isPrefixOf(document.key.path)) {
10582
- control.done();
10583
- }
10584
- else if (queryMatches(query, document)) {
10704
+ if (collection.isPrefixOf(document.key.path)) {
10585
10705
  results = results.insert(document.key, document);
10586
10706
  }
10707
+ else {
10708
+ control.done();
10709
+ }
10587
10710
  })
10588
10711
  .next(() => results);
10589
10712
  }
@@ -10621,14 +10744,9 @@ class IndexedDbRemoteDocumentCacheImpl {
10621
10744
  return MutableDocument.newInvalidDocument(documentKey);
10622
10745
  }
10623
10746
  }
10624
- /**
10625
- * Creates a new IndexedDbRemoteDocumentCache.
10626
- *
10627
- * @param serializer - The document serializer.
10628
- * @param indexManager - The query indexes that need to be maintained.
10629
- */
10630
- function newIndexedDbRemoteDocumentCache(serializer, indexManager) {
10631
- return new IndexedDbRemoteDocumentCacheImpl(serializer, indexManager);
10747
+ /** Creates a new IndexedDbRemoteDocumentCache. */
10748
+ function newIndexedDbRemoteDocumentCache(serializer) {
10749
+ return new IndexedDbRemoteDocumentCacheImpl(serializer);
10632
10750
  }
10633
10751
  /**
10634
10752
  * Returns the set of documents that have changed since the specified read
@@ -11074,14 +11192,17 @@ function createNamedQueriesStore(db) {
11074
11192
  });
11075
11193
  }
11076
11194
  function createFieldIndex(db) {
11077
- db.createObjectStore(DbIndexConfiguration.store, {
11078
- keyPath: DbIndexConfiguration.keyPath
11195
+ const indexConfiguratioStore = db.createObjectStore(DbIndexConfiguration.store, {
11196
+ keyPath: DbIndexConfiguration.keyPath,
11197
+ autoIncrement: true
11079
11198
  });
11080
- db.createObjectStore(DbIndexState.store, {
11199
+ indexConfiguratioStore.createIndex(DbIndexConfiguration.collectionGroupIndex, DbIndexConfiguration.collectionGroupIndexPath, { unique: false });
11200
+ const indexStateStore = db.createObjectStore(DbIndexState.store, {
11081
11201
  keyPath: DbIndexState.keyPath
11082
11202
  });
11083
- db.createObjectStore(DbIndexEntries.store, {
11084
- keyPath: DbIndexEntries.keyPath
11203
+ indexStateStore.createIndex(DbIndexState.sequenceNumberIndex, DbIndexState.sequenceNumberIndexPath, { unique: false });
11204
+ db.createObjectStore(DbIndexEntry.store, {
11205
+ keyPath: DbIndexEntry.keyPath
11085
11206
  });
11086
11207
  }
11087
11208
 
@@ -11194,7 +11315,7 @@ class IndexedDbPersistence {
11194
11315
  * If set to true, forcefully obtains database access. Existing tabs will
11195
11316
  * no longer be able to access IndexedDB.
11196
11317
  */
11197
- forceOwningTab) {
11318
+ forceOwningTab, schemaVersion = SCHEMA_VERSION) {
11198
11319
  this.allowTabSynchronization = allowTabSynchronization;
11199
11320
  this.persistenceKey = persistenceKey;
11200
11321
  this.clientId = clientId;
@@ -11203,6 +11324,7 @@ class IndexedDbPersistence {
11203
11324
  this.document = document;
11204
11325
  this.sequenceNumberSyncer = sequenceNumberSyncer;
11205
11326
  this.forceOwningTab = forceOwningTab;
11327
+ this.schemaVersion = schemaVersion;
11206
11328
  this.listenSequence = null;
11207
11329
  this._started = false;
11208
11330
  this.isPrimary = false;
@@ -11224,10 +11346,9 @@ class IndexedDbPersistence {
11224
11346
  this.referenceDelegate = new IndexedDbLruDelegateImpl(this, lruParams);
11225
11347
  this.dbName = persistenceKey + MAIN_DATABASE;
11226
11348
  this.serializer = new LocalSerializer(serializer);
11227
- this.simpleDb = new SimpleDb(this.dbName, SCHEMA_VERSION, new SchemaConverter(this.serializer));
11349
+ this.simpleDb = new SimpleDb(this.dbName, this.schemaVersion, new SchemaConverter(this.serializer));
11228
11350
  this.targetCache = new IndexedDbTargetCache(this.referenceDelegate, this.serializer);
11229
- this.indexManager = new IndexedDbIndexManager();
11230
- this.remoteDocumentCache = newIndexedDbRemoteDocumentCache(this.serializer, this.indexManager);
11351
+ this.remoteDocumentCache = newIndexedDbRemoteDocumentCache(this.serializer);
11231
11352
  this.bundleCache = new IndexedDbBundleCache();
11232
11353
  if (this.window && this.window.localStorage) {
11233
11354
  this.webStorage = this.window.localStorage;
@@ -11563,8 +11684,8 @@ class IndexedDbPersistence {
11563
11684
  get started() {
11564
11685
  return this._started;
11565
11686
  }
11566
- getMutationQueue(user) {
11567
- return IndexedDbMutationQueue.forUser(user, this.serializer, this.indexManager, this.referenceDelegate);
11687
+ getMutationQueue(user, indexManager) {
11688
+ return IndexedDbMutationQueue.forUser(user, this.serializer, indexManager, this.referenceDelegate);
11568
11689
  }
11569
11690
  getTargetCache() {
11570
11691
  return this.targetCache;
@@ -11572,8 +11693,8 @@ class IndexedDbPersistence {
11572
11693
  getRemoteDocumentCache() {
11573
11694
  return this.remoteDocumentCache;
11574
11695
  }
11575
- getIndexManager() {
11576
- return this.indexManager;
11696
+ getIndexManager(user) {
11697
+ return new IndexedDbIndexManager(user);
11577
11698
  }
11578
11699
  getBundleCache() {
11579
11700
  return this.bundleCache;
@@ -11581,11 +11702,12 @@ class IndexedDbPersistence {
11581
11702
  runTransaction(action, mode, transactionOperation) {
11582
11703
  logDebug(LOG_TAG$d, 'Starting transaction:', action);
11583
11704
  const simpleDbMode = mode === 'readonly' ? 'readonly' : 'readwrite';
11705
+ const objectStores = getObjectStores(this.schemaVersion);
11584
11706
  let persistenceTransaction;
11585
11707
  // Do all transactions as readwrite against all object stores, since we
11586
11708
  // are the only reader/writer.
11587
11709
  return this.simpleDb
11588
- .runTransaction(action, simpleDbMode, ALL_STORES, simpleDbTxn => {
11710
+ .runTransaction(action, simpleDbMode, objectStores, simpleDbTxn => {
11589
11711
  persistenceTransaction = new IndexedDbTransaction(simpleDbTxn, this.listenSequence
11590
11712
  ? this.listenSequence.next()
11591
11713
  : ListenSequence.INVALID);
@@ -11957,39 +12079,28 @@ class LocalDocumentsView {
11957
12079
  getDocumentsMatchingCollectionQuery(transaction, query, sinceReadTime) {
11958
12080
  // Query the remote documents and overlay mutations.
11959
12081
  let results;
11960
- let mutationBatches;
11961
12082
  return this.remoteDocumentCache
11962
- .getDocumentsMatchingQuery(transaction, query, sinceReadTime)
12083
+ .getAll(transaction, query.path, sinceReadTime)
11963
12084
  .next(queryResults => {
11964
12085
  results = queryResults;
11965
12086
  return this.mutationQueue.getAllMutationBatchesAffectingQuery(transaction, query);
11966
12087
  })
11967
- .next(matchingMutationBatches => {
11968
- mutationBatches = matchingMutationBatches;
11969
- // It is possible that a PatchMutation can make a document match a query, even if
11970
- // the version in the RemoteDocumentCache is not a match yet (waiting for server
11971
- // to ack). To handle this, we find all document keys affected by the PatchMutations
11972
- // that are not in `result` yet, and back fill them via `remoteDocumentCache.getEntries`,
11973
- // otherwise those `PatchMutations` will be ignored because no base document can be found,
11974
- // and lead to missing result for the query.
11975
- return this.addMissingBaseDocuments(transaction, mutationBatches, results).next(mergedDocuments => {
11976
- results = mergedDocuments;
11977
- for (const batch of mutationBatches) {
11978
- for (const mutation of batch.mutations) {
11979
- const key = mutation.key;
11980
- let document = results.get(key);
11981
- if (document == null) {
11982
- // Create invalid document to apply mutations on top of
11983
- document = MutableDocument.newInvalidDocument(key);
11984
- results = results.insert(key, document);
11985
- }
11986
- mutationApplyToLocalView(mutation, document, batch.localWriteTime);
11987
- if (!document.isFoundDocument()) {
11988
- results = results.remove(key);
11989
- }
12088
+ .next(mutationBatches => {
12089
+ for (const batch of mutationBatches) {
12090
+ for (const mutation of batch.mutations) {
12091
+ const key = mutation.key;
12092
+ let document = results.get(key);
12093
+ if (document == null) {
12094
+ // Create invalid document to apply mutations on top of
12095
+ document = MutableDocument.newInvalidDocument(key);
12096
+ results = results.insert(key, document);
12097
+ }
12098
+ mutationApplyToLocalView(mutation, document, batch.localWriteTime);
12099
+ if (!document.isFoundDocument()) {
12100
+ results = results.remove(key);
11990
12101
  }
11991
12102
  }
11992
- });
12103
+ }
11993
12104
  })
11994
12105
  .next(() => {
11995
12106
  // Finally, filter out any documents that don't actually match
@@ -12002,29 +12113,6 @@ class LocalDocumentsView {
12002
12113
  return results;
12003
12114
  });
12004
12115
  }
12005
- addMissingBaseDocuments(transaction, matchingMutationBatches, existingDocuments) {
12006
- let missingBaseDocEntriesForPatching = documentKeySet();
12007
- for (const batch of matchingMutationBatches) {
12008
- for (const mutation of batch.mutations) {
12009
- if (mutation instanceof PatchMutation &&
12010
- existingDocuments.get(mutation.key) === null) {
12011
- missingBaseDocEntriesForPatching =
12012
- missingBaseDocEntriesForPatching.add(mutation.key);
12013
- }
12014
- }
12015
- }
12016
- let mergedDocuments = existingDocuments;
12017
- return this.remoteDocumentCache
12018
- .getEntries(transaction, missingBaseDocEntriesForPatching)
12019
- .next(missingBaseDocs => {
12020
- missingBaseDocs.forEach((key, doc) => {
12021
- if (doc.isFoundDocument()) {
12022
- mergedDocuments = mergedDocuments.insert(key, doc);
12023
- }
12024
- });
12025
- return mergedDocuments;
12026
- });
12027
- }
12028
12116
  }
12029
12117
 
12030
12118
  /**
@@ -12083,11 +12171,18 @@ class LocalStoreImpl {
12083
12171
  * PORTING NOTE: This is only used for multi-tab synchronization.
12084
12172
  */
12085
12173
  this.lastDocumentChangeReadTime = SnapshotVersion.min();
12086
- this.mutationQueue = persistence.getMutationQueue(initialUser);
12087
12174
  this.remoteDocuments = persistence.getRemoteDocumentCache();
12088
12175
  this.targetCache = persistence.getTargetCache();
12089
- this.localDocuments = new LocalDocumentsView(this.remoteDocuments, this.mutationQueue, this.persistence.getIndexManager());
12090
12176
  this.bundleCache = persistence.getBundleCache();
12177
+ this.initializeUserComponents(initialUser);
12178
+ }
12179
+ initializeUserComponents(user) {
12180
+ // TODO(indexing): Add spec tests that test these components change after a
12181
+ // user change
12182
+ this.indexManager = this.persistence.getIndexManager(user);
12183
+ this.mutationQueue = this.persistence.getMutationQueue(user, this.indexManager);
12184
+ this.localDocuments = new LocalDocumentsView(this.remoteDocuments, this.mutationQueue, this.indexManager);
12185
+ this.remoteDocuments.setIndexManager(this.indexManager);
12091
12186
  this.queryEngine.setLocalDocumentsView(this.localDocuments);
12092
12187
  }
12093
12188
  collectGarbage(garbageCollector) {
@@ -12109,8 +12204,6 @@ persistence, queryEngine, initialUser, serializer) {
12109
12204
  // change.
12110
12205
  async function localStoreHandleUserChange(localStore, user) {
12111
12206
  const localStoreImpl = debugCast(localStore);
12112
- let newMutationQueue = localStoreImpl.mutationQueue;
12113
- let newLocalDocuments = localStoreImpl.localDocuments;
12114
12207
  const result = await localStoreImpl.persistence.runTransaction('Handle user change', 'readonly', txn => {
12115
12208
  // Swap out the mutation queue, grabbing the pending mutation batches
12116
12209
  // before and after.
@@ -12119,11 +12212,8 @@ async function localStoreHandleUserChange(localStore, user) {
12119
12212
  .getAllMutationBatches(txn)
12120
12213
  .next(promisedOldBatches => {
12121
12214
  oldBatches = promisedOldBatches;
12122
- newMutationQueue = localStoreImpl.persistence.getMutationQueue(user);
12123
- // Recreate our LocalDocumentsView using the new
12124
- // MutationQueue.
12125
- newLocalDocuments = new LocalDocumentsView(localStoreImpl.remoteDocuments, newMutationQueue, localStoreImpl.persistence.getIndexManager());
12126
- return newMutationQueue.getAllMutationBatches(txn);
12215
+ localStoreImpl.initializeUserComponents(user);
12216
+ return localStoreImpl.mutationQueue.getAllMutationBatches(txn);
12127
12217
  })
12128
12218
  .next(newBatches => {
12129
12219
  const removedBatchIds = [];
@@ -12144,7 +12234,7 @@ async function localStoreHandleUserChange(localStore, user) {
12144
12234
  }
12145
12235
  // Return the set of all (potentially) changed documents and the list
12146
12236
  // of mutation batch IDs that were affected by change.
12147
- return newLocalDocuments
12237
+ return localStoreImpl.localDocuments
12148
12238
  .getDocuments(txn, changedKeys)
12149
12239
  .next(affectedDocuments => {
12150
12240
  return {
@@ -12155,9 +12245,6 @@ async function localStoreHandleUserChange(localStore, user) {
12155
12245
  });
12156
12246
  });
12157
12247
  });
12158
- localStoreImpl.mutationQueue = newMutationQueue;
12159
- localStoreImpl.localDocuments = newLocalDocuments;
12160
- localStoreImpl.queryEngine.setLocalDocumentsView(localStoreImpl.localDocuments);
12161
12248
  return result;
12162
12249
  }
12163
12250
  /* Accepts locally generated Mutations and commit them to storage. */
@@ -13223,14 +13310,16 @@ class MemoryRemoteDocumentCacheImpl {
13223
13310
  * expected to just return 0 to avoid unnecessarily doing the work of
13224
13311
  * calculating the size.
13225
13312
  */
13226
- constructor(indexManager, sizer) {
13227
- this.indexManager = indexManager;
13313
+ constructor(sizer) {
13228
13314
  this.sizer = sizer;
13229
13315
  /** Underlying cache of documents and their read times. */
13230
13316
  this.docs = documentEntryMap();
13231
13317
  /** Size of all cached documents. */
13232
13318
  this.size = 0;
13233
13319
  }
13320
+ setIndexManager(indexManager) {
13321
+ this.indexManager = indexManager;
13322
+ }
13234
13323
  /**
13235
13324
  * Adds the supplied entry to the cache and updates the cache size as appropriate.
13236
13325
  *
@@ -13278,21 +13367,22 @@ class MemoryRemoteDocumentCacheImpl {
13278
13367
  });
13279
13368
  return PersistencePromise.resolve(results);
13280
13369
  }
13281
- getDocumentsMatchingQuery(transaction, query, sinceReadTime) {
13370
+ getAll(transaction, collectionPath, sinceReadTime) {
13282
13371
  let results = mutableDocumentMap();
13283
13372
  // Documents are ordered by key, so we can use a prefix scan to narrow down
13284
13373
  // the documents we need to match the query against.
13285
- const prefix = new DocumentKey(query.path.child(''));
13374
+ const prefix = new DocumentKey(collectionPath.child(''));
13286
13375
  const iterator = this.docs.getIteratorFrom(prefix);
13287
13376
  while (iterator.hasNext()) {
13288
13377
  const { key, value: { document } } = iterator.getNext();
13289
- if (!query.path.isPrefixOf(key.path)) {
13378
+ if (!collectionPath.isPrefixOf(key.path)) {
13290
13379
  break;
13291
13380
  }
13292
- if (document.readTime.compareTo(sinceReadTime) <= 0) {
13381
+ if (key.path.length > collectionPath.length + 1) {
13382
+ // Exclude entries from subcollections.
13293
13383
  continue;
13294
13384
  }
13295
- if (!queryMatches(query, document)) {
13385
+ if (document.readTime.compareTo(sinceReadTime) <= 0) {
13296
13386
  continue;
13297
13387
  }
13298
13388
  results = results.insert(document.key, document.mutableCopy());
@@ -13314,13 +13404,12 @@ class MemoryRemoteDocumentCacheImpl {
13314
13404
  /**
13315
13405
  * Creates a new memory-only RemoteDocumentCache.
13316
13406
  *
13317
- * @param indexManager - A class that manages collection group indices.
13318
13407
  * @param sizer - Used to assess the size of a document. For eager GC, this is
13319
13408
  * expected to just return 0 to avoid unnecessarily doing the work of
13320
13409
  * calculating the size.
13321
13410
  */
13322
- function newMemoryRemoteDocumentCache(indexManager, sizer) {
13323
- return new MemoryRemoteDocumentCacheImpl(indexManager, sizer);
13411
+ function newMemoryRemoteDocumentCache(sizer) {
13412
+ return new MemoryRemoteDocumentCacheImpl(sizer);
13324
13413
  }
13325
13414
  /**
13326
13415
  * Handles the details of adding and updating documents in the MemoryRemoteDocumentCache.
@@ -13521,7 +13610,7 @@ class MemoryPersistence {
13521
13610
  this.targetCache = new MemoryTargetCache(this);
13522
13611
  const sizer = (doc) => this.referenceDelegate.documentSize(doc);
13523
13612
  this.indexManager = new MemoryIndexManager();
13524
- this.remoteDocumentCache = newMemoryRemoteDocumentCache(this.indexManager, sizer);
13613
+ this.remoteDocumentCache = newMemoryRemoteDocumentCache(sizer);
13525
13614
  this.serializer = new LocalSerializer(serializer);
13526
13615
  this.bundleCache = new MemoryBundleCache(this.serializer);
13527
13616
  }
@@ -13542,13 +13631,15 @@ class MemoryPersistence {
13542
13631
  setNetworkEnabled() {
13543
13632
  // No op.
13544
13633
  }
13545
- getIndexManager() {
13634
+ getIndexManager(user) {
13635
+ // We do not currently support indices for memory persistence, so we can
13636
+ // return the same shared instance of the memory index manager.
13546
13637
  return this.indexManager;
13547
13638
  }
13548
- getMutationQueue(user) {
13639
+ getMutationQueue(user, indexManager) {
13549
13640
  let queue = this.mutationQueues[user.toKey()];
13550
13641
  if (!queue) {
13551
- queue = new MemoryMutationQueue(this.indexManager, this.referenceDelegate);
13642
+ queue = new MemoryMutationQueue(indexManager, this.referenceDelegate);
13552
13643
  this.mutationQueues[user.toKey()] = queue;
13553
13644
  }
13554
13645
  return queue;
@@ -20511,12 +20602,12 @@ async function syncEngineListen(syncEngine, query) {
20511
20602
  }
20512
20603
  else {
20513
20604
  const targetData = await localStoreAllocateTarget(syncEngineImpl.localStore, queryToTarget(query));
20514
- const status = syncEngineImpl.sharedClientState.addLocalQueryTarget(targetData.targetId);
20515
- targetId = targetData.targetId;
20516
- viewSnapshot = await initializeViewAndComputeSnapshot(syncEngineImpl, query, targetId, status === 'current');
20517
20605
  if (syncEngineImpl.isPrimaryClient) {
20518
20606
  remoteStoreListen(syncEngineImpl.remoteStore, targetData);
20519
20607
  }
20608
+ const status = syncEngineImpl.sharedClientState.addLocalQueryTarget(targetData.targetId);
20609
+ targetId = targetData.targetId;
20610
+ viewSnapshot = await initializeViewAndComputeSnapshot(syncEngineImpl, query, targetId, status === 'current');
20520
20611
  }
20521
20612
  return viewSnapshot;
20522
20613
  }