eviction_v3.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// Use of this source code is governed by a BSD-style license that can be
3f5256e16dfc425c1d466f6308d4026d529ce9e0bHoward Hinnant// found in the LICENSE file.
4bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant#ifndef NET_DISK_CACHE_BLOCKFILE_EVICTION_V3_H_
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant#define NET_DISK_CACHE_BLOCKFILE_EVICTION_V3_H_
7bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
8bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "base/basictypes.h"
9bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "base/memory/weak_ptr.h"
10bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "net/disk_cache/blockfile/disk_format_v3.h"
11bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "net/disk_cache/blockfile/index_table_v3.h"
12bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
13namespace disk_cache {
14
15class BackendImplV3;
16class CacheRankingsBlock;
17class EntryImplV3;
18
19namespace Rankings {
20typedef int List;
21}
22
23// This class implements the eviction algorithm for the cache and it is tightly
24// integrated with BackendImpl.
25class EvictionV3 {
26 public:
27  EvictionV3();
28  ~EvictionV3();
29
30  void Init(BackendImplV3* backend);
31  void Stop();
32
33  // Deletes entries from the cache until the current size is below the limit.
34  // If empty is true, the whole cache will be trimmed, regardless of being in
35  // use.
36  void TrimCache(bool empty);
37
38  // Notifications of interesting events for a given entry.
39  void OnOpenEntry(EntryImplV3* entry);
40  void OnCreateEntry(EntryImplV3* entry);
41
42  // Testing interface.
43  void SetTestMode();
44  void TrimDeletedList(bool empty);
45
46 private:
47  void PostDelayedTrim();
48  void DelayedTrim();
49  bool ShouldTrim();
50  bool ShouldTrimDeleted();
51  bool EvictEntry(CacheRankingsBlock* node, bool empty, Rankings::List list);
52
53  void TrimCacheV2(bool empty);
54  void TrimDeleted(bool empty);
55
56  bool NodeIsOldEnough(CacheRankingsBlock* node, int list);
57  int SelectListByLength();
58  void ReportListStats();
59
60  BackendImplV3* backend_;
61  IndexTable* index_;
62  IndexHeaderV3* header_;
63  int max_size_;
64  int trim_delays_;
65  bool lru_;
66  bool first_trim_;
67  bool trimming_;
68  bool delay_trim_;
69  bool init_;
70  bool test_mode_;
71  base::WeakPtrFactory<EvictionV3> ptr_factory_;
72
73  DISALLOW_COPY_AND_ASSIGN(EvictionV3);
74};
75
76}  // namespace disk_cache
77
78#endif  // NET_DISK_CACHE_BLOCKFILE_EVICTION_V3_H_
79