| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- /*
- * 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.
- */
- #import "Firestore/Source/Model/FSTMutationBatch.h"
- #include <algorithm>
- #include <utility>
- #import "FIRTimestamp.h"
- #import "Firestore/Source/Model/FSTDocument.h"
- #import "Firestore/Source/Model/FSTMutation.h"
- #include "Firestore/core/src/firebase/firestore/model/document_map.h"
- #include "Firestore/core/src/firebase/firestore/objc/objc_compatibility.h"
- #include "Firestore/core/src/firebase/firestore/timestamp_internal.h"
- #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
- #include "Firestore/core/src/firebase/firestore/util/hashing.h"
- namespace objc = firebase::firestore::objc;
- using firebase::Timestamp;
- using firebase::TimestampInternal;
- using firebase::firestore::model::BatchId;
- using firebase::firestore::model::DocumentKey;
- using firebase::firestore::model::DocumentKeyHash;
- using firebase::firestore::model::DocumentKeySet;
- using firebase::firestore::model::DocumentVersionMap;
- using firebase::firestore::model::MaybeDocumentMap;
- using firebase::firestore::model::SnapshotVersion;
- using firebase::firestore::util::Hash;
- NS_ASSUME_NONNULL_BEGIN
- @implementation FSTMutationBatch {
- Timestamp _localWriteTime;
- std::vector<FSTMutation *> _baseMutations;
- std::vector<FSTMutation *> _mutations;
- }
- - (instancetype)initWithBatchID:(BatchId)batchID
- localWriteTime:(const Timestamp &)localWriteTime
- baseMutations:(std::vector<FSTMutation *> &&)baseMutations
- mutations:(std::vector<FSTMutation *> &&)mutations {
- HARD_ASSERT(!mutations.empty(), "Cannot create an empty mutation batch");
- self = [super init];
- if (self) {
- _batchID = batchID;
- _localWriteTime = localWriteTime;
- _baseMutations = std::move(baseMutations);
- _mutations = std::move(mutations);
- }
- return self;
- }
- - (const std::vector<FSTMutation *> &)baseMutations {
- return _baseMutations;
- }
- - (const std::vector<FSTMutation *> &)mutations {
- return _mutations;
- }
- - (BOOL)isEqual:(id)other {
- if (self == other) {
- return YES;
- } else if (![other isKindOfClass:[FSTMutationBatch class]]) {
- return NO;
- }
- FSTMutationBatch *otherBatch = (FSTMutationBatch *)other;
- return self.batchID == otherBatch.batchID && self.localWriteTime == otherBatch.localWriteTime &&
- objc::Equals(_baseMutations, otherBatch.baseMutations) &&
- objc::Equals(_mutations, otherBatch.mutations);
- }
- - (NSUInteger)hash {
- NSUInteger result = (NSUInteger)self.batchID;
- result = result * 31 + TimestampInternal::Hash(self.localWriteTime);
- for (FSTMutation *mutation : _baseMutations) {
- result = result * 31 + [mutation hash];
- }
- for (FSTMutation *mutation : _mutations) {
- result = result * 31 + [mutation hash];
- }
- return result;
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"<FSTMutationBatch: id=%d, localWriteTime=%s, mutations=%@>",
- self.batchID, self.localWriteTime.ToString().c_str(),
- objc::Description(_mutations)];
- }
- - (FSTMaybeDocument *_Nullable)applyToRemoteDocument:(FSTMaybeDocument *_Nullable)maybeDoc
- documentKey:(const DocumentKey &)documentKey
- mutationBatchResult:
- (FSTMutationBatchResult *_Nullable)mutationBatchResult {
- HARD_ASSERT(!maybeDoc || maybeDoc.key == documentKey,
- "applyTo: key %s doesn't match maybeDoc key %s", documentKey.ToString(),
- maybeDoc.key.ToString());
- HARD_ASSERT(mutationBatchResult.mutationResults.size() == _mutations.size(),
- "Mismatch between mutations length (%s) and results length (%s)", _mutations.size(),
- mutationBatchResult.mutationResults.size());
- for (size_t i = 0; i < _mutations.size(); i++) {
- FSTMutation *mutation = _mutations[i];
- FSTMutationResult *mutationResult = mutationBatchResult.mutationResults[i];
- if (mutation.key == documentKey) {
- maybeDoc = [mutation applyToRemoteDocument:maybeDoc mutationResult:mutationResult];
- }
- }
- return maybeDoc;
- }
- - (FSTMaybeDocument *_Nullable)applyToLocalDocument:(FSTMaybeDocument *_Nullable)maybeDoc
- documentKey:(const DocumentKey &)documentKey {
- HARD_ASSERT(!maybeDoc || maybeDoc.key == documentKey,
- "applyTo: key %s doesn't match maybeDoc key %s", documentKey.ToString(),
- maybeDoc.key.ToString());
- // First, apply the base state. This allows us to apply non-idempotent transform against a
- // consistent set of values.
- for (FSTMutation *mutation : _baseMutations) {
- if (mutation.key == documentKey) {
- maybeDoc = [mutation applyToLocalDocument:maybeDoc
- baseDocument:maybeDoc
- localWriteTime:self.localWriteTime];
- }
- }
- FSTMaybeDocument *baseDoc = maybeDoc;
- // Second, apply all user-provided mutations.
- for (FSTMutation *mutation : _mutations) {
- if (mutation.key == documentKey) {
- maybeDoc = [mutation applyToLocalDocument:maybeDoc
- baseDocument:baseDoc
- localWriteTime:self.localWriteTime];
- }
- }
- return maybeDoc;
- }
- - (MaybeDocumentMap)applyToLocalDocumentSet:(const MaybeDocumentMap &)documentSet {
- // TODO(mrschmidt): This implementation is O(n^2). If we iterate through the mutations first (as
- // done in `applyToLocalDocument:documentKey:`), we can reduce the complexity to O(n).
- MaybeDocumentMap mutatedDocuments = documentSet;
- for (FSTMutation *mutation : _mutations) {
- const DocumentKey &key = mutation.key;
- auto maybeDocument = mutatedDocuments.find(key);
- FSTMaybeDocument *mutatedDocument = [self
- applyToLocalDocument:(maybeDocument != mutatedDocuments.end() ? maybeDocument->second : nil)
- documentKey:key];
- if (mutatedDocument) {
- mutatedDocuments = mutatedDocuments.insert(key, mutatedDocument);
- }
- }
- return mutatedDocuments;
- }
- - (DocumentKeySet)keys {
- DocumentKeySet set;
- for (FSTMutation *mutation : _mutations) {
- set = set.insert(mutation.key);
- }
- return set;
- }
- @end
- #pragma mark - FSTMutationBatchResult
- @interface FSTMutationBatchResult ()
- - (instancetype)initWithBatch:(FSTMutationBatch *)batch
- commitVersion:(SnapshotVersion)commitVersion
- mutationResults:(std::vector<FSTMutationResult *>)mutationResults
- streamToken:(nullable NSData *)streamToken
- docVersions:(DocumentVersionMap)docVersions NS_DESIGNATED_INITIALIZER;
- @end
- @implementation FSTMutationBatchResult {
- SnapshotVersion _commitVersion;
- std::vector<FSTMutationResult *> _mutationResults;
- DocumentVersionMap _docVersions;
- }
- - (instancetype)initWithBatch:(FSTMutationBatch *)batch
- commitVersion:(SnapshotVersion)commitVersion
- mutationResults:(std::vector<FSTMutationResult *>)mutationResults
- streamToken:(nullable NSData *)streamToken
- docVersions:(DocumentVersionMap)docVersions {
- if (self = [super init]) {
- _batch = batch;
- _commitVersion = std::move(commitVersion);
- _mutationResults = std::move(mutationResults);
- _streamToken = streamToken;
- _docVersions = std::move(docVersions);
- }
- return self;
- }
- - (const SnapshotVersion &)commitVersion {
- return _commitVersion;
- }
- - (const std::vector<FSTMutationResult *> &)mutationResults {
- return _mutationResults;
- }
- - (const DocumentVersionMap &)docVersions {
- return _docVersions;
- }
- + (instancetype)resultWithBatch:(FSTMutationBatch *)batch
- commitVersion:(SnapshotVersion)commitVersion
- mutationResults:(std::vector<FSTMutationResult *>)mutationResults
- streamToken:(nullable NSData *)streamToken {
- HARD_ASSERT(batch.mutations.size() == mutationResults.size(),
- "Mutations sent %s must equal results received %s", batch.mutations.size(),
- mutationResults.size());
- DocumentVersionMap docVersions;
- std::vector<FSTMutation *> mutations = batch.mutations;
- for (size_t i = 0; i < mutations.size(); i++) {
- absl::optional<SnapshotVersion> version = mutationResults[i].version;
- if (!version) {
- // deletes don't have a version, so we substitute the commitVersion
- // of the entire batch.
- version = commitVersion;
- }
- docVersions[mutations[i].key] = version.value();
- }
- return [[FSTMutationBatchResult alloc] initWithBatch:batch
- commitVersion:std::move(commitVersion)
- mutationResults:std::move(mutationResults)
- streamToken:streamToken
- docVersions:std::move(docVersions)];
- }
- @end
- NS_ASSUME_NONNULL_END
|