Commit 15b05cf8 authored by felipeg@chromium.org's avatar felipeg@chromium.org

Add Cache size to the Simple Index.


Review URL: https://chromiumcodereview.appspot.com/13913010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@193419 0039d316-1c4b-4281-b951-d872f2087c98
parent 91c74303
...@@ -28,11 +28,15 @@ std::string GetEntryHashForKey(const std::string& key) { ...@@ -28,11 +28,15 @@ std::string GetEntryHashForKey(const std::string& key) {
namespace SimpleIndexFile { namespace SimpleIndexFile {
EntryMetadata::EntryMetadata() : EntryMetadata::EntryMetadata() :
last_used_time(0) {} last_used_time(0),
entry_size(0) {
}
EntryMetadata::EntryMetadata(const std::string& hash_key_p, EntryMetadata::EntryMetadata(const std::string& hash_key_p,
base::Time last_used_time_p) : base::Time last_used_time_p,
last_used_time(last_used_time_p.ToInternalValue()) { uint64 entry_size_p) :
last_used_time(last_used_time_p.ToInternalValue()),
entry_size(entry_size_p) {
DCHECK_EQ(kEntryHashKeySize, implicit_cast<int>(hash_key_p.size())); DCHECK_EQ(kEntryHashKeySize, implicit_cast<int>(hash_key_p.size()));
hash_key_p.copy(hash_key, kEntryHashKeySize); hash_key_p.copy(hash_key, kEntryHashKeySize);
} }
......
...@@ -56,12 +56,15 @@ namespace SimpleIndexFile { ...@@ -56,12 +56,15 @@ namespace SimpleIndexFile {
uint64 initial_magic_number; uint64 initial_magic_number;
uint32 version; uint32 version;
uint64 number_of_entries; uint64 number_of_entries;
uint64 cache_size; // Total cache storage size in bytes.
}; };
// We must keep this struct a POD. // We must keep this struct a POD.
struct EntryMetadata { struct EntryMetadata {
EntryMetadata(); EntryMetadata();
EntryMetadata(const std::string& hash_key_p, base::Time last_used_time_p); EntryMetadata(const std::string& hash_key_p,
base::Time last_used_time_p,
uint64 entry_size_p);
base::Time GetLastUsedTime() const; base::Time GetLastUsedTime() const;
std::string GetHashKey() const; std::string GetHashKey() const;
...@@ -79,6 +82,8 @@ namespace SimpleIndexFile { ...@@ -79,6 +82,8 @@ namespace SimpleIndexFile {
// If you want to make calculations/comparisons, you should use the // If you want to make calculations/comparisons, you should use the
// base::Time() class. Use the GetLastUsedTime() method above. // base::Time() class. Use the GetLastUsedTime() method above.
int64 last_used_time; int64 last_used_time;
uint64 entry_size; // Storage size in bytes.
}; };
const size_t kEntryMetadataSize = sizeof(EntryMetadata); const size_t kEntryMetadataSize = sizeof(EntryMetadata);
......
...@@ -150,7 +150,8 @@ int SimpleEntryImpl::ReadData(int index, ...@@ -150,7 +150,8 @@ int SimpleEntryImpl::ReadData(int index,
index_->UseIfExists(key_); index_->UseIfExists(key_);
SynchronousOperationCallback sync_operation_callback = SynchronousOperationCallback sync_operation_callback =
base::Bind(&SimpleEntryImpl::EntryOperationComplete, base::Bind(&SimpleEntryImpl::EntryOperationComplete,
callback, weak_ptr_factory_.GetWeakPtr(), synchronous_entry_); index_, callback, weak_ptr_factory_.GetWeakPtr(),
synchronous_entry_);
WorkerPool::PostTask(FROM_HERE, WorkerPool::PostTask(FROM_HERE,
base::Bind(&SimpleSynchronousEntry::ReadData, base::Bind(&SimpleSynchronousEntry::ReadData,
base::Unretained(synchronous_entry_), base::Unretained(synchronous_entry_),
...@@ -175,7 +176,8 @@ int SimpleEntryImpl::WriteData(int index, ...@@ -175,7 +176,8 @@ int SimpleEntryImpl::WriteData(int index,
index_->UseIfExists(key_); index_->UseIfExists(key_);
SynchronousOperationCallback sync_operation_callback = SynchronousOperationCallback sync_operation_callback =
base::Bind(&SimpleEntryImpl::EntryOperationComplete, base::Bind(&SimpleEntryImpl::EntryOperationComplete,
callback, weak_ptr_factory_.GetWeakPtr(), synchronous_entry_); index_, callback, weak_ptr_factory_.GetWeakPtr(),
synchronous_entry_);
WorkerPool::PostTask(FROM_HERE, WorkerPool::PostTask(FROM_HERE,
base::Bind(&SimpleSynchronousEntry::WriteData, base::Bind(&SimpleSynchronousEntry::WriteData,
base::Unretained(synchronous_entry_), base::Unretained(synchronous_entry_),
...@@ -273,10 +275,15 @@ void SimpleEntryImpl::CreationOperationComplete( ...@@ -273,10 +275,15 @@ void SimpleEntryImpl::CreationOperationComplete(
// static // static
void SimpleEntryImpl::EntryOperationComplete( void SimpleEntryImpl::EntryOperationComplete(
base::WeakPtr<SimpleIndex> index,
const CompletionCallback& completion_callback, const CompletionCallback& completion_callback,
base::WeakPtr<SimpleEntryImpl> entry, base::WeakPtr<SimpleEntryImpl> entry,
SimpleSynchronousEntry* sync_entry, SimpleSynchronousEntry* sync_entry,
int result) { int result) {
DCHECK(sync_entry);
if (index)
index->UpdateEntrySize(sync_entry->key(), sync_entry->GetFileSize());
if (entry) { if (entry) {
DCHECK(entry->synchronous_entry_in_use_by_worker_); DCHECK(entry->synchronous_entry_in_use_by_worker_);
entry->synchronous_entry_in_use_by_worker_ = false; entry->synchronous_entry_in_use_by_worker_ = false;
......
...@@ -100,6 +100,7 @@ class SimpleEntryImpl : public Entry { ...@@ -100,6 +100,7 @@ class SimpleEntryImpl : public Entry {
// operation, such as ReadData() or WriteData(). Calls |completion_callback|. // operation, such as ReadData() or WriteData(). Calls |completion_callback|.
// If |entry| no longer exists, then it ensures |sync_entry| is closed. // If |entry| no longer exists, then it ensures |sync_entry| is closed.
static void EntryOperationComplete( static void EntryOperationComplete(
base::WeakPtr<SimpleIndex> index,
const CompletionCallback& completion_callback, const CompletionCallback& completion_callback,
base::WeakPtr<SimpleEntryImpl> entry, base::WeakPtr<SimpleEntryImpl> entry,
SimpleSynchronousEntry* sync_entry, SimpleSynchronousEntry* sync_entry,
......
...@@ -112,11 +112,15 @@ bool SimpleIndex::Initialize() { ...@@ -112,11 +112,15 @@ bool SimpleIndex::Initialize() {
} }
void SimpleIndex::Insert(const std::string& key) { void SimpleIndex::Insert(const std::string& key) {
// Upon insert we don't know yet the size of the entry.
// It will be updated later when the SimpleEntryImpl finishes opening or
// creating the new entry, and then UpdateEntrySize will be called.
InsertInternal(SimpleIndexFile::EntryMetadata(GetEntryHashForKey(key), InsertInternal(SimpleIndexFile::EntryMetadata(GetEntryHashForKey(key),
base::Time::Now())); base::Time::Now(), 0));
} }
void SimpleIndex::Remove(const std::string& key) { void SimpleIndex::Remove(const std::string& key) {
UpdateEntrySize(key, 0);
entries_set_.erase(GetEntryHashForKey(key)); entries_set_.erase(GetEntryHashForKey(key));
} }
...@@ -132,6 +136,19 @@ bool SimpleIndex::UseIfExists(const std::string& key) { ...@@ -132,6 +136,19 @@ bool SimpleIndex::UseIfExists(const std::string& key) {
return true; return true;
} }
bool SimpleIndex::UpdateEntrySize(const std::string& key, uint64 entry_size) {
EntrySet::iterator it = entries_set_.find(GetEntryHashForKey(key));
if (it == entries_set_.end())
return false;
// Update the total cache size with the new entry size.
cache_size_ -= it->second.entry_size;
cache_size_ += entry_size;
it->second.entry_size = entry_size;
return true;
}
void SimpleIndex::InsertInternal( void SimpleIndex::InsertInternal(
const SimpleIndexFile::EntryMetadata& entry_metadata) { const SimpleIndexFile::EntryMetadata& entry_metadata) {
entries_set_.insert(make_pair(entry_metadata.GetHashKey(), entry_metadata)); entries_set_.insert(make_pair(entry_metadata.GetHashKey(), entry_metadata));
...@@ -143,11 +160,16 @@ bool SimpleIndex::RestoreFromDisk() { ...@@ -143,11 +160,16 @@ bool SimpleIndex::RestoreFromDisk() {
CloseIndexFile(); CloseIndexFile();
file_util::Delete(index_filename_, /* recursive = */ false); file_util::Delete(index_filename_, /* recursive = */ false);
entries_set_.clear(); entries_set_.clear();
const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_0");
// TODO(felipeg,gavinp): Fix this once we have a one-file per entry format.
COMPILE_ASSERT(kSimpleEntryFileCount == 3,
file_pattern_must_match_file_count);
const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*_[0-2]");
FileEnumerator enumerator(path_, FileEnumerator enumerator(path_,
false /* recursive */, false /* recursive */,
FileEnumerator::FILES, FileEnumerator::FILES,
file_pattern); file_pattern);
for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); for (base::FilePath file_path = enumerator.Next(); !file_path.empty();
file_path = enumerator.Next()) { file_path = enumerator.Next()) {
const base::FilePath::StringType base_name = file_path.BaseName().value(); const base::FilePath::StringType base_name = file_path.BaseName().value();
...@@ -166,8 +188,18 @@ bool SimpleIndex::RestoreFromDisk() { ...@@ -166,8 +188,18 @@ bool SimpleIndex::RestoreFromDisk() {
#endif #endif
if (last_used_time.is_null()) if (last_used_time.is_null())
last_used_time = FileEnumerator::GetLastModifiedTime(find_info); last_used_time = FileEnumerator::GetLastModifiedTime(find_info);
InsertInternal(SimpleIndexFile::EntryMetadata(hash_key, last_used_time));
int64 file_size = FileEnumerator::GetFilesize(find_info);
EntrySet::iterator it = entries_set_.find(hash_key);
if (it == entries_set_.end()) {
InsertInternal(SimpleIndexFile::EntryMetadata(
hash_key, last_used_time, file_size));
} else {
// Summing up the total size of the entry through all the *_[0-2] files
it->second.entry_size += file_size;
}
} }
// TODO(felipeg): Detect unrecoverable problems and return false here. // TODO(felipeg): Detect unrecoverable problems and return false here.
return true; return true;
} }
......
...@@ -47,6 +47,11 @@ class SimpleIndex ...@@ -47,6 +47,11 @@ class SimpleIndex
void Cleanup(); void Cleanup();
// Update the size (in bytes) of an entry, in the metadata stored in the
// index. This should be the total disk-file size including all streams of the
// entry.
bool UpdateEntrySize(const std::string& key, uint64 entry_size);
private: private:
// TODO(felipeg): This way we are storing the hash_key string twice (as the // TODO(felipeg): This way we are storing the hash_key string twice (as the
// hash_map::key and as a member of EntryMetadata. We could save space if we // hash_map::key and as a member of EntryMetadata. We could save space if we
...@@ -73,6 +78,7 @@ class SimpleIndex ...@@ -73,6 +78,7 @@ class SimpleIndex
const base::FilePath path_; const base::FilePath path_;
EntrySet entries_set_; EntrySet entries_set_;
uint64 cache_size_; // Total cache storage size in bytes.
base::FilePath index_filename_; base::FilePath index_filename_;
base::PlatformFile index_file_; base::PlatformFile index_file_;
......
...@@ -50,6 +50,10 @@ int32 DataSizeFromKeyAndFileSize(size_t key_size, int64 file_size) { ...@@ -50,6 +50,10 @@ int32 DataSizeFromKeyAndFileSize(size_t key_size, int64 file_size) {
return data_size; return data_size;
} }
int64 FileSizeFromKeyAndDataSize(size_t key_size, int32 data_size) {
return data_size + key_size + sizeof(disk_cache::SimpleFileHeader);
}
int64 FileOffsetFromDataOffset(size_t key_size, int data_offset) { int64 FileOffsetFromDataOffset(size_t key_size, int data_offset) {
const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key_size; const int64 headers_size = sizeof(disk_cache::SimpleFileHeader) + key_size;
return headers_size + data_offset; return headers_size + data_offset;
...@@ -212,6 +216,14 @@ bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) { ...@@ -212,6 +216,14 @@ bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) {
return true; return true;
} }
int64 SimpleSynchronousEntry::GetFileSize() const {
int64 file_size = 0;
for (int i = 0; i < kSimpleEntryFileCount; ++i) {
file_size += FileSizeFromKeyAndDataSize(key_.size(), data_size_[i]);
}
return file_size;
}
bool SimpleSynchronousEntry::InitializeForOpen() { bool SimpleSynchronousEntry::InitializeForOpen() {
DCHECK(!initialized_); DCHECK(!initialized_);
if (!OpenOrCreateFiles(false)) if (!OpenOrCreateFiles(false))
......
...@@ -83,6 +83,8 @@ class SimpleSynchronousEntry { ...@@ -83,6 +83,8 @@ class SimpleSynchronousEntry {
base::Time last_modified() const { return last_modified_; } base::Time last_modified() const { return last_modified_; }
int32 data_size(int index) const { return data_size_[index]; } int32 data_size(int index) const { return data_size_[index]; }
int64 GetFileSize() const;
private: private:
SimpleSynchronousEntry( SimpleSynchronousEntry(
const scoped_refptr<base::TaskRunner>& callback_runner, const scoped_refptr<base::TaskRunner>& callback_runner,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment