FSTRemoteEvent.mm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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/Remote/FSTRemoteEvent.h"
  17. #include <map>
  18. #include <utility>
  19. #import "Firestore/Source/Core/FSTSnapshotVersion.h"
  20. #import "Firestore/Source/Model/FSTDocument.h"
  21. #import "Firestore/Source/Remote/FSTWatchChange.h"
  22. #import "Firestore/Source/Util/FSTAssert.h"
  23. #import "Firestore/Source/Util/FSTClasses.h"
  24. #import "Firestore/Source/Util/FSTLogger.h"
  25. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  26. using firebase::firestore::model::DocumentKey;
  27. NS_ASSUME_NONNULL_BEGIN
  28. #pragma mark - FSTTargetMapping
  29. @interface FSTTargetMapping ()
  30. /** Private mutator method to add a document key to the mapping */
  31. - (void)addDocumentKey:(const DocumentKey &)documentKey;
  32. /** Private mutator method to remove a document key from the mapping */
  33. - (void)removeDocumentKey:(const DocumentKey &)documentKey;
  34. @end
  35. @implementation FSTTargetMapping
  36. - (void)addDocumentKey:(const DocumentKey &)documentKey {
  37. @throw FSTAbstractMethodException(); // NOLINT
  38. }
  39. - (void)removeDocumentKey:(const DocumentKey &)documentKey {
  40. @throw FSTAbstractMethodException(); // NOLINT
  41. }
  42. @end
  43. #pragma mark - FSTResetMapping
  44. @interface FSTResetMapping ()
  45. @property(nonatomic, strong) FSTDocumentKeySet *documents;
  46. @end
  47. @implementation FSTResetMapping
  48. + (instancetype)mappingWithDocuments:(NSArray<FSTDocument *> *)documents {
  49. FSTResetMapping *mapping = [[FSTResetMapping alloc] init];
  50. for (FSTDocument *doc in documents) {
  51. mapping.documents = [mapping.documents setByAddingObject:doc.key];
  52. }
  53. return mapping;
  54. }
  55. - (instancetype)init {
  56. self = [super init];
  57. if (self) {
  58. _documents = [FSTDocumentKeySet keySet];
  59. }
  60. return self;
  61. }
  62. - (BOOL)isEqual:(id)other {
  63. if (other == self) {
  64. return YES;
  65. }
  66. if (![other isMemberOfClass:[FSTResetMapping class]]) {
  67. return NO;
  68. }
  69. FSTResetMapping *otherMapping = (FSTResetMapping *)other;
  70. return [self.documents isEqual:otherMapping.documents];
  71. }
  72. - (NSUInteger)hash {
  73. return self.documents.hash;
  74. }
  75. - (void)addDocumentKey:(const DocumentKey &)documentKey {
  76. self.documents = [self.documents setByAddingObject:documentKey];
  77. }
  78. - (void)removeDocumentKey:(const DocumentKey &)documentKey {
  79. self.documents = [self.documents setByRemovingObject:documentKey];
  80. }
  81. @end
  82. #pragma mark - FSTUpdateMapping
  83. @interface FSTUpdateMapping ()
  84. @property(nonatomic, strong) FSTDocumentKeySet *addedDocuments;
  85. @property(nonatomic, strong) FSTDocumentKeySet *removedDocuments;
  86. @end
  87. @implementation FSTUpdateMapping
  88. + (FSTUpdateMapping *)mappingWithAddedDocuments:(NSArray<FSTDocument *> *)added
  89. removedDocuments:(NSArray<FSTDocument *> *)removed {
  90. FSTUpdateMapping *mapping = [[FSTUpdateMapping alloc] init];
  91. for (FSTDocument *doc in added) {
  92. mapping.addedDocuments = [mapping.addedDocuments setByAddingObject:doc.key];
  93. }
  94. for (FSTDocument *doc in removed) {
  95. mapping.removedDocuments = [mapping.removedDocuments setByAddingObject:doc.key];
  96. }
  97. return mapping;
  98. }
  99. - (instancetype)init {
  100. self = [super init];
  101. if (self) {
  102. _addedDocuments = [FSTDocumentKeySet keySet];
  103. _removedDocuments = [FSTDocumentKeySet keySet];
  104. }
  105. return self;
  106. }
  107. - (BOOL)isEqual:(id)other {
  108. if (other == self) {
  109. return YES;
  110. }
  111. if (![other isMemberOfClass:[FSTUpdateMapping class]]) {
  112. return NO;
  113. }
  114. FSTUpdateMapping *otherMapping = (FSTUpdateMapping *)other;
  115. return [self.addedDocuments isEqual:otherMapping.addedDocuments] &&
  116. [self.removedDocuments isEqual:otherMapping.removedDocuments];
  117. }
  118. - (NSUInteger)hash {
  119. return self.addedDocuments.hash * 31 + self.removedDocuments.hash;
  120. }
  121. - (FSTDocumentKeySet *)applyTo:(FSTDocumentKeySet *)keys {
  122. __block FSTDocumentKeySet *result = keys;
  123. [self.addedDocuments enumerateObjectsUsingBlock:^(FSTDocumentKey *key, BOOL *stop) {
  124. result = [result setByAddingObject:key];
  125. }];
  126. [self.removedDocuments enumerateObjectsUsingBlock:^(FSTDocumentKey *key, BOOL *stop) {
  127. result = [result setByRemovingObject:key];
  128. }];
  129. return result;
  130. }
  131. - (void)addDocumentKey:(const DocumentKey &)documentKey {
  132. self.addedDocuments = [self.addedDocuments setByAddingObject:documentKey];
  133. self.removedDocuments = [self.removedDocuments setByRemovingObject:documentKey];
  134. }
  135. - (void)removeDocumentKey:(const DocumentKey &)documentKey {
  136. self.addedDocuments = [self.addedDocuments setByRemovingObject:documentKey];
  137. self.removedDocuments = [self.removedDocuments setByAddingObject:documentKey];
  138. }
  139. @end
  140. #pragma mark - FSTTargetChange
  141. @interface FSTTargetChange ()
  142. @property(nonatomic, assign) FSTCurrentStatusUpdate currentStatusUpdate;
  143. @property(nonatomic, strong, nullable) FSTTargetMapping *mapping;
  144. @property(nonatomic, strong) FSTSnapshotVersion *snapshotVersion;
  145. @property(nonatomic, strong) NSData *resumeToken;
  146. @end
  147. @implementation FSTTargetChange
  148. - (instancetype)init {
  149. if (self = [super init]) {
  150. _currentStatusUpdate = FSTCurrentStatusUpdateNone;
  151. _resumeToken = [NSData data];
  152. }
  153. return self;
  154. }
  155. + (instancetype)changeWithDocuments:(NSArray<FSTMaybeDocument *> *)docs
  156. currentStatusUpdate:(FSTCurrentStatusUpdate)currentStatusUpdate {
  157. FSTUpdateMapping *mapping = [[FSTUpdateMapping alloc] init];
  158. for (FSTMaybeDocument *doc in docs) {
  159. if ([doc isKindOfClass:[FSTDeletedDocument class]]) {
  160. mapping.removedDocuments = [mapping.removedDocuments setByAddingObject:doc.key];
  161. } else {
  162. mapping.addedDocuments = [mapping.addedDocuments setByAddingObject:doc.key];
  163. }
  164. }
  165. FSTTargetChange *change = [[FSTTargetChange alloc] init];
  166. change.mapping = mapping;
  167. change.currentStatusUpdate = currentStatusUpdate;
  168. return change;
  169. }
  170. + (instancetype)changeWithMapping:(FSTTargetMapping *)mapping
  171. snapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  172. currentStatusUpdate:(FSTCurrentStatusUpdate)currentStatusUpdate {
  173. FSTTargetChange *change = [[FSTTargetChange alloc] init];
  174. change.mapping = mapping;
  175. change.snapshotVersion = snapshotVersion;
  176. change.currentStatusUpdate = currentStatusUpdate;
  177. return change;
  178. }
  179. - (FSTTargetMapping *)mapping {
  180. if (!_mapping) {
  181. // Create an FSTUpdateMapping by default, since resets are always explicit
  182. _mapping = [[FSTUpdateMapping alloc] init];
  183. }
  184. return _mapping;
  185. }
  186. /**
  187. * Sets the resume token but only when it has a new value. Empty resumeTokens are
  188. * discarded.
  189. */
  190. - (void)setResumeToken:(NSData *)resumeToken {
  191. if (resumeToken.length > 0) {
  192. _resumeToken = resumeToken;
  193. }
  194. }
  195. @end
  196. #pragma mark - FSTRemoteEvent
  197. @interface FSTRemoteEvent () {
  198. NSMutableDictionary<FSTBoxedTargetID *, FSTTargetChange *> *_targetChanges;
  199. }
  200. - (instancetype)
  201. initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  202. targetChanges:(NSMutableDictionary<FSTBoxedTargetID *, FSTTargetChange *> *)targetChanges
  203. documentUpdates:(std::map<DocumentKey, FSTMaybeDocument *>)documentUpdates;
  204. @property(nonatomic, strong) FSTSnapshotVersion *snapshotVersion;
  205. @end
  206. @implementation FSTRemoteEvent {
  207. std::map<DocumentKey, FSTMaybeDocument *> _documentUpdates;
  208. }
  209. + (instancetype)
  210. eventWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  211. targetChanges:(NSMutableDictionary<NSNumber *, FSTTargetChange *> *)targetChanges
  212. documentUpdates:(std::map<DocumentKey, FSTMaybeDocument *>)documentUpdates {
  213. return [[FSTRemoteEvent alloc] initWithSnapshotVersion:snapshotVersion
  214. targetChanges:targetChanges
  215. documentUpdates:std::move(documentUpdates)];
  216. }
  217. - (instancetype)initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  218. targetChanges:
  219. (NSMutableDictionary<NSNumber *, FSTTargetChange *> *)targetChanges
  220. documentUpdates:(std::map<DocumentKey, FSTMaybeDocument *>)documentUpdates {
  221. self = [super init];
  222. if (self) {
  223. _snapshotVersion = snapshotVersion;
  224. _targetChanges = targetChanges;
  225. _documentUpdates = std::move(documentUpdates);
  226. }
  227. return self;
  228. }
  229. - (NSDictionary<FSTBoxedTargetID *, FSTTargetChange *> *)targetChanges {
  230. return static_cast<NSDictionary<FSTBoxedTargetID *, FSTTargetChange *> *>(_targetChanges);
  231. }
  232. - (const std::map<DocumentKey, FSTMaybeDocument *> &)documentUpdates {
  233. return _documentUpdates;
  234. }
  235. /** Adds a document update to this remote event */
  236. - (void)addDocumentUpdate:(FSTMaybeDocument *)document {
  237. _documentUpdates[document.key] = document;
  238. }
  239. /** Handles an existence filter mismatch */
  240. - (void)handleExistenceFilterMismatchForTargetID:(FSTBoxedTargetID *)targetID {
  241. // An existence filter mismatch will reset the query and we need to reset the mapping to contain
  242. // no documents and an empty resume token.
  243. //
  244. // Note:
  245. // * The reset mapping is empty, specifically forcing the consumer of the change to
  246. // forget all keys for this targetID;
  247. // * The resume snapshot for this target must be reset
  248. // * The target must be unacked because unwatching and rewatching introduces a race for
  249. // changes.
  250. //
  251. // TODO(dimond): keep track of reset targets not to raise.
  252. FSTTargetChange *targetChange =
  253. [FSTTargetChange changeWithMapping:[[FSTResetMapping alloc] init]
  254. snapshotVersion:[FSTSnapshotVersion noVersion]
  255. currentStatusUpdate:FSTCurrentStatusUpdateMarkNotCurrent];
  256. _targetChanges[targetID] = targetChange;
  257. }
  258. @end
  259. #pragma mark - FSTWatchChangeAggregator
  260. @interface FSTWatchChangeAggregator ()
  261. /** The snapshot version for every target change this creates. */
  262. @property(nonatomic, strong, readonly) FSTSnapshotVersion *snapshotVersion;
  263. /** Keeps track of the current target mappings */
  264. @property(nonatomic, strong, readonly)
  265. NSMutableDictionary<FSTBoxedTargetID *, FSTTargetChange *> *targetChanges;
  266. /** The set of open listens on the client */
  267. @property(nonatomic, strong, readonly)
  268. NSDictionary<FSTBoxedTargetID *, FSTQueryData *> *listenTargets;
  269. /** Whether this aggregator was frozen and can no longer be modified */
  270. @property(nonatomic, assign) BOOL frozen;
  271. @end
  272. @implementation FSTWatchChangeAggregator {
  273. NSMutableDictionary<FSTBoxedTargetID *, FSTExistenceFilter *> *_existenceFilters;
  274. /** Keeps track of document to update */
  275. std::map<DocumentKey, FSTMaybeDocument *> _documentUpdates;
  276. }
  277. - (instancetype)
  278. initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  279. listenTargets:(NSDictionary<FSTBoxedTargetID *, FSTQueryData *> *)listenTargets
  280. pendingTargetResponses:(NSDictionary<FSTBoxedTargetID *, NSNumber *> *)pendingTargetResponses {
  281. self = [super init];
  282. if (self) {
  283. _snapshotVersion = snapshotVersion;
  284. _frozen = NO;
  285. _targetChanges = [NSMutableDictionary dictionary];
  286. _listenTargets = listenTargets;
  287. _pendingTargetResponses = [NSMutableDictionary dictionaryWithDictionary:pendingTargetResponses];
  288. _existenceFilters = [NSMutableDictionary dictionary];
  289. }
  290. return self;
  291. }
  292. - (NSDictionary<FSTBoxedTargetID *, FSTExistenceFilter *> *)existenceFilters {
  293. return static_cast<NSDictionary<FSTBoxedTargetID *, FSTExistenceFilter *> *>(_existenceFilters);
  294. }
  295. - (FSTTargetChange *)targetChangeForTargetID:(FSTBoxedTargetID *)targetID {
  296. FSTTargetChange *change = self.targetChanges[targetID];
  297. if (!change) {
  298. change = [[FSTTargetChange alloc] init];
  299. change.snapshotVersion = self.snapshotVersion;
  300. self.targetChanges[targetID] = change;
  301. }
  302. return change;
  303. }
  304. - (void)addWatchChanges:(NSArray<FSTWatchChange *> *)watchChanges {
  305. FSTAssert(!self.frozen, @"Trying to modify frozen FSTWatchChangeAggregator");
  306. for (FSTWatchChange *watchChange in watchChanges) {
  307. [self addWatchChange:watchChange];
  308. }
  309. }
  310. - (void)addWatchChange:(FSTWatchChange *)watchChange {
  311. FSTAssert(!self.frozen, @"Trying to modify frozen FSTWatchChangeAggregator");
  312. if ([watchChange isKindOfClass:[FSTDocumentWatchChange class]]) {
  313. [self addDocumentChange:(FSTDocumentWatchChange *)watchChange];
  314. } else if ([watchChange isKindOfClass:[FSTWatchTargetChange class]]) {
  315. [self addTargetChange:(FSTWatchTargetChange *)watchChange];
  316. } else if ([watchChange isKindOfClass:[FSTExistenceFilterWatchChange class]]) {
  317. [self addExistenceFilterChange:(FSTExistenceFilterWatchChange *)watchChange];
  318. } else {
  319. FSTFail(@"Unknown watch change: %@", watchChange);
  320. }
  321. }
  322. - (void)addDocumentChange:(FSTDocumentWatchChange *)docChange {
  323. BOOL relevant = NO;
  324. for (FSTBoxedTargetID *targetID in docChange.updatedTargetIDs) {
  325. if ([self isActiveTarget:targetID]) {
  326. FSTTargetChange *change = [self targetChangeForTargetID:targetID];
  327. [change.mapping addDocumentKey:docChange.documentKey];
  328. relevant = YES;
  329. }
  330. }
  331. for (FSTBoxedTargetID *targetID in docChange.removedTargetIDs) {
  332. if ([self isActiveTarget:targetID]) {
  333. FSTTargetChange *change = [self targetChangeForTargetID:targetID];
  334. [change.mapping removeDocumentKey:docChange.documentKey];
  335. relevant = YES;
  336. }
  337. }
  338. // Only update the document if there is a new document to replace, this might be just a target
  339. // update instead.
  340. if (docChange.document && relevant) {
  341. _documentUpdates[docChange.documentKey] = docChange.document;
  342. }
  343. }
  344. - (void)addTargetChange:(FSTWatchTargetChange *)targetChange {
  345. for (FSTBoxedTargetID *targetID in targetChange.targetIDs) {
  346. FSTTargetChange *change = [self targetChangeForTargetID:targetID];
  347. switch (targetChange.state) {
  348. case FSTWatchTargetChangeStateNoChange:
  349. if ([self isActiveTarget:targetID]) {
  350. // Creating the change above satisfies the semantics of no-change.
  351. change.resumeToken = targetChange.resumeToken;
  352. }
  353. break;
  354. case FSTWatchTargetChangeStateAdded:
  355. [self recordResponseForTargetID:targetID];
  356. if (![self.pendingTargetResponses objectForKey:targetID]) {
  357. // We have a freshly added target, so we need to reset any state that we had previously
  358. // This can happen e.g. when remove and add back a target for existence filter
  359. // mismatches.
  360. change.mapping = nil;
  361. change.currentStatusUpdate = FSTCurrentStatusUpdateNone;
  362. [_existenceFilters removeObjectForKey:targetID];
  363. }
  364. change.resumeToken = targetChange.resumeToken;
  365. break;
  366. case FSTWatchTargetChangeStateRemoved:
  367. // We need to keep track of removed targets to we can post-filter and remove any target
  368. // changes.
  369. [self recordResponseForTargetID:targetID];
  370. FSTAssert(!targetChange.cause, @"WatchChangeAggregator does not handle errored targets.");
  371. break;
  372. case FSTWatchTargetChangeStateCurrent:
  373. if ([self isActiveTarget:targetID]) {
  374. change.currentStatusUpdate = FSTCurrentStatusUpdateMarkCurrent;
  375. change.resumeToken = targetChange.resumeToken;
  376. }
  377. break;
  378. case FSTWatchTargetChangeStateReset:
  379. if ([self isActiveTarget:targetID]) {
  380. // Overwrite any existing target mapping with a reset mapping. Every subsequent update
  381. // will modify the reset mapping, not an update mapping.
  382. change.mapping = [[FSTResetMapping alloc] init];
  383. change.resumeToken = targetChange.resumeToken;
  384. }
  385. break;
  386. default:
  387. FSTWarn(@"Unknown target watch change type: %ld", (long)targetChange.state);
  388. }
  389. }
  390. }
  391. /**
  392. * Records that we got a watch target add/remove by decrementing the number of pending target
  393. * responses that we have.
  394. */
  395. - (void)recordResponseForTargetID:(FSTBoxedTargetID *)targetID {
  396. NSNumber *count = [self.pendingTargetResponses objectForKey:targetID];
  397. int newCount = count ? [count intValue] - 1 : -1;
  398. if (newCount == 0) {
  399. [self.pendingTargetResponses removeObjectForKey:targetID];
  400. } else {
  401. [self.pendingTargetResponses setObject:[NSNumber numberWithInt:newCount] forKey:targetID];
  402. }
  403. }
  404. /**
  405. * Returns true if the given targetId is active. Active targets are those for which there are no
  406. * pending requests to add a listen and are in the current list of targets the client cares about.
  407. *
  408. * Clients can repeatedly listen and stop listening to targets, so this check is useful in
  409. * preventing in preventing race conditions for a target where events arrive but the server hasn't
  410. * yet acknowledged the intended change in state.
  411. */
  412. - (BOOL)isActiveTarget:(FSTBoxedTargetID *)targetID {
  413. return [self.listenTargets objectForKey:targetID] &&
  414. ![self.pendingTargetResponses objectForKey:targetID];
  415. }
  416. - (void)addExistenceFilterChange:(FSTExistenceFilterWatchChange *)existenceFilterChange {
  417. FSTBoxedTargetID *targetID = @(existenceFilterChange.targetID);
  418. if ([self isActiveTarget:targetID]) {
  419. _existenceFilters[targetID] = existenceFilterChange.filter;
  420. }
  421. }
  422. - (FSTRemoteEvent *)remoteEvent {
  423. NSMutableDictionary<FSTBoxedTargetID *, FSTTargetChange *> *targetChanges = self.targetChanges;
  424. NSMutableArray *targetsToRemove = [NSMutableArray array];
  425. // Apply any inactive targets.
  426. for (FSTBoxedTargetID *targetID in [targetChanges keyEnumerator]) {
  427. if (![self isActiveTarget:targetID]) {
  428. [targetsToRemove addObject:targetID];
  429. }
  430. }
  431. [targetChanges removeObjectsForKeys:targetsToRemove];
  432. // Mark this aggregator as frozen so no further modifications are made.
  433. self.frozen = YES;
  434. return [FSTRemoteEvent eventWithSnapshotVersion:self.snapshotVersion
  435. targetChanges:targetChanges
  436. documentUpdates:_documentUpdates];
  437. }
  438. @end
  439. NS_ASSUME_NONNULL_END