FSTLRUGarbageCollector.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/Local/FSTQueryData.h"
  18. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  19. #include "Firestore/core/src/firebase/firestore/model/types.h"
  20. @protocol FSTQueryCache;
  21. @class FSTLRUGarbageCollector;
  22. extern const firebase::firestore::model::ListenSequenceNumber 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:
  37. (void (^)(const firebase::firestore::model::DocumentKey &key,
  38. firebase::firestore::model::ListenSequenceNumber sequenceNumber,
  39. BOOL *stop))block;
  40. /**
  41. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  42. * to the given sequence number. Returns the number of documents removed.
  43. */
  44. - (int)removeOrphanedDocumentsThroughSequenceNumber:
  45. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber;
  46. /**
  47. * Removes all targets that are not currently being listened to and have a sequence number less than
  48. * or equal to the given sequence number. Returns the number of targets removed.
  49. */
  50. - (int)removeTargetsThroughSequenceNumber:
  51. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber
  52. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries;
  53. /** Access to the underlying LRU Garbage collector instance. */
  54. @property(strong, nonatomic, readonly) FSTLRUGarbageCollector *gc;
  55. @end
  56. /**
  57. * FSTLRUGarbageCollector defines the LRU algorithm used to clean up old documents and targets. It
  58. * is persistence-agnostic, as long as proper delegate is provided.
  59. */
  60. @interface FSTLRUGarbageCollector : NSObject
  61. - (instancetype)initWithQueryCache:(id<FSTQueryCache>)queryCache
  62. delegate:(id<FSTLRUDelegate>)delegate;
  63. /**
  64. * Given a target percentile, return the number of queries that make up that percentage of the
  65. * queries that are cached. For instance, if 20 queries are cached, and the percentile is 40, the
  66. * result will be 8.
  67. */
  68. - (int)queryCountForPercentile:(NSUInteger)percentile;
  69. /**
  70. * Given a number of queries n, return the nth sequence number in the cache.
  71. */
  72. - (firebase::firestore::model::ListenSequenceNumber)sequenceNumberForQueryCount:
  73. (NSUInteger)queryCount;
  74. /**
  75. * Removes queries that are not currently live (as indicated by presence in the liveQueries map) and
  76. * have a sequence number less than or equal to the given sequence number.
  77. */
  78. - (int)removeQueriesUpThroughSequenceNumber:
  79. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber
  80. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries;
  81. /**
  82. * Removes all unreferenced documents from the cache that have a sequence number less than or equal
  83. * to the given sequence number. Returns the number of documents removed.
  84. */
  85. - (int)removeOrphanedDocumentsThroughSequenceNumber:
  86. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber;
  87. @end