FSTDocumentSet.mm 5.5 KB

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