FSTLRUGarbageCollector.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2018 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 <Foundation/Foundation.h>
  17. #include <string>
  18. #import "FIRFirestoreSettings.h"
  19. #import "Firestore/Source/Local/FSTQueryData.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  21. #include "Firestore/core/src/firebase/firestore/model/types.h"
  22. @protocol FSTQueryCache;
  23. @class FSTLRUGarbageCollector;
  24. extern const firebase::firestore::model::ListenSequenceNumber kFSTListenSequenceNumberInvalid;
  25. namespace firebase {
  26. namespace firestore {
  27. namespace local {
  28. struct LruParams {
  29. static const int64_t CacheSizeUnlimited = -1;
  30. static LruParams Default() {
  31. return LruParams{100 * 1024 * 1024, 10, 1000};
  32. }
  33. static LruParams Disabled() {
  34. return LruParams{kFIRFirestoreCacheSizeUnlimited, 0, 0};
  35. }
  36. static LruParams WithCacheSize(int64_t cacheSize) {
  37. LruParams params = Default();
  38. params.minBytesThreshold = cacheSize;
  39. return params;
  40. }
  41. int64_t minBytesThreshold;
  42. int percentileToCollect;
  43. int maximumSequenceNumbersToCollect;
  44. };
  45. struct LruResults {
  46. static LruResults DidNotRun() {
  47. return LruResults{/* didRun= */ false, 0, 0, 0};
  48. }
  49. bool didRun;
  50. int sequenceNumbersCollected;
  51. int targetsRemoved;
  52. int documentsRemoved;
  53. };
  54. } // namespace local
  55. } // namespace firestore
  56. } // namespace firebase
  57. /**
  58. * Persistence layers intending to use LRU Garbage collection should implement this protocol. This
  59. * protocol defines the operations that the LRU garbage collector needs from the persistence layer.
  60. */
  61. @protocol FSTLRUDelegate
  62. /**
  63. * Enumerates all the targets that the delegate is aware of. This is typically all of the targets in
  64. * an FSTQueryCache.
  65. */
  66. - (void)enumerateTargetsUsingBlock:(void (^)(FSTQueryData *queryData, BOOL *stop))block;
  67. /**
  68. * Enumerates all of the outstanding mutations.
  69. */
  70. - (void)enumerateMutationsUsingBlock:
  71. (void (^)(const firebase::firestore::model::DocumentKey &key,
  72. firebase::firestore::model::ListenSequenceNumber sequenceNumber,
  73. BOOL *stop))block;
  74. /**
  75. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  76. * to the given sequence number. Returns the number of documents removed.
  77. */
  78. - (int)removeOrphanedDocumentsThroughSequenceNumber:
  79. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber;
  80. /**
  81. * Removes all targets that are not currently being listened to and have a sequence number less than
  82. * or equal to the given sequence number. Returns the number of targets removed.
  83. */
  84. - (int)removeTargetsThroughSequenceNumber:
  85. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber
  86. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries;
  87. - (size_t)byteSize;
  88. /** Returns the number of targets and orphaned documents cached. */
  89. - (int32_t)sequenceNumberCount;
  90. /** Access to the underlying LRU Garbage collector instance. */
  91. @property(strong, nonatomic, readonly) FSTLRUGarbageCollector *gc;
  92. @end
  93. /**
  94. * FSTLRUGarbageCollector defines the LRU algorithm used to clean up old documents and targets. It
  95. * is persistence-agnostic, as long as proper delegate is provided.
  96. */
  97. @interface FSTLRUGarbageCollector : NSObject
  98. - (instancetype)initWithDelegate:(id<FSTLRUDelegate>)delegate
  99. params:(firebase::firestore::local::LruParams)params
  100. NS_DESIGNATED_INITIALIZER;
  101. - (instancetype)init NS_UNAVAILABLE;
  102. /**
  103. * Given a target percentile, return the number of queries that make up that percentage of the
  104. * queries that are cached. For instance, if 20 queries are cached, and the percentile is 40, the
  105. * result will be 8.
  106. */
  107. - (int)queryCountForPercentile:(NSUInteger)percentile;
  108. /**
  109. * Given a number of queries n, return the nth sequence number in the cache.
  110. */
  111. - (firebase::firestore::model::ListenSequenceNumber)sequenceNumberForQueryCount:
  112. (NSUInteger)queryCount;
  113. /**
  114. * Removes queries that are not currently live (as indicated by presence in the liveQueries map) and
  115. * have a sequence number less than or equal to the given sequence number.
  116. */
  117. - (int)removeQueriesUpThroughSequenceNumber:
  118. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber
  119. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries;
  120. /**
  121. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  122. * to the given sequence number. Returns the number of documents removed.
  123. */
  124. - (int)removeOrphanedDocumentsThroughSequenceNumber:
  125. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber;
  126. - (size_t)byteSize;
  127. - (firebase::firestore::local::LruResults)collectWithLiveTargets:
  128. (NSDictionary<NSNumber *, FSTQueryData *> *)liveTargets;
  129. @end