| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
| 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
| 7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 8 #include <list> | 10 #include <list> |
| 9 #include <vector> | 11 #include <vector> |
| 10 | 12 |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 13 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 19 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 20 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 35 | 36 |
| 36 namespace disk_cache { | 37 namespace disk_cache { |
| 37 | 38 |
| 38 class SimpleIndexDelegate; | 39 class SimpleIndexDelegate; |
| 39 class SimpleIndexFile; | 40 class SimpleIndexFile; |
| 40 struct SimpleIndexLoadResult; | 41 struct SimpleIndexLoadResult; |
| 41 | 42 |
| 42 class NET_EXPORT_PRIVATE EntryMetadata { | 43 class NET_EXPORT_PRIVATE EntryMetadata { |
| 43 public: | 44 public: |
| 44 EntryMetadata(); | 45 EntryMetadata(); |
| 45 EntryMetadata(base::Time last_used_time, uint64 entry_size); | 46 EntryMetadata(base::Time last_used_time, uint64_t entry_size); |
| 46 | 47 |
| 47 base::Time GetLastUsedTime() const; | 48 base::Time GetLastUsedTime() const; |
| 48 void SetLastUsedTime(const base::Time& last_used_time); | 49 void SetLastUsedTime(const base::Time& last_used_time); |
| 49 | 50 |
| 50 uint64 GetEntrySize() const; | 51 uint64_t GetEntrySize() const; |
| 51 void SetEntrySize(uint64 entry_size); | 52 void SetEntrySize(uint64_t entry_size); |
| 52 | 53 |
| 53 // Serialize the data into the provided pickle. | 54 // Serialize the data into the provided pickle. |
| 54 void Serialize(base::Pickle* pickle) const; | 55 void Serialize(base::Pickle* pickle) const; |
| 55 bool Deserialize(base::PickleIterator* it); | 56 bool Deserialize(base::PickleIterator* it); |
| 56 | 57 |
| 57 static base::TimeDelta GetLowerEpsilonForTimeComparisons() { | 58 static base::TimeDelta GetLowerEpsilonForTimeComparisons() { |
| 58 return base::TimeDelta::FromSeconds(1); | 59 return base::TimeDelta::FromSeconds(1); |
| 59 } | 60 } |
| 60 static base::TimeDelta GetUpperEpsilonForTimeComparisons() { | 61 static base::TimeDelta GetUpperEpsilonForTimeComparisons() { |
| 61 return base::TimeDelta(); | 62 return base::TimeDelta(); |
| 62 } | 63 } |
| 63 | 64 |
| 64 private: | 65 private: |
| 65 friend class SimpleIndexFileTest; | 66 friend class SimpleIndexFileTest; |
| 66 | 67 |
| 67 // There are tens of thousands of instances of EntryMetadata in memory, so the | 68 // There are tens of thousands of instances of EntryMetadata in memory, so the |
| 68 // size of each entry matters. Even when the values used to set these members | 69 // size of each entry matters. Even when the values used to set these members |
| 69 // are originally calculated as >32-bit types, the actual necessary size for | 70 // are originally calculated as >32-bit types, the actual necessary size for |
| 70 // each shouldn't exceed 32 bits, so we use 32-bit types here. | 71 // each shouldn't exceed 32 bits, so we use 32-bit types here. |
| 71 uint32 last_used_time_seconds_since_epoch_; | 72 uint32_t last_used_time_seconds_since_epoch_; |
| 72 int32 entry_size_; // Storage size in bytes. | 73 int32_t entry_size_; // Storage size in bytes. |
| 73 }; | 74 }; |
| 74 static_assert(sizeof(EntryMetadata) == 8, "incorrect metadata size"); | 75 static_assert(sizeof(EntryMetadata) == 8, "incorrect metadata size"); |
| 75 | 76 |
| 76 // This class is not Thread-safe. | 77 // This class is not Thread-safe. |
| 77 class NET_EXPORT_PRIVATE SimpleIndex | 78 class NET_EXPORT_PRIVATE SimpleIndex |
| 78 : public base::SupportsWeakPtr<SimpleIndex> { | 79 : public base::SupportsWeakPtr<SimpleIndex> { |
| 79 public: | 80 public: |
| 80 typedef std::vector<uint64> HashList; | 81 typedef std::vector<uint64_t> HashList; |
| 81 | 82 |
| 82 SimpleIndex(const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, | 83 SimpleIndex(const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, |
| 83 SimpleIndexDelegate* delegate, | 84 SimpleIndexDelegate* delegate, |
| 84 net::CacheType cache_type, | 85 net::CacheType cache_type, |
| 85 scoped_ptr<SimpleIndexFile> simple_index_file); | 86 scoped_ptr<SimpleIndexFile> simple_index_file); |
| 86 | 87 |
| 87 virtual ~SimpleIndex(); | 88 virtual ~SimpleIndex(); |
| 88 | 89 |
| 89 void Initialize(base::Time cache_mtime); | 90 void Initialize(base::Time cache_mtime); |
| 90 | 91 |
| 91 void SetMaxSize(uint64 max_bytes); | 92 void SetMaxSize(uint64_t max_bytes); |
| 92 uint64 max_size() const { return max_size_; } | 93 uint64_t max_size() const { return max_size_; } |
| 93 | 94 |
| 94 void Insert(uint64 entry_hash); | 95 void Insert(uint64_t entry_hash); |
| 95 void Remove(uint64 entry_hash); | 96 void Remove(uint64_t entry_hash); |
| 96 | 97 |
| 97 // Check whether the index has the entry given the hash of its key. | 98 // Check whether the index has the entry given the hash of its key. |
| 98 bool Has(uint64 entry_hash) const; | 99 bool Has(uint64_t entry_hash) const; |
| 99 | 100 |
| 100 // Update the last used time of the entry with the given key and return true | 101 // Update the last used time of the entry with the given key and return true |
| 101 // iff the entry exist in the index. | 102 // iff the entry exist in the index. |
| 102 bool UseIfExists(uint64 entry_hash); | 103 bool UseIfExists(uint64_t entry_hash); |
| 103 | 104 |
| 104 void WriteToDisk(); | 105 void WriteToDisk(); |
| 105 | 106 |
| 106 // Update the size (in bytes) of an entry, in the metadata stored in the | 107 // Update the size (in bytes) of an entry, in the metadata stored in the |
| 107 // index. This should be the total disk-file size including all streams of the | 108 // index. This should be the total disk-file size including all streams of the |
| 108 // entry. | 109 // entry. |
| 109 bool UpdateEntrySize(uint64 entry_hash, int64 entry_size); | 110 bool UpdateEntrySize(uint64_t entry_hash, int64_t entry_size); |
| 110 | 111 |
| 111 typedef base::hash_map<uint64, EntryMetadata> EntrySet; | 112 typedef base::hash_map<uint64_t, EntryMetadata> EntrySet; |
| 112 | 113 |
| 113 static void InsertInEntrySet(uint64 entry_hash, | 114 static void InsertInEntrySet(uint64_t entry_hash, |
| 114 const EntryMetadata& entry_metadata, | 115 const EntryMetadata& entry_metadata, |
| 115 EntrySet* entry_set); | 116 EntrySet* entry_set); |
| 116 | 117 |
| 117 // Executes the |callback| when the index is ready. Allows multiple callbacks. | 118 // Executes the |callback| when the index is ready. Allows multiple callbacks. |
| 118 int ExecuteWhenReady(const net::CompletionCallback& callback); | 119 int ExecuteWhenReady(const net::CompletionCallback& callback); |
| 119 | 120 |
| 120 // Returns entries from the index that have last accessed time matching the | 121 // Returns entries from the index that have last accessed time matching the |
| 121 // range between |initial_time| and |end_time| where open intervals are | 122 // range between |initial_time| and |end_time| where open intervals are |
| 122 // possible according to the definition given in |DoomEntriesBetween()| in the | 123 // possible according to the definition given in |DoomEntriesBetween()| in the |
| 123 // disk cache backend interface. | 124 // disk cache backend interface. |
| 124 scoped_ptr<HashList> GetEntriesBetween(const base::Time initial_time, | 125 scoped_ptr<HashList> GetEntriesBetween(const base::Time initial_time, |
| 125 const base::Time end_time); | 126 const base::Time end_time); |
| 126 | 127 |
| 127 // Returns the list of all entries key hash. | 128 // Returns the list of all entries key hash. |
| 128 scoped_ptr<HashList> GetAllHashes(); | 129 scoped_ptr<HashList> GetAllHashes(); |
| 129 | 130 |
| 130 // Returns number of indexed entries. | 131 // Returns number of indexed entries. |
| 131 int32 GetEntryCount() const; | 132 int32_t GetEntryCount() const; |
| 132 | 133 |
| 133 // Returns the size of the entire cache in bytes. Can only be called after the | 134 // Returns the size of the entire cache in bytes. Can only be called after the |
| 134 // index has been initialized. | 135 // index has been initialized. |
| 135 uint64 GetCacheSize() const; | 136 uint64_t GetCacheSize() const; |
| 136 | 137 |
| 137 // Returns whether the index has been initialized yet. | 138 // Returns whether the index has been initialized yet. |
| 138 bool initialized() const { return initialized_; } | 139 bool initialized() const { return initialized_; } |
| 139 | 140 |
| 140 private: | 141 private: |
| 141 friend class SimpleIndexTest; | 142 friend class SimpleIndexTest; |
| 142 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, IndexSizeCorrectOnMerge); | 143 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, IndexSizeCorrectOnMerge); |
| 143 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWriteQueued); | 144 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWriteQueued); |
| 144 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWriteExecuted); | 145 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWriteExecuted); |
| 145 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWritePostponed); | 146 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWritePostponed); |
| 146 | 147 |
| 147 void StartEvictionIfNeeded(); | 148 void StartEvictionIfNeeded(); |
| 148 void EvictionDone(int result); | 149 void EvictionDone(int result); |
| 149 | 150 |
| 150 void PostponeWritingToDisk(); | 151 void PostponeWritingToDisk(); |
| 151 | 152 |
| 152 void UpdateEntryIteratorSize(EntrySet::iterator* it, int64 entry_size); | 153 void UpdateEntryIteratorSize(EntrySet::iterator* it, int64_t entry_size); |
| 153 | 154 |
| 154 // Must run on IO Thread. | 155 // Must run on IO Thread. |
| 155 void MergeInitializingSet(scoped_ptr<SimpleIndexLoadResult> load_result); | 156 void MergeInitializingSet(scoped_ptr<SimpleIndexLoadResult> load_result); |
| 156 | 157 |
| 157 #if defined(OS_ANDROID) | 158 #if defined(OS_ANDROID) |
| 158 void OnApplicationStateChange(base::android::ApplicationState state); | 159 void OnApplicationStateChange(base::android::ApplicationState state); |
| 159 | 160 |
| 160 scoped_ptr<base::android::ApplicationStatusListener> app_status_listener_; | 161 scoped_ptr<base::android::ApplicationStatusListener> app_status_listener_; |
| 161 #endif | 162 #endif |
| 162 | 163 |
| 163 // The owner of |this| must ensure the |delegate_| outlives |this|. | 164 // The owner of |this| must ensure the |delegate_| outlives |this|. |
| 164 SimpleIndexDelegate* delegate_; | 165 SimpleIndexDelegate* delegate_; |
| 165 | 166 |
| 166 EntrySet entries_set_; | 167 EntrySet entries_set_; |
| 167 | 168 |
| 168 const net::CacheType cache_type_; | 169 const net::CacheType cache_type_; |
| 169 uint64 cache_size_; // Total cache storage size in bytes. | 170 uint64_t cache_size_; // Total cache storage size in bytes. |
| 170 uint64 max_size_; | 171 uint64_t max_size_; |
| 171 uint64 high_watermark_; | 172 uint64_t high_watermark_; |
| 172 uint64 low_watermark_; | 173 uint64_t low_watermark_; |
| 173 bool eviction_in_progress_; | 174 bool eviction_in_progress_; |
| 174 base::TimeTicks eviction_start_time_; | 175 base::TimeTicks eviction_start_time_; |
| 175 | 176 |
| 176 // This stores all the entry_hash of entries that are removed during | 177 // This stores all the entry_hash of entries that are removed during |
| 177 // initialization. | 178 // initialization. |
| 178 base::hash_set<uint64> removed_entries_; | 179 base::hash_set<uint64_t> removed_entries_; |
| 179 bool initialized_; | 180 bool initialized_; |
| 180 | 181 |
| 181 scoped_ptr<SimpleIndexFile> index_file_; | 182 scoped_ptr<SimpleIndexFile> index_file_; |
| 182 | 183 |
| 183 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; | 184 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; |
| 184 | 185 |
| 185 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 186 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
| 186 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 187 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
| 187 base::ThreadChecker io_thread_checker_; | 188 base::ThreadChecker io_thread_checker_; |
| 188 | 189 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 199 | 200 |
| 200 // Set to true when the app is on the background. When the app is in the | 201 // Set to true when the app is on the background. When the app is in the |
| 201 // background we can write the index much more frequently, to insure fresh | 202 // background we can write the index much more frequently, to insure fresh |
| 202 // index on next startup. | 203 // index on next startup. |
| 203 bool app_on_background_; | 204 bool app_on_background_; |
| 204 }; | 205 }; |
| 205 | 206 |
| 206 } // namespace disk_cache | 207 } // namespace disk_cache |
| 207 | 208 |
| 208 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 209 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
| OLD | NEW |