FSTMemoryQueryCache.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/Local/FSTMemoryQueryCache.h"
  17. #import "Firestore/Source/Core/FSTQuery.h"
  18. #import "Firestore/Source/Core/FSTSnapshotVersion.h"
  19. #import "Firestore/Source/Local/FSTQueryData.h"
  20. #import "Firestore/Source/Local/FSTReferenceSet.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface FSTMemoryQueryCache ()
  23. /** Maps a query to the data about that query. */
  24. @property(nonatomic, strong, readonly) NSMutableDictionary<FSTQuery *, FSTQueryData *> *queries;
  25. /** A ordered bidirectional mapping between documents and the remote target IDs. */
  26. @property(nonatomic, strong, readonly) FSTReferenceSet *references;
  27. /** The highest numbered target ID encountered. */
  28. @property(nonatomic, assign) FSTTargetID highestTargetID;
  29. @end
  30. @implementation FSTMemoryQueryCache {
  31. /** The last received snapshot version. */
  32. FSTSnapshotVersion *_lastRemoteSnapshotVersion;
  33. }
  34. - (instancetype)init {
  35. if (self = [super init]) {
  36. _queries = [NSMutableDictionary dictionary];
  37. _references = [[FSTReferenceSet alloc] init];
  38. _lastRemoteSnapshotVersion = [FSTSnapshotVersion noVersion];
  39. }
  40. return self;
  41. }
  42. #pragma mark - FSTQueryCache implementation
  43. #pragma mark Query tracking
  44. - (void)start {
  45. // Nothing to do.
  46. }
  47. - (void)shutdown {
  48. // No resources to release.
  49. }
  50. - (FSTTargetID)highestTargetID {
  51. return _highestTargetID;
  52. }
  53. - (FSTSnapshotVersion *)lastRemoteSnapshotVersion {
  54. return _lastRemoteSnapshotVersion;
  55. }
  56. - (void)setLastRemoteSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  57. group:(FSTWriteGroup *)group {
  58. _lastRemoteSnapshotVersion = snapshotVersion;
  59. }
  60. - (void)addQueryData:(FSTQueryData *)queryData group:(__unused FSTWriteGroup *)group {
  61. self.queries[queryData.query] = queryData;
  62. if (queryData.targetID > self.highestTargetID) {
  63. self.highestTargetID = queryData.targetID;
  64. }
  65. }
  66. - (void)removeQueryData:(FSTQueryData *)queryData group:(__unused FSTWriteGroup *)group {
  67. [self.queries removeObjectForKey:queryData.query];
  68. [self.references removeReferencesForID:queryData.targetID];
  69. }
  70. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query {
  71. return self.queries[query];
  72. }
  73. #pragma mark Reference tracking
  74. - (void)addMatchingKeys:(FSTDocumentKeySet *)keys
  75. forTargetID:(FSTTargetID)targetID
  76. group:(__unused FSTWriteGroup *)group {
  77. [self.references addReferencesToKeys:keys forID:targetID];
  78. }
  79. - (void)removeMatchingKeys:(FSTDocumentKeySet *)keys
  80. forTargetID:(FSTTargetID)targetID
  81. group:(__unused FSTWriteGroup *)group {
  82. [self.references removeReferencesToKeys:keys forID:targetID];
  83. }
  84. - (void)removeMatchingKeysForTargetID:(FSTTargetID)targetID group:(__unused FSTWriteGroup *)group {
  85. [self.references removeReferencesForID:targetID];
  86. }
  87. - (FSTDocumentKeySet *)matchingKeysForTargetID:(FSTTargetID)targetID {
  88. return [self.references referencedKeysForID:targetID];
  89. }
  90. #pragma mark - FSTGarbageSource implementation
  91. - (nullable id<FSTGarbageCollector>)garbageCollector {
  92. return self.references.garbageCollector;
  93. }
  94. - (void)setGarbageCollector:(nullable id<FSTGarbageCollector>)garbageCollector {
  95. self.references.garbageCollector = garbageCollector;
  96. }
  97. - (BOOL)containsKey:(FSTDocumentKey *)key {
  98. return [self.references containsKey:key];
  99. }
  100. @end
  101. NS_ASSUME_NONNULL_END