FSTLocalDocumentsView.m 7.4 KB

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