FSTViewSnapshot.mm 5.6 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/Core/FSTViewSnapshot.h"
  17. #include <string>
  18. #include <utility>
  19. #import "Firestore/Source/Core/FSTQuery.h"
  20. #import "Firestore/Source/Model/FSTDocument.h"
  21. #import "Firestore/Source/Model/FSTDocumentSet.h"
  22. #include "Firestore/core/src/firebase/firestore/immutable/sorted_map.h"
  23. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  24. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  25. #include "Firestore/core/src/firebase/firestore/util/hashing.h"
  26. #include "Firestore/core/src/firebase/firestore/util/objc_compatibility.h"
  27. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  28. #include "Firestore/core/src/firebase/firestore/util/string_format.h"
  29. #include "absl/strings/str_join.h"
  30. namespace objc = firebase::firestore::util::objc;
  31. using firebase::firestore::core::DocumentViewChange;
  32. using firebase::firestore::immutable::SortedMap;
  33. using firebase::firestore::model::DocumentKey;
  34. using firebase::firestore::model::DocumentKeySet;
  35. using firebase::firestore::util::Hash;
  36. using firebase::firestore::util::StringFormat;
  37. using firebase::firestore::util::WrapNSString;
  38. NS_ASSUME_NONNULL_BEGIN
  39. @implementation FSTViewSnapshot {
  40. std::vector<DocumentViewChange> _documentChanges;
  41. }
  42. - (const std::vector<DocumentViewChange> &)documentChanges {
  43. return _documentChanges;
  44. }
  45. - (instancetype)initWithQuery:(FSTQuery *)query
  46. documents:(FSTDocumentSet *)documents
  47. oldDocuments:(FSTDocumentSet *)oldDocuments
  48. documentChanges:(std::vector<DocumentViewChange>)documentChanges
  49. fromCache:(BOOL)fromCache
  50. mutatedKeys:(DocumentKeySet)mutatedKeys
  51. syncStateChanged:(BOOL)syncStateChanged
  52. excludesMetadataChanges:(BOOL)excludesMetadataChanges {
  53. self = [super init];
  54. if (self) {
  55. _query = query;
  56. _documents = documents;
  57. _oldDocuments = oldDocuments;
  58. _documentChanges = std::move(documentChanges);
  59. _fromCache = fromCache;
  60. _mutatedKeys = mutatedKeys;
  61. _syncStateChanged = syncStateChanged;
  62. _excludesMetadataChanges = excludesMetadataChanges;
  63. }
  64. return self;
  65. }
  66. + (instancetype)snapshotForInitialDocuments:(FSTDocumentSet *)documents
  67. query:(FSTQuery *)query
  68. mutatedKeys:(DocumentKeySet)mutatedKeys
  69. fromCache:(BOOL)fromCache
  70. excludesMetadataChanges:(BOOL)excludesMetadataChanges {
  71. std::vector<DocumentViewChange> viewChanges;
  72. for (FSTDocument *doc in documents.documentEnumerator) {
  73. viewChanges.emplace_back(doc, DocumentViewChange::Type::kAdded);
  74. }
  75. return [[FSTViewSnapshot alloc]
  76. initWithQuery:query
  77. documents:documents
  78. oldDocuments:[FSTDocumentSet documentSetWithComparator:query.comparator]
  79. documentChanges:std::move(viewChanges)
  80. fromCache:fromCache
  81. mutatedKeys:mutatedKeys
  82. syncStateChanged:YES
  83. excludesMetadataChanges:excludesMetadataChanges];
  84. }
  85. - (BOOL)hasPendingWrites {
  86. return _mutatedKeys.size() != 0;
  87. }
  88. - (NSString *)description {
  89. return [NSString
  90. stringWithFormat:@"<FSTViewSnapshot query:%@ documents:%@ oldDocument:%@ changes:%@ "
  91. "fromCache:%@ mutatedKeys:%zu syncStateChanged:%@ "
  92. "excludesMetadataChanges%@>",
  93. self.query, self.documents, self.oldDocuments,
  94. objc::Description(_documentChanges), (self.fromCache ? @"YES" : @"NO"),
  95. static_cast<size_t>(self.mutatedKeys.size()),
  96. (self.syncStateChanged ? @"YES" : @"NO"),
  97. (self.excludesMetadataChanges ? @"YES" : @"NO")];
  98. }
  99. - (BOOL)isEqual:(id)object {
  100. if (self == object) {
  101. return YES;
  102. } else if (![object isKindOfClass:[FSTViewSnapshot class]]) {
  103. return NO;
  104. }
  105. FSTViewSnapshot *other = object;
  106. return [self.query isEqual:other.query] && [self.documents isEqual:other.documents] &&
  107. [self.oldDocuments isEqual:other.oldDocuments] &&
  108. _documentChanges == other.documentChanges && self.fromCache == other.fromCache &&
  109. self.mutatedKeys == other.mutatedKeys && self.syncStateChanged == other.syncStateChanged &&
  110. self.excludesMetadataChanges == other.excludesMetadataChanges;
  111. }
  112. - (NSUInteger)hash {
  113. // Note: We are omitting `mutatedKeys` from the hash, since we don't have a straightforward
  114. // way to compute its hash value. Since `FSTViewSnapshot` is currently not stored in an
  115. // NSDictionary, this has no side effects.
  116. NSUInteger result = [self.query hash];
  117. result = 31 * result + [self.documents hash];
  118. result = 31 * result + [self.oldDocuments hash];
  119. result = 31 * result + Hash(_documentChanges);
  120. result = 31 * result + (self.fromCache ? 1231 : 1237);
  121. result = 31 * result + (self.syncStateChanged ? 1231 : 1237);
  122. result = 31 * result + (self.excludesMetadataChanges ? 1231 : 1237);
  123. return result;
  124. }
  125. @end
  126. NS_ASSUME_NONNULL_END