FSTMutationQueue.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "Firestore/core/src/firebase/firestore/model/document_key.h"
  18. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  19. #include "Firestore/core/src/firebase/firestore/model/types.h"
  20. @class FSTMutation;
  21. @class FSTMutationBatch;
  22. @class FSTQuery;
  23. @class FIRTimestamp;
  24. NS_ASSUME_NONNULL_BEGIN
  25. #pragma mark - FSTMutationQueue
  26. /** A queue of mutations to apply to the remote store. */
  27. @protocol FSTMutationQueue <NSObject>
  28. /**
  29. * Starts the mutation queue, performing any initial reads that might be required to establish
  30. * invariants, etc.
  31. */
  32. - (void)start;
  33. /** Returns YES if this queue contains no mutation batches. */
  34. - (BOOL)isEmpty;
  35. /** Acknowledges the given batch. */
  36. - (void)acknowledgeBatch:(FSTMutationBatch *)batch streamToken:(nullable NSData *)streamToken;
  37. /** Returns the current stream token for this mutation queue. */
  38. - (nullable NSData *)lastStreamToken;
  39. /** Sets the stream token for this mutation queue. */
  40. - (void)setLastStreamToken:(nullable NSData *)streamToken;
  41. /** Creates a new mutation batch and adds it to this mutation queue. */
  42. - (FSTMutationBatch *)addMutationBatchWithWriteTime:(FIRTimestamp *)localWriteTime
  43. mutations:(NSArray<FSTMutation *> *)mutations;
  44. /** Loads the mutation batch with the given batchID. */
  45. - (nullable FSTMutationBatch *)lookupMutationBatch:(firebase::firestore::model::BatchId)batchID;
  46. /**
  47. * Gets the first unacknowledged mutation batch after the passed in batchId in the mutation queue
  48. * or nil if empty.
  49. *
  50. * @param batchID The batch to search after, or kBatchIdUnknown for the first mutation in the
  51. * queue.
  52. *
  53. * @return the next mutation or nil if there wasn't one.
  54. */
  55. - (nullable FSTMutationBatch *)nextMutationBatchAfterBatchID:
  56. (firebase::firestore::model::BatchId)batchID;
  57. /** Gets all mutation batches in the mutation queue. */
  58. // TODO(mikelehen): PERF: Current consumer only needs mutated keys; if we can provide that
  59. // cheaply, we should replace this.
  60. - (NSArray<FSTMutationBatch *> *)allMutationBatches;
  61. /**
  62. * Finds all mutation batches that could @em possibly affect the given document key. Not all
  63. * mutations in a batch will necessarily affect the document key, so when looping through the
  64. * batch you'll need to check that the mutation itself matches the key.
  65. *
  66. * Note that because of this requirement implementations are free to return mutation batches that
  67. * don't contain the document key at all if it's convenient.
  68. */
  69. // TODO(mcg): This should really return an NSEnumerator
  70. - (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingDocumentKey:
  71. (const firebase::firestore::model::DocumentKey &)documentKey;
  72. /**
  73. * Finds all mutation batches that could @em possibly affect the given document keys. Not all
  74. * mutations in a batch will necessarily affect each key, so when looping through the batches you'll
  75. * need to check that the mutation itself matches the key.
  76. *
  77. * Note that because of this requirement implementations are free to return mutation batches that
  78. * don't contain any of the given document keys at all if it's convenient.
  79. */
  80. // TODO(mcg): This should really return an NSEnumerator
  81. - (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingDocumentKeys:
  82. (const firebase::firestore::model::DocumentKeySet &)documentKeys;
  83. /**
  84. * Finds all mutation batches that could affect the results for the given query. Not all
  85. * mutations in a batch will necessarily affect the query, so when looping through the batch
  86. * you'll need to check that the mutation itself matches the query.
  87. *
  88. * Note that because of this requirement implementations are free to return mutation batches that
  89. * don't match the query at all if it's convenient.
  90. *
  91. * NOTE: A FSTPatchMutation does not need to include all fields in the query filter criteria in
  92. * order to be a match (but any fields it does contain do need to match).
  93. */
  94. // TODO(mikelehen): This should perhaps return an NSEnumerator, though I'm not sure we can avoid
  95. // loading them all in memory.
  96. - (NSArray<FSTMutationBatch *> *)allMutationBatchesAffectingQuery:(FSTQuery *)query;
  97. /**
  98. * Removes the given mutation batch from the queue. This is useful in two circumstances:
  99. *
  100. * + Removing applied mutations from the head of the queue
  101. * + Removing rejected mutations from anywhere in the queue
  102. */
  103. - (void)removeMutationBatch:(FSTMutationBatch *)batch;
  104. /** Performs a consistency check, examining the mutation queue for any leaks, if possible. */
  105. - (void)performConsistencyCheck;
  106. @end
  107. NS_ASSUME_NONNULL_END