Chromium Code Reviews| Index: net/disk_cache/simple/simple_index.cc |
| diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc |
| index c41c829bdefd808db4ea55dded03e54f4574b641..42b6eafded46cf43f62f7b83b94f04a9372b2c1a 100644 |
| --- a/net/disk_cache/simple/simple_index.cc |
| +++ b/net/disk_cache/simple/simple_index.cc |
| @@ -60,7 +60,8 @@ EntryMetadata::EntryMetadata() |
| EntryMetadata::EntryMetadata(base::Time last_used_time, |
| base::StrictNumeric<uint32_t> entry_size) |
| - : last_used_time_seconds_since_epoch_(0), entry_size_(entry_size) { |
| + : last_used_time_seconds_since_epoch_(0), entry_size_(0) { |
| + SetEntrySize(entry_size); // to round/pack properly. |
| SetLastUsedTime(last_used_time); |
| } |
| @@ -88,11 +89,23 @@ void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) { |
| } |
| uint32_t EntryMetadata::GetEntrySize() const { |
| - return entry_size_; |
| + return entry_size_ & 0xFFFFFF00; |
|
jkarlin
2017/06/08 14:44:06
I'd rather add a byte (or a uint32_t even) to the
|
| +} |
| + |
| +uint8_t EntryMetadata::GetOracleByte() const { |
| + return entry_size_ & 0xFF; |
| } |
| void EntryMetadata::SetEntrySize(base::StrictNumeric<uint32_t> entry_size) { |
| - entry_size_ = entry_size; |
| + uint8_t save_oracle_byte = GetOracleByte(); |
| + // ### what happens if we overflow here? |
| + entry_size_ = ((static_cast<uint32_t>(entry_size) + 255) & 0xFFFFFF00) | |
| + save_oracle_byte; |
| +} |
| + |
| +void EntryMetadata::SetOracleByte(uint8_t oracle_byte) { |
| + uint32_t save_size = GetEntrySize(); |
| + entry_size_ = (save_size | oracle_byte); |
| } |
| void EntryMetadata::Serialize(base::Pickle* pickle) const { |
| @@ -104,7 +117,8 @@ void EntryMetadata::Serialize(base::Pickle* pickle) const { |
| pickle->WriteUInt64(entry_size_); |
| } |
| -bool EntryMetadata::Deserialize(base::PickleIterator* it) { |
| +bool EntryMetadata::Deserialize(base::PickleIterator* it, |
| + bool has_oracle_byte) { |
| DCHECK(it); |
| int64_t tmp_last_used_time; |
| uint64_t tmp_entry_size; |
| @@ -112,7 +126,12 @@ bool EntryMetadata::Deserialize(base::PickleIterator* it) { |
| tmp_entry_size > std::numeric_limits<decltype(entry_size_)>::max()) |
| return false; |
| SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); |
| - entry_size_ = static_cast<uint32_t>(tmp_entry_size); |
| + if (has_oracle_byte) { |
| + entry_size_ = static_cast<uint32_t>(tmp_entry_size); |
| + } else { |
| + SetEntrySize(static_cast<uint32_t>(tmp_entry_size)); |
| + SetOracleByte(0); |
| + } |
| return true; |
| } |
| @@ -281,6 +300,15 @@ bool SimpleIndex::Has(uint64_t hash) const { |
| return !initialized_ || entries_set_.count(hash) > 0; |
| } |
| +bool SimpleIndex::HasWithOracleByte(uint64_t entry_hash, uint8_t* oracle_byte) { |
| + EntrySet::iterator it = entries_set_.find(entry_hash); |
| + if (it == entries_set_.end()) |
| + // If not initialized, always return true, forcing it to go to the disk. |
| + return !initialized_; |
| + *oracle_byte = it->second.GetOracleByte(); |
| + return true; |
| +} |
| + |
| bool SimpleIndex::UseIfExists(uint64_t entry_hash) { |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| // Always update the last used time, even if it is during initialization. |
| @@ -363,6 +391,16 @@ bool SimpleIndex::UpdateEntrySize(uint64_t entry_hash, |
| return true; |
| } |
| +void SimpleIndex::UpdateEntryOracleByte(uint64_t entry_hash, |
| + uint8_t oracle_byte) { |
| + DCHECK(io_thread_checker_.CalledOnValidThread()); |
| + EntrySet::iterator it = entries_set_.find(entry_hash); |
| + if (it == entries_set_.end()) |
| + return; |
| + it->second.SetOracleByte(oracle_byte); |
| + PostponeWritingToDisk(); |
| +} |
| + |
| void SimpleIndex::EvictionDone(int result) { |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| @@ -410,8 +448,9 @@ void SimpleIndex::UpdateEntryIteratorSize( |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| DCHECK_GE(cache_size_, (*it)->second.GetEntrySize()); |
| cache_size_ -= (*it)->second.GetEntrySize(); |
| - cache_size_ += static_cast<uint32_t>(entry_size); |
| (*it)->second.SetEntrySize(entry_size); |
| + // We use GetEntrySize to get consistent rounding. |
| + cache_size_ += (*it)->second.GetEntrySize(); |
| } |
| void SimpleIndex::MergeInitializingSet( |