FSTMutationBatch.h 4.4 KB

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