FSTMutationQueue.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/Local/FSTGarbageCollector.h"
  19. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  21. @class FSTMutation;
  22. @class FSTMutationBatch;
  23. @class FSTQuery;
  24. @class FIRTimestamp;
  25. NS_ASSUME_NONNULL_BEGIN
  26. #pragma mark - FSTMutationQueue
  27. /** A queue of mutations to apply to the remote store. */
  28. @protocol FSTMutationQueue <NSObject, FSTGarbageSource>
  29. /**
  30. * Starts the mutation queue, performing any initial reads that might be required to establish
  31. * invariants, etc.
  32. *
  33. * After starting, the mutation queue must guarantee that the highestAcknowledgedBatchID is less
  34. * than nextBatchID. This prevents the local store from creating new batches that the mutation
  35. * queue would consider erroneously acknowledged.
  36. */
  37. - (void)start;
  38. /** Returns YES if this queue contains no mutation batches. */
  39. - (BOOL)isEmpty;
  40. /**
  41. * Returns the next FSTBatchID that will be assigned to a new mutation batch.
  42. *
  43. * Callers generally don't care about this value except to test that the mutation queue is
  44. * properly maintaining the invariant that highestAcknowledgedBatchID is less than nextBatchID.
  45. */
  46. - (FSTBatchID)nextBatchID;
  47. /**
  48. * Returns the highest batchID that has been acknowledged. If no batches have been acknowledged
  49. * or if there are no batches in the queue this can return kFSTBatchIDUnknown.
  50. */
  51. - (FSTBatchID)highestAcknowledgedBatchID;
  52. /** Acknowledges the given batch. */
  53. - (void)acknowledgeBatch:(FSTMutationBatch *)batch streamToken:(nullable NSData *)streamToken;
  54. /** Returns the current stream token for this mutation queue. */
  55. - (nullable NSData *)lastStreamToken;
  56. /** Sets the stream token for this mutation queue. */
  57. - (void)setLastStreamToken:(nullable NSData *)streamToken;
  58. /** Creates a new mutation batch and adds it to this mutation queue. */
  59. - (FSTMutationBatch *)addMutationBatchWithWriteTime:(FIRTimestamp *)localWriteTime
  60. mutations:(NSArray<FSTMutation *> *)mutations;
  61. /** Loads the mutation batch with the given batchID. */
  62. - (nullable FSTMutationBatch *)lookupMutationBatch:(FSTBatchID)batchID;
  63. /**
  64. * Gets the first unacknowledged mutation batch after the passed in batchId in the mutation queue
  65. * or nil if empty.
  66. *
  67. * @param batchID The batch to search after, or kFSTBatchIDUnknown for the first mutation in the
  68. * queue.
  69. *
  70. * @return the next mutation or nil if there wasn't one.
  71. */
  72. - (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:(FSTBatchID)batchID;
  73. /** Gets all mutation batches in the mutation queue. */
  74. // TODO(mikelehen): PERF: Current consumer only needs mutated keys; if we can provide that
  75. // cheaply, we should replace this.
  76. - (NSArray<FSTMutationBatch *> *)allMutationBatches;
  77. /**
  78. * Finds all mutations with a batchID less than or equal to the given batchID.
  79. *
  80. * Generally the caller should be asking for the next unacknowledged batchID and the number of
  81. * acknowledged batches should be very small when things are functioning well.
  82. *
  83. * @param batchID The batch to search through.
  84. *
  85. * @return an NSArray containing all batches with matching batchIDs.
  86. */
  87. // TODO(mcg): This should really return NSEnumerator and the caller should be adjusted to only
  88. // loop through these once.
  89. - (NSArray<FSTMutationBatch *> *)allMutationBatchesThroughBatchID:(FSTBatchID)batchID;
  90. /**
  91. * Finds all mutation batches that could @em possibly affect the given document key. Not all
  92. * mutations in a batch will necessarily affect the document key, so when looping through the
  93. * batch you'll need to check that the mutation itself matches the key.
  94. *
  95. * Note that because of this requirement implementations are free to return mutation batches that
  96. * don't contain the document key at all if it's convenient.
  97. */
  98. // TODO(mcg): This should really return an NSEnumerator
  99. - (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingDocumentKey:
  100. (const firebase::firestore::model::DocumentKey &)documentKey;
  101. /**
  102. * Finds all mutation batches that could @em possibly affect the given document keys. Not all
  103. * mutations in a batch will necessarily affect each key, so when looping through the batches you'll
  104. * need to check that the mutation itself matches the key.
  105. *
  106. * Note that because of this requirement implementations are free to return mutation batches that
  107. * don't contain any of the given document keys at all if it's convenient.
  108. */
  109. // TODO(mcg): This should really return an NSEnumerator
  110. - (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingDocumentKeys:
  111. (const firebase::firestore::model::DocumentKeySet &)documentKeys;
  112. /**
  113. * Finds all mutation batches that could affect the results for the given query. Not all
  114. * mutations in a batch will necessarily affect the query, so when looping through the batch
  115. * you'll need to check that the mutation itself matches the query.
  116. *
  117. * Note that because of this requirement implementations are free to return mutation batches that
  118. * don't match the query at all if it's convenient.
  119. *
  120. * NOTE: A FSTPatchMutation does not need to include all fields in the query filter criteria in
  121. * order to be a match (but any fields it does contain do need to match).
  122. */
  123. // TODO(mikelehen): This should perhaps return an NSEnumerator, though I'm not sure we can avoid
  124. // loading them all in memory.
  125. - (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingQuery:(FSTQuery *)query;
  126. /**
  127. * Removes the given mutation batches from the queue. This is useful in two circumstances:
  128. *
  129. * + Removing applied mutations from the head of the queue
  130. * + Removing rejected mutations from anywhere in the queue
  131. *
  132. * In both cases, the array of mutations to remove must be a contiguous range of batchIds. This is
  133. * most easily accomplished by loading mutations with @a -allMutationBatchesThroughBatchID:.
  134. */
  135. - (void)removeMutationBatches:(NSArray<FSTMutationBatch *> *)batches;
  136. /** Performs a consistency check, examining the mutation queue for any leaks, if possible. */
  137. - (void)performConsistencyCheck;
  138. @end
  139. NS_ASSUME_NONNULL_END