FSTMutationBatch.h 4.4 KB

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