FSTLocalDocumentsView.mm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/Core/FSTSnapshotVersion.h"
  19. #import "Firestore/Source/Local/FSTMutationQueue.h"
  20. #import "Firestore/Source/Local/FSTRemoteDocumentCache.h"
  21. #import "Firestore/Source/Model/FSTDocument.h"
  22. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  23. #import "Firestore/Source/Model/FSTMutation.h"
  24. #import "Firestore/Source/Model/FSTMutationBatch.h"
  25. #import "Firestore/Source/Util/FSTAssert.h"
  26. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  27. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  28. using firebase::firestore::model::DocumentKey;
  29. using firebase::firestore::model::ResourcePath;
  30. NS_ASSUME_NONNULL_BEGIN
  31. @interface FSTLocalDocumentsView ()
  32. - (instancetype)initWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
  33. mutationQueue:(id<FSTMutationQueue>)mutationQueue
  34. NS_DESIGNATED_INITIALIZER;
  35. @property(nonatomic, strong, readonly) id<FSTRemoteDocumentCache> remoteDocumentCache;
  36. @property(nonatomic, strong, readonly) id<FSTMutationQueue> mutationQueue;
  37. @end
  38. @implementation FSTLocalDocumentsView
  39. + (instancetype)viewWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
  40. mutationQueue:(id<FSTMutationQueue>)mutationQueue {
  41. return [[FSTLocalDocumentsView alloc] initWithRemoteDocumentCache:remoteDocumentCache
  42. mutationQueue:mutationQueue];
  43. }
  44. - (instancetype)initWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
  45. mutationQueue:(id<FSTMutationQueue>)mutationQueue {
  46. if (self = [super init]) {
  47. _remoteDocumentCache = remoteDocumentCache;
  48. _mutationQueue = mutationQueue;
  49. }
  50. return self;
  51. }
  52. - (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key {
  53. FSTMaybeDocument *_Nullable remoteDoc = [self.remoteDocumentCache entryForKey:key];
  54. return [self localDocument:remoteDoc key:key];
  55. }
  56. - (FSTMaybeDocumentDictionary *)documentsForKeys:(FSTDocumentKeySet *)keys {
  57. FSTMaybeDocumentDictionary *results = [FSTMaybeDocumentDictionary maybeDocumentDictionary];
  58. for (FSTDocumentKey *key in keys.objectEnumerator) {
  59. // TODO(mikelehen): PERF: Consider fetching all remote documents at once rather than one-by-one.
  60. FSTMaybeDocument *maybeDoc = [self documentForKey:key];
  61. // TODO(http://b/32275378): Don't conflate missing / deleted.
  62. if (!maybeDoc) {
  63. maybeDoc = [FSTDeletedDocument documentWithKey:key version:[FSTSnapshotVersion noVersion]];
  64. }
  65. results = [results dictionaryBySettingObject:maybeDoc forKey:key];
  66. }
  67. return results;
  68. }
  69. - (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query {
  70. if (DocumentKey::IsDocumentKey(query.path)) {
  71. return [self documentsMatchingDocumentQuery:query.path];
  72. } else {
  73. return [self documentsMatchingCollectionQuery:query];
  74. }
  75. }
  76. - (FSTDocumentDictionary *)documentsMatchingDocumentQuery:(const ResourcePath &)docPath {
  77. FSTDocumentDictionary *result = [FSTDocumentDictionary documentDictionary];
  78. // Just do a simple document lookup.
  79. FSTMaybeDocument *doc = [self documentForKey:DocumentKey{docPath}];
  80. if ([doc isKindOfClass:[FSTDocument class]]) {
  81. result = [result dictionaryBySettingObject:(FSTDocument *)doc forKey:doc.key];
  82. }
  83. return result;
  84. }
  85. - (FSTDocumentDictionary *)documentsMatchingCollectionQuery:(FSTQuery *)query {
  86. // Query the remote documents and overlay mutations.
  87. // TODO(mikelehen): There may be significant overlap between the mutations affecting these
  88. // remote documents and the allMutationBatchesAffectingQuery mutations. Consider optimizing.
  89. __block FSTDocumentDictionary *results = [self.remoteDocumentCache documentsMatchingQuery:query];
  90. results = [self localDocuments:results];
  91. // Now use the mutation queue to discover any other documents that may match the query after
  92. // applying mutations.
  93. FSTDocumentKeySet *matchingKeys = [FSTDocumentKeySet keySet];
  94. NSArray<FSTMutationBatch *> *matchingMutationBatches =
  95. [self.mutationQueue allMutationBatchesAffectingQuery:query];
  96. for (FSTMutationBatch *batch in matchingMutationBatches) {
  97. for (FSTMutation *mutation in batch.mutations) {
  98. // TODO(mikelehen): PERF: Check if this mutation actually affects the query to reduce work.
  99. // If the key is already in the results, we can skip it.
  100. if (![results containsKey:mutation.key]) {
  101. matchingKeys = [matchingKeys setByAddingObject:mutation.key];
  102. }
  103. }
  104. }
  105. // Now add in results for the matchingKeys.
  106. for (FSTDocumentKey *key in matchingKeys.objectEnumerator) {
  107. FSTMaybeDocument *doc = [self documentForKey:key];
  108. if ([doc isKindOfClass:[FSTDocument class]]) {
  109. results = [results dictionaryBySettingObject:(FSTDocument *)doc forKey:key];
  110. }
  111. }
  112. // Finally, filter out any documents that don't actually match the query. Note that the extra
  113. // reference here prevents ARC from deallocating the initial unfiltered results while we're
  114. // enumerating them.
  115. FSTDocumentDictionary *unfiltered = results;
  116. [unfiltered
  117. enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTDocument *doc, BOOL *stop) {
  118. if (![query matchesDocument:doc]) {
  119. results = [results dictionaryByRemovingObjectForKey:key];
  120. }
  121. }];
  122. return results;
  123. }
  124. /**
  125. * Takes a remote document and applies local mutations to generate the local view of the
  126. * document.
  127. *
  128. * @param document The base remote document to apply mutations to.
  129. * @param documentKey The key of the document (necessary when remoteDocument is nil).
  130. */
  131. - (nullable FSTMaybeDocument *)localDocument:(nullable FSTMaybeDocument *)document
  132. key:(const DocumentKey &)documentKey {
  133. NSArray<FSTMutationBatch *> *batches =
  134. [self.mutationQueue allMutationBatchesAffectingDocumentKey:documentKey];
  135. for (FSTMutationBatch *batch in batches) {
  136. document = [batch applyTo:document documentKey:documentKey];
  137. }
  138. return document;
  139. }
  140. /**
  141. * Takes a set of remote documents and applies local mutations to generate the local view of
  142. * the documents.
  143. *
  144. * @param documents The base remote documents to apply mutations to.
  145. * @return The local view of the documents.
  146. */
  147. - (FSTDocumentDictionary *)localDocuments:(FSTDocumentDictionary *)documents {
  148. __block FSTDocumentDictionary *result = documents;
  149. [documents enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTDocument *remoteDocument,
  150. BOOL *stop) {
  151. FSTMaybeDocument *mutatedDoc = [self localDocument:remoteDocument key:key];
  152. if ([mutatedDoc isKindOfClass:[FSTDeletedDocument class]]) {
  153. result = [result dictionaryByRemovingObjectForKey:key];
  154. } else if ([mutatedDoc isKindOfClass:[FSTDocument class]]) {
  155. result = [result dictionaryBySettingObject:(FSTDocument *)mutatedDoc forKey:key];
  156. } else {
  157. FSTFail(@"Unknown document: %@", mutatedDoc);
  158. }
  159. }];
  160. return result;
  161. }
  162. @end
  163. NS_ASSUME_NONNULL_END