FSTDocumentSet.m 5.7 KB

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