FSTDocument.mm 4.5 KB

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