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 #include "net/disk_cache/simple/simple_index.h" | 5 #include "net/disk_cache/simple/simple_index.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 &entries_buffer.get()[entries_buffer_offset], &entry_metadata); | 105 &entries_buffer.get()[entries_buffer_offset], &entry_metadata); |
106 InsertInternal(entry_metadata); | 106 InsertInternal(entry_metadata); |
107 entries_buffer_offset += SimpleIndexFile::kEntryMetadataSize; | 107 entries_buffer_offset += SimpleIndexFile::kEntryMetadataSize; |
108 } | 108 } |
109 DCHECK_EQ(header.number_of_entries, entries_set_.size()); | 109 DCHECK_EQ(header.number_of_entries, entries_set_.size()); |
110 CloseIndexFile(); | 110 CloseIndexFile(); |
111 return true; | 111 return true; |
112 } | 112 } |
113 | 113 |
114 void SimpleIndex::Insert(const std::string& key) { | 114 void SimpleIndex::Insert(const std::string& key) { |
| 115 // Upon insert we don't know yet the size of the entry. |
| 116 // It will be updated later when the SimpleEntryImpl finishes opening or |
| 117 // creating the new entry, and then UpdateEntrySize will be called. |
115 InsertInternal(SimpleIndexFile::EntryMetadata(GetEntryHashForKey(key), | 118 InsertInternal(SimpleIndexFile::EntryMetadata(GetEntryHashForKey(key), |
116 base::Time::Now())); | 119 base::Time::Now(), 0)); |
117 } | 120 } |
118 | 121 |
119 void SimpleIndex::Remove(const std::string& key) { | 122 void SimpleIndex::Remove(const std::string& key) { |
| 123 UpdateEntrySize(key, 0); |
120 entries_set_.erase(GetEntryHashForKey(key)); | 124 entries_set_.erase(GetEntryHashForKey(key)); |
121 } | 125 } |
122 | 126 |
123 bool SimpleIndex::Has(const std::string& key) const { | 127 bool SimpleIndex::Has(const std::string& key) const { |
124 return entries_set_.count(GetEntryHashForKey(key)) != 0; | 128 return entries_set_.count(GetEntryHashForKey(key)) != 0; |
125 } | 129 } |
126 | 130 |
127 bool SimpleIndex::UseIfExists(const std::string& key) { | 131 bool SimpleIndex::UseIfExists(const std::string& key) { |
128 EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key)); | 132 EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key)); |
129 if (it == entries_set_.end()) | 133 if (it == entries_set_.end()) |
130 return false; | 134 return false; |
131 it->second.SetLastUsedTime(base::Time::Now()); | 135 it->second.SetLastUsedTime(base::Time::Now()); |
132 return true; | 136 return true; |
133 } | 137 } |
134 | 138 |
| 139 bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) { |
| 140 EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key)); |
| 141 if (it == entries_set_.end()) |
| 142 return false; |
| 143 |
| 144 // Update the total cache size with the new entry size. |
| 145 cache_size_ -= it->second.entry_size; |
| 146 cache_size_ += entry_size; |
| 147 it->second.entry_size = entry_size; |
| 148 |
| 149 return true; |
| 150 } |
| 151 |
135 void SimpleIndex::InsertInternal( | 152 void SimpleIndex::InsertInternal( |
136 const SimpleIndexFile::EntryMetadata& entry_metadata) { | 153 const SimpleIndexFile::EntryMetadata& entry_metadata) { |
137 entries_set_.insert(make_pair(entry_metadata.GetHashKey(), entry_metadata)); | 154 entries_set_.insert(make_pair(entry_metadata.GetHashKey(), entry_metadata)); |
138 } | 155 } |
139 | 156 |
140 bool SimpleIndex::RestoreFromDisk() { | 157 bool SimpleIndex::RestoreFromDisk() { |
141 using file_util::FileEnumerator; | 158 using file_util::FileEnumerator; |
142 LOG(INFO) << "Simple Cache Index is being restored from disk."; | 159 LOG(INFO) << "Simple Cache Index is being restored from disk."; |
143 CloseIndexFile(); | 160 CloseIndexFile(); |
144 file_util::Delete(index_filename_, /* recursive = */ false); | 161 file_util::Delete(index_filename_, /* recursive = */ false); |
145 entries_set_.clear(); | 162 entries_set_.clear(); |
146 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_0"); | 163 |
| 164 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. |
| 165 COMPILE_ASSERT(kSimpleEntryFileCount == 3, |
| 166 file_pattern_must_match_file_count); |
| 167 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]"); |
147 FileEnumerator enumerator(path_, | 168 FileEnumerator enumerator(path_, |
148 false /* recursive */, | 169 false /* recursive */, |
149 FileEnumerator::FILES, | 170 FileEnumerator::FILES, |
150 file_pattern); | 171 file_pattern); |
| 172 |
151 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); | 173 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); |
152 file_path = enumerator.Next()) { | 174 file_path = enumerator.Next()) { |
153 const base::FilePath::StringType base_name = file_path.BaseName().value(); | 175 const base::FilePath::StringType base_name = file_path.BaseName().value(); |
154 // Converting to std::string is OK since we never use UTF8 wide chars in our | 176 // Converting to std::string is OK since we never use UTF8 wide chars in our |
155 // file names. | 177 // file names. |
156 const std::string hash_name(base_name.begin(), base_name.end()); | 178 const std::string hash_name(base_name.begin(), base_name.end()); |
157 const std::string hash_key = hash_name.substr(0, kEntryHashKeySize); | 179 const std::string hash_key = hash_name.substr(0, kEntryHashKeySize); |
158 | 180 |
159 FileEnumerator::FindInfo find_info = {}; | 181 FileEnumerator::FindInfo find_info = {}; |
160 enumerator.GetFindInfo(&find_info); | 182 enumerator.GetFindInfo(&find_info); |
161 base::Time last_used_time; | 183 base::Time last_used_time; |
162 #if defined(OS_POSIX) | 184 #if defined(OS_POSIX) |
163 // For POSIX systems, a last access time is available. However, it's not | 185 // For POSIX systems, a last access time is available. However, it's not |
164 // guaranteed to be more accurate than mtime. It is no worse though. | 186 // guaranteed to be more accurate than mtime. It is no worse though. |
165 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); | 187 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); |
166 #endif | 188 #endif |
167 if (last_used_time.is_null()) | 189 if (last_used_time.is_null()) |
168 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); | 190 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); |
169 InsertInternal(SimpleIndexFile::EntryMetadata(hash_key, last_used_time)); | 191 |
| 192 int64 file_size = FileEnumerator::GetFilesize(find_info); |
| 193 EntrySet::iterator it = entries_set_.find(hash_key); |
| 194 if (it == entries_set_.end()) { |
| 195 InsertInternal(SimpleIndexFile::EntryMetadata( |
| 196 hash_key, last_used_time, file_size)); |
| 197 } else { |
| 198 // Summing up the total size of the entry through all the *_[0-2] files |
| 199 it->second.entry_size += file_size; |
| 200 } |
170 } | 201 } |
| 202 |
171 // TODO(felipeg): Detect unrecoverable problems and return false here. | 203 // TODO(felipeg): Detect unrecoverable problems and return false here. |
172 return true; | 204 return true; |
173 } | 205 } |
174 | 206 |
175 void SimpleIndex::Serialize(std::string* out_buffer) { | 207 void SimpleIndex::Serialize(std::string* out_buffer) { |
176 DCHECK(out_buffer); | 208 DCHECK(out_buffer); |
177 SimpleIndexFile::Header header; | 209 SimpleIndexFile::Header header; |
178 SimpleIndexFile::Footer footer; | 210 SimpleIndexFile::Footer footer; |
179 | 211 |
180 header.initial_magic_number = kSimpleIndexInitialMagicNumber; | 212 header.initial_magic_number = kSimpleIndexInitialMagicNumber; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 << temp_filename.value(); | 281 << temp_filename.value(); |
250 file_util::Delete(temp_filename, /* recursive = */ false); | 282 file_util::Delete(temp_filename, /* recursive = */ false); |
251 return; | 283 return; |
252 } | 284 } |
253 // Swap temp and index_file. | 285 // Swap temp and index_file. |
254 bool result = file_util::ReplaceFile(temp_filename, index_filename); | 286 bool result = file_util::ReplaceFile(temp_filename, index_filename); |
255 DCHECK(result); | 287 DCHECK(result); |
256 } | 288 } |
257 | 289 |
258 } // namespace disk_cache | 290 } // namespace disk_cache |
OLD | NEW |