FSTDocumentSet.mm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include <utility>
  17. #import "Firestore/Source/Model/FSTDocumentSet.h"
  18. #import "Firestore/Source/Model/FSTDocument.h"
  19. #import "Firestore/third_party/Immutable/FSTImmutableSortedSet.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  21. using firebase::firestore::model::DocumentMap;
  22. using firebase::firestore::model::DocumentKey;
  23. NS_ASSUME_NONNULL_BEGIN
  24. /**
  25. * The type of the main collection of documents in an FSTDocumentSet.
  26. * @see FSTDocumentSet#sortedSet
  27. */
  28. typedef FSTImmutableSortedSet<FSTDocument *> SetType;
  29. @interface FSTDocumentSet ()
  30. - (instancetype)initWithIndex:(DocumentMap &&)index
  31. set:(SetType *)sortedSet NS_DESIGNATED_INITIALIZER;
  32. /**
  33. * The main collection of documents in the FSTDocumentSet. The documents are ordered by a
  34. * comparator supplied from a query. The SetType collection exists in addition to the index to
  35. * allow ordered traversal of the FSTDocumentSet.
  36. */
  37. @property(nonatomic, strong, readonly) SetType *sortedSet;
  38. @end
  39. @implementation FSTDocumentSet {
  40. /**
  41. * An index of the documents in the FSTDocumentSet, indexed by document key. The index
  42. * exists to guarantee the uniqueness of document keys in the set and to allow lookup and removal
  43. * of documents by key.
  44. */
  45. DocumentMap _index;
  46. }
  47. + (instancetype)documentSetWithComparator:(NSComparator)comparator {
  48. SetType *set = [FSTImmutableSortedSet setWithComparator:comparator];
  49. return [[FSTDocumentSet alloc] initWithIndex:DocumentMap {} set:set];
  50. }
  51. - (instancetype)initWithIndex:(DocumentMap &&)index set:(SetType *)sortedSet {
  52. self = [super init];
  53. if (self) {
  54. _index = std::move(index);
  55. _sortedSet = sortedSet;
  56. }
  57. return self;
  58. }
  59. - (BOOL)isEqual:(id)other {
  60. if (other == self) {
  61. return YES;
  62. }
  63. if (![other isMemberOfClass:[FSTDocumentSet class]]) {
  64. return NO;
  65. }
  66. FSTDocumentSet *otherSet = (FSTDocumentSet *)other;
  67. if ([self count] != [otherSet count]) {
  68. return NO;
  69. }
  70. NSEnumerator<FSTDocument *> *selfIter = [self.sortedSet objectEnumerator];
  71. NSEnumerator<FSTDocument *> *otherIter = [otherSet.sortedSet objectEnumerator];
  72. FSTDocument *selfDoc = [selfIter nextObject];
  73. FSTDocument *otherDoc = [otherIter nextObject];
  74. while (selfDoc) {
  75. if (![selfDoc isEqual:otherDoc]) {
  76. return NO;
  77. }
  78. selfDoc = [selfIter nextObject];
  79. otherDoc = [otherIter nextObject];
  80. }
  81. return YES;
  82. }
  83. - (NSUInteger)hash {
  84. NSUInteger hash = 0;
  85. for (FSTDocument *doc in self.sortedSet.objectEnumerator) {
  86. hash = 31 * hash + [doc hash];
  87. }
  88. return hash;
  89. }
  90. - (NSString *)description {
  91. return [self.sortedSet description];
  92. }
  93. - (NSUInteger)count {
  94. return _index.size();
  95. }
  96. - (BOOL)isEmpty {
  97. return _index.empty();
  98. }
  99. - (BOOL)containsKey:(const DocumentKey &)key {
  100. return _index.underlying_map().find(key) != _index.underlying_map().end();
  101. }
  102. - (FSTDocument *_Nullable)documentForKey:(const DocumentKey &)key {
  103. auto found = _index.underlying_map().find(key);
  104. return found != _index.underlying_map().end() ? static_cast<FSTDocument *>(found->second) : nil;
  105. }
  106. - (FSTDocument *_Nullable)firstDocument {
  107. return [self.sortedSet firstObject];
  108. }
  109. - (FSTDocument *_Nullable)lastDocument {
  110. return [self.sortedSet lastObject];
  111. }
  112. - (NSUInteger)indexOfKey:(const DocumentKey &)key {
  113. FSTDocument *doc = [self documentForKey:key];
  114. return doc ? [self.sortedSet indexOfObject:doc] : NSNotFound;
  115. }
  116. - (NSEnumerator<FSTDocument *> *)documentEnumerator {
  117. return [self.sortedSet objectEnumerator];
  118. }
  119. - (NSArray *)arrayValue {
  120. NSMutableArray<FSTDocument *> *result = [NSMutableArray arrayWithCapacity:self.count];
  121. for (FSTDocument *doc in self.documentEnumerator) {
  122. [result addObject:doc];
  123. }
  124. return result;
  125. }
  126. - (const DocumentMap &)mapValue {
  127. return _index;
  128. }
  129. - (instancetype)documentSetByAddingDocument:(FSTDocument *_Nullable)document {
  130. // TODO(mcg): look into making document nonnull.
  131. if (!document) {
  132. return self;
  133. }
  134. // Remove any prior mapping of the document's key before adding, preventing sortedSet from
  135. // accumulating values that aren't in the index.
  136. FSTDocumentSet *removed = [self documentSetByRemovingKey:document.key];
  137. DocumentMap index = removed->_index.insert(document.key, document);
  138. SetType *set = [removed.sortedSet setByAddingObject:document];
  139. return [[FSTDocumentSet alloc] initWithIndex:std::move(index) set:set];
  140. }
  141. - (instancetype)documentSetByRemovingKey:(const DocumentKey &)key {
  142. FSTDocument *doc = [self documentForKey:key];
  143. if (!doc) {
  144. return self;
  145. }
  146. DocumentMap index = _index.erase(key);
  147. SetType *set = [self.sortedSet setByRemovingObject:doc];
  148. return [[FSTDocumentSet alloc] initWithIndex:std::move(index) set:set];
  149. }
  150. @end
  151. NS_ASSUME_NONNULL_END