FIRWriteBatch.mm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "FIRWriteBatch.h"
  17. #include <algorithm>
  18. #include <utility>
  19. #include <vector>
  20. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  21. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  22. #import "Firestore/Source/API/FIRWriteBatch+Internal.h"
  23. #import "Firestore/Source/API/FSTUserDataConverter.h"
  24. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  25. #import "Firestore/Source/Model/FSTMutation.h"
  26. #include "Firestore/core/src/firebase/firestore/api/input_validation.h"
  27. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  28. using firebase::firestore::api::ThrowIllegalState;
  29. using firebase::firestore::api::ThrowInvalidArgument;
  30. using firebase::firestore::core::ParsedSetData;
  31. using firebase::firestore::core::ParsedUpdateData;
  32. using firebase::firestore::model::Precondition;
  33. NS_ASSUME_NONNULL_BEGIN
  34. #pragma mark - FIRWriteBatch
  35. @interface FIRWriteBatch ()
  36. - (instancetype)initWithFirestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
  37. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  38. @property(nonatomic, assign) BOOL committed;
  39. @end
  40. @implementation FIRWriteBatch (Internal)
  41. + (instancetype)writeBatchWithFirestore:(FIRFirestore *)firestore {
  42. return [[FIRWriteBatch alloc] initWithFirestore:firestore];
  43. }
  44. @end
  45. @implementation FIRWriteBatch {
  46. std::vector<FSTMutation *> _mutations;
  47. }
  48. - (instancetype)initWithFirestore:(FIRFirestore *)firestore {
  49. self = [super init];
  50. if (self) {
  51. _firestore = firestore;
  52. }
  53. return self;
  54. }
  55. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  56. forDocument:(FIRDocumentReference *)document {
  57. return [self setData:data forDocument:document merge:NO];
  58. }
  59. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  60. forDocument:(FIRDocumentReference *)document
  61. merge:(BOOL)merge {
  62. [self verifyNotCommitted];
  63. [self validateReference:document];
  64. ParsedSetData parsed = merge ? [self.firestore.dataConverter parsedMergeData:data fieldMask:nil]
  65. : [self.firestore.dataConverter parsedSetData:data];
  66. std::vector<FSTMutation *> append_mutations =
  67. std::move(parsed).ToMutations(document.key, Precondition::None());
  68. std::move(append_mutations.begin(), append_mutations.end(), std::back_inserter(_mutations));
  69. return self;
  70. }
  71. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  72. forDocument:(FIRDocumentReference *)document
  73. mergeFields:(NSArray<id> *)mergeFields {
  74. [self verifyNotCommitted];
  75. [self validateReference:document];
  76. ParsedSetData parsed = [self.firestore.dataConverter parsedMergeData:data fieldMask:mergeFields];
  77. std::vector<FSTMutation *> append_mutations =
  78. std::move(parsed).ToMutations(document.key, Precondition::None());
  79. std::move(append_mutations.begin(), append_mutations.end(), std::back_inserter(_mutations));
  80. return self;
  81. }
  82. - (FIRWriteBatch *)updateData:(NSDictionary<id, id> *)fields
  83. forDocument:(FIRDocumentReference *)document {
  84. [self verifyNotCommitted];
  85. [self validateReference:document];
  86. ParsedUpdateData parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  87. std::vector<FSTMutation *> append_mutations =
  88. std::move(parsed).ToMutations(document.key, Precondition::Exists(true));
  89. std::move(append_mutations.begin(), append_mutations.end(), std::back_inserter(_mutations));
  90. return self;
  91. }
  92. - (FIRWriteBatch *)deleteDocument:(FIRDocumentReference *)document {
  93. [self verifyNotCommitted];
  94. [self validateReference:document];
  95. _mutations.push_back([[FSTDeleteMutation alloc] initWithKey:document.key
  96. precondition:Precondition::None()]);
  97. ;
  98. return self;
  99. }
  100. - (void)commit {
  101. [self commitWithCompletion:nil];
  102. }
  103. - (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
  104. [self verifyNotCommitted];
  105. self.committed = TRUE;
  106. [self.firestore.client writeMutations:std::move(_mutations) completion:completion];
  107. }
  108. - (void)verifyNotCommitted {
  109. if (self.committed) {
  110. ThrowIllegalState("A write batch can no longer be used after commit has been called.");
  111. }
  112. }
  113. - (void)validateReference:(FIRDocumentReference *)reference {
  114. if (reference.firestore != self.firestore) {
  115. ThrowInvalidArgument("Provided document reference is from a different Firestore instance.");
  116. }
  117. }
  118. @end
  119. NS_ASSUME_NONNULL_END