FSTDocument.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/Core/FSTSnapshotVersion.h"
  19. #import "Firestore/Source/Model/FSTFieldValue.h"
  20. #import "Firestore/Source/Util/FSTAssert.h"
  21. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  22. #include "Firestore/core/src/firebase/firestore/model/field_path.h"
  23. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  24. namespace util = firebase::firestore::util;
  25. using firebase::firestore::model::DocumentKey;
  26. using firebase::firestore::model::FieldPath;
  27. NS_ASSUME_NONNULL_BEGIN
  28. @interface FSTMaybeDocument ()
  29. - (instancetype)initWithKey:(DocumentKey)key
  30. version:(FSTSnapshotVersion *)version NS_DESIGNATED_INITIALIZER;
  31. @end
  32. @implementation FSTMaybeDocument {
  33. DocumentKey _key;
  34. }
  35. - (instancetype)initWithKey:(DocumentKey)key version:(FSTSnapshotVersion *)version {
  36. FSTAssert(!!version, @"Version must not be nil.");
  37. self = [super init];
  38. if (self) {
  39. _key = std::move(key);
  40. _version = version;
  41. }
  42. return self;
  43. }
  44. - (id)copyWithZone:(NSZone *_Nullable)zone {
  45. // All document types are immutable
  46. return self;
  47. }
  48. - (const DocumentKey &)key {
  49. return _key;
  50. }
  51. @end
  52. @implementation FSTDocument
  53. + (instancetype)documentWithData:(FSTObjectValue *)data
  54. key:(DocumentKey)key
  55. version:(FSTSnapshotVersion *)version
  56. hasLocalMutations:(BOOL)mutations {
  57. return [[FSTDocument alloc] initWithData:data
  58. key:std::move(key)
  59. version:version
  60. hasLocalMutations:mutations];
  61. }
  62. - (instancetype)initWithData:(FSTObjectValue *)data
  63. key:(DocumentKey)key
  64. version:(FSTSnapshotVersion *)version
  65. hasLocalMutations:(BOOL)mutations {
  66. self = [super initWithKey:std::move(key) version:version];
  67. if (self) {
  68. _data = data;
  69. _localMutations = mutations;
  70. }
  71. return self;
  72. }
  73. - (BOOL)isEqual:(id)other {
  74. if (other == self) {
  75. return YES;
  76. }
  77. if (![other isKindOfClass:[FSTDocument class]]) {
  78. return NO;
  79. }
  80. FSTDocument *otherDoc = other;
  81. return [self.key isEqual:otherDoc.key] && [self.version isEqual:otherDoc.version] &&
  82. [self.data isEqual:otherDoc.data] && self.hasLocalMutations == otherDoc.hasLocalMutations;
  83. }
  84. - (NSUInteger)hash {
  85. NSUInteger result = [self.key hash];
  86. result = result * 31 + [self.version hash];
  87. result = result * 31 + [self.data hash];
  88. result = result * 31 + (self.hasLocalMutations ? 1 : 0);
  89. return result;
  90. }
  91. - (NSString *)description {
  92. return [NSString stringWithFormat:@"<FSTDocument: key:%s version:%@ localMutations:%@ data:%@>",
  93. self.key.ToString().c_str(), self.version,
  94. self.localMutations ? @"YES" : @"NO", self.data];
  95. }
  96. - (nullable FSTFieldValue *)fieldForPath:(const FieldPath &)path {
  97. return [_data valueForPath:path];
  98. }
  99. @end
  100. @implementation FSTDeletedDocument
  101. + (instancetype)documentWithKey:(DocumentKey)key version:(FSTSnapshotVersion *)version {
  102. return [[FSTDeletedDocument alloc] initWithKey:std::move(key) version:version];
  103. }
  104. - (BOOL)isEqual:(id)other {
  105. if (other == self) {
  106. return YES;
  107. }
  108. if (![other isKindOfClass:[FSTDeletedDocument class]]) {
  109. return NO;
  110. }
  111. FSTDocument *otherDoc = other;
  112. return [self.key isEqual:otherDoc.key] && [self.version isEqual:otherDoc.version];
  113. }
  114. - (NSUInteger)hash {
  115. NSUInteger result = [self.key hash];
  116. result = result * 31 + [self.version hash];
  117. return result;
  118. }
  119. @end
  120. const NSComparator FSTDocumentComparatorByKey =
  121. ^NSComparisonResult(FSTMaybeDocument *doc1, FSTMaybeDocument *doc2) {
  122. return [doc1.key compare:doc2.key];
  123. };
  124. NS_ASSUME_NONNULL_END