FSTMutationBatch.h 4.5 KB

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