2017-10-03 08:55:22 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Source/Local/FSTLocalDocumentsView.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Source/Core/FSTQuery.h"
|
|
|
|
|
#import "Firestore/Source/Local/FSTMutationQueue.h"
|
|
|
|
|
#import "Firestore/Source/Local/FSTRemoteDocumentCache.h"
|
|
|
|
|
#import "Firestore/Source/Model/FSTDocument.h"
|
|
|
|
|
#import "Firestore/Source/Model/FSTDocumentDictionary.h"
|
|
|
|
|
#import "Firestore/Source/Model/FSTMutation.h"
|
|
|
|
|
#import "Firestore/Source/Model/FSTMutationBatch.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2018-03-23 17:52:01 -04:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
|
2018-03-14 08:57:31 -04:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
|
2018-04-25 14:05:36 -04:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
|
2018-05-24 11:00:23 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
|
2018-03-14 08:57:31 -04:00
|
|
|
|
2018-03-23 17:52:01 -04:00
|
|
|
using firebase::firestore::model::DocumentKey;
|
2018-03-14 08:57:31 -04:00
|
|
|
using firebase::firestore::model::ResourcePath;
|
2018-04-25 14:05:36 -04:00
|
|
|
using firebase::firestore::model::SnapshotVersion;
|
2018-05-05 08:10:51 -07:00
|
|
|
using firebase::firestore::model::DocumentKeySet;
|
2018-03-14 08:57:31 -04:00
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@interface FSTLocalDocumentsView ()
|
|
|
|
|
- (instancetype)initWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
|
|
|
|
|
mutationQueue:(id<FSTMutationQueue>)mutationQueue
|
|
|
|
|
NS_DESIGNATED_INITIALIZER;
|
|
|
|
|
@property(nonatomic, strong, readonly) id<FSTRemoteDocumentCache> remoteDocumentCache;
|
|
|
|
|
@property(nonatomic, strong, readonly) id<FSTMutationQueue> mutationQueue;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FSTLocalDocumentsView
|
|
|
|
|
|
|
|
|
|
+ (instancetype)viewWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
|
|
|
|
|
mutationQueue:(id<FSTMutationQueue>)mutationQueue {
|
|
|
|
|
return [[FSTLocalDocumentsView alloc] initWithRemoteDocumentCache:remoteDocumentCache
|
|
|
|
|
mutationQueue:mutationQueue];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithRemoteDocumentCache:(id<FSTRemoteDocumentCache>)remoteDocumentCache
|
|
|
|
|
mutationQueue:(id<FSTMutationQueue>)mutationQueue {
|
|
|
|
|
if (self = [super init]) {
|
|
|
|
|
_remoteDocumentCache = remoteDocumentCache;
|
|
|
|
|
_mutationQueue = mutationQueue;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-23 17:52:01 -04:00
|
|
|
- (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key {
|
2018-07-10 13:59:25 -04:00
|
|
|
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 {
|
2018-07-16 18:20:00 -04:00
|
|
|
FSTMaybeDocument *_Nullable document = [self.remoteDocumentCache entryForKey:key];
|
|
|
|
|
for (FSTMutationBatch *batch in batches) {
|
|
|
|
|
document = [batch applyTo:document documentKey:key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return document;
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-05 08:10:51 -07:00
|
|
|
- (FSTMaybeDocumentDictionary *)documentsForKeys:(const DocumentKeySet &)keys {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTMaybeDocumentDictionary *results = [FSTMaybeDocumentDictionary maybeDocumentDictionary];
|
2018-07-10 13:59:25 -04:00
|
|
|
NSArray<FSTMutationBatch *> *batches =
|
|
|
|
|
[self.mutationQueue allMutationBatchesAffectingDocumentKeys:keys];
|
2018-05-05 08:10:51 -07:00
|
|
|
for (const DocumentKey &key : keys) {
|
2017-10-03 08:55:22 -07:00
|
|
|
// TODO(mikelehen): PERF: Consider fetching all remote documents at once rather than one-by-one.
|
2018-07-10 13:59:25 -04:00
|
|
|
FSTMaybeDocument *maybeDoc = [self documentForKey:key inBatches:batches];
|
2017-10-03 08:55:22 -07:00
|
|
|
// TODO(http://b/32275378): Don't conflate missing / deleted.
|
|
|
|
|
if (!maybeDoc) {
|
2018-04-25 14:05:36 -04:00
|
|
|
maybeDoc = [FSTDeletedDocument documentWithKey:key version:SnapshotVersion::None()];
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
results = [results dictionaryBySettingObject:maybeDoc forKey:key];
|
|
|
|
|
}
|
2018-07-10 13:59:25 -04:00
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query {
|
2018-03-23 17:52:01 -04:00
|
|
|
if (DocumentKey::IsDocumentKey(query.path)) {
|
2018-03-14 08:57:31 -04:00
|
|
|
return [self documentsMatchingDocumentQuery:query.path];
|
2017-10-03 08:55:22 -07:00
|
|
|
} else {
|
|
|
|
|
return [self documentsMatchingCollectionQuery:query];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 08:57:31 -04:00
|
|
|
- (FSTDocumentDictionary *)documentsMatchingDocumentQuery:(const ResourcePath &)docPath {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTDocumentDictionary *result = [FSTDocumentDictionary documentDictionary];
|
|
|
|
|
// Just do a simple document lookup.
|
2018-03-23 17:52:01 -04:00
|
|
|
FSTMaybeDocument *doc = [self documentForKey:DocumentKey{docPath}];
|
2017-10-03 08:55:22 -07:00
|
|
|
if ([doc isKindOfClass:[FSTDocument class]]) {
|
|
|
|
|
result = [result dictionaryBySettingObject:(FSTDocument *)doc forKey:doc.key];
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (FSTDocumentDictionary *)documentsMatchingCollectionQuery:(FSTQuery *)query {
|
|
|
|
|
__block FSTDocumentDictionary *results = [self.remoteDocumentCache documentsMatchingQuery:query];
|
2018-07-16 18:20:00 -04:00
|
|
|
// Get locally persisted mutation batches.
|
|
|
|
|
NSArray<FSTMutationBatch *> *matchingBatches =
|
2017-10-03 08:55:22 -07:00
|
|
|
[self.mutationQueue allMutationBatchesAffectingQuery:query];
|
|
|
|
|
|
2018-07-16 18:20:00 -04:00
|
|
|
for (FSTMutationBatch *batch in matchingBatches) {
|
|
|
|
|
for (FSTMutation *mutation in batch.mutations) {
|
|
|
|
|
// Only process documents belonging to the collection.
|
|
|
|
|
if (!query.path.IsImmediateParentOf(mutation.key.path())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FSTDocumentKey *key = static_cast<FSTDocumentKey *>(mutation.key);
|
|
|
|
|
// baseDoc may be nil for the documents that weren't yet written to the backend.
|
|
|
|
|
FSTMaybeDocument *baseDoc = results[key];
|
|
|
|
|
FSTMaybeDocument *mutatedDoc =
|
|
|
|
|
[mutation applyTo:baseDoc baseDocument:baseDoc localWriteTime:batch.localWriteTime];
|
|
|
|
|
|
2018-07-18 14:55:10 -07:00
|
|
|
if (!mutatedDoc || [mutatedDoc isKindOfClass:[FSTDeletedDocument class]]) {
|
2018-07-16 18:20:00 -04:00
|
|
|
results = [results dictionaryByRemovingObjectForKey:key];
|
|
|
|
|
} else if ([mutatedDoc isKindOfClass:[FSTDocument class]]) {
|
|
|
|
|
results = [results dictionaryBySettingObject:(FSTDocument *)mutatedDoc forKey:key];
|
|
|
|
|
} else {
|
|
|
|
|
HARD_FAIL("Unknown document: %s", mutatedDoc);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
// enumerating them.
|
|
|
|
|
FSTDocumentDictionary *unfiltered = results;
|
|
|
|
|
[unfiltered
|
|
|
|
|
enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTDocument *doc, BOOL *stop) {
|
|
|
|
|
if (![query matchesDocument:doc]) {
|
|
|
|
|
results = [results dictionaryByRemovingObjectForKey:key];
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|