FSTMutationBatch.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 BatchID that was searched for and not found or a batch ID value known to be before all known
  38. * batches.
  39. *
  40. * BatchId values from the local store are non-negative so this value is before all batches.
  41. */
  42. extern const firebase::firestore::model::BatchId kFSTBatchIDUnknown;
  43. /**
  44. * A batch of mutations that will be sent as one unit to the backend. Batches can be marked as a
  45. * tombstone if the mutation queue does not remove them immediately. When a batch is a tombstone
  46. * it has no mutations.
  47. */
  48. @interface FSTMutationBatch : NSObject
  49. /** Initializes a mutation batch with the given batchID, localWriteTime, and mutations. */
  50. - (instancetype)initWithBatchID:(firebase::firestore::model::BatchId)batchID
  51. localWriteTime:(FIRTimestamp *)localWriteTime
  52. mutations:(NSArray<FSTMutation *> *)mutations NS_DESIGNATED_INITIALIZER;
  53. - (id)init NS_UNAVAILABLE;
  54. /**
  55. * Applies all the mutations in this FSTMutationBatch to the specified 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)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc
  64. documentKey:(const firebase::firestore::model::DocumentKey &)documentKey
  65. mutationBatchResult:(FSTMutationBatchResult *_Nullable)mutationBatchResult;
  66. /**
  67. * A helper version of applyTo for applying mutations locally (without a mutation batch result from
  68. * the backend).
  69. */
  70. - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc
  71. documentKey:(const firebase::firestore::model::DocumentKey &)documentKey;
  72. /**
  73. * Returns YES if this mutation batch has already been removed from the mutation queue.
  74. *
  75. * Note that not all implementations of the FSTMutationQueue necessarily use tombstones as a part
  76. * of their implementation and generally speaking no code outside the mutation queues should really
  77. * care about this.
  78. */
  79. - (BOOL)isTombstone;
  80. /** Converts this batch to a tombstone. */
  81. - (FSTMutationBatch *)toTombstone;
  82. /** Returns the set of unique keys referenced by all mutations in the batch. */
  83. - (firebase::firestore::model::DocumentKeySet)keys;
  84. @property(nonatomic, assign, readonly) firebase::firestore::model::BatchId batchID;
  85. @property(nonatomic, strong, readonly) FIRTimestamp *localWriteTime;
  86. @property(nonatomic, strong, readonly) NSArray<FSTMutation *> *mutations;
  87. @end
  88. #pragma mark - FSTMutationBatchResult
  89. /** The result of applying a mutation batch to the backend. */
  90. @interface FSTMutationBatchResult : NSObject
  91. - (instancetype)init NS_UNAVAILABLE;
  92. /**
  93. * Creates a new FSTMutationBatchResult for the given batch and results. There must be one result
  94. * for each mutation in the batch. This static factory caches a document=>version mapping
  95. * (as docVersions).
  96. */
  97. + (instancetype)resultWithBatch:(FSTMutationBatch *)batch
  98. commitVersion:(firebase::firestore::model::SnapshotVersion)commitVersion
  99. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  100. streamToken:(nullable NSData *)streamToken;
  101. - (const firebase::firestore::model::SnapshotVersion &)commitVersion;
  102. @property(nonatomic, strong, readonly) FSTMutationBatch *batch;
  103. @property(nonatomic, strong, readonly) NSArray<FSTMutationResult *> *mutationResults;
  104. @property(nonatomic, strong, readonly, nullable) NSData *streamToken;
  105. - (const firebase::firestore::model::DocumentVersionMap &)docVersions;
  106. @end
  107. NS_ASSUME_NONNULL_END