@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
@@ -12,7 +12,7 @@ var grpcJs = require('@grpc/grpc-js');
12
12
  var protoLoader = require('@grpc/proto-loader');
13
13
 
14
14
  const name = "@firebase/firestore";
15
- const version$1 = "3.4.4-canary.0482aa5b7";
15
+ const version$1 = "3.4.4-canary.3fe2c4a03";
16
16
 
17
17
  /**
18
18
  * @license
@@ -65,7 +65,7 @@ User.GOOGLE_CREDENTIALS = new User('google-credentials-uid');
65
65
  User.FIRST_PARTY = new User('first-party-uid');
66
66
  User.MOCK_USER = new User('mock-user');
67
67
 
68
- const version = "9.6.5-canary.0482aa5b7";
68
+ const version = "9.6.6-canary.3fe2c4a03";
69
69
 
70
70
  /**
71
71
  * @license
@@ -1778,7 +1778,10 @@ DbNamedQuery.keyPath = 'name';
1778
1778
  /** An object representing the global configuration for a field index. */
1779
1779
  class DbIndexConfiguration {
1780
1780
  constructor(
1781
- /** The index id for this entry. */
1781
+ /**
1782
+ * The index id for this entry. Undefined for indexes that are not yet
1783
+ * persisted.
1784
+ */
1782
1785
  indexId,
1783
1786
  /** The collection group this index belongs to. */
1784
1787
  collectionGroup,
@@ -1792,6 +1795,16 @@ class DbIndexConfiguration {
1792
1795
  /** Name of the IndexedDb object store. */
1793
1796
  DbIndexConfiguration.store = 'indexConfiguration';
1794
1797
  DbIndexConfiguration.keyPath = 'indexId';
1798
+ /**
1799
+ * An index that provides access to the index configurations by collection
1800
+ * group.
1801
+ *
1802
+ * PORTING NOTE: iOS and Android maintain this index in-memory, but this is
1803
+ * not possible here as the Web client supports concurrent access to
1804
+ * persistence via multi-tab.
1805
+ */
1806
+ DbIndexConfiguration.collectionGroupIndex = 'collectionGroupIndex';
1807
+ DbIndexConfiguration.collectionGroupIndexPath = 'collectionGroup';
1795
1808
  /**
1796
1809
  * An object describing how up-to-date the index backfill is for each user and
1797
1810
  * index.
@@ -1833,8 +1846,18 @@ class DbIndexState {
1833
1846
  /** Name of the IndexedDb object store. */
1834
1847
  DbIndexState.store = 'indexState';
1835
1848
  DbIndexState.keyPath = ['indexId', 'uid'];
1849
+ /**
1850
+ * An index that provides access to documents in a collection sorted by last
1851
+ * update time. Used by the backfiller.
1852
+ *
1853
+ * PORTING NOTE: iOS and Android maintain this index in-memory, but this is
1854
+ * not possible here as the Web client supports concurrent access to
1855
+ * persistence via multi-tab.
1856
+ */
1857
+ DbIndexState.sequenceNumberIndex = 'sequenceNumberIndex';
1858
+ DbIndexState.sequenceNumberIndexPath = ['uid', 'sequenceNumber'];
1836
1859
  /** An object that stores the encoded entries for all documents and fields. */
1837
- class DbIndexEntries {
1860
+ class DbIndexEntry {
1838
1861
  constructor(
1839
1862
  /** The index id for this entry. */
1840
1863
  indexId,
@@ -1854,8 +1877,8 @@ class DbIndexEntries {
1854
1877
  }
1855
1878
  }
1856
1879
  /** Name of the IndexedDb object store. */
1857
- DbIndexEntries.store = 'indexEntries';
1858
- DbIndexEntries.keyPath = [
1880
+ DbIndexEntry.store = 'indexEntries';
1881
+ DbIndexEntry.keyPath = [
1859
1882
  'indexId',
1860
1883
  'uid',
1861
1884
  'arrayValue',
@@ -1885,18 +1908,24 @@ const V8_STORES = [...V6_STORES, DbCollectionParent.store];
1885
1908
  // V9 does not change the set of stores.
1886
1909
  // V10 does not change the set of stores.
1887
1910
  const V11_STORES = [...V8_STORES, DbBundle.store, DbNamedQuery.store];
1888
- [
1911
+ const V12_STORES = [
1889
1912
  ...V11_STORES,
1890
1913
  DbIndexConfiguration.store,
1891
1914
  DbIndexState.store,
1892
- DbIndexEntries.store
1915
+ DbIndexEntry.store
1893
1916
  ];
1894
- /**
1895
- * The list of all default IndexedDB stores used throughout the SDK. This is
1896
- * used when creating transactions so that access across all stores is done
1897
- * atomically.
1898
- */
1899
- const ALL_STORES = V11_STORES;
1917
+ /** Returns the object stores for the provided schema. */
1918
+ function getObjectStores(schemaVersion) {
1919
+ if (schemaVersion === 12) {
1920
+ return V12_STORES;
1921
+ }
1922
+ else if (schemaVersion === 11) {
1923
+ return V11_STORES;
1924
+ }
1925
+ else {
1926
+ fail();
1927
+ }
1928
+ }
1900
1929
 
1901
1930
  /**
1902
1931
  * @license
@@ -2193,6 +2222,15 @@ class SimpleDbTransaction {
2193
2222
  this.transaction.abort();
2194
2223
  }
2195
2224
  }
2225
+ maybeCommit() {
2226
+ // If the browser supports V3 IndexedDB, we invoke commit() explicitly to
2227
+ // speed up index DB processing if the event loop remains blocks.
2228
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2229
+ const maybeV3IndexedDb = this.transaction;
2230
+ if (!this.aborted && typeof maybeV3IndexedDb.commit === 'function') {
2231
+ maybeV3IndexedDb.commit();
2232
+ }
2233
+ }
2196
2234
  /**
2197
2235
  * Returns a SimpleDbStore<KeyType, ValueType> for the specified store. All
2198
2236
  * operations performed on the SimpleDbStore happen within the context of this
@@ -2389,6 +2427,10 @@ class SimpleDb {
2389
2427
  this.db = await this.ensureDb(action);
2390
2428
  const transaction = SimpleDbTransaction.open(this.db, action, readonly ? 'readonly' : 'readwrite', objectStores);
2391
2429
  const transactionFnResult = transactionFn(transaction)
2430
+ .next(result => {
2431
+ transaction.maybeCommit();
2432
+ return result;
2433
+ })
2392
2434
  .catch(error => {
2393
2435
  // Abort the transaction if there was an error.
2394
2436
  transaction.abort(error);
@@ -4421,12 +4463,12 @@ class FieldIndex {
4421
4463
  /** The collection ID this index applies to. */
4422
4464
  collectionGroup,
4423
4465
  /** The field segments for this index. */
4424
- segments,
4466
+ fields,
4425
4467
  /** Shows how up-to-date the index is for the current user. */
4426
4468
  indexState) {
4427
4469
  this.indexId = indexId;
4428
4470
  this.collectionGroup = collectionGroup;
4429
- this.segments = segments;
4471
+ this.fields = fields;
4430
4472
  this.indexState = indexState;
4431
4473
  }
4432
4474
  }
@@ -8687,6 +8729,19 @@ function fromBundleMetadata(metadata) {
8687
8729
  version: metadata.version,
8688
8730
  createTime: fromVersion(metadata.createTime)
8689
8731
  };
8732
+ }
8733
+ function toDbIndexConfiguration(index) {
8734
+ return new DbIndexConfiguration(index.indexId, index.collectionGroup, index.fields.map(s => [s.fieldPath.canonicalString(), s.kind]));
8735
+ }
8736
+ function fromDbIndexConfiguration(index, state) {
8737
+ const decodedState = state
8738
+ ? new IndexState(state.sequenceNumber, new IndexOffset(fromDbTimestamp(state.readTime), new DocumentKey(decodeResourcePath(state.documentKey)), state.largestBatchId))
8739
+ : IndexState.empty();
8740
+ const decodedSegments = index.fields.map(([fieldPath, kind]) => new IndexSegment(FieldPath$1.fromServerFormat(fieldPath), kind));
8741
+ return new FieldIndex(index.indexId, index.collectionGroup, decodedSegments, decodedState);
8742
+ }
8743
+ function toDbIndexState(indexId, user, sequenceNumber, offset) {
8744
+ return new DbIndexState(indexId, user.uid || '', sequenceNumber, toDbTimestamp(offset.readTime), encodeResourcePath(offset.documentKey.path), offset.largestBatchId);
8690
8745
  }
8691
8746
 
8692
8747
  /**
@@ -8859,17 +8914,22 @@ class MemoryCollectionParentIndex {
8859
8914
  */
8860
8915
  /**
8861
8916
  * A persisted implementation of IndexManager.
8917
+ *
8918
+ * PORTING NOTE: Unlike iOS and Android, the Web SDK does not memoize index
8919
+ * data as it supports multi-tab access.
8862
8920
  */
8863
8921
  class IndexedDbIndexManager {
8864
- constructor() {
8922
+ constructor(user) {
8923
+ this.user = user;
8865
8924
  /**
8866
8925
  * An in-memory copy of the index entries we've already written since the SDK
8867
8926
  * launched. Used to avoid re-writing the same entry repeatedly.
8868
8927
  *
8869
- * This is *NOT* a complete cache of what's in persistence and so can never be used to
8870
- * satisfy reads.
8928
+ * This is *NOT* a complete cache of what's in persistence and so can never be
8929
+ * used to satisfy reads.
8871
8930
  */
8872
8931
  this.collectionParentsCache = new MemoryCollectionParentIndex();
8932
+ this.uid = user.uid || '';
8873
8933
  }
8874
8934
  /**
8875
8935
  * Adds a new entry to the collection parent index.
@@ -8917,12 +8977,25 @@ class IndexedDbIndexManager {
8917
8977
  });
8918
8978
  }
8919
8979
  addFieldIndex(transaction, index) {
8920
- // TODO(indexing): Implement
8921
- return PersistencePromise.resolve();
8980
+ // TODO(indexing): Verify that the auto-incrementing index ID works in
8981
+ // Safari & Firefox.
8982
+ const indexes = indexConfigurationStore(transaction);
8983
+ const dbIndex = toDbIndexConfiguration(index);
8984
+ delete dbIndex.indexId; // `indexId` is auto-populated by IndexedDb
8985
+ return indexes.add(dbIndex).next();
8922
8986
  }
8923
8987
  deleteFieldIndex(transaction, index) {
8924
- // TODO(indexing): Implement
8925
- return PersistencePromise.resolve();
8988
+ const indexes = indexConfigurationStore(transaction);
8989
+ const states = indexStateStore(transaction);
8990
+ const entries = indexEntriesStore(transaction);
8991
+ return indexes
8992
+ .delete(index.indexId)
8993
+ .next(() => states.delete(IDBKeyRange.bound([index.indexId], [index.indexId + 1],
8994
+ /*lowerOpen=*/ false,
8995
+ /*upperOpen=*/ true)))
8996
+ .next(() => entries.delete(IDBKeyRange.bound([index.indexId], [index.indexId + 1],
8997
+ /*lowerOpen=*/ false,
8998
+ /*upperOpen=*/ true)));
8926
8999
  }
8927
9000
  getDocumentsMatchingTarget(transaction, fieldIndex, target) {
8928
9001
  // TODO(indexing): Implement
@@ -8933,21 +9006,55 @@ class IndexedDbIndexManager {
8933
9006
  return PersistencePromise.resolve(null);
8934
9007
  }
8935
9008
  getFieldIndexes(transaction, collectionGroup) {
8936
- // TODO(indexing): Implement
8937
- return PersistencePromise.resolve([]);
9009
+ const indexes = indexConfigurationStore(transaction);
9010
+ const states = indexStateStore(transaction);
9011
+ return (collectionGroup
9012
+ ? indexes.loadAll(DbIndexConfiguration.collectionGroupIndex, IDBKeyRange.bound(collectionGroup, collectionGroup))
9013
+ : indexes.loadAll()).next(indexConfigs => {
9014
+ const result = [];
9015
+ return PersistencePromise.forEach(indexConfigs, (indexConfig) => {
9016
+ return states
9017
+ .get([indexConfig.indexId, this.uid])
9018
+ .next(indexState => {
9019
+ result.push(fromDbIndexConfiguration(indexConfig, indexState));
9020
+ });
9021
+ }).next(() => result);
9022
+ });
8938
9023
  }
8939
9024
  getNextCollectionGroupToUpdate(transaction) {
8940
- // TODO(indexing): Implement
8941
- return PersistencePromise.resolve(null);
9025
+ return this.getFieldIndexes(transaction).next(indexes => {
9026
+ if (indexes.length === 0) {
9027
+ return null;
9028
+ }
9029
+ indexes.sort((l, r) => l.indexState.sequenceNumber - r.indexState.sequenceNumber);
9030
+ return indexes[0].collectionGroup;
9031
+ });
8942
9032
  }
8943
9033
  updateCollectionGroup(transaction, collectionGroup, offset) {
8944
- // TODO(indexing): Implement
8945
- return PersistencePromise.resolve();
9034
+ const indexes = indexConfigurationStore(transaction);
9035
+ const states = indexStateStore(transaction);
9036
+ return this.getNextSequenceNumber(transaction).next(nextSequenceNumber => indexes
9037
+ .loadAll(DbIndexConfiguration.collectionGroupIndex, IDBKeyRange.bound(collectionGroup, collectionGroup))
9038
+ .next(configs => PersistencePromise.forEach(configs, (config) => states.put(toDbIndexState(config.indexId, this.user, nextSequenceNumber, offset)))));
8946
9039
  }
8947
9040
  updateIndexEntries(transaction, documents) {
8948
9041
  // TODO(indexing): Implement
8949
9042
  return PersistencePromise.resolve();
8950
9043
  }
9044
+ getNextSequenceNumber(transaction) {
9045
+ let nextSequenceNumber = 1;
9046
+ const states = indexStateStore(transaction);
9047
+ return states
9048
+ .iterate({
9049
+ index: DbIndexState.sequenceNumberIndex,
9050
+ reverse: true,
9051
+ range: IDBKeyRange.upperBound([this.uid, Number.MAX_SAFE_INTEGER])
9052
+ }, (_, state, controller) => {
9053
+ controller.done();
9054
+ nextSequenceNumber = state.sequenceNumber + 1;
9055
+ })
9056
+ .next(() => nextSequenceNumber);
9057
+ }
8951
9058
  }
8952
9059
  /**
8953
9060
  * Helper to get a typed SimpleDbStore for the collectionParents
@@ -8955,6 +9062,24 @@ class IndexedDbIndexManager {
8955
9062
  */
8956
9063
  function collectionParentsStore(txn) {
8957
9064
  return getStore(txn, DbCollectionParent.store);
9065
+ }
9066
+ /**
9067
+ * Helper to get a typed SimpleDbStore for the index entry object store.
9068
+ */
9069
+ function indexEntriesStore(txn) {
9070
+ return getStore(txn, DbIndexEntry.store);
9071
+ }
9072
+ /**
9073
+ * Helper to get a typed SimpleDbStore for the index configuration object store.
9074
+ */
9075
+ function indexConfigurationStore(txn) {
9076
+ return getStore(txn, DbIndexConfiguration.store);
9077
+ }
9078
+ /**
9079
+ * Helper to get a typed SimpleDbStore for the index state object store.
9080
+ */
9081
+ function indexStateStore(txn) {
9082
+ return getStore(txn, DbIndexState.store);
8958
9083
  }
8959
9084
 
8960
9085
  /**
@@ -10424,12 +10549,10 @@ class RemoteDocumentChangeBuffer {
10424
10549
  * `newIndexedDbRemoteDocumentCache()`.
10425
10550
  */
10426
10551
  class IndexedDbRemoteDocumentCacheImpl {
10427
- /**
10428
- * @param serializer - The document serializer.
10429
- * @param indexManager - The query indexes that need to be maintained.
10430
- */
10431
- constructor(serializer, indexManager) {
10552
+ constructor(serializer) {
10432
10553
  this.serializer = serializer;
10554
+ }
10555
+ setIndexManager(indexManager) {
10433
10556
  this.indexManager = indexManager;
10434
10557
  }
10435
10558
  /**
@@ -10551,21 +10674,21 @@ class IndexedDbRemoteDocumentCacheImpl {
10551
10674
  }
10552
10675
  });
10553
10676
  }
10554
- getDocumentsMatchingQuery(transaction, query, sinceReadTime) {
10677
+ getAll(transaction, collection, sinceReadTime) {
10555
10678
  let results = mutableDocumentMap();
10556
- const immediateChildrenPathLength = query.path.length + 1;
10679
+ const immediateChildrenPathLength = collection.length + 1;
10557
10680
  const iterationOptions = {};
10558
10681
  if (sinceReadTime.isEqual(SnapshotVersion.min())) {
10559
10682
  // Documents are ordered by key, so we can use a prefix scan to narrow
10560
10683
  // down the documents we need to match the query against.
10561
- const startKey = query.path.toArray();
10684
+ const startKey = collection.toArray();
10562
10685
  iterationOptions.range = IDBKeyRange.lowerBound(startKey);
10563
10686
  }
10564
10687
  else {
10565
10688
  // Execute an index-free query and filter by read time. This is safe
10566
10689
  // since all document changes to queries that have a
10567
10690
  // lastLimboFreeSnapshotVersion (`sinceReadTime`) have a read time set.
10568
- const collectionKey = query.path.toArray();
10691
+ const collectionKey = collection.toArray();
10569
10692
  const readTimeKey = toDbTimestampKey(sinceReadTime);
10570
10693
  iterationOptions.range = IDBKeyRange.lowerBound([collectionKey, readTimeKey],
10571
10694
  /* open= */ true);
@@ -10582,12 +10705,12 @@ class IndexedDbRemoteDocumentCacheImpl {
10582
10705
  return;
10583
10706
  }
10584
10707
  const document = this.maybeDecodeDocument(DocumentKey.fromSegments(key), dbRemoteDoc);
10585
- if (!query.path.isPrefixOf(document.key.path)) {
10586
- control.done();
10587
- }
10588
- else if (queryMatches(query, document)) {
10708
+ if (collection.isPrefixOf(document.key.path)) {
10589
10709
  results = results.insert(document.key, document);
10590
10710
  }
10711
+ else {
10712
+ control.done();
10713
+ }
10591
10714
  })
10592
10715
  .next(() => results);
10593
10716
  }
@@ -10625,14 +10748,9 @@ class IndexedDbRemoteDocumentCacheImpl {
10625
10748
  return MutableDocument.newInvalidDocument(documentKey);
10626
10749
  }
10627
10750
  }
10628
- /**
10629
- * Creates a new IndexedDbRemoteDocumentCache.
10630
- *
10631
- * @param serializer - The document serializer.
10632
- * @param indexManager - The query indexes that need to be maintained.
10633
- */
10634
- function newIndexedDbRemoteDocumentCache(serializer, indexManager) {
10635
- return new IndexedDbRemoteDocumentCacheImpl(serializer, indexManager);
10751
+ /** Creates a new IndexedDbRemoteDocumentCache. */
10752
+ function newIndexedDbRemoteDocumentCache(serializer) {
10753
+ return new IndexedDbRemoteDocumentCacheImpl(serializer);
10636
10754
  }
10637
10755
  /**
10638
10756
  * Returns the set of documents that have changed since the specified read
@@ -11078,14 +11196,17 @@ function createNamedQueriesStore(db) {
11078
11196
  });
11079
11197
  }
11080
11198
  function createFieldIndex(db) {
11081
- db.createObjectStore(DbIndexConfiguration.store, {
11082
- keyPath: DbIndexConfiguration.keyPath
11199
+ const indexConfiguratioStore = db.createObjectStore(DbIndexConfiguration.store, {
11200
+ keyPath: DbIndexConfiguration.keyPath,
11201
+ autoIncrement: true
11083
11202
  });
11084
- db.createObjectStore(DbIndexState.store, {
11203
+ indexConfiguratioStore.createIndex(DbIndexConfiguration.collectionGroupIndex, DbIndexConfiguration.collectionGroupIndexPath, { unique: false });
11204
+ const indexStateStore = db.createObjectStore(DbIndexState.store, {
11085
11205
  keyPath: DbIndexState.keyPath
11086
11206
  });
11087
- db.createObjectStore(DbIndexEntries.store, {
11088
- keyPath: DbIndexEntries.keyPath
11207
+ indexStateStore.createIndex(DbIndexState.sequenceNumberIndex, DbIndexState.sequenceNumberIndexPath, { unique: false });
11208
+ db.createObjectStore(DbIndexEntry.store, {
11209
+ keyPath: DbIndexEntry.keyPath
11089
11210
  });
11090
11211
  }
11091
11212
 
@@ -11198,7 +11319,7 @@ class IndexedDbPersistence {
11198
11319
  * If set to true, forcefully obtains database access. Existing tabs will
11199
11320
  * no longer be able to access IndexedDB.
11200
11321
  */
11201
- forceOwningTab) {
11322
+ forceOwningTab, schemaVersion = SCHEMA_VERSION) {
11202
11323
  this.allowTabSynchronization = allowTabSynchronization;
11203
11324
  this.persistenceKey = persistenceKey;
11204
11325
  this.clientId = clientId;
@@ -11207,6 +11328,7 @@ class IndexedDbPersistence {
11207
11328
  this.document = document;
11208
11329
  this.sequenceNumberSyncer = sequenceNumberSyncer;
11209
11330
  this.forceOwningTab = forceOwningTab;
11331
+ this.schemaVersion = schemaVersion;
11210
11332
  this.listenSequence = null;
11211
11333
  this._started = false;
11212
11334
  this.isPrimary = false;
@@ -11228,10 +11350,9 @@ class IndexedDbPersistence {
11228
11350
  this.referenceDelegate = new IndexedDbLruDelegateImpl(this, lruParams);
11229
11351
  this.dbName = persistenceKey + MAIN_DATABASE;
11230
11352
  this.serializer = new LocalSerializer(serializer);
11231
- this.simpleDb = new SimpleDb(this.dbName, SCHEMA_VERSION, new SchemaConverter(this.serializer));
11353
+ this.simpleDb = new SimpleDb(this.dbName, this.schemaVersion, new SchemaConverter(this.serializer));
11232
11354
  this.targetCache = new IndexedDbTargetCache(this.referenceDelegate, this.serializer);
11233
- this.indexManager = new IndexedDbIndexManager();
11234
- this.remoteDocumentCache = newIndexedDbRemoteDocumentCache(this.serializer, this.indexManager);
11355
+ this.remoteDocumentCache = newIndexedDbRemoteDocumentCache(this.serializer);
11235
11356
  this.bundleCache = new IndexedDbBundleCache();
11236
11357
  if (this.window && this.window.localStorage) {
11237
11358
  this.webStorage = this.window.localStorage;
@@ -11567,8 +11688,8 @@ class IndexedDbPersistence {
11567
11688
  get started() {
11568
11689
  return this._started;
11569
11690
  }
11570
- getMutationQueue(user) {
11571
- return IndexedDbMutationQueue.forUser(user, this.serializer, this.indexManager, this.referenceDelegate);
11691
+ getMutationQueue(user, indexManager) {
11692
+ return IndexedDbMutationQueue.forUser(user, this.serializer, indexManager, this.referenceDelegate);
11572
11693
  }
11573
11694
  getTargetCache() {
11574
11695
  return this.targetCache;
@@ -11576,8 +11697,8 @@ class IndexedDbPersistence {
11576
11697
  getRemoteDocumentCache() {
11577
11698
  return this.remoteDocumentCache;
11578
11699
  }
11579
- getIndexManager() {
11580
- return this.indexManager;
11700
+ getIndexManager(user) {
11701
+ return new IndexedDbIndexManager(user);
11581
11702
  }
11582
11703
  getBundleCache() {
11583
11704
  return this.bundleCache;
@@ -11585,11 +11706,12 @@ class IndexedDbPersistence {
11585
11706
  runTransaction(action, mode, transactionOperation) {
11586
11707
  logDebug(LOG_TAG$d, 'Starting transaction:', action);
11587
11708
  const simpleDbMode = mode === 'readonly' ? 'readonly' : 'readwrite';
11709
+ const objectStores = getObjectStores(this.schemaVersion);
11588
11710
  let persistenceTransaction;
11589
11711
  // Do all transactions as readwrite against all object stores, since we
11590
11712
  // are the only reader/writer.
11591
11713
  return this.simpleDb
11592
- .runTransaction(action, simpleDbMode, ALL_STORES, simpleDbTxn => {
11714
+ .runTransaction(action, simpleDbMode, objectStores, simpleDbTxn => {
11593
11715
  persistenceTransaction = new IndexedDbTransaction(simpleDbTxn, this.listenSequence
11594
11716
  ? this.listenSequence.next()
11595
11717
  : ListenSequence.INVALID);
@@ -11961,39 +12083,28 @@ class LocalDocumentsView {
11961
12083
  getDocumentsMatchingCollectionQuery(transaction, query, sinceReadTime) {
11962
12084
  // Query the remote documents and overlay mutations.
11963
12085
  let results;
11964
- let mutationBatches;
11965
12086
  return this.remoteDocumentCache
11966
- .getDocumentsMatchingQuery(transaction, query, sinceReadTime)
12087
+ .getAll(transaction, query.path, sinceReadTime)
11967
12088
  .next(queryResults => {
11968
12089
  results = queryResults;
11969
12090
  return this.mutationQueue.getAllMutationBatchesAffectingQuery(transaction, query);
11970
12091
  })
11971
- .next(matchingMutationBatches => {
11972
- mutationBatches = matchingMutationBatches;
11973
- // It is possible that a PatchMutation can make a document match a query, even if
11974
- // the version in the RemoteDocumentCache is not a match yet (waiting for server
11975
- // to ack). To handle this, we find all document keys affected by the PatchMutations
11976
- // that are not in `result` yet, and back fill them via `remoteDocumentCache.getEntries`,
11977
- // otherwise those `PatchMutations` will be ignored because no base document can be found,
11978
- // and lead to missing result for the query.
11979
- return this.addMissingBaseDocuments(transaction, mutationBatches, results).next(mergedDocuments => {
11980
- results = mergedDocuments;
11981
- for (const batch of mutationBatches) {
11982
- for (const mutation of batch.mutations) {
11983
- const key = mutation.key;
11984
- let document = results.get(key);
11985
- if (document == null) {
11986
- // Create invalid document to apply mutations on top of
11987
- document = MutableDocument.newInvalidDocument(key);
11988
- results = results.insert(key, document);
11989
- }
11990
- mutationApplyToLocalView(mutation, document, batch.localWriteTime);
11991
- if (!document.isFoundDocument()) {
11992
- results = results.remove(key);
11993
- }
12092
+ .next(mutationBatches => {
12093
+ for (const batch of mutationBatches) {
12094
+ for (const mutation of batch.mutations) {
12095
+ const key = mutation.key;
12096
+ let document = results.get(key);
12097
+ if (document == null) {
12098
+ // Create invalid document to apply mutations on top of
12099
+ document = MutableDocument.newInvalidDocument(key);
12100
+ results = results.insert(key, document);
12101
+ }
12102
+ mutationApplyToLocalView(mutation, document, batch.localWriteTime);
12103
+ if (!document.isFoundDocument()) {
12104
+ results = results.remove(key);
11994
12105
  }
11995
12106
  }
11996
- });
12107
+ }
11997
12108
  })
11998
12109
  .next(() => {
11999
12110
  // Finally, filter out any documents that don't actually match
@@ -12006,29 +12117,6 @@ class LocalDocumentsView {
12006
12117
  return results;
12007
12118
  });
12008
12119
  }
12009
- addMissingBaseDocuments(transaction, matchingMutationBatches, existingDocuments) {
12010
- let missingBaseDocEntriesForPatching = documentKeySet();
12011
- for (const batch of matchingMutationBatches) {
12012
- for (const mutation of batch.mutations) {
12013
- if (mutation instanceof PatchMutation &&
12014
- existingDocuments.get(mutation.key) === null) {
12015
- missingBaseDocEntriesForPatching =
12016
- missingBaseDocEntriesForPatching.add(mutation.key);
12017
- }
12018
- }
12019
- }
12020
- let mergedDocuments = existingDocuments;
12021
- return this.remoteDocumentCache
12022
- .getEntries(transaction, missingBaseDocEntriesForPatching)
12023
- .next(missingBaseDocs => {
12024
- missingBaseDocs.forEach((key, doc) => {
12025
- if (doc.isFoundDocument()) {
12026
- mergedDocuments = mergedDocuments.insert(key, doc);
12027
- }
12028
- });
12029
- return mergedDocuments;
12030
- });
12031
- }
12032
12120
  }
12033
12121
 
12034
12122
  /**
@@ -12087,11 +12175,18 @@ class LocalStoreImpl {
12087
12175
  * PORTING NOTE: This is only used for multi-tab synchronization.
12088
12176
  */
12089
12177
  this.lastDocumentChangeReadTime = SnapshotVersion.min();
12090
- this.mutationQueue = persistence.getMutationQueue(initialUser);
12091
12178
  this.remoteDocuments = persistence.getRemoteDocumentCache();
12092
12179
  this.targetCache = persistence.getTargetCache();
12093
- this.localDocuments = new LocalDocumentsView(this.remoteDocuments, this.mutationQueue, this.persistence.getIndexManager());
12094
12180
  this.bundleCache = persistence.getBundleCache();
12181
+ this.initializeUserComponents(initialUser);
12182
+ }
12183
+ initializeUserComponents(user) {
12184
+ // TODO(indexing): Add spec tests that test these components change after a
12185
+ // user change
12186
+ this.indexManager = this.persistence.getIndexManager(user);
12187
+ this.mutationQueue = this.persistence.getMutationQueue(user, this.indexManager);
12188
+ this.localDocuments = new LocalDocumentsView(this.remoteDocuments, this.mutationQueue, this.indexManager);
12189
+ this.remoteDocuments.setIndexManager(this.indexManager);
12095
12190
  this.queryEngine.setLocalDocumentsView(this.localDocuments);
12096
12191
  }
12097
12192
  collectGarbage(garbageCollector) {
@@ -12113,8 +12208,6 @@ persistence, queryEngine, initialUser, serializer) {
12113
12208
  // change.
12114
12209
  async function localStoreHandleUserChange(localStore, user) {
12115
12210
  const localStoreImpl = debugCast(localStore);
12116
- let newMutationQueue = localStoreImpl.mutationQueue;
12117
- let newLocalDocuments = localStoreImpl.localDocuments;
12118
12211
  const result = await localStoreImpl.persistence.runTransaction('Handle user change', 'readonly', txn => {
12119
12212
  // Swap out the mutation queue, grabbing the pending mutation batches
12120
12213
  // before and after.
@@ -12123,11 +12216,8 @@ async function localStoreHandleUserChange(localStore, user) {
12123
12216
  .getAllMutationBatches(txn)
12124
12217
  .next(promisedOldBatches => {
12125
12218
  oldBatches = promisedOldBatches;
12126
- newMutationQueue = localStoreImpl.persistence.getMutationQueue(user);
12127
- // Recreate our LocalDocumentsView using the new
12128
- // MutationQueue.
12129
- newLocalDocuments = new LocalDocumentsView(localStoreImpl.remoteDocuments, newMutationQueue, localStoreImpl.persistence.getIndexManager());
12130
- return newMutationQueue.getAllMutationBatches(txn);
12219
+ localStoreImpl.initializeUserComponents(user);
12220
+ return localStoreImpl.mutationQueue.getAllMutationBatches(txn);
12131
12221
  })
12132
12222
  .next(newBatches => {
12133
12223
  const removedBatchIds = [];
@@ -12148,7 +12238,7 @@ async function localStoreHandleUserChange(localStore, user) {
12148
12238
  }
12149
12239
  // Return the set of all (potentially) changed documents and the list
12150
12240
  // of mutation batch IDs that were affected by change.
12151
- return newLocalDocuments
12241
+ return localStoreImpl.localDocuments
12152
12242
  .getDocuments(txn, changedKeys)
12153
12243
  .next(affectedDocuments => {
12154
12244
  return {
@@ -12159,9 +12249,6 @@ async function localStoreHandleUserChange(localStore, user) {
12159
12249
  });
12160
12250
  });
12161
12251
  });
12162
- localStoreImpl.mutationQueue = newMutationQueue;
12163
- localStoreImpl.localDocuments = newLocalDocuments;
12164
- localStoreImpl.queryEngine.setLocalDocumentsView(localStoreImpl.localDocuments);
12165
12252
  return result;
12166
12253
  }
12167
12254
  /* Accepts locally generated Mutations and commit them to storage. */
@@ -13227,14 +13314,16 @@ class MemoryRemoteDocumentCacheImpl {
13227
13314
  * expected to just return 0 to avoid unnecessarily doing the work of
13228
13315
  * calculating the size.
13229
13316
  */
13230
- constructor(indexManager, sizer) {
13231
- this.indexManager = indexManager;
13317
+ constructor(sizer) {
13232
13318
  this.sizer = sizer;
13233
13319
  /** Underlying cache of documents and their read times. */
13234
13320
  this.docs = documentEntryMap();
13235
13321
  /** Size of all cached documents. */
13236
13322
  this.size = 0;
13237
13323
  }
13324
+ setIndexManager(indexManager) {
13325
+ this.indexManager = indexManager;
13326
+ }
13238
13327
  /**
13239
13328
  * Adds the supplied entry to the cache and updates the cache size as appropriate.
13240
13329
  *
@@ -13282,21 +13371,22 @@ class MemoryRemoteDocumentCacheImpl {
13282
13371
  });
13283
13372
  return PersistencePromise.resolve(results);
13284
13373
  }
13285
- getDocumentsMatchingQuery(transaction, query, sinceReadTime) {
13374
+ getAll(transaction, collectionPath, sinceReadTime) {
13286
13375
  let results = mutableDocumentMap();
13287
13376
  // Documents are ordered by key, so we can use a prefix scan to narrow down
13288
13377
  // the documents we need to match the query against.
13289
- const prefix = new DocumentKey(query.path.child(''));
13378
+ const prefix = new DocumentKey(collectionPath.child(''));
13290
13379
  const iterator = this.docs.getIteratorFrom(prefix);
13291
13380
  while (iterator.hasNext()) {
13292
13381
  const { key, value: { document } } = iterator.getNext();
13293
- if (!query.path.isPrefixOf(key.path)) {
13382
+ if (!collectionPath.isPrefixOf(key.path)) {
13294
13383
  break;
13295
13384
  }
13296
- if (document.readTime.compareTo(sinceReadTime) <= 0) {
13385
+ if (key.path.length > collectionPath.length + 1) {
13386
+ // Exclude entries from subcollections.
13297
13387
  continue;
13298
13388
  }
13299
- if (!queryMatches(query, document)) {
13389
+ if (document.readTime.compareTo(sinceReadTime) <= 0) {
13300
13390
  continue;
13301
13391
  }
13302
13392
  results = results.insert(document.key, document.mutableCopy());
@@ -13318,13 +13408,12 @@ class MemoryRemoteDocumentCacheImpl {
13318
13408
  /**
13319
13409
  * Creates a new memory-only RemoteDocumentCache.
13320
13410
  *
13321
- * @param indexManager - A class that manages collection group indices.
13322
13411
  * @param sizer - Used to assess the size of a document. For eager GC, this is
13323
13412
  * expected to just return 0 to avoid unnecessarily doing the work of
13324
13413
  * calculating the size.
13325
13414
  */
13326
- function newMemoryRemoteDocumentCache(indexManager, sizer) {
13327
- return new MemoryRemoteDocumentCacheImpl(indexManager, sizer);
13415
+ function newMemoryRemoteDocumentCache(sizer) {
13416
+ return new MemoryRemoteDocumentCacheImpl(sizer);
13328
13417
  }
13329
13418
  /**
13330
13419
  * Handles the details of adding and updating documents in the MemoryRemoteDocumentCache.
@@ -13525,7 +13614,7 @@ class MemoryPersistence {
13525
13614
  this.targetCache = new MemoryTargetCache(this);
13526
13615
  const sizer = (doc) => this.referenceDelegate.documentSize(doc);
13527
13616
  this.indexManager = new MemoryIndexManager();
13528
- this.remoteDocumentCache = newMemoryRemoteDocumentCache(this.indexManager, sizer);
13617
+ this.remoteDocumentCache = newMemoryRemoteDocumentCache(sizer);
13529
13618
  this.serializer = new LocalSerializer(serializer);
13530
13619
  this.bundleCache = new MemoryBundleCache(this.serializer);
13531
13620
  }
@@ -13546,13 +13635,15 @@ class MemoryPersistence {
13546
13635
  setNetworkEnabled() {
13547
13636
  // No op.
13548
13637
  }
13549
- getIndexManager() {
13638
+ getIndexManager(user) {
13639
+ // We do not currently support indices for memory persistence, so we can
13640
+ // return the same shared instance of the memory index manager.
13550
13641
  return this.indexManager;
13551
13642
  }
13552
- getMutationQueue(user) {
13643
+ getMutationQueue(user, indexManager) {
13553
13644
  let queue = this.mutationQueues[user.toKey()];
13554
13645
  if (!queue) {
13555
- queue = new MemoryMutationQueue(this.indexManager, this.referenceDelegate);
13646
+ queue = new MemoryMutationQueue(indexManager, this.referenceDelegate);
13556
13647
  this.mutationQueues[user.toKey()] = queue;
13557
13648
  }
13558
13649
  return queue;
@@ -20515,12 +20606,12 @@ async function syncEngineListen(syncEngine, query) {
20515
20606
  }
20516
20607
  else {
20517
20608
  const targetData = await localStoreAllocateTarget(syncEngineImpl.localStore, queryToTarget(query));
20518
- const status = syncEngineImpl.sharedClientState.addLocalQueryTarget(targetData.targetId);
20519
- targetId = targetData.targetId;
20520
- viewSnapshot = await initializeViewAndComputeSnapshot(syncEngineImpl, query, targetId, status === 'current');
20521
20609
  if (syncEngineImpl.isPrimaryClient) {
20522
20610
  remoteStoreListen(syncEngineImpl.remoteStore, targetData);
20523
20611
  }
20612
+ const status = syncEngineImpl.sharedClientState.addLocalQueryTarget(targetData.targetId);
20613
+ targetId = targetData.targetId;
20614
+ viewSnapshot = await initializeViewAndComputeSnapshot(syncEngineImpl, query, targetId, status === 'current');
20524
20615
  }
20525
20616
  return viewSnapshot;
20526
20617
  }