FSTLRUGarbageCollector.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include <unordered_map>
  19. #import "FIRFirestoreSettings.h"
  20. #import "Firestore/Source/Local/FSTQueryData.h"
  21. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  22. #include "Firestore/core/src/firebase/firestore/model/types.h"
  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:
  87. (const std::unordered_map<firebase::firestore::model::TargetId,
  88. FSTQueryData *> &)liveQueries;
  89. - (size_t)byteSize;
  90. /** Returns the number of targets and orphaned documents cached. */
  91. - (size_t)sequenceNumberCount;
  92. /** Access to the underlying LRU Garbage collector instance. */
  93. @property(strong, nonatomic, readonly) FSTLRUGarbageCollector *gc;
  94. @end
  95. /**
  96. * FSTLRUGarbageCollector defines the LRU algorithm used to clean up old documents and targets. It
  97. * is persistence-agnostic, as long as proper delegate is provided.
  98. */
  99. @interface FSTLRUGarbageCollector : NSObject
  100. - (instancetype)initWithDelegate:(id<FSTLRUDelegate>)delegate
  101. params:(firebase::firestore::local::LruParams)params
  102. NS_DESIGNATED_INITIALIZER;
  103. - (instancetype)init NS_UNAVAILABLE;
  104. /**
  105. * Given a target percentile, return the number of queries that make up that percentage of the
  106. * queries that are cached. For instance, if 20 queries are cached, and the percentile is 40, the
  107. * result will be 8.
  108. */
  109. - (int)queryCountForPercentile:(NSUInteger)percentile;
  110. /**
  111. * Given a number of queries n, return the nth sequence number in the cache.
  112. */
  113. - (firebase::firestore::model::ListenSequenceNumber)sequenceNumberForQueryCount:
  114. (NSUInteger)queryCount;
  115. /**
  116. * Removes queries that are not currently live (as indicated by presence in the liveQueries map) and
  117. * have a sequence number less than or equal to the given sequence number.
  118. */
  119. - (int)removeQueriesUpThroughSequenceNumber:
  120. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber
  121. liveQueries:
  122. (const std::unordered_map<firebase::firestore::model::TargetId,
  123. FSTQueryData *> &)liveQueries;
  124. /**
  125. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  126. * to the given sequence number. Returns the number of documents removed.
  127. */
  128. - (int)removeOrphanedDocumentsThroughSequenceNumber:
  129. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber;
  130. - (size_t)byteSize;
  131. - (firebase::firestore::local::LruResults)collectWithLiveTargets:
  132. (const std::unordered_map<firebase::firestore::model::TargetId, FSTQueryData *> &)liveTargets;
  133. @end