FSTLocalDocumentsView.mm 7.5 KB

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