FSTLRUGarbageCollector.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #import "Firestore/Source/Core/FSTTypes.h"
  18. #import "Firestore/Source/Local/FSTQueryData.h"
  19. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  20. @protocol FSTQueryCache;
  21. @class FSTLRUGarbageCollector;
  22. extern const FSTListenSequenceNumber kFSTListenSequenceNumberInvalid;
  23. /**
  24. * Persistence layers intending to use LRU Garbage collection should implement this protocol. This
  25. * protocol defines the operations that the LRU garbage collector needs from the persistence layer.
  26. */
  27. @protocol FSTLRUDelegate
  28. /**
  29. * Enumerates all the targets that the delegate is aware of. This is typically all of the targets in
  30. * an FSTQueryCache.
  31. */
  32. - (void)enumerateTargetsUsingBlock:(void (^)(FSTQueryData *queryData, BOOL *stop))block;
  33. /**
  34. * Enumerates all of the outstanding mutations.
  35. */
  36. - (void)enumerateMutationsUsingBlock:(void (^)(const firebase::firestore::model::DocumentKey &key,
  37. FSTListenSequenceNumber sequenceNumber,
  38. BOOL *stop))block;
  39. /**
  40. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  41. * to the given sequence number. Returns the number of documents removed.
  42. */
  43. - (int)removeOrphanedDocumentsThroughSequenceNumber:(FSTListenSequenceNumber)sequenceNumber;
  44. /**
  45. * Removes all targets that are not currently being listened to and have a sequence number less than
  46. * or equal to the given sequence number. Returns the number of targets removed.
  47. */
  48. - (int)removeTargetsThroughSequenceNumber:(FSTListenSequenceNumber)sequenceNumber
  49. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries;
  50. /** Access to the underlying LRU Garbage collector instance. */
  51. @property(strong, nonatomic, readonly) FSTLRUGarbageCollector *gc;
  52. @end
  53. /**
  54. * FSTLRUGarbageCollector defines the LRU algorithm used to clean up old documents and targets. It
  55. * is persistence-agnostic, as long as proper delegate is provided.
  56. */
  57. @interface FSTLRUGarbageCollector : NSObject
  58. - (instancetype)initWithQueryCache:(id<FSTQueryCache>)queryCache
  59. delegate:(id<FSTLRUDelegate>)delegate;
  60. /**
  61. * Given a target percentile, return the number of queries that make up that percentage of the
  62. * queries that are cached. For instance, if 20 queries are cached, and the percentile is 40, the
  63. * result will be 8.
  64. */
  65. - (int)queryCountForPercentile:(NSUInteger)percentile;
  66. /**
  67. * Given a number of queries n, return the nth sequence number in the cache.
  68. */
  69. - (FSTListenSequenceNumber)sequenceNumberForQueryCount:(NSUInteger)queryCount;
  70. /**
  71. * Removes queries that are not currently live (as indicated by presence in the liveQueries map) and
  72. * have a sequence number less than or equal to the given sequence number.
  73. */
  74. - (int)removeQueriesUpThroughSequenceNumber:(FSTListenSequenceNumber)sequenceNumber
  75. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries;
  76. /**
  77. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  78. * to the given sequence number. Returns the number of documents removed.
  79. */
  80. - (int)removeOrphanedDocumentsThroughSequenceNumber:(FSTListenSequenceNumber)sequenceNumber;
  81. @end