FSTDocument.mm 4.1 KB

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