FSTDocument.mm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "Firestore/Source/Model/FSTDocument.h"
  17. #include <utility>
  18. #import "Firestore/Source/Model/FSTFieldValue.h"
  19. #import "Firestore/Source/Util/FSTClasses.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  21. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  22. #include "Firestore/core/src/firebase/firestore/util/comparison.h"
  23. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  24. #include "Firestore/core/src/firebase/firestore/util/hashing.h"
  25. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  26. namespace util = firebase::firestore::util;
  27. using firebase::firestore::model::DocumentKey;
  28. using firebase::firestore::model::FieldPath;
  29. using firebase::firestore::model::SnapshotVersion;
  30. NS_ASSUME_NONNULL_BEGIN
  31. @interface FSTMaybeDocument ()
  32. - (instancetype)initWithKey:(DocumentKey)key
  33. version:(SnapshotVersion)version NS_DESIGNATED_INITIALIZER;
  34. @end
  35. @implementation FSTMaybeDocument {
  36. DocumentKey _key;
  37. SnapshotVersion _version;
  38. }
  39. - (instancetype)initWithKey:(DocumentKey)key version:(SnapshotVersion)version {
  40. self = [super init];
  41. if (self) {
  42. _key = std::move(key);
  43. _version = std::move(version);
  44. }
  45. return self;
  46. }
  47. - (bool)hasPendingWrites {
  48. @throw FSTAbstractMethodException(); // NOLINT
  49. }
  50. - (id)copyWithZone:(NSZone *_Nullable)zone {
  51. // All document types are immutable
  52. return self;
  53. }
  54. - (const DocumentKey &)key {
  55. return _key;
  56. }
  57. - (const SnapshotVersion &)version {
  58. return _version;
  59. }
  60. @end
  61. @implementation FSTDocument {
  62. FSTDocumentState _documentState;
  63. }
  64. + (instancetype)documentWithData:(FSTObjectValue *)data
  65. key:(DocumentKey)key
  66. version:(SnapshotVersion)version
  67. state:(FSTDocumentState)state {
  68. return [[FSTDocument alloc] initWithData:data
  69. key:std::move(key)
  70. version:std::move(version)
  71. state:state];
  72. }
  73. + (instancetype)documentWithData:(FSTObjectValue *)data
  74. key:(DocumentKey)key
  75. version:(SnapshotVersion)version
  76. state:(FSTDocumentState)state
  77. proto:(GCFSDocument *)proto {
  78. return [[FSTDocument alloc] initWithData:data
  79. key:std::move(key)
  80. version:std::move(version)
  81. state:state
  82. proto:proto];
  83. }
  84. - (instancetype)initWithData:(FSTObjectValue *)data
  85. key:(DocumentKey)key
  86. version:(SnapshotVersion)version
  87. state:(FSTDocumentState)state {
  88. self = [super initWithKey:std::move(key) version:std::move(version)];
  89. if (self) {
  90. _data = data;
  91. _documentState = state;
  92. _proto = nil;
  93. }
  94. return self;
  95. }
  96. - (instancetype)initWithData:(FSTObjectValue *)data
  97. key:(DocumentKey)key
  98. version:(SnapshotVersion)version
  99. state:(FSTDocumentState)state
  100. proto:(GCFSDocument *)proto {
  101. self = [super initWithKey:std::move(key) version:std::move(version)];
  102. if (self) {
  103. _data = data;
  104. _documentState = state;
  105. _proto = proto;
  106. }
  107. return self;
  108. }
  109. - (bool)hasLocalMutations {
  110. return _documentState == FSTDocumentStateLocalMutations;
  111. }
  112. - (bool)hasCommittedMutations {
  113. return _documentState == FSTDocumentStateCommittedMutations;
  114. }
  115. - (bool)hasPendingWrites {
  116. return self.hasLocalMutations || self.hasCommittedMutations;
  117. }
  118. - (BOOL)isEqual:(id)other {
  119. if (other == self) {
  120. return YES;
  121. }
  122. if (![other isKindOfClass:[FSTDocument class]]) {
  123. return NO;
  124. }
  125. FSTDocument *otherDoc = other;
  126. return self.key == otherDoc.key && self.version == otherDoc.version &&
  127. _documentState == otherDoc->_documentState && [self.data isEqual:otherDoc.data];
  128. }
  129. - (NSUInteger)hash {
  130. NSUInteger result = self.key.Hash();
  131. result = result * 31 + self.version.Hash();
  132. result = result * 31 + [self.data hash];
  133. result = result * 31 + _documentState;
  134. return result;
  135. }
  136. - (NSString *)description {
  137. return [NSString stringWithFormat:@"<FSTDocument: key:%s version:%s documentState:%ld data:%@>",
  138. self.key.ToString().c_str(),
  139. self.version.timestamp().ToString().c_str(),
  140. (long)_documentState, self.data];
  141. }
  142. - (nullable FSTFieldValue *)fieldForPath:(const FieldPath &)path {
  143. return [_data valueForPath:path];
  144. }
  145. @end
  146. @implementation FSTDeletedDocument {
  147. bool _hasCommittedMutations;
  148. }
  149. + (instancetype)documentWithKey:(DocumentKey)key
  150. version:(SnapshotVersion)version
  151. hasCommittedMutations:(bool)committedMutations {
  152. FSTDeletedDocument *deletedDocument = [[FSTDeletedDocument alloc] initWithKey:std::move(key)
  153. version:std::move(version)];
  154. if (deletedDocument) {
  155. deletedDocument->_hasCommittedMutations = committedMutations;
  156. }
  157. return deletedDocument;
  158. }
  159. - (bool)hasCommittedMutations {
  160. return _hasCommittedMutations;
  161. }
  162. - (bool)hasPendingWrites {
  163. return self.hasCommittedMutations;
  164. }
  165. - (BOOL)isEqual:(id)other {
  166. if (other == self) {
  167. return YES;
  168. }
  169. if (![other isKindOfClass:[FSTDeletedDocument class]]) {
  170. return NO;
  171. }
  172. FSTDeletedDocument *otherDoc = other;
  173. return self.key == otherDoc.key && self.version == otherDoc.version &&
  174. _hasCommittedMutations == otherDoc->_hasCommittedMutations;
  175. }
  176. - (NSUInteger)hash {
  177. NSUInteger result = self.key.Hash();
  178. result = result * 31 + self.version.Hash();
  179. result = result * 31 + (_hasCommittedMutations ? 1 : 0);
  180. return result;
  181. }
  182. - (NSString *)description {
  183. return [NSString
  184. stringWithFormat:@"<FSTDeletedDocument: key:%s version:%s committedMutations:%d>",
  185. self.key.ToString().c_str(), self.version.timestamp().ToString().c_str(),
  186. _hasCommittedMutations];
  187. }
  188. @end
  189. @implementation FSTUnknownDocument
  190. + (instancetype)documentWithKey:(DocumentKey)key version:(SnapshotVersion)version {
  191. return [[FSTUnknownDocument alloc] initWithKey:std::move(key) version:std::move(version)];
  192. }
  193. - (bool)hasPendingWrites {
  194. return true;
  195. }
  196. - (BOOL)isEqual:(id)other {
  197. if (other == self) {
  198. return YES;
  199. }
  200. if (![other isKindOfClass:[FSTUnknownDocument class]]) {
  201. return NO;
  202. }
  203. FSTDocument *otherDoc = other;
  204. return self.key == otherDoc.key && self.version == otherDoc.version;
  205. }
  206. - (NSUInteger)hash {
  207. NSUInteger result = self.key.Hash();
  208. result = result * 31 + self.version.Hash();
  209. return result;
  210. }
  211. - (NSString *)description {
  212. return [NSString stringWithFormat:@"<FSTUnknownDocument: key:%s version:%s>",
  213. self.key.ToString().c_str(),
  214. self.version.timestamp().ToString().c_str()];
  215. }
  216. @end
  217. NS_ASSUME_NONNULL_END