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. #include <unordered_map>
  19. #import "Firestore/Source/Local/FSTQueryData.h"
  20. #include "Firestore/core/src/firebase/firestore/api/settings.h"
  21. #include "Firestore/core/src/firebase/firestore/local/query_cache.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  23. #include "Firestore/core/src/firebase/firestore/model/types.h"
  24. @class FSTLRUGarbageCollector;
  25. namespace model = firebase::firestore::model;
  26. extern const model::ListenSequenceNumber kFSTListenSequenceNumberInvalid;
  27. namespace firebase {
  28. namespace firestore {
  29. namespace local {
  30. struct LruParams {
  31. static LruParams Default() {
  32. return LruParams{100 * 1024 * 1024, 10, 1000};
  33. }
  34. static LruParams Disabled() {
  35. return LruParams{api::Settings::CacheSizeUnlimited, 0, 0};
  36. }
  37. static LruParams WithCacheSize(int64_t cacheSize) {
  38. LruParams params = Default();
  39. params.minBytesThreshold = cacheSize;
  40. return params;
  41. }
  42. int64_t minBytesThreshold;
  43. int percentileToCollect;
  44. int maximumSequenceNumbersToCollect;
  45. };
  46. struct LruResults {
  47. static LruResults DidNotRun() {
  48. return LruResults{/* didRun= */ false, 0, 0, 0};
  49. }
  50. bool didRun;
  51. int sequenceNumbersCollected;
  52. int targetsRemoved;
  53. int documentsRemoved;
  54. };
  55. } // namespace local
  56. } // namespace firestore
  57. } // namespace firebase
  58. namespace local = firebase::firestore::local;
  59. /**
  60. * Persistence layers intending to use LRU Garbage collection should implement this protocol. This
  61. * protocol defines the operations that the LRU garbage collector needs from the persistence layer.
  62. */
  63. @protocol FSTLRUDelegate
  64. /**
  65. * Enumerates all the targets that the delegate is aware of. This is typically all of the targets in
  66. * an FSTQueryCache.
  67. */
  68. - (void)enumerateTargetsUsingCallback:(const local::TargetCallback &)callback;
  69. /**
  70. * Enumerates all of the outstanding mutations.
  71. */
  72. - (void)enumerateMutationsUsingCallback:(const local::OrphanedDocumentCallback &)callback;
  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:(model::ListenSequenceNumber)sequenceNumber;
  78. /**
  79. * Removes all targets that are not currently being listened to and have a sequence number less than
  80. * or equal to the given sequence number. Returns the number of targets removed.
  81. */
  82. - (int)removeTargetsThroughSequenceNumber:(model::ListenSequenceNumber)sequenceNumber
  83. liveQueries:
  84. (const std::unordered_map<model::TargetId, FSTQueryData *> &)
  85. 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:(local::LruParams)params NS_DESIGNATED_INITIALIZER;
  99. - (instancetype)init NS_UNAVAILABLE;
  100. /**
  101. * Given a target percentile, return the number of queries that make up that percentage of the
  102. * queries that are cached. For instance, if 20 queries are cached, and the percentile is 40, the
  103. * result will be 8.
  104. */
  105. - (int)queryCountForPercentile:(NSUInteger)percentile;
  106. /**
  107. * Given a number of queries n, return the nth sequence number in the cache.
  108. */
  109. - (model::ListenSequenceNumber)sequenceNumberForQueryCount:(NSUInteger)queryCount;
  110. /**
  111. * Removes queries that are not currently live (as indicated by presence in the liveQueries map) and
  112. * have a sequence number less than or equal to the given sequence number.
  113. */
  114. - (int)removeQueriesUpThroughSequenceNumber:(model::ListenSequenceNumber)sequenceNumber
  115. liveQueries:
  116. (const std::unordered_map<model::TargetId, FSTQueryData *> &)
  117. liveQueries;
  118. /**
  119. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  120. * to the given sequence number. Returns the number of documents removed.
  121. */
  122. - (int)removeOrphanedDocumentsThroughSequenceNumber:(model::ListenSequenceNumber)sequenceNumber;
  123. - (size_t)byteSize;
  124. - (local::LruResults)collectWithLiveTargets:
  125. (const std::unordered_map<model::TargetId, FSTQueryData *> &)liveTargets;
  126. @end