FSTMemoryQueryCache.mm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include <utility>
  18. #import "Firestore/Source/Core/FSTQuery.h"
  19. #import "Firestore/Source/Local/FSTMemoryPersistence.h"
  20. #import "Firestore/Source/Local/FSTQueryData.h"
  21. #import "Firestore/Source/Local/FSTReferenceSet.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  23. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  24. using firebase::firestore::model::SnapshotVersion;
  25. using firebase::firestore::model::DocumentKeySet;
  26. using firebase::firestore::model::DocumentKey;
  27. NS_ASSUME_NONNULL_BEGIN
  28. @interface FSTMemoryQueryCache ()
  29. /** Maps a query to the data about that query. */
  30. @property(nonatomic, strong, readonly) NSMutableDictionary<FSTQuery *, FSTQueryData *> *queries;
  31. /** A ordered bidirectional mapping between documents and the remote target IDs. */
  32. @property(nonatomic, strong, readonly) FSTReferenceSet *references;
  33. /** The highest numbered target ID encountered. */
  34. @property(nonatomic, assign) FSTTargetID highestTargetID;
  35. @property(nonatomic, assign) FSTListenSequenceNumber highestListenSequenceNumber;
  36. @end
  37. @implementation FSTMemoryQueryCache {
  38. FSTMemoryPersistence *_persistence;
  39. /** The last received snapshot version. */
  40. SnapshotVersion _lastRemoteSnapshotVersion;
  41. }
  42. - (instancetype)initWithPersistence:(FSTMemoryPersistence *)persistence {
  43. if (self = [super init]) {
  44. _persistence = persistence;
  45. _queries = [NSMutableDictionary dictionary];
  46. _references = [[FSTReferenceSet alloc] init];
  47. _lastRemoteSnapshotVersion = SnapshotVersion::None();
  48. }
  49. return self;
  50. }
  51. #pragma mark - FSTQueryCache implementation
  52. #pragma mark Query tracking
  53. - (void)start {
  54. // Nothing to do.
  55. }
  56. - (FSTTargetID)highestTargetID {
  57. return _highestTargetID;
  58. }
  59. - (FSTListenSequenceNumber)highestListenSequenceNumber {
  60. return _highestListenSequenceNumber;
  61. }
  62. - (const SnapshotVersion &)lastRemoteSnapshotVersion {
  63. return _lastRemoteSnapshotVersion;
  64. }
  65. - (void)setLastRemoteSnapshotVersion:(SnapshotVersion)snapshotVersion {
  66. _lastRemoteSnapshotVersion = std::move(snapshotVersion);
  67. }
  68. - (void)addQueryData:(FSTQueryData *)queryData {
  69. self.queries[queryData.query] = queryData;
  70. if (queryData.targetID > self.highestTargetID) {
  71. self.highestTargetID = queryData.targetID;
  72. }
  73. if (queryData.sequenceNumber > self.highestListenSequenceNumber) {
  74. self.highestListenSequenceNumber = queryData.sequenceNumber;
  75. }
  76. }
  77. - (void)updateQueryData:(FSTQueryData *)queryData {
  78. self.queries[queryData.query] = queryData;
  79. if (queryData.targetID > self.highestTargetID) {
  80. self.highestTargetID = queryData.targetID;
  81. }
  82. if (queryData.sequenceNumber > self.highestListenSequenceNumber) {
  83. self.highestListenSequenceNumber = queryData.sequenceNumber;
  84. }
  85. }
  86. - (int32_t)count {
  87. return (int32_t)[self.queries count];
  88. }
  89. - (void)removeQueryData:(FSTQueryData *)queryData {
  90. [self.queries removeObjectForKey:queryData.query];
  91. [self.references removeReferencesForID:queryData.targetID];
  92. }
  93. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query {
  94. return self.queries[query];
  95. }
  96. #pragma mark Reference tracking
  97. - (void)addMatchingKeys:(const DocumentKeySet &)keys forTargetID:(FSTTargetID)targetID {
  98. [self.references addReferencesToKeys:keys forID:targetID];
  99. for (const DocumentKey &key : keys) {
  100. [_persistence.referenceDelegate addReference:key target:targetID];
  101. }
  102. }
  103. - (void)removeMatchingKeys:(const DocumentKeySet &)keys forTargetID:(FSTTargetID)targetID {
  104. [self.references removeReferencesToKeys:keys forID:targetID];
  105. for (const DocumentKey &key : keys) {
  106. [_persistence.referenceDelegate removeReference:key target:targetID];
  107. }
  108. }
  109. - (void)removeMatchingKeysForTargetID:(FSTTargetID)targetID {
  110. [self.references removeReferencesForID:targetID];
  111. }
  112. - (DocumentKeySet)matchingKeysForTargetID:(FSTTargetID)targetID {
  113. return [self.references referencedKeysForID:targetID];
  114. }
  115. #pragma mark - FSTGarbageSource implementation
  116. - (nullable id<FSTGarbageCollector>)garbageCollector {
  117. return self.references.garbageCollector;
  118. }
  119. - (void)setGarbageCollector:(nullable id<FSTGarbageCollector>)garbageCollector {
  120. self.references.garbageCollector = garbageCollector;
  121. }
  122. - (BOOL)containsKey:(const firebase::firestore::model::DocumentKey &)key {
  123. return [self.references containsKey:key];
  124. }
  125. @end
  126. NS_ASSUME_NONNULL_END