FSTDocument.m 3.9 KB

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