FSTMemoryQueryCache.mm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. @interface FSTMemoryQueryCache ()
  24. /** Maps a query to the data about that query. */
  25. @property(nonatomic, strong, readonly) NSMutableDictionary<FSTQuery *, FSTQueryData *> *queries;
  26. /** A ordered bidirectional mapping between documents and the remote target IDs. */
  27. @property(nonatomic, strong, readonly) FSTReferenceSet *references;
  28. /** The highest numbered target ID encountered. */
  29. @property(nonatomic, assign) FSTTargetID highestTargetID;
  30. @property(nonatomic, assign) FSTListenSequenceNumber highestListenSequenceNumber;
  31. /** The last received snapshot version. */
  32. @property(nonatomic, strong) FSTSnapshotVersion *lastRemoteSnapshotVersion;
  33. @end
  34. @implementation FSTMemoryQueryCache
  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. - (FSTTargetID)highestTargetID {
  49. return _highestTargetID;
  50. }
  51. - (FSTListenSequenceNumber)highestListenSequenceNumber {
  52. return _highestListenSequenceNumber;
  53. }
  54. /*- (FSTSnapshotVersion *)lastRemoteSnapshotVersion {
  55. return _lastRemoteSnapshotVersion;
  56. }
  57. - (void)setLastRemoteSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  58. group:(FSTWriteGroup *)group {
  59. _lastRemoteSnapshotVersion = snapshotVersion;
  60. }*/
  61. - (void)addQueryData:(FSTQueryData *)queryData {
  62. self.queries[queryData.query] = queryData;
  63. if (queryData.targetID > self.highestTargetID) {
  64. self.highestTargetID = queryData.targetID;
  65. }
  66. if (queryData.sequenceNumber > self.highestListenSequenceNumber) {
  67. self.highestListenSequenceNumber = queryData.sequenceNumber;
  68. }
  69. }
  70. - (void)updateQueryData:(FSTQueryData *)queryData {
  71. self.queries[queryData.query] = queryData;
  72. if (queryData.targetID > self.highestTargetID) {
  73. self.highestTargetID = queryData.targetID;
  74. }
  75. if (queryData.sequenceNumber > self.highestListenSequenceNumber) {
  76. self.highestListenSequenceNumber = queryData.sequenceNumber;
  77. }
  78. }
  79. - (int32_t)count {
  80. return (int32_t)[self.queries count];
  81. }
  82. - (void)removeQueryData:(FSTQueryData *)queryData {
  83. [self.queries removeObjectForKey:queryData.query];
  84. [self.references removeReferencesForID:queryData.targetID];
  85. }
  86. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query {
  87. return self.queries[query];
  88. }
  89. #pragma mark Reference tracking
  90. - (void)addMatchingKeys:(FSTDocumentKeySet *)keys forTargetID:(FSTTargetID)targetID {
  91. [self.references addReferencesToKeys:keys forID:targetID];
  92. }
  93. - (void)removeMatchingKeys:(FSTDocumentKeySet *)keys forTargetID:(FSTTargetID)targetID {
  94. [self.references removeReferencesToKeys:keys forID:targetID];
  95. }
  96. - (void)removeMatchingKeysForTargetID:(FSTTargetID)targetID {
  97. [self.references removeReferencesForID:targetID];
  98. }
  99. - (FSTDocumentKeySet *)matchingKeysForTargetID:(FSTTargetID)targetID {
  100. return [self.references referencedKeysForID:targetID];
  101. }
  102. #pragma mark - FSTGarbageSource implementation
  103. - (nullable id<FSTGarbageCollector>)garbageCollector {
  104. return self.references.garbageCollector;
  105. }
  106. - (void)setGarbageCollector:(nullable id<FSTGarbageCollector>)garbageCollector {
  107. self.references.garbageCollector = garbageCollector;
  108. }
  109. - (BOOL)containsKey:(const firebase::firestore::model::DocumentKey &)key {
  110. return [self.references containsKey:key];
  111. }
  112. @end
  113. NS_ASSUME_NONNULL_END