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