FSTLocalDocumentsView.mm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 applyTo: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 version:SnapshotVersion::None()];
  78. }
  79. results = [results dictionaryBySettingObject:maybeDoc forKey:key];
  80. }
  81. return results;
  82. }
  83. - (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query {
  84. if (DocumentKey::IsDocumentKey(query.path)) {
  85. return [self documentsMatchingDocumentQuery:query.path];
  86. } else {
  87. return [self documentsMatchingCollectionQuery:query];
  88. }
  89. }
  90. - (FSTDocumentDictionary *)documentsMatchingDocumentQuery:(const ResourcePath &)docPath {
  91. FSTDocumentDictionary *result = [FSTDocumentDictionary documentDictionary];
  92. // Just do a simple document lookup.
  93. FSTMaybeDocument *doc = [self documentForKey:DocumentKey{docPath}];
  94. if ([doc isKindOfClass:[FSTDocument class]]) {
  95. result = [result dictionaryBySettingObject:(FSTDocument *)doc forKey:doc.key];
  96. }
  97. return result;
  98. }
  99. - (FSTDocumentDictionary *)documentsMatchingCollectionQuery:(FSTQuery *)query {
  100. __block FSTDocumentDictionary *results = [self.remoteDocumentCache documentsMatchingQuery:query];
  101. // Get locally persisted mutation batches.
  102. NSArray<FSTMutationBatch *> *matchingBatches =
  103. [self.mutationQueue allMutationBatchesAffectingQuery:query];
  104. for (FSTMutationBatch *batch in matchingBatches) {
  105. for (FSTMutation *mutation in batch.mutations) {
  106. // Only process documents belonging to the collection.
  107. if (!query.path.IsImmediateParentOf(mutation.key.path())) {
  108. continue;
  109. }
  110. FSTDocumentKey *key = static_cast<FSTDocumentKey *>(mutation.key);
  111. // baseDoc may be nil for the documents that weren't yet written to the backend.
  112. FSTMaybeDocument *baseDoc = results[key];
  113. FSTMaybeDocument *mutatedDoc =
  114. [mutation applyTo:baseDoc baseDocument:baseDoc localWriteTime:batch.localWriteTime];
  115. if (!mutatedDoc || [mutatedDoc isKindOfClass:[FSTDeletedDocument class]]) {
  116. results = [results dictionaryByRemovingObjectForKey:key];
  117. } else if ([mutatedDoc isKindOfClass:[FSTDocument class]]) {
  118. results = [results dictionaryBySettingObject:(FSTDocument *)mutatedDoc forKey:key];
  119. } else {
  120. HARD_FAIL("Unknown document: %s", mutatedDoc);
  121. }
  122. }
  123. }
  124. // Finally, filter out any documents that don't actually match the query. Note that the extra
  125. // reference here prevents ARC from deallocating the initial unfiltered results while we're
  126. // enumerating them.
  127. FSTDocumentDictionary *unfiltered = results;
  128. [unfiltered
  129. enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTDocument *doc, BOOL *stop) {
  130. if (![query matchesDocument:doc]) {
  131. results = [results dictionaryByRemovingObjectForKey:key];
  132. }
  133. }];
  134. return results;
  135. }
  136. @end
  137. NS_ASSUME_NONNULL_END