Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: net/disk_cache/simple/simple_index.cc

Issue 13913010: Add Cache size to the Simple Index. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 SynchronousEntryImpl finish and the
gavinp 2013/04/10 10:33:23 Wording suggestion: It will be updated later when
felipeg 2013/04/10 11:45:35 Done.
117 // 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 // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format.
gavinp 2013/04/10 10:33:23 COMPILE_ASSERT(kSimpleEntryFileCount == 3, file_pa
felipeg 2013/04/10 11:45:35 Done.
164 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
gavinp 2013/04/10 10:33:23 Would: ??????????_[0-2] Be a good idea?
felipeg 2013/04/10 11:45:35 It doesn't matter, we will change it anyway
147 FileEnumerator enumerator(path_, 165 FileEnumerator enumerator(path_,
148 false /* recursive */, 166 false /* recursive */,
149 FileEnumerator::FILES, 167 FileEnumerator::FILES,
150 file_pattern); 168 file_pattern);
169
151 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); 170 for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
152 file_path = enumerator.Next()) { 171 file_path = enumerator.Next()) {
153 const base::FilePath::StringType base_name = file_path.BaseName().value(); 172 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 173 // Converting to std::string is OK since we never use UTF8 wide chars in our
155 // file names. 174 // file names.
156 const std::string hash_name(base_name.begin(), base_name.end()); 175 const std::string hash_name(base_name.begin(), base_name.end());
157 const std::string hash_key = hash_name.substr(0, kEntryHashKeySize); 176 const std::string hash_key = hash_name.substr(0, kEntryHashKeySize);
158 177
159 FileEnumerator::FindInfo find_info = {}; 178 FileEnumerator::FindInfo find_info = {};
160 enumerator.GetFindInfo(&find_info); 179 enumerator.GetFindInfo(&find_info);
161 base::Time last_used_time; 180 base::Time last_used_time;
162 #if defined(OS_POSIX) 181 #if defined(OS_POSIX)
163 // For POSIX systems, a last access time is available. However, it's not 182 // 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. 183 // guaranteed to be more accurate than mtime. It is no worse though.
165 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); 184 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime);
166 #endif 185 #endif
167 if (last_used_time.is_null()) 186 if (last_used_time.is_null())
168 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); 187 last_used_time = FileEnumerator::GetLastModifiedTime(find_info);
169 InsertInternal(SimpleIndexFile::EntryMetadata(hash_key, last_used_time)); 188
189 int64 file_size = FileEnumerator::GetFilesize(find_info);
190 EntrySet::iterator it = entries_set_.find(hash_key);
gavinp 2013/04/10 10:33:23 Note that in the not found case (33% of enumeratio
felipeg 2013/04/10 11:45:35 it is a hash map two searches doesn't matter compa
gavinp 2013/04/10 14:49:29 Good point on the hash. Note also that every time
191 if (it == entries_set_.end()) {
192 InsertInternal(SimpleIndexFile::EntryMetadata(
193 hash_key, last_used_time, file_size));
194 } else {
195 // Summing up the total size of the entry through all the *_[0-2] files
196 it->second.entry_size += file_size;
197 }
170 } 198 }
199
171 // TODO(felipeg): Detect unrecoverable problems and return false here. 200 // TODO(felipeg): Detect unrecoverable problems and return false here.
172 return true; 201 return true;
173 } 202 }
174 203
175 void SimpleIndex::Serialize(std::string* out_buffer) { 204 void SimpleIndex::Serialize(std::string* out_buffer) {
176 DCHECK(out_buffer); 205 DCHECK(out_buffer);
177 SimpleIndexFile::Header header; 206 SimpleIndexFile::Header header;
178 SimpleIndexFile::Footer footer; 207 SimpleIndexFile::Footer footer;
179 208
180 header.initial_magic_number = kSimpleIndexInitialMagicNumber; 209 header.initial_magic_number = kSimpleIndexInitialMagicNumber;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 << temp_filename.value(); 278 << temp_filename.value();
250 file_util::Delete(temp_filename, /* recursive = */ false); 279 file_util::Delete(temp_filename, /* recursive = */ false);
251 return; 280 return;
252 } 281 }
253 // Swap temp and index_file. 282 // Swap temp and index_file.
254 bool result = file_util::ReplaceFile(temp_filename, index_filename); 283 bool result = file_util::ReplaceFile(temp_filename, index_filename);
255 DCHECK(result); 284 DCHECK(result);
256 } 285 }
257 286
258 } // namespace disk_cache 287 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698