FSTMemoryQueryCache.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. @property(nonatomic, assign) FSTListenSequenceNumber highestListenSequenceNumber;
  30. @end
  31. @implementation FSTMemoryQueryCache {
  32. /** The last received snapshot version. */
  33. FSTSnapshotVersion *_lastRemoteSnapshotVersion;
  34. }
  35. - (instancetype)init {
  36. if (self = [super init]) {
  37. _queries = [NSMutableDictionary dictionary];
  38. _references = [[FSTReferenceSet alloc] init];
  39. _lastRemoteSnapshotVersion = [FSTSnapshotVersion noVersion];
  40. }
  41. return self;
  42. }
  43. #pragma mark - FSTQueryCache implementation
  44. #pragma mark Query tracking
  45. - (void)start {
  46. // Nothing to do.
  47. }
  48. - (void)shutdown {
  49. // No resources to release.
  50. }
  51. - (FSTTargetID)highestTargetID {
  52. return _highestTargetID;
  53. }
  54. - (FSTListenSequenceNumber)highestListenSequenceNumber {
  55. return _highestListenSequenceNumber;
  56. }
  57. - (FSTSnapshotVersion *)lastRemoteSnapshotVersion {
  58. return _lastRemoteSnapshotVersion;
  59. }
  60. - (void)setLastRemoteSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  61. group:(FSTWriteGroup *)group {
  62. _lastRemoteSnapshotVersion = snapshotVersion;
  63. }
  64. - (void)addQueryData:(FSTQueryData *)queryData group:(__unused FSTWriteGroup *)group {
  65. self.queries[queryData.query] = queryData;
  66. if (queryData.targetID > self.highestTargetID) {
  67. self.highestTargetID = queryData.targetID;
  68. }
  69. if (queryData.sequenceNumber > self.highestListenSequenceNumber) {
  70. self.highestListenSequenceNumber = queryData.sequenceNumber;
  71. }
  72. }
  73. - (void)updateQueryData:(FSTQueryData *)queryData group:(FSTWriteGroup *)group {
  74. self.queries[queryData.query] = queryData;
  75. if (queryData.targetID > self.highestTargetID) {
  76. self.highestTargetID = queryData.targetID;
  77. }
  78. if (queryData.sequenceNumber > self.highestListenSequenceNumber) {
  79. self.highestListenSequenceNumber = queryData.sequenceNumber;
  80. }
  81. }
  82. - (int32_t)count {
  83. return (int32_t)[self.queries count];
  84. }
  85. - (void)removeQueryData:(FSTQueryData *)queryData group:(__unused FSTWriteGroup *)group {
  86. [self.queries removeObjectForKey:queryData.query];
  87. [self.references removeReferencesForID:queryData.targetID];
  88. }
  89. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query {
  90. return self.queries[query];
  91. }
  92. #pragma mark Reference tracking
  93. - (void)addMatchingKeys:(FSTDocumentKeySet *)keys
  94. forTargetID:(FSTTargetID)targetID
  95. group:(__unused FSTWriteGroup *)group {
  96. [self.references addReferencesToKeys:keys forID:targetID];
  97. }
  98. - (void)removeMatchingKeys:(FSTDocumentKeySet *)keys
  99. forTargetID:(FSTTargetID)targetID
  100. group:(__unused FSTWriteGroup *)group {
  101. [self.references removeReferencesToKeys:keys forID:targetID];
  102. }
  103. - (void)removeMatchingKeysForTargetID:(FSTTargetID)targetID group:(__unused FSTWriteGroup *)group {
  104. [self.references removeReferencesForID:targetID];
  105. }
  106. - (FSTDocumentKeySet *)matchingKeysForTargetID:(FSTTargetID)targetID {
  107. return [self.references referencedKeysForID:targetID];
  108. }
  109. #pragma mark - FSTGarbageSource implementation
  110. - (nullable id<FSTGarbageCollector>)garbageCollector {
  111. return self.references.garbageCollector;
  112. }
  113. - (void)setGarbageCollector:(nullable id<FSTGarbageCollector>)garbageCollector {
  114. self.references.garbageCollector = garbageCollector;
  115. }
  116. - (BOOL)containsKey:(FSTDocumentKey *)key {
  117. return [self.references containsKey:key];
  118. }
  119. @end
  120. NS_ASSUME_NONNULL_END