Files
firebase-ios-sdk/Firestore/Source/Local/FSTLRUGarbageCollector.h
T
Gil 9a3ba6e66d Make disabling GC prevent even scheduling it (#2862)
* Make disabling GC prevent even scheduling it

Make the linkage of kFIRFirestoreCacheSizeUnlimited explicitly
extern "C". This previously had no fixed linkage in the header and must
have accidentally worked because the implementation had C++ linkage.

Move internal CacheSizeUnlimited to api::Settings and the
definition of kFIRFirestoreCacheSizeUnlimited into the Objective-C
language binding.

Migrate settings defaults to C++ settings

Ensure that all defaults are compiler initialized to ensure there are no
initialization order problems.
2019-04-18 18:17:34 -07:00

162 lines
5.2 KiB
Objective-C

/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <Foundation/Foundation.h>
#include <string>
#include <unordered_map>
#import "Firestore/Source/Local/FSTQueryData.h"
#include "Firestore/core/src/firebase/firestore/api/settings.h"
#include "Firestore/core/src/firebase/firestore/local/query_cache.h"
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/src/firebase/firestore/model/types.h"
@class FSTLRUGarbageCollector;
namespace model = firebase::firestore::model;
extern const model::ListenSequenceNumber kFSTListenSequenceNumberInvalid;
namespace firebase {
namespace firestore {
namespace local {
struct LruParams {
static LruParams Default() {
return LruParams{100 * 1024 * 1024, 10, 1000};
}
static LruParams Disabled() {
return LruParams{api::Settings::CacheSizeUnlimited, 0, 0};
}
static LruParams WithCacheSize(int64_t cacheSize) {
LruParams params = Default();
params.minBytesThreshold = cacheSize;
return params;
}
int64_t minBytesThreshold;
int percentileToCollect;
int maximumSequenceNumbersToCollect;
};
struct LruResults {
static LruResults DidNotRun() {
return LruResults{/* didRun= */ false, 0, 0, 0};
}
bool didRun;
int sequenceNumbersCollected;
int targetsRemoved;
int documentsRemoved;
};
} // namespace local
} // namespace firestore
} // namespace firebase
namespace local = firebase::firestore::local;
/**
* Persistence layers intending to use LRU Garbage collection should implement this protocol. This
* protocol defines the operations that the LRU garbage collector needs from the persistence layer.
*/
@protocol FSTLRUDelegate
/**
* Enumerates all the targets that the delegate is aware of. This is typically all of the targets in
* an FSTQueryCache.
*/
- (void)enumerateTargetsUsingCallback:(const local::TargetCallback &)callback;
/**
* Enumerates all of the outstanding mutations.
*/
- (void)enumerateMutationsUsingCallback:(const local::OrphanedDocumentCallback &)callback;
/**
* Removes all unreferenced documents from the cache that have a sequence number less than or equal
* to the given sequence number. Returns the number of documents removed.
*/
- (int)removeOrphanedDocumentsThroughSequenceNumber:(model::ListenSequenceNumber)sequenceNumber;
/**
* Removes all targets that are not currently being listened to and have a sequence number less than
* or equal to the given sequence number. Returns the number of targets removed.
*/
- (int)removeTargetsThroughSequenceNumber:(model::ListenSequenceNumber)sequenceNumber
liveQueries:
(const std::unordered_map<model::TargetId, FSTQueryData *> &)
liveQueries;
- (size_t)byteSize;
/** Returns the number of targets and orphaned documents cached. */
- (size_t)sequenceNumberCount;
/** Access to the underlying LRU Garbage collector instance. */
@property(strong, nonatomic, readonly) FSTLRUGarbageCollector *gc;
@end
/**
* FSTLRUGarbageCollector defines the LRU algorithm used to clean up old documents and targets. It
* is persistence-agnostic, as long as proper delegate is provided.
*/
@interface FSTLRUGarbageCollector : NSObject
- (instancetype)initWithDelegate:(id<FSTLRUDelegate>)delegate
params:(local::LruParams)params NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
/**
* Given a target percentile, return the number of queries that make up that percentage of the
* queries that are cached. For instance, if 20 queries are cached, and the percentile is 40, the
* result will be 8.
*/
- (int)queryCountForPercentile:(NSUInteger)percentile;
/**
* Given a number of queries n, return the nth sequence number in the cache.
*/
- (model::ListenSequenceNumber)sequenceNumberForQueryCount:(NSUInteger)queryCount;
/**
* Removes queries that are not currently live (as indicated by presence in the liveQueries map) and
* have a sequence number less than or equal to the given sequence number.
*/
- (int)removeQueriesUpThroughSequenceNumber:(model::ListenSequenceNumber)sequenceNumber
liveQueries:
(const std::unordered_map<model::TargetId, FSTQueryData *> &)
liveQueries;
/**
* Removes all unreferenced documents from the cache that have a sequence number less than or equal
* to the given sequence number. Returns the number of documents removed.
*/
- (int)removeOrphanedDocumentsThroughSequenceNumber:(model::ListenSequenceNumber)sequenceNumber;
- (size_t)byteSize;
- (local::LruResults)collectWithLiveTargets:
(const std::unordered_map<model::TargetId, FSTQueryData *> &)liveTargets;
@end