FSTLocalDocumentsView.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Local/FSTLocalDocumentsView.h"
  17. #import "Firestore/Source/Core/FSTQuery.h"
  18. #import "Firestore/Source/Local/FSTMutationQueue.h"
  19. #import "Firestore/Source/Local/FSTRemoteDocumentCache.h"
  20. #import "Firestore/Source/Model/FSTDocument.h"
  21. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  22. #import "Firestore/Source/Model/FSTMutation.h"
  23. #import "Firestore/Source/Model/FSTMutationBatch.h"
  24. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  25. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  26. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  27. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  28. using firebase::firestore::model::DocumentKey;
  29. using firebase::firestore::model::ResourcePath;
  30. using firebase::firestore::model::SnapshotVersion;
  31. using firebase::firestore::model::DocumentKeySet;
  32. NS_ASSUME_NONNULL_BEGIN
  33. @interface FSTLocalDocumentsView ()
  34. - (instancetype)initWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
  35. mutationQueue:(id<FSTMutationQueue>)mutationQueue
  36. NS_DESIGNATED_INITIALIZER;
  37. @property(nonatomic, strong, readonly) id<FSTRemoteDocumentCache> remoteDocumentCache;
  38. @property(nonatomic, strong, readonly) id<FSTMutationQueue> mutationQueue;
  39. @end
  40. @implementation FSTLocalDocumentsView
  41. + (instancetype)viewWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
  42. mutationQueue:(id<FSTMutationQueue>)mutationQueue {
  43. return [[FSTLocalDocumentsView alloc] initWithRemoteDocumentCache:remoteDocumentCache
  44. mutationQueue:mutationQueue];
  45. }
  46. - (instancetype)initWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
  47. mutationQueue:(id<FSTMutationQueue>)mutationQueue {
  48. if (self = [super init]) {
  49. _remoteDocumentCache = remoteDocumentCache;
  50. _mutationQueue = mutationQueue;
  51. }
  52. return self;
  53. }
  54. - (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key {
  55. NSArray<FSTMutationBatch *> *batches =
  56. [self.mutationQueue allMutationBatchesAffectingDocumentKey:key];
  57. return [self documentForKey:key inBatches:batches];
  58. }
  59. // Internal version of documentForKey: which allows reusing `batches`.
  60. - (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key
  61. inBatches:(NSArray<FSTMutationBatch *> *)batches {
  62. FSTMaybeDocument *_Nullable document = [self.remoteDocumentCache entryForKey:key];
  63. for (FSTMutationBatch *batch in batches) {
  64. document = [batch applyToLocalDocument:document documentKey:key];
  65. }
  66. return document;
  67. }
  68. - (FSTMaybeDocumentDictionary *)documentsForKeys:(const DocumentKeySet &)keys {
  69. FSTMaybeDocumentDictionary *results = [FSTMaybeDocumentDictionary maybeDocumentDictionary];
  70. NSArray<FSTMutationBatch *> *batches =
  71. [self.mutationQueue allMutationBatchesAffectingDocumentKeys:keys];
  72. for (const DocumentKey &key : keys) {
  73. // TODO(mikelehen): PERF: Consider fetching all remote documents at once rather than one-by-one.
  74. FSTMaybeDocument *maybeDoc = [self documentForKey:key inBatches:batches];
  75. // TODO(http://b/32275378): Don't conflate missing / deleted.
  76. if (!maybeDoc) {
  77. maybeDoc = [FSTDeletedDocument documentWithKey:key
  78. version:SnapshotVersion::None()
  79. hasCommittedMutations:NO];
  80. }
  81. results = [results dictionaryBySettingObject:maybeDoc forKey:key];
  82. }
  83. return results;
  84. }
  85. - (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query {
  86. if (DocumentKey::IsDocumentKey(query.path)) {
  87. return [self documentsMatchingDocumentQuery:query.path];
  88. } else {
  89. return [self documentsMatchingCollectionQuery:query];
  90. }
  91. }
  92. - (FSTDocumentDictionary *)documentsMatchingDocumentQuery:(const ResourcePath &)docPath {
  93. FSTDocumentDictionary *result = [FSTDocumentDictionary documentDictionary];
  94. // Just do a simple document lookup.
  95. FSTMaybeDocument *doc = [self documentForKey:DocumentKey{docPath}];
  96. if ([doc isKindOfClass:[FSTDocument class]]) {
  97. result = [result dictionaryBySettingObject:(FSTDocument *)doc forKey:doc.key];
  98. }
  99. return result;
  100. }
  101. - (FSTDocumentDictionary *)documentsMatchingCollectionQuery:(FSTQuery *)query {
  102. __block FSTDocumentDictionary *results = [self.remoteDocumentCache documentsMatchingQuery:query];
  103. // Get locally persisted mutation batches.
  104. NSArray<FSTMutationBatch *> *matchingBatches =
  105. [self.mutationQueue allMutationBatchesAffectingQuery:query];
  106. for (FSTMutationBatch *batch in matchingBatches) {
  107. for (FSTMutation *mutation in batch.mutations) {
  108. // Only process documents belonging to the collection.
  109. if (!query.path.IsImmediateParentOf(mutation.key.path())) {
  110. continue;
  111. }
  112. FSTDocumentKey *key = static_cast<FSTDocumentKey *>(mutation.key);
  113. // baseDoc may be nil for the documents that weren't yet written to the backend.
  114. FSTMaybeDocument *baseDoc = results[key];
  115. FSTMaybeDocument *mutatedDoc = [mutation applyToLocalDocument:baseDoc
  116. baseDocument:baseDoc
  117. localWriteTime:batch.localWriteTime];
  118. if ([mutatedDoc isKindOfClass:[FSTDocument class]]) {
  119. results = [results dictionaryBySettingObject:(FSTDocument *)mutatedDoc forKey:key];
  120. } else {
  121. results = [results dictionaryByRemovingObjectForKey:key];
  122. }
  123. }
  124. }
  125. // Finally, filter out any documents that don't actually match the query. Note that the extra
  126. // reference here prevents ARC from deallocating the initial unfiltered results while we're
  127. // enumerating them.
  128. FSTDocumentDictionary *unfiltered = results;
  129. [unfiltered
  130. enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTDocument *doc, BOOL *stop) {
  131. if (![query matchesDocument:doc]) {
  132. results = [results dictionaryByRemovingObjectForKey:key];
  133. }
  134. }];
  135. return results;
  136. }
  137. @end
  138. NS_ASSUME_NONNULL_END