FSTMutationBatch.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <Foundation/Foundation.h>
  17. #include <unordered_map>
  18. #include <vector>
  19. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  21. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  22. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  23. #include "Firestore/core/src/firebase/firestore/model/types.h"
  24. @class FIRTimestamp;
  25. @class FSTMaybeDocument;
  26. @class FSTMutation;
  27. @class FSTMutationResult;
  28. @class FSTMutationBatchResult;
  29. namespace firebase {
  30. namespace firestore {
  31. namespace model {
  32. // TODO(wilhuff): make this type a member of MutationBatchResult once that's a C++ class.
  33. using DocumentVersionMap = std::unordered_map<DocumentKey, SnapshotVersion, DocumentKeyHash>;
  34. } // namespace model
  35. } // namespace firestore
  36. } // namespace firebase
  37. NS_ASSUME_NONNULL_BEGIN
  38. /**
  39. * A batch of mutations that will be sent as one unit to the backend. Batches can be marked as a
  40. * tombstone if the mutation queue does not remove them immediately. When a batch is a tombstone
  41. * it has no mutations.
  42. */
  43. @interface FSTMutationBatch : NSObject
  44. /**
  45. * Initializes a mutation batch with the given batchID, localWriteTime, base mutations, and
  46. * mutations.
  47. */
  48. - (instancetype)initWithBatchID:(firebase::firestore::model::BatchId)batchID
  49. localWriteTime:(FIRTimestamp *)localWriteTime
  50. baseMutations:(std::vector<FSTMutation *> &&)baseMutations
  51. mutations:(std::vector<FSTMutation *> &&)mutations NS_DESIGNATED_INITIALIZER;
  52. - (id)init NS_UNAVAILABLE;
  53. /**
  54. * Applies all the mutations in this FSTMutationBatch to the specified document to create a new
  55. * remote document.
  56. *
  57. * @param maybeDoc The document to apply mutations to.
  58. * @param documentKey The key of the document to apply mutations to.
  59. * @param mutationBatchResult The result of applying the MutationBatch to the backend. If omitted
  60. * it's assumed that this is a local (latency-compensated) application and documents will have
  61. * their hasLocalMutations flag set.
  62. */
  63. - (FSTMaybeDocument *_Nullable)
  64. applyToRemoteDocument:(FSTMaybeDocument *_Nullable)maybeDoc
  65. documentKey:(const firebase::firestore::model::DocumentKey &)documentKey
  66. mutationBatchResult:(FSTMutationBatchResult *_Nullable)mutationBatchResult;
  67. /**
  68. * A helper version of applyTo for applying mutations locally (without a mutation batch result from
  69. * the backend).
  70. */
  71. - (FSTMaybeDocument *_Nullable)
  72. applyToLocalDocument:(FSTMaybeDocument *_Nullable)maybeDoc
  73. documentKey:(const firebase::firestore::model::DocumentKey &)documentKey;
  74. /** Computes the local view for all provided documents given the mutations in this batch. */
  75. - (firebase::firestore::model::MaybeDocumentMap)applyToLocalDocumentSet:
  76. (const firebase::firestore::model::MaybeDocumentMap &)documentSet;
  77. /** Returns the set of unique keys referenced by all mutations in the batch. */
  78. - (firebase::firestore::model::DocumentKeySet)keys;
  79. /** The unique ID of this mutation batch. */
  80. @property(nonatomic, assign, readonly) firebase::firestore::model::BatchId batchID;
  81. /** The original write time of this mutation. */
  82. @property(nonatomic, strong, readonly) FIRTimestamp *localWriteTime;
  83. /**
  84. * Mutations that are used to populate the base values when this mutation is applied locally. This
  85. * can be used to locally overwrite values that are persisted in the remote document cache. Base
  86. * mutations are never sent to the backend.
  87. */
  88. - (const std::vector<FSTMutation *> &)baseMutations;
  89. /**
  90. * The user-provided mutations in this mutation batch. User-provided mutations are applied both
  91. * locally and remotely on the backend.
  92. */
  93. - (const std::vector<FSTMutation *> &)mutations;
  94. @end
  95. #pragma mark - FSTMutationBatchResult
  96. /** The result of applying a mutation batch to the backend. */
  97. @interface FSTMutationBatchResult : NSObject
  98. - (instancetype)init NS_UNAVAILABLE;
  99. /**
  100. * Creates a new FSTMutationBatchResult for the given batch and results. There must be one result
  101. * for each mutation in the batch. This static factory caches a document=>version mapping
  102. * (as docVersions).
  103. */
  104. + (instancetype)resultWithBatch:(FSTMutationBatch *)batch
  105. commitVersion:(firebase::firestore::model::SnapshotVersion)commitVersion
  106. mutationResults:(std::vector<FSTMutationResult *>)mutationResults
  107. streamToken:(nullable NSData *)streamToken;
  108. - (const firebase::firestore::model::SnapshotVersion &)commitVersion;
  109. - (const std::vector<FSTMutationResult *> &)mutationResults;
  110. @property(nonatomic, strong, readonly) FSTMutationBatch *batch;
  111. @property(nonatomic, strong, readonly, nullable) NSData *streamToken;
  112. - (const firebase::firestore::model::DocumentVersionMap &)docVersions;
  113. @end
  114. NS_ASSUME_NONNULL_END