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/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. extern const firebase::firestore::model::ListenSequenceNumber kFSTListenSequenceNumberInvalid;
  26. namespace firebase {
  27. namespace firestore {
  28. namespace local {
  29. struct LruParams {
  30. static const int64_t CacheSizeUnlimited = -1;
  31. static LruParams Default() {
  32. return LruParams{100 * 1024 * 1024, 10, 1000};
  33. }
  34. static LruParams Disabled() {
  35. return LruParams{kFIRFirestoreCacheSizeUnlimited, 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. /**
  59. * Persistence layers intending to use LRU Garbage collection should implement this protocol. This
  60. * protocol defines the operations that the LRU garbage collector needs from the persistence layer.
  61. */
  62. @protocol FSTLRUDelegate
  63. /**
  64. * Enumerates all the targets that the delegate is aware of. This is typically all of the targets in
  65. * an FSTQueryCache.
  66. */
  67. - (void)enumerateTargetsUsingCallback:(const firebase::firestore::local::TargetCallback &)callback;
  68. /**
  69. * Enumerates all of the outstanding mutations.
  70. */
  71. - (void)enumerateMutationsUsingCallback:
  72. (const firebase::firestore::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:
  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:
  86. (const std::unordered_map<firebase::firestore::model::TargetId,
  87. FSTQueryData *> &)liveQueries;
  88. - (size_t)byteSize;
  89. /** Returns the number of targets and orphaned documents cached. */
  90. - (size_t)sequenceNumberCount;
  91. /** Access to the underlying LRU Garbage collector instance. */
  92. @property(strong, nonatomic, readonly) FSTLRUGarbageCollector *gc;
  93. @end
  94. /**
  95. * FSTLRUGarbageCollector defines the LRU algorithm used to clean up old documents and targets. It
  96. * is persistence-agnostic, as long as proper delegate is provided.
  97. */
  98. @interface FSTLRUGarbageCollector : NSObject
  99. - (instancetype)initWithDelegate:(id<FSTLRUDelegate>)delegate
  100. params:(firebase::firestore::local::LruParams)params
  101. NS_DESIGNATED_INITIALIZER;
  102. - (instancetype)init NS_UNAVAILABLE;
  103. /**
  104. * Given a target percentile, return the number of queries that make up that percentage of the
  105. * queries that are cached. For instance, if 20 queries are cached, and the percentile is 40, the
  106. * result will be 8.
  107. */
  108. - (int)queryCountForPercentile:(NSUInteger)percentile;
  109. /**
  110. * Given a number of queries n, return the nth sequence number in the cache.
  111. */
  112. - (firebase::firestore::model::ListenSequenceNumber)sequenceNumberForQueryCount:
  113. (NSUInteger)queryCount;
  114. /**
  115. * Removes queries that are not currently live (as indicated by presence in the liveQueries map) and
  116. * have a sequence number less than or equal to the given sequence number.
  117. */
  118. - (int)removeQueriesUpThroughSequenceNumber:
  119. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber
  120. liveQueries:
  121. (const std::unordered_map<firebase::firestore::model::TargetId,
  122. FSTQueryData *> &)liveQueries;
  123. /**
  124. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  125. * to the given sequence number. Returns the number of documents removed.
  126. */
  127. - (int)removeOrphanedDocumentsThroughSequenceNumber:
  128. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber;
  129. - (size_t)byteSize;
  130. - (firebase::firestore::local::LruResults)collectWithLiveTargets:
  131. (const std::unordered_map<firebase::firestore::model::TargetId, FSTQueryData *> &)liveTargets;
  132. @end