mutation_batch.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2019 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. #ifndef FIRESTORE_CORE_SRC_MODEL_MUTATION_BATCH_H_
  17. #define FIRESTORE_CORE_SRC_MODEL_MUTATION_BATCH_H_
  18. #include <iosfwd>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include "Firestore/core/include/firebase/firestore/timestamp.h"
  23. #include "Firestore/core/src/model/model_fwd.h"
  24. #include "Firestore/core/src/model/mutation.h"
  25. #include "Firestore/core/src/model/types.h"
  26. namespace firebase {
  27. namespace firestore {
  28. namespace model {
  29. class MutableDocument;
  30. /**
  31. * A BatchID that was searched for and not found or a batch ID value known to
  32. * be before all known batches.
  33. *
  34. * BatchId values from the local store are non-negative so this value is before
  35. * all batches.
  36. */
  37. constexpr BatchId kBatchIdUnknown = -1;
  38. /**
  39. * A batch of mutations that will be sent as one unit to the backend. Batches
  40. * can be marked as a tombstone if the mutation queue does not remove them
  41. * immediately. When a batch is a tombstone it has no mutations.
  42. */
  43. class MutationBatch {
  44. public:
  45. MutationBatch(int batch_id,
  46. Timestamp local_write_time,
  47. std::vector<Mutation> base_mutations,
  48. std::vector<Mutation> mutations);
  49. /** The unique ID of this mutation batch. */
  50. int batch_id() const {
  51. return batch_id_;
  52. }
  53. /**
  54. * Returns the local time at which the mutation batch was created / written;
  55. * used to assign local times to server timestamps, etc.
  56. */
  57. const Timestamp& local_write_time() const {
  58. return local_write_time_;
  59. }
  60. /**
  61. * Mutations that are used to populate the base values when this mutation is
  62. * applied locally. This can be used to locally overwrite values that are
  63. * persisted in the remote document cache. Base mutations are never sent to
  64. * the backend.
  65. */
  66. const std::vector<Mutation>& base_mutations() const {
  67. return base_mutations_;
  68. }
  69. /**
  70. * The user-provided mutations in this mutation batch. User-provided
  71. * mutations are applied both locally and remotely on the backend.
  72. */
  73. const std::vector<Mutation>& mutations() const {
  74. return mutations_;
  75. }
  76. /**
  77. * Applies all the mutations in this MutationBatch to the specified document
  78. * to create a new remote document.
  79. *
  80. * @param document The document to which to apply mutations.
  81. * @param mutation_batch_result The result of applying the MutationBatch to
  82. * the backend.
  83. */
  84. void ApplyToRemoteDocument(
  85. MutableDocument& document,
  86. const MutationBatchResult& mutation_batch_result) const;
  87. /**
  88. * Estimates the latency compensated view of all the mutations in this batch
  89. * applied to the given MaybeDocument.
  90. *
  91. * Unlike ApplyToRemoteDocument, this method is used before the mutation has
  92. * been committed and so it's possible that the mutation is operating on a
  93. * locally non-existent document and may produce a non-existent document.
  94. *
  95. * @param document The document to which to apply mutations.
  96. */
  97. void ApplyToLocalDocument(MutableDocument& document) const;
  98. /**
  99. * Computes the local view for all provided documents given the mutations in
  100. * this batch.
  101. */
  102. void ApplyToLocalDocumentSet(DocumentMap& document_map) const;
  103. /**
  104. * Returns the set of unique keys referenced by all mutations in the batch.
  105. */
  106. DocumentKeySet keys() const;
  107. friend bool operator==(const MutationBatch& lhs, const MutationBatch& rhs);
  108. std::string ToString() const;
  109. friend std::ostream& operator<<(std::ostream& os, const MutationBatch& batch);
  110. private:
  111. int batch_id_;
  112. Timestamp local_write_time_;
  113. std::vector<Mutation> base_mutations_;
  114. std::vector<Mutation> mutations_;
  115. };
  116. inline bool operator!=(const MutationBatch& lhs, const MutationBatch& rhs) {
  117. return !(lhs == rhs);
  118. }
  119. } // namespace model
  120. } // namespace firestore
  121. } // namespace firebase
  122. #endif // FIRESTORE_CORE_SRC_MODEL_MUTATION_BATCH_H_