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

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: fixing the branching issue 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
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 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
gavinp 2013/04/09 15:27:37 This is a scary embedding of SimpleBackend::kSimpl
felipeg 2013/04/09 16:24:35 Added a TODO
147 FileEnumerator enumerator(path_, 164 FileEnumerator enumerator(path_,
148 false /* recursive */, 165 false /* recursive */,
149 FileEnumerator::FILES, 166 FileEnumerator::FILES,
150 file_pattern); 167 file_pattern);
168
151 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); 169 for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
152 file_path = enumerator.Next()) { 170 file_path = enumerator.Next()) {
153 const base::FilePath::StringType base_name = file_path.BaseName().value(); 171 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 172 // Converting to std::string is OK since we never use UTF8 wide chars in our
155 // file names. 173 // file names.
156 const std::string hash_name(base_name.begin(), base_name.end()); 174 const std::string hash_name(base_name.begin(), base_name.end());
157 const std::string hash_key = hash_name.substr(0, kEntryHashKeySize); 175 const std::string hash_key = hash_name.substr(0, kEntryHashKeySize);
158 176
159 FileEnumerator::FindInfo find_info = {}; 177 FileEnumerator::FindInfo find_info = {};
160 enumerator.GetFindInfo(&find_info); 178 enumerator.GetFindInfo(&find_info);
161 base::Time last_used_time; 179 base::Time last_used_time;
162 #if defined(OS_POSIX) 180 #if defined(OS_POSIX)
163 // For POSIX systems, a last access time is available. However, it's not 181 // 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. 182 // guaranteed to be more accurate than mtime. It is no worse though.
165 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime); 183 last_used_time = base::Time::FromTimeT(find_info.stat.st_atime);
166 #endif 184 #endif
167 if (last_used_time.is_null()) 185 if (last_used_time.is_null())
168 last_used_time = FileEnumerator::GetLastModifiedTime(find_info); 186 last_used_time = FileEnumerator::GetLastModifiedTime(find_info);
169 InsertInternal(SimpleIndexFile::EntryMetadata(hash_key, last_used_time)); 187
188 int64 file_size = FileEnumerator::GetFilesize(find_info);
189 EntrySet::iterator it = entries_set_.find(hash_key);
190 if (it == entries_set_.end()) {
191 InsertInternal(SimpleIndexFile::EntryMetadata(
192 hash_key, last_used_time, file_size));
193 } else {
194 // Summing up the total size of the entry through all the *_[0-2] files
195 it->second.entry_size += file_size;
196 }
170 } 197 }
198
171 // TODO(felipeg): Detect unrecoverable problems and return false here. 199 // TODO(felipeg): Detect unrecoverable problems and return false here.
172 return true; 200 return true;
173 } 201 }
174 202
175 void SimpleIndex::Serialize(std::string* out_buffer) { 203 void SimpleIndex::Serialize(std::string* out_buffer) {
176 DCHECK(out_buffer); 204 DCHECK(out_buffer);
177 SimpleIndexFile::Header header; 205 SimpleIndexFile::Header header;
178 SimpleIndexFile::Footer footer; 206 SimpleIndexFile::Footer footer;
179 207
180 header.initial_magic_number = kSimpleIndexInitialMagicNumber; 208 header.initial_magic_number = kSimpleIndexInitialMagicNumber;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 << temp_filename.value(); 277 << temp_filename.value();
250 file_util::Delete(temp_filename, /* recursive = */ false); 278 file_util::Delete(temp_filename, /* recursive = */ false);
251 return; 279 return;
252 } 280 }
253 // Swap temp and index_file. 281 // Swap temp and index_file.
254 bool result = file_util::ReplaceFile(temp_filename, index_filename); 282 bool result = file_util::ReplaceFile(temp_filename, index_filename);
255 DCHECK(result); 283 DCHECK(result);
256 } 284 }
257 285
258 } // namespace disk_cache 286 } // namespace disk_cache
OLDNEW
« net/disk_cache/simple/simple_entry_impl.cc ('K') | « net/disk_cache/simple/simple_index.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698