diff options
author | Konstantin Varlamov <var-const@users.noreply.github.com> | 2018-07-10 13:59:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-10 13:59:25 -0400 |
commit | ff95ffc61b6c6cf4b8ec69183d35e08497fbfd1a (patch) | |
tree | f70e21a1f04c5490cd5293091e0f24cd7f685bd2 /Firestore/Source | |
parent | 6d6ed82d4200125a0642ffc513cb6fe4e3c1b5d3 (diff) |
In FSTLocalDocumentsView, search for all batches affecting a set of keys in one go (#1505)
This uses the newly-added allMutationBatchesAffectingDocumentKeys to find/deserialize all such batches in one go and then reuse the batches while processing a set of keys.
Diffstat (limited to 'Firestore/Source')
-rw-r--r-- | Firestore/Source/Local/FSTLocalDocumentsView.mm | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/Firestore/Source/Local/FSTLocalDocumentsView.mm b/Firestore/Source/Local/FSTLocalDocumentsView.mm index 48c963e..4337879 100644 --- a/Firestore/Source/Local/FSTLocalDocumentsView.mm +++ b/Firestore/Source/Local/FSTLocalDocumentsView.mm @@ -62,21 +62,32 @@ NS_ASSUME_NONNULL_BEGIN } - (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key { + NSArray<FSTMutationBatch *> *batches = + [self.mutationQueue allMutationBatchesAffectingDocumentKey:key]; + return [self documentForKey:key inBatches:batches]; +} + +// Internal version of documentForKey: which allows reusing `batches`. +- (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key + inBatches:(NSArray<FSTMutationBatch *> *)batches { FSTMaybeDocument *_Nullable remoteDoc = [self.remoteDocumentCache entryForKey:key]; - return [self localDocument:remoteDoc key:key]; + return [self localDocument:remoteDoc key:key inBatches:batches]; } - (FSTMaybeDocumentDictionary *)documentsForKeys:(const DocumentKeySet &)keys { FSTMaybeDocumentDictionary *results = [FSTMaybeDocumentDictionary maybeDocumentDictionary]; + NSArray<FSTMutationBatch *> *batches = + [self.mutationQueue allMutationBatchesAffectingDocumentKeys:keys]; for (const DocumentKey &key : keys) { // TODO(mikelehen): PERF: Consider fetching all remote documents at once rather than one-by-one. - FSTMaybeDocument *maybeDoc = [self documentForKey:key]; + FSTMaybeDocument *maybeDoc = [self documentForKey:key inBatches:batches]; // TODO(http://b/32275378): Don't conflate missing / deleted. if (!maybeDoc) { maybeDoc = [FSTDeletedDocument documentWithKey:key version:SnapshotVersion::None()]; } results = [results dictionaryBySettingObject:maybeDoc forKey:key]; } + return results; } @@ -122,12 +133,13 @@ NS_ASSUME_NONNULL_BEGIN } // Now add in results for the matchingKeys. - for (const DocumentKey &key : matchingKeys) { - FSTMaybeDocument *doc = [self documentForKey:key]; - if ([doc isKindOfClass:[FSTDocument class]]) { - results = [results dictionaryBySettingObject:(FSTDocument *)doc forKey:key]; - } - } + FSTMaybeDocumentDictionary *matchingKeysDocs = [self documentsForKeys:matchingKeys]; + [matchingKeysDocs + enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTMaybeDocument *doc, BOOL *stop) { + if ([doc isKindOfClass:[FSTDocument class]]) { + results = [results dictionaryBySettingObject:(FSTDocument *)doc forKey:key]; + } + }]; // Finally, filter out any documents that don't actually match the query. Note that the extra // reference here prevents ARC from deallocating the initial unfiltered results while we're @@ -151,9 +163,8 @@ NS_ASSUME_NONNULL_BEGIN * @param documentKey The key of the document (necessary when remoteDocument is nil). */ - (nullable FSTMaybeDocument *)localDocument:(nullable FSTMaybeDocument *)document - key:(const DocumentKey &)documentKey { - NSArray<FSTMutationBatch *> *batches = - [self.mutationQueue allMutationBatchesAffectingDocumentKey:documentKey]; + key:(const DocumentKey &)documentKey + inBatches:(NSArray<FSTMutationBatch *> *)batches { for (FSTMutationBatch *batch in batches) { document = [batch applyTo:document documentKey:documentKey]; } @@ -169,10 +180,18 @@ NS_ASSUME_NONNULL_BEGIN * @return The local view of the documents. */ - (FSTDocumentDictionary *)localDocuments:(FSTDocumentDictionary *)documents { + __block DocumentKeySet keySet; + [documents + enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTDocument *doc, BOOL *stop) { + keySet = keySet.insert(doc.key); + }]; + NSArray<FSTMutationBatch *> *batches = + [self.mutationQueue allMutationBatchesAffectingDocumentKeys:keySet]; + __block FSTDocumentDictionary *result = documents; [documents enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTDocument *remoteDocument, BOOL *stop) { - FSTMaybeDocument *mutatedDoc = [self localDocument:remoteDocument key:key]; + FSTMaybeDocument *mutatedDoc = [self localDocument:remoteDocument key:key inBatches:batches]; if ([mutatedDoc isKindOfClass:[FSTDeletedDocument class]]) { result = [result dictionaryByRemovingObjectForKey:key]; } else if ([mutatedDoc isKindOfClass:[FSTDocument class]]) { |