#ifndef NET_DISK_CACHE_SQL_EVICTION_CANDIDATE_AGGREGATOR_H_
#define NET_DISK_CACHE_SQL_EVICTION_CANDIDATE_AGGREGATOR_H_
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "net/base/net_export.h"
#include "net/disk_cache/sql/sql_persistent_store.h"
namespace disk_cache {
class NET_EXPORT_PRIVATE EvictionCandidateAggregator
: public base::RefCountedThreadSafe<EvictionCandidateAggregator> {
public:
struct NET_EXPORT_PRIVATE EvictionCandidate {
EvictionCandidate(SqlPersistentStore::ResId res_id,
SqlPersistentStore::ShardId shard_id,
int64_t bytes_usage,
base::Time last_used);
~EvictionCandidate();
EvictionCandidate(EvictionCandidate&&);
EvictionCandidate& operator=(EvictionCandidate&&);
SqlPersistentStore::ResId res_id;
SqlPersistentStore::ShardId shard_id;
int64_t bytes_usage;
base::Time last_used;
};
using EvictionCandidateList = std::vector<EvictionCandidate>;
using EvictionCandidateSelectedCallback =
base::OnceCallback<void(std::vector<SqlPersistentStore::ResId>,
int64_t bytes_usage,
base::TimeTicks post_task_time)>;
explicit EvictionCandidateAggregator(
int64_t size_to_be_removed,
std::vector<scoped_refptr<base::SequencedTaskRunner>> task_runners);
void OnCandidate(SqlPersistentStore::ShardId shard_id,
EvictionCandidateList candidate,
EvictionCandidateSelectedCallback selected_callback);
private:
friend class base::RefCountedThreadSafe<EvictionCandidateAggregator>;
~EvictionCandidateAggregator();
bool AddCandidates(
SqlPersistentStore::ShardId shard_id,
EvictionCandidateList new_candidates,
EvictionCandidateSelectedCallback new_selected_callback,
std::vector<EvictionCandidateList>& candidates_per_shard_out,
std::vector<EvictionCandidateSelectedCallback>& selected_callbacks_out);
void AggregateCandidatesAndRunCallbacks(
std::vector<EvictionCandidateList> candidates_per_shard,
std::vector<EvictionCandidateSelectedCallback> selected_callbacks,
SqlPersistentStore::ShardId last_shard_id);
size_t GetSizeOfShards() const;
const int64_t size_to_be_removed_;
const std::vector<scoped_refptr<base::SequencedTaskRunner>> task_runners_;
base::Lock lock_;
std::vector<EvictionCandidateList> candidates_per_shard_ GUARDED_BY(lock_);
std::vector<EvictionCandidateSelectedCallback> selected_callbacks_
GUARDED_BY(lock_);
SEQUENCE_CHECKER(sequence_checker_);
};
}
#endif