FSTLRUGarbageCollector.h 5.2 KB

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