FSTMutationBatch.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #import "Firestore/Source/Core/FSTTypes.h"
  18. #import "Firestore/Source/Model/FSTDocumentKeySet.h"
  19. #import "Firestore/Source/Model/FSTDocumentVersionDictionary.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  21. @class FSTMutation;
  22. @class FIRTimestamp;
  23. @class FSTMutationResult;
  24. @class FSTMutationBatchResult;
  25. @class FSTSnapshotVersion;
  26. NS_ASSUME_NONNULL_BEGIN
  27. /**
  28. * A BatchID that was searched for and not found or a batch ID value known to be before all known
  29. * batches.
  30. *
  31. * FSTBatchID values from the local store are non-negative so this value is before all batches.
  32. */
  33. extern const FSTBatchID kFSTBatchIDUnknown;
  34. /**
  35. * A batch of mutations that will be sent as one unit to the backend. Batches can be marked as a
  36. * tombstone if the mutation queue does not remove them immediately. When a batch is a tombstone
  37. * it has no mutations.
  38. */
  39. @interface FSTMutationBatch : NSObject
  40. /** Initializes a mutation batch with the given batchID, localWriteTime, and mutations. */
  41. - (instancetype)initWithBatchID:(FSTBatchID)batchID
  42. localWriteTime:(FIRTimestamp *)localWriteTime
  43. mutations:(NSArray<FSTMutation *> *)mutations NS_DESIGNATED_INITIALIZER;
  44. - (id)init NS_UNAVAILABLE;
  45. /**
  46. * Applies all the mutations in this FSTMutationBatch to the specified document.
  47. *
  48. * @param maybeDoc The document to apply mutations to.
  49. * @param documentKey The key of the document to apply mutations to.
  50. * @param mutationBatchResult The result of applying the MutationBatch to the backend. If omitted
  51. * it's assumed that this is a local (latency-compensated) application and documents will have
  52. * their hasLocalMutations flag set.
  53. */
  54. - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc
  55. documentKey:(const firebase::firestore::model::DocumentKey &)documentKey
  56. mutationBatchResult:(FSTMutationBatchResult *_Nullable)mutationBatchResult;
  57. /**
  58. * A helper version of applyTo for applying mutations locally (without a mutation batch result from
  59. * the backend).
  60. */
  61. - (FSTMaybeDocument *_Nullable)applyTo:(FSTMaybeDocument *_Nullable)maybeDoc
  62. documentKey:(const firebase::firestore::model::DocumentKey &)documentKey;
  63. /**
  64. * Returns YES if this mutation batch has already been removed from the mutation queue.
  65. *
  66. * Note that not all implementations of the FSTMutationQueue necessarily use tombstones as a part
  67. * of their implementation and generally speaking no code outside the mutation queues should really
  68. * care about this.
  69. */
  70. - (BOOL)isTombstone;
  71. /** Converts this batch to a tombstone. */
  72. - (FSTMutationBatch *)toTombstone;
  73. /** Returns the set of unique keys referenced by all mutations in the batch. */
  74. - (FSTDocumentKeySet *)keys;
  75. @property(nonatomic, assign, readonly) FSTBatchID batchID;
  76. @property(nonatomic, strong, readonly) FIRTimestamp *localWriteTime;
  77. @property(nonatomic, strong, readonly) NSArray<FSTMutation *> *mutations;
  78. @end
  79. #pragma mark - FSTMutationBatchResult
  80. /** The result of applying a mutation batch to the backend. */
  81. @interface FSTMutationBatchResult : NSObject
  82. - (instancetype)init NS_UNAVAILABLE;
  83. /**
  84. * Creates a new FSTMutationBatchResult for the given batch and results. There must be one result
  85. * for each mutation in the batch. This static factory caches a document=>version mapping
  86. * (as docVersions).
  87. */
  88. + (instancetype)resultWithBatch:(FSTMutationBatch *)batch
  89. commitVersion:(FSTSnapshotVersion *)commitVersion
  90. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults
  91. streamToken:(nullable NSData *)streamToken;
  92. @property(nonatomic, strong, readonly) FSTMutationBatch *batch;
  93. @property(nonatomic, strong, readonly) FSTSnapshotVersion *commitVersion;
  94. @property(nonatomic, strong, readonly) NSArray<FSTMutationResult *> *mutationResults;
  95. @property(nonatomic, strong, readonly, nullable) NSData *streamToken;
  96. @property(nonatomic, strong, readonly) FSTDocumentVersionDictionary *docVersions;
  97. @end
  98. NS_ASSUME_NONNULL_END