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

Unified Diff: net/disk_cache/v3/entry_impl_v3.cc

Issue 17507006: Disk cache v3 ref2 Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Incl IndexTable cl Created 7 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/disk_cache/v3/entry_impl_v3.h ('k') | net/disk_cache/v3/eviction_v3.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/disk_cache/v3/entry_impl_v3.cc
===================================================================
--- net/disk_cache/v3/entry_impl_v3.cc (revision 232523)
+++ net/disk_cache/v3/entry_impl_v3.cc (working copy)
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "net/disk_cache/entry_impl.h"
+#include "net/disk_cache/v3/entry_impl_v3.h"
#include "base/hash.h"
#include "base/message_loop/message_loop.h"
@@ -10,12 +10,14 @@
#include "base/strings/string_util.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
-#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/bitmap.h"
#include "net/disk_cache/cache_util.h"
#include "net/disk_cache/histogram_macros.h"
#include "net/disk_cache/net_log_parameters.h"
-#include "net/disk_cache/sparse_control.h"
+#include "net/disk_cache/storage_block-inl.h"
+#include "net/disk_cache/v3/backend_impl_v3.h"
+#include "net/disk_cache/v3/disk_format_v3.h"
+#include "net/disk_cache/v3/sparse_control_v3.h"
using base::Time;
using base::TimeDelta;
@@ -23,26 +25,35 @@
namespace {
+const int kMinBufferSize = disk_cache::kMaxBlockSize;
const int kMaxBufferSize = 1024 * 1024; // 1 MB.
+const int kKeyIndex = 0;
} // namespace
namespace disk_cache {
+typedef StorageBlock<EntryRecord> CacheEntryBlockV3;
+typedef StorageBlock<ShortEntryRecord> CacheShortEntryBlock;
+
// This class handles individual memory buffers that store data before it is
// sent to disk. The buffer can start at any offset, but if we try to write to
-// anywhere in the first 16KB of the file (kMaxBlockSize), we set the offset to
+// anywhere in the first 16KB of the file (kMaxBlockSize), we set the offset to jaja
// zero. The buffer grows up to a size determined by the backend, to keep the
// total memory used under control.
-class EntryImpl::UserBuffer {
+class EntryImplV3::UserBuffer {
public:
- explicit UserBuffer(BackendImpl* backend)
- : backend_(backend->GetWeakPtr()), offset_(0), grow_allowed_(true) {
- buffer_.reserve(kMaxBlockSize);
+ explicit UserBuffer(BackendImplV3* backend)
+ : backend_(backend->GetWeakPtr()),
+ offset_(0),
+ grow_allowed_(true),
+ force_size_(false) {
+ buffer_ = new net::GrowableIOBuffer();
+ buffer_->SetCapacity(kMinBufferSize);
}
~UserBuffer() {
if (backend_)
- backend_->BufferDeleted(capacity() - kMaxBlockSize);
+ backend_->BufferDeleted(capacity() - kMinBufferSize);
}
// Returns true if we can handle writing |len| bytes to |offset|.
@@ -63,26 +74,28 @@
// Read |len| bytes from |buf| at the given |offset|.
int Read(int offset, IOBuffer* buf, int len);
- // Prepare this buffer for reuse.
- void Reset();
+ void Rebase();
- char* Data() { return buffer_.size() ? &buffer_[0] : NULL; }
- int Size() { return static_cast<int>(buffer_.size()); }
+ void ForceSize(bool value);
+
+ net::IOBuffer* Get();
+ int Size() { return static_cast<int>(buffer_->offset()); }
int Start() { return offset_; }
int End() { return offset_ + Size(); }
private:
- int capacity() { return static_cast<int>(buffer_.capacity()); }
+ int capacity() { return buffer_->capacity(); }
bool GrowBuffer(int required, int limit);
- base::WeakPtr<BackendImpl> backend_;
+ base::WeakPtr<BackendImplV3> backend_;
int offset_;
- std::vector<char> buffer_;
+ scoped_refptr<net::GrowableIOBuffer> buffer_;
bool grow_allowed_;
+ bool force_size_;
DISALLOW_COPY_AND_ASSIGN(UserBuffer);
};
-bool EntryImpl::UserBuffer::PreWrite(int offset, int len) {
+bool EntryImplV3::UserBuffer::PreWrite(int offset, int len) {
DCHECK_GE(offset, 0);
DCHECK_GE(len, 0);
DCHECK_GE(offset + len, 0);
@@ -95,39 +108,39 @@
if (offset + len <= capacity())
return true;
- // If we are writing to the first 16K (kMaxBlockSize), we want to keep the
- // buffer offset_ at 0.
- if (!Size() && offset > kMaxBlockSize)
+ if (!Size())
return GrowBuffer(len, kMaxBufferSize);
int required = offset - offset_ + len;
return GrowBuffer(required, kMaxBufferSize * 6 / 5);
}
-void EntryImpl::UserBuffer::Truncate(int offset) {
+void EntryImplV3::UserBuffer::Truncate(int offset) {
DCHECK_GE(offset, 0);
DCHECK_GE(offset, offset_);
DVLOG(3) << "Buffer truncate at " << offset << " current " << offset_;
offset -= offset_;
if (Size() >= offset)
- buffer_.resize(offset);
+ buffer_->set_offset(offset);
}
-void EntryImpl::UserBuffer::Write(int offset, IOBuffer* buf, int len) {
+void EntryImplV3::UserBuffer::Write(int offset, IOBuffer* buf, int len) {
DCHECK_GE(offset, 0);
DCHECK_GE(len, 0);
DCHECK_GE(offset + len, 0);
DCHECK_GE(offset, offset_);
DVLOG(3) << "Buffer write at " << offset << " current " << offset_;
- if (!Size() && offset > kMaxBlockSize)
+ if (!Size())
offset_ = offset;
offset -= offset_;
- if (offset > Size())
- buffer_.resize(offset);
+ if (offset > Size()) {
+ memset(buffer_->data(), 0, offset - Size());
+ buffer_->set_offset(offset);
+ }
if (!len)
return;
@@ -136,17 +149,18 @@
int valid_len = Size() - offset;
int copy_len = std::min(valid_len, len);
if (copy_len) {
- memcpy(&buffer_[offset], buffer, copy_len);
+ memcpy(buffer_->StartOfBuffer() + offset, buffer, copy_len);
len -= copy_len;
buffer += copy_len;
}
if (!len)
return;
- buffer_.insert(buffer_.end(), buffer, buffer + len);
+ memcpy(buffer_->data(), buffer, len);
+ buffer_->set_offset(buffer_->offset() + len);
}
-bool EntryImpl::UserBuffer::PreRead(int eof, int offset, int* len) {
+bool EntryImplV3::UserBuffer::PreRead(int eof, int offset, int* len) {
DCHECK_GE(offset, 0);
DCHECK_GT(*len, 0);
@@ -171,7 +185,7 @@
return (offset - offset_ < Size());
}
-int EntryImpl::UserBuffer::Read(int offset, IOBuffer* buf, int len) {
+int EntryImplV3::UserBuffer::Read(int offset, IOBuffer* buf, int len) {
DCHECK_GE(offset, 0);
DCHECK_GT(len, 0);
DCHECK(Size() || offset < offset_);
@@ -192,24 +206,28 @@
DCHECK_GE(start, 0);
DCHECK_GE(available, 0);
len = std::min(len, available);
- memcpy(buf->data() + clean_bytes, &buffer_[start], len);
+ memcpy(buf->data() + clean_bytes, buffer_->StartOfBuffer() + start, len);
return len + clean_bytes;
}
-void EntryImpl::UserBuffer::Reset() {
- if (!grow_allowed_) {
- if (backend_)
- backend_->BufferDeleted(capacity() - kMaxBlockSize);
- grow_allowed_ = true;
- std::vector<char> tmp;
- buffer_.swap(tmp);
- buffer_.reserve(kMaxBlockSize);
- }
+void EntryImplV3::UserBuffer::Rebase() {
+ DCHECK(!Size());
+ DCHECK(offset_ < capacity());
+ memset(buffer_->data(), 0, offset_);
+ buffer_->set_offset(offset_);
offset_ = 0;
- buffer_.clear();
}
-bool EntryImpl::UserBuffer::GrowBuffer(int required, int limit) {
+void EntryImplV3::UserBuffer::ForceSize(bool value) {
+ force_size_ = value;
+}
+
+net::IOBuffer* EntryImplV3::UserBuffer::Get() {
+ buffer_->set_offset(0);
+ return buffer_.get();
+}
+
+bool EntryImplV3::UserBuffer::GrowBuffer(int required, int limit) {
DCHECK_GE(required, 0);
int current_size = capacity();
if (required <= current_size)
@@ -221,169 +239,183 @@
if (!backend_)
return false;
- int to_add = std::max(required - current_size, kMaxBlockSize * 4);
+ int to_add = std::max(required - current_size, kMinBufferSize * 4);
to_add = std::max(current_size, to_add);
required = std::min(current_size + to_add, limit);
- grow_allowed_ = backend_->IsAllocAllowed(current_size, required);
+ grow_allowed_ = backend_->IsAllocAllowed(current_size, required, force_size_);
+ force_size_ = false;
if (!grow_allowed_)
return false;
DVLOG(3) << "Buffer grow to " << required;
- buffer_.reserve(required);
+ buffer_->SetCapacity(required);
return true;
}
// ------------------------------------------------------------------------
-EntryImpl::EntryImpl(BackendImpl* backend, Addr address, bool read_only)
- : entry_(NULL, Addr(0)), node_(NULL, Addr(0)),
- backend_(backend->GetWeakPtr()), doomed_(false), read_only_(read_only),
- dirty_(false) {
- entry_.LazyInit(backend->File(address), address);
+EntryImplV3::EntryImplV3(BackendImplV3* backend, Addr address, bool read_only)
+ : backend_(backend->GetWeakPtr()),
+ address_(address),
+ num_handles_(0),
+ doomed_(false),
+ read_only_(read_only),
+ dirty_(true),
+ modified_(false),
+ callback_(base::Bind(&EntryImplV3::OnIOComplete,
+ base::Unretained(this))) {
for (int i = 0; i < kNumStreams; i++) {
unreported_size_[i] = 0;
}
}
-bool EntryImpl::CreateEntry(Addr node_address, const std::string& key,
- uint32 hash) {
- Trace("Create entry In");
- EntryStore* entry_store = entry_.Data();
- RankingsNode* node = node_.Data();
- memset(entry_store, 0, sizeof(EntryStore) * entry_.address().num_blocks());
- memset(node, 0, sizeof(RankingsNode));
- if (!node_.LazyInit(backend_->File(node_address), node_address))
- return false;
+EntryImplV3::EntryImplV3(BackendImplV3* backend,
+ Addr address,
+ const std::string& key,
+ scoped_ptr<EntryRecord> record)
+ : entry_(record.Pass()),
+ backend_(backend->GetWeakPtr()),
+ key_(key),
+ address_(address),
+ num_handles_(0),
+ doomed_(false),
+ read_only_(false),
+ dirty_(false),
+ modified_(false),
+ callback_(base::Bind(&EntryImplV3::OnIOComplete,
+ base::Unretained(this))) {
+ for (int i = 0; i < kNumStreams; i++) {
+ unreported_size_[i] = 0;
+ }
+}
- entry_store->rankings_node = node_address.value();
- node->contents = entry_.address().value();
+EntryImplV3::EntryImplV3(BackendImplV3* backend,
+ Addr address,
+ const std::string& key,
+ scoped_ptr<ShortEntryRecord> record)
+ : short_entry_(record.Pass()),
+ backend_(backend->GetWeakPtr()),
+ key_(key),
+ address_(address),
+ num_handles_(0),
+ doomed_(false),
+ read_only_(false),
+ dirty_(false),
+ modified_(false),
+ callback_(base::Bind(&EntryImplV3::OnIOComplete,
+ base::Unretained(this))) {
+ for (int i = 0; i < kNumStreams; i++) {
+ unreported_size_[i] = 0;
+ }
+}
- entry_store->hash = hash;
- entry_store->creation_time = Time::Now().ToInternalValue();
- entry_store->key_len = static_cast<int32>(key.size());
- if (entry_store->key_len > kMaxInternalKeyLength) {
- Addr address(0);
- if (!CreateBlock(entry_store->key_len + 1, &address))
- return false;
+void EntryImplV3::CreateEntry(const std::string& key, uint32 hash,
+ ShortEntryRecord* old_info) {
+ Trace("Create entry In");
- entry_store->long_key = address.value();
- File* key_file = GetBackingFile(address, kKeyFileIndex);
- key_ = key;
+ key_ = key;
+ entry_.reset(new EntryRecord);
+ memset(entry_.get(), 0, sizeof(*entry_.get()));
+ entry_->key_len = static_cast<int>(key.size());
+ entry_->hash = hash;
+ entry_->creation_time = backend_->GetCurrentTime().ToInternalValue();
+ entry_->last_access_time = entry_->creation_time;
+ entry_->last_modified_time = entry_->creation_time;
+ dirty_ = true;
+ backend_->UpdateRank(this, true);
- size_t offset = 0;
- if (address.is_block_file())
- offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;
+ if (old_info) {
+ entry_->reuse_count = old_info->reuse_count;
+ entry_->refetch_count = old_info->refetch_count;
+ }
- if (!key_file || !key_file->Write(key.data(), key.size(), offset)) {
- DeleteData(address, kKeyFileIndex);
- return false;
- }
-
- if (address.is_separate_file())
- key_file->SetLength(key.size() + 1);
- } else {
- memcpy(entry_store->key, key.data(), key.size());
- entry_store->key[key.size()] = '\0';
- }
backend_->ModifyStorageSize(0, static_cast<int32>(key.size()));
CACHE_UMA(COUNTS, "KeySize", 0, static_cast<int32>(key.size()));
- node->dirty = backend_->GetCurrentEntryId();
+
+ WriteKey();
+ num_handles_++;
Log("Create Entry ");
- return true;
}
-uint32 EntryImpl::GetHash() {
- return entry_.Data()->hash;
+void EntryImplV3::OnOpenEntry() {
+ num_handles_++;
}
-bool EntryImpl::IsSameEntry(const std::string& key, uint32 hash) {
- if (entry_.Data()->hash != hash ||
- static_cast<size_t>(entry_.Data()->key_len) != key.size())
- return false;
+scoped_ptr<ShortEntryRecord> EntryImplV3::GetShortEntryRecord() {
+ return short_entry_.Pass();
+}
- return (key.compare(GetKey()) == 0);
+uint32 EntryImplV3::GetHash() const {
+ return entry_->hash;
}
-void EntryImpl::InternalDoom() {
- net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM);
- DCHECK(node_.HasData());
- if (!node_.Data()->dirty) {
- node_.Data()->dirty = backend_->GetCurrentEntryId();
- node_.Store();
- }
- doomed_ = true;
+Addr EntryImplV3::GetAddress() const {
+ return address_;
}
-// This only includes checks that relate to the first block of the entry (the
-// first 256 bytes), and values that should be set from the entry creation.
-// Basically, even if there is something wrong with this entry, we want to see
-// if it is possible to load the rankings node and delete them together.
-bool EntryImpl::SanityCheck() {
- if (!entry_.VerifyHash())
- return false;
+int EntryImplV3::GetReuseCounter() const {
+ return entry_->reuse_count;
+}
- EntryStore* stored = entry_.Data();
- if (!stored->rankings_node || stored->key_len <= 0)
- return false;
+void EntryImplV3::SetReuseCounter(int counter) {
+ DCHECK_LT(counter, 256);
+ DCHECK_GE(counter, 0);
+ entry_->reuse_count = static_cast<uint8>(counter);
+ dirty_ = true;
+}
- if (stored->reuse_count < 0 || stored->refetch_count < 0)
- return false;
+int EntryImplV3::GetRefetchCounter() const {
+ return entry_->refetch_count;
+}
- Addr rankings_addr(stored->rankings_node);
- if (!rankings_addr.SanityCheckForRankings())
- return false;
+void EntryImplV3::SetRefetchCounter(int counter) {
+ DCHECK_LT(counter, 256);
+ DCHECK_GE(counter, 0);
+ entry_->refetch_count = static_cast<uint8>(counter);
+ dirty_ = true;
+}
- Addr next_addr(stored->next);
- if (next_addr.is_initialized() && !next_addr.SanityCheckForEntry()) {
- STRESS_NOTREACHED();
+bool EntryImplV3::IsSameEntry(const std::string& key, uint32 hash) {
+ if (entry_->hash != hash ||
+ static_cast<size_t>(entry_->key_len) != key.size())
return false;
- }
- STRESS_DCHECK(next_addr.value() != entry_.address().value());
- if (stored->state > ENTRY_DOOMED || stored->state < ENTRY_NORMAL)
- return false;
+ return (key.compare(GetKey()) == 0);
+}
- Addr key_addr(stored->long_key);
- if ((stored->key_len <= kMaxInternalKeyLength && key_addr.is_initialized()) ||
- (stored->key_len > kMaxInternalKeyLength && !key_addr.is_initialized()))
- return false;
+void EntryImplV3::InternalDoom() {
+ DCHECK(!doomed_);
+ net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM);
+ doomed_ = true;
+ dirty_ = true;
+}
- if (!key_addr.SanityCheck())
- return false;
+bool EntryImplV3::SanityCheck() {
+ DCHECK(BasicSanityCheck(*entry_.get()));
- if (key_addr.is_initialized() &&
- ((stored->key_len < kMaxBlockSize && key_addr.is_separate_file()) ||
- (stored->key_len >= kMaxBlockSize && key_addr.is_block_file())))
+ if (entry_->reuse_count < 0 || entry_->refetch_count < 0)
return false;
- int num_blocks = NumBlocksForEntry(stored->key_len);
- if (entry_.address().num_blocks() != num_blocks)
+ if (entry_->state > ENTRY_USED || entry_->state < ENTRY_NEW)
return false;
return true;
}
-bool EntryImpl::DataSanityCheck() {
- EntryStore* stored = entry_.Data();
- Addr key_addr(stored->long_key);
-
- // The key must be NULL terminated.
- if (!key_addr.is_initialized() && stored->key[stored->key_len])
+bool EntryImplV3::DataSanityCheck() {
+ if (entry_->hash != base::Hash(GetKey()))
return false;
- if (stored->hash != base::Hash(GetKey()))
- return false;
-
for (int i = 0; i < kNumStreams; i++) {
- Addr data_addr(stored->data_addr[i]);
- int data_size = stored->data_size[i];
+ Addr data_addr(entry_->data_addr[i]);
+ int data_size = entry_->data_size[i];
if (data_size < 0)
return false;
if (!data_size && data_addr.is_initialized())
return false;
- if (!data_addr.SanityCheck())
+ if (!data_addr.SanityCheckV3())
return false;
if (!data_size)
continue;
@@ -395,40 +427,76 @@
return true;
}
-void EntryImpl::FixForDelete() {
- EntryStore* stored = entry_.Data();
- Addr key_addr(stored->long_key);
+// Static.
+bool EntryImplV3::BasicSanityCheck(const EntryRecord& record) {
+ CacheEntryBlockV3 entry_block;
+ entry_block.SetData(const_cast<EntryRecord*>(&record));
+ if (!entry_block.VerifyHash())
+ return false;
- if (!key_addr.is_initialized())
- stored->key[stored->key_len] = '\0';
+ if (record.key_len <= 0 || record.data_size[0] < record.key_len)
+ return false;
- for (int i = 0; i < kNumStreams; i++) {
- Addr data_addr(stored->data_addr[i]);
- int data_size = stored->data_size[i];
- if (data_addr.is_initialized()) {
- if ((data_size <= kMaxBlockSize && data_addr.is_separate_file()) ||
- (data_size > kMaxBlockSize && data_addr.is_block_file()) ||
- !data_addr.SanityCheck()) {
- STRESS_NOTREACHED();
- // The address is weird so don't attempt to delete it.
- stored->data_addr[i] = 0;
- // In general, trust the stored size as it should be in sync with the
- // total size tracked by the backend.
- }
- }
- if (data_size < 0)
- stored->data_size[i] = 0;
- }
- entry_.Store();
+ Addr data_addr(record.data_addr[0]);
+ if (!data_addr.is_initialized() || !data_addr.SanityCheckV3())
+ return false;
+
+ if (record.data_size[0] <= kMaxBlockSize && data_addr.is_separate_file())
+ return false;
+
+ if (record.data_size[0] > kMaxBlockSize && data_addr.is_block_file())
+ return false;
+
+ return true;
}
-void EntryImpl::SetTimes(base::Time last_used, base::Time last_modified) {
- node_.Data()->last_used = last_used.ToInternalValue();
- node_.Data()->last_modified = last_modified.ToInternalValue();
- node_.set_modified();
+// Static.
+bool EntryImplV3::DeletedSanityCheck(const ShortEntryRecord& record) {
+ CacheShortEntryBlock entry_block;
+ entry_block.SetData(const_cast<ShortEntryRecord*>(&record));
+ if (!entry_block.VerifyHash())
+ return false;
+
+ if (record.key_len <= 0)
+ return false;
+
+ return true;
}
-void EntryImpl::BeginLogging(net::NetLog* net_log, bool created) {
+void EntryImplV3::FixForDelete() {
+ //EntryStore* stored = entry_.Data();
+ //Addr key_addr(stored->long_key);
+
+ //if (!key_addr.is_initialized())
+ // stored->key[stored->key_len] = '\0';
+
+ //for (int i = 0; i < kNumStreams; i++) {
+ // Addr data_addr(stored->data_addr[i]);
+ // int data_size = stored->data_size[i];
+ // if (data_addr.is_initialized()) {
+ // if ((data_size <= kMaxBlockSize && data_addr.is_separate_file()) ||
+ // (data_size > kMaxBlockSize && data_addr.is_block_file()) ||
+ // !data_addr.SanityCheck()) {
+ // STRESS_NOTREACHED();
+ // // The address is weird so don't attempt to delete it.
+ // stored->data_addr[i] = 0;
+ // // In general, trust the stored size as it should be in sync with the
+ // // total size tracked by the backend.
+ // }
+ // }
+ // if (data_size < 0)
+ // stored->data_size[i] = 0;
+ //}
+ //entry_.Store();
+}
+
+void EntryImplV3::SetTimes(base::Time last_used, base::Time last_modified) {
+ entry_->last_access_time = last_used.ToInternalValue();
+ entry_->last_modified_time = last_modified.ToInternalValue();
+ dirty_ = true;
+}
+
+void EntryImplV3::BeginLogging(net::NetLog* net_log, bool created) {
DCHECK(!net_log_.net_log());
net_log_ = net::BoundNetLog::Make(
net_log, net::NetLog::SOURCE_DISK_CACHE_ENTRY);
@@ -437,112 +505,92 @@
CreateNetLogEntryCreationCallback(this, created));
}
-const net::BoundNetLog& EntryImpl::net_log() const {
+const net::BoundNetLog& EntryImplV3::net_log() const {
return net_log_;
}
+void EntryImplV3::NotifyDestructionForTest(const CompletionCallback& callback) {
+ DCHECK(destruction_callback_.is_null());
+ destruction_callback_ = callback;
+}
+
// ------------------------------------------------------------------------
-void EntryImpl::Doom() {
- if (background_queue_)
- background_queue_->DoomEntryImpl(this);
-}
-
-void EntryImpl::DoomImpl() {
+void EntryImplV3::Doom() {
if (doomed_ || !backend_)
return;
- SetPointerForInvalidEntry(backend_->GetCurrentEntryId());
backend_->InternalDoomEntry(this);
}
-void EntryImpl::Close() {
- if (background_queue_)
- background_queue_->CloseEntryImpl(this);
+void EntryImplV3::Close() {
+ num_handles_--;
+ if (!num_handles_) {
+ if (sparse_.get())
+ sparse_->Close();
+
+ if (!pending_operations_.empty()) {
+ PendingOperation op =
+ { PENDING_CLEANUP, 0, 0, NULL, 0, CompletionCallback(), false };
+ pending_operations_.push(op);
+ } else {
+ Cleanup();
+ }
+ }
+ Release();
}
-std::string EntryImpl::GetKey() const {
- CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_);
- int key_len = entry->Data()->key_len;
- if (key_len <= kMaxInternalKeyLength)
- return std::string(entry->Data()->key);
-
- // We keep a copy of the key so that we can always return it, even if the
- // backend is disabled.
- if (!key_.empty())
- return key_;
-
- Addr address(entry->Data()->long_key);
- DCHECK(address.is_initialized());
- size_t offset = 0;
- if (address.is_block_file())
- offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;
-
- COMPILE_ASSERT(kNumStreams == kKeyFileIndex, invalid_key_index);
- File* key_file = const_cast<EntryImpl*>(this)->GetBackingFile(address,
- kKeyFileIndex);
- if (!key_file)
- return std::string();
-
- ++key_len; // We store a trailing \0 on disk that we read back below.
- if (!offset && key_file->GetLength() != static_cast<size_t>(key_len))
- return std::string();
-
- if (!key_file->Read(WriteInto(&key_, key_len), key_len, offset))
- key_.clear();
+std::string EntryImplV3::GetKey() const {
return key_;
}
-Time EntryImpl::GetLastUsed() const {
- CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_);
- return Time::FromInternalValue(node->Data()->last_used);
+Time EntryImplV3::GetLastUsed() const {
+ return Time::FromInternalValue(entry_->last_access_time);
}
-Time EntryImpl::GetLastModified() const {
- CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_);
- return Time::FromInternalValue(node->Data()->last_modified);
+Time EntryImplV3::GetLastModified() const {
+ return Time::FromInternalValue(entry_->last_modified_time);
}
-int32 EntryImpl::GetDataSize(int index) const {
+int32 EntryImplV3::GetDataSize(int index) const {
if (index < 0 || index >= kNumStreams)
return 0;
- CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_);
- return entry->Data()->data_size[index];
+ return GetAdjustedSize(index, entry_->data_size[index]);
}
-int EntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len,
+int32 EntryImplV3::GetAdjustedSize(int index, int real_size) const {
+ DCHECK_GE(index, 0);
+ DCHECK_LE(index, kNumStreams);
+
+ if (index == kKeyIndex)
+ return real_size - key_.size();
+
+ return real_size;
+}
+
+int EntryImplV3::ReadData(int index, int offset, IOBuffer* buf, int buf_len,
const CompletionCallback& callback) {
- if (callback.is_null())
- return ReadDataImpl(index, offset, buf, buf_len, callback);
-
- DCHECK(node_.Data()->dirty || read_only_);
if (index < 0 || index >= kNumStreams)
return net::ERR_INVALID_ARGUMENT;
- int entry_size = entry_.Data()->data_size[index];
- if (offset >= entry_size || offset < 0 || !buf_len)
- return 0;
-
if (buf_len < 0)
return net::ERR_INVALID_ARGUMENT;
- if (!background_queue_)
- return net::ERR_UNEXPECTED;
-
- background_queue_->ReadData(this, index, offset, buf, buf_len, callback);
- return net::ERR_IO_PENDING;
-}
-
-int EntryImpl::ReadDataImpl(int index, int offset, IOBuffer* buf, int buf_len,
- const CompletionCallback& callback) {
+ if (!pending_operations_.empty()) {
+ PendingOperation op =
+ { PENDING_READ, index, offset, buf, buf_len, callback, false };
+ pending_operations_.push(op);
+ return net::ERR_IO_PENDING;
+ }
+
if (net_log_.IsLoggingAllEvents()) {
net_log_.BeginEvent(
net::NetLog::TYPE_ENTRY_READ_DATA,
CreateNetLogReadWriteDataCallback(index, offset, buf_len, false));
}
- int result = InternalReadData(index, offset, buf, buf_len, callback);
+ int result = ReadDataImpl(index, offset, buf, buf_len, NULL, callback);
if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) {
net_log_.EndEvent(
@@ -552,37 +600,29 @@
return result;
}
-int EntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len,
+int EntryImplV3::WriteData(int index, int offset, IOBuffer* buf, int buf_len,
const CompletionCallback& callback, bool truncate) {
- if (callback.is_null())
- return WriteDataImpl(index, offset, buf, buf_len, callback, truncate);
-
- DCHECK(node_.Data()->dirty || read_only_);
if (index < 0 || index >= kNumStreams)
return net::ERR_INVALID_ARGUMENT;
if (offset < 0 || buf_len < 0)
return net::ERR_INVALID_ARGUMENT;
- if (!background_queue_)
- return net::ERR_UNEXPECTED;
+ if (!pending_operations_.empty()) {
+ PendingOperation op =
+ { PENDING_WRITE, index, offset, buf, buf_len, callback, truncate };
+ pending_operations_.push(op);
+ return net::ERR_IO_PENDING;
+ }
- background_queue_->WriteData(this, index, offset, buf, buf_len, truncate,
- callback);
- return net::ERR_IO_PENDING;
-}
-
-int EntryImpl::WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len,
- const CompletionCallback& callback,
- bool truncate) {
if (net_log_.IsLoggingAllEvents()) {
net_log_.BeginEvent(
net::NetLog::TYPE_ENTRY_WRITE_DATA,
CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate));
}
- int result = InternalWriteData(index, offset, buf, buf_len, callback,
- truncate);
+ int result = WriteDataImpl(index, offset, buf, buf_len, NULL, callback,
+ truncate);
if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) {
net_log_.EndEvent(
@@ -592,109 +632,58 @@
return result;
}
-int EntryImpl::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
+int EntryImplV3::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
const CompletionCallback& callback) {
- if (callback.is_null())
- return ReadSparseDataImpl(offset, buf, buf_len, callback);
+ if (!sparse_.get())
+ sparse_.reset(new SparseControlV3(this));
- if (!background_queue_)
- return net::ERR_UNEXPECTED;
-
- background_queue_->ReadSparseData(this, offset, buf, buf_len, callback);
- return net::ERR_IO_PENDING;
-}
-
-int EntryImpl::ReadSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len,
- const CompletionCallback& callback) {
- DCHECK(node_.Data()->dirty || read_only_);
- int result = InitSparseData();
- if (net::OK != result)
- return result;
-
TimeTicks start = TimeTicks::Now();
- result = sparse_->StartIO(SparseControl::kReadOperation, offset, buf, buf_len,
- callback);
+ int result = sparse_->StartIO(SparseControlV3::kReadOperation, offset, buf,
+ buf_len, callback);
ReportIOTime(kSparseRead, start);
return result;
}
-int EntryImpl::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
+int EntryImplV3::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
const CompletionCallback& callback) {
- if (callback.is_null())
- return WriteSparseDataImpl(offset, buf, buf_len, callback);
+ if (!sparse_.get())
+ sparse_.reset(new SparseControlV3(this));
- if (!background_queue_)
- return net::ERR_UNEXPECTED;
-
- background_queue_->WriteSparseData(this, offset, buf, buf_len, callback);
- return net::ERR_IO_PENDING;
-}
-
-int EntryImpl::WriteSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len,
- const CompletionCallback& callback) {
- DCHECK(node_.Data()->dirty || read_only_);
- int result = InitSparseData();
- if (net::OK != result)
- return result;
-
TimeTicks start = TimeTicks::Now();
- result = sparse_->StartIO(SparseControl::kWriteOperation, offset, buf,
- buf_len, callback);
+ int result = sparse_->StartIO(SparseControlV3::kWriteOperation, offset, buf,
+ buf_len, callback);
ReportIOTime(kSparseWrite, start);
return result;
}
-int EntryImpl::GetAvailableRange(int64 offset, int len, int64* start,
+int EntryImplV3::GetAvailableRange(int64 offset, int len, int64* start,
const CompletionCallback& callback) {
- if (!background_queue_)
- return net::ERR_UNEXPECTED;
+ if (!sparse_.get())
+ sparse_.reset(new SparseControlV3(this));
- background_queue_->GetAvailableRange(this, offset, len, start, callback);
- return net::ERR_IO_PENDING;
+ return sparse_->GetAvailableRange(offset, len, start, callback);
}
-int EntryImpl::GetAvailableRangeImpl(int64 offset, int len, int64* start) {
- int result = InitSparseData();
- if (net::OK != result)
- return result;
-
- return sparse_->GetAvailableRange(offset, len, start);
-}
-
-bool EntryImpl::CouldBeSparse() const {
+bool EntryImplV3::CouldBeSparse() const {
if (sparse_.get())
- return true;
+ return sparse_->CouldBeSparse();
- scoped_ptr<SparseControl> sparse;
- sparse.reset(new SparseControl(const_cast<EntryImpl*>(this)));
+ scoped_ptr<SparseControlV3> sparse;
+ sparse.reset(new SparseControlV3(const_cast<EntryImplV3*>(this)));
return sparse->CouldBeSparse();
}
-void EntryImpl::CancelSparseIO() {
- if (background_queue_)
- background_queue_->CancelSparseIO(this);
-}
-
-void EntryImpl::CancelSparseIOImpl() {
+void EntryImplV3::CancelSparseIO() {
if (!sparse_.get())
return;
sparse_->CancelIO();
}
-int EntryImpl::ReadyForSparseIO(const CompletionCallback& callback) {
+int EntryImplV3::ReadyForSparseIO(const CompletionCallback& callback) {
if (!sparse_.get())
return net::OK;
- if (!background_queue_)
- return net::ERR_UNEXPECTED;
-
- background_queue_->ReadyForSparseIO(this, callback);
- return net::ERR_IO_PENDING;
-}
-
-int EntryImpl::ReadyForSparseIOImpl(const CompletionCallback& callback) {
- DCHECK(sparse_.get());
return sparse_->ReadyToUse(callback);
}
@@ -706,67 +695,99 @@
// read partial information from an entry (don't have to worry about returning
// data related to a previous cache entry because the range was not fully
// written before).
-EntryImpl::~EntryImpl() {
- if (!backend_) {
- entry_.clear_modified();
- node_.clear_modified();
+EntryImplV3::~EntryImplV3() {
+ if (!backend_)
return;
- }
- Log("~EntryImpl in");
+ Log("~EntryImplV3 in");
- // Save the sparse info to disk. This will generate IO for this entry and
- // maybe for a child entry, so it is important to do it before deleting this
- // entry.
- sparse_.reset();
+ DCHECK(!dirty_);
// Remove this entry from the list of open entries.
- backend_->OnEntryDestroyBegin(entry_.address());
+ backend_->OnEntryDestroyBegin(address_);
+ Trace("~EntryImplV3 out 0x%p", reinterpret_cast<void*>(this));
+ net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_ENTRY_IMPL);
+ backend_->OnEntryDestroyEnd();
+
+ if (!destruction_callback_.is_null())
+ destruction_callback_.Run(net::OK);
+}
+
+void EntryImplV3::Cleanup() {
+ if (!backend_ || !dirty_)
+ return;
+
+ Log("Cleanup in");
+
+ bool success = true;
if (doomed_) {
- DeleteEntryData(true);
+ success = DeleteEntryData();
} else {
#if defined(NET_BUILD_STRESS_CACHE)
SanityCheck();
#endif
net_log_.AddEvent(net::NetLog::TYPE_ENTRY_CLOSE);
- bool ret = true;
for (int index = 0; index < kNumStreams; index++) {
if (user_buffers_[index].get()) {
- if (!(ret = Flush(index, 0)))
- LOG(ERROR) << "Failed to save user data";
+ int rv = Flush(index, 0);
+ if (rv != net::OK) {
+ DCHECK_EQ(rv, net::ERR_IO_PENDING);
+ PendingOperation op =
+ { PENDING_DONE, 0, 0, NULL, 0, CompletionCallback(), false };
+ pending_operations_.push(op);
+ }
}
+ Addr address(entry_->data_addr[index]);
+ if (address.is_separate_file())
+ backend_->Close(this, address);
if (unreported_size_[index]) {
backend_->ModifyStorageSize(
- entry_.Data()->data_size[index] - unreported_size_[index],
- entry_.Data()->data_size[index]);
+ entry_->data_size[index] - unreported_size_[index],
+ entry_->data_size[index]);
}
}
- if (!ret) {
- // There was a failure writing the actual data. Mark the entry as dirty.
- int current_id = backend_->GetCurrentEntryId();
- node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1;
- node_.Store();
- } else if (node_.HasData() && !dirty_ && node_.Data()->dirty) {
- node_.Data()->dirty = 0;
- node_.Store();
+ if (dirty_) {
+ entry_->state = ENTRY_USED;
+ WriteEntryData();
}
+
+ backend_->OnEntryCleanup(this);
}
- Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this));
- net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_ENTRY_IMPL);
- backend_->OnEntryDestroyEnd();
+ if (success)
+ dirty_ = false;
+ Trace("~Cleanup out 0x%p", reinterpret_cast<void*>(this));
}
-int EntryImpl::InternalReadData(int index, int offset,
- IOBuffer* buf, int buf_len,
- const CompletionCallback& callback) {
- DCHECK(node_.Data()->dirty || read_only_);
+void EntryImplV3::WriteKey() {
+ DCHECK(!user_buffers_[kKeyIndex]);
+ DCHECK(!entry_->data_addr[kKeyIndex]);
+ DCHECK(!entry_->data_size[kKeyIndex]);
+
+ user_buffers_[kKeyIndex].reset(new UserBuffer(backend_.get()));
+ scoped_refptr<net::IOBuffer> buffer(new net::WrappedIOBuffer(key_.data()));
+
+ user_buffers_[kKeyIndex]->ForceSize(true);
+ bool rv = user_buffers_[kKeyIndex]->PreWrite(0, key_.size() + 1024);
+ DCHECK(rv);
+ user_buffers_[kKeyIndex]->ForceSize(false);
+ user_buffers_[kKeyIndex]->Write(0, buffer, key_.size());
+ UpdateSize(kKeyIndex, 0, key_.size());
+}
+
+int EntryImplV3::ReadDataImpl(int index, int offset,
+ IOBuffer* buf, int buf_len,
+ PendingOperation* operation,
+ const CompletionCallback& callback) {
DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len;
if (index < 0 || index >= kNumStreams)
return net::ERR_INVALID_ARGUMENT;
- int entry_size = entry_.Data()->data_size[index];
+ if (index == kKeyIndex)
+ offset += key_.size();
+
+ int entry_size = entry_->data_size[index];
if (offset >= entry_size || offset < 0 || !buf_len)
return 0;
@@ -781,12 +802,15 @@
if (offset + buf_len > entry_size)
buf_len = entry_size - offset;
+ if (!buf_len)
+ return 0;
+
UpdateRank(false);
backend_->OnEvent(Stats::READ_DATA);
backend_->OnRead(buf_len);
- Addr address(entry_.Data()->data_addr[index]);
+ Addr address(entry_->data_addr[index]);
int eof = address.is_initialized() ? entry_size : 0;
if (user_buffers_[index].get() &&
user_buffers_[index]->PreRead(eof, offset, &buf_len)) {
@@ -796,58 +820,24 @@
return buf_len;
}
- address.set_value(entry_.Data()->data_addr[index]);
+ address.set_value(entry_->data_addr[index]);//? again?
DCHECK(address.is_initialized());
if (!address.is_initialized()) {
- DoomImpl();
+ Doom();//?
return net::ERR_FAILED;
}
- File* file = GetBackingFile(address, index);
- if (!file) {
- DoomImpl();
- LOG(ERROR) << "No file for " << std::hex << address.value();
- return net::ERR_FILE_NOT_FOUND;
- }
-
- size_t file_offset = offset;
- if (address.is_block_file()) {
- DCHECK_LE(offset + buf_len, kMaxBlockSize);
- file_offset += address.start_block() * address.BlockSize() +
- kBlockHeaderSize;
- }
-
- SyncCallback* io_callback = NULL;
- if (!callback.is_null()) {
- io_callback = new SyncCallback(this, buf, callback,
- net::NetLog::TYPE_ENTRY_READ_DATA);
- }
-
- TimeTicks start_async = TimeTicks::Now();
-
- bool completed;
- if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) {
- if (io_callback)
- io_callback->Discard();
- DoomImpl();
- return net::ERR_CACHE_READ_FAILURE;
- }
-
- if (io_callback && completed)
- io_callback->Discard();
-
- if (io_callback)
- ReportIOTime(kReadAsync1, start_async);
-
- ReportIOTime(kRead, start);
- return (completed || callback.is_null()) ? buf_len : net::ERR_IO_PENDING;
+ if (operation)
+ operation->action = PENDING_DONE;
+ backend_->ReadData(this, address, offset, buf, buf_len, callback);
+ return net::ERR_IO_PENDING;
}
-int EntryImpl::InternalWriteData(int index, int offset,
- IOBuffer* buf, int buf_len,
- const CompletionCallback& callback,
- bool truncate) {
- DCHECK(node_.Data()->dirty || read_only_);
+int EntryImplV3::WriteDataImpl(int index, int offset,
+ IOBuffer* buf, int buf_len,
+ PendingOperation* operation,
+ const CompletionCallback& callback,
+ bool truncate) {
DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len;
if (index < 0 || index >= kNumStreams)
return net::ERR_INVALID_ARGUMENT;
@@ -870,114 +860,109 @@
return net::ERR_FAILED;
}
+ int actual_offset = (index == kKeyIndex) ? offset + key_.size() : offset;
+
TimeTicks start = TimeTicks::Now();
// Read the size at this point (it may change inside prepare).
- int entry_size = entry_.Data()->data_size[index];
- bool extending = entry_size < offset + buf_len;
- truncate = truncate && entry_size > offset + buf_len;
- Trace("To PrepareTarget 0x%x", entry_.address().value());
- if (!PrepareTarget(index, offset, buf_len, truncate))
- return net::ERR_FAILED;
+ int entry_size = entry_->data_size[index];
+ bool extending = entry_size < actual_offset + buf_len;
+ truncate = truncate && entry_size > actual_offset + buf_len;
+ Trace("To PrepareTarget 0x%x", address_.value());
- Trace("From PrepareTarget 0x%x", entry_.address().value());
+ int rv = PrepareTarget(index, actual_offset, buf_len, truncate);
+ if (rv == net::ERR_IO_PENDING) {
+ if (operation) {
+ DCHECK_EQ(operation->action, PENDING_WRITE);
+ operation->action = PENDING_FLUSH;
+ } else {
+ PendingOperation op =
+ { PENDING_FLUSH, index, offset, buf, buf_len, callback, truncate };
+ pending_operations_.push(op);
+ }
+ return rv;
+ }
+
+ if (rv != net::OK)
+ return rv;
+
+ Trace("From PrepareTarget 0x%x", address_.value());
if (extending || truncate)
- UpdateSize(index, entry_size, offset + buf_len);
+ UpdateSize(index, entry_size, actual_offset + buf_len);
UpdateRank(true);
+ OnEntryModified();
backend_->OnEvent(Stats::WRITE_DATA);
backend_->OnWrite(buf_len);
if (user_buffers_[index].get()) {
// Complete the operation locally.
- user_buffers_[index]->Write(offset, buf, buf_len);
+ user_buffers_[index]->Write(actual_offset, buf, buf_len);
ReportIOTime(kWrite, start);
return buf_len;
}
- Addr address(entry_.Data()->data_addr[index]);
- if (offset + buf_len == 0) {
+ Addr address(entry_->data_addr[index]);
+ if (actual_offset + buf_len == 0) {
if (truncate) {
DCHECK(!address.is_initialized());
}
return 0;
}
- File* file = GetBackingFile(address, index);
- if (!file)
- return net::ERR_FILE_NOT_FOUND;
+ if (address.is_separate_file() && (truncate || (extending && !buf_len)))
+ backend_->Truncate(this, address, actual_offset + buf_len);
- size_t file_offset = offset;
- if (address.is_block_file()) {
- DCHECK_LE(offset + buf_len, kMaxBlockSize);
- file_offset += address.start_block() * address.BlockSize() +
- kBlockHeaderSize;
- } else if (truncate || (extending && !buf_len)) {
- if (!file->SetLength(offset + buf_len))
- return net::ERR_FAILED;
- }
-
if (!buf_len)
return 0;
- SyncCallback* io_callback = NULL;
- if (!callback.is_null()) {
- io_callback = new SyncCallback(this, buf, callback,
- net::NetLog::TYPE_ENTRY_WRITE_DATA);
- }
-
- TimeTicks start_async = TimeTicks::Now();
-
- bool completed;
- if (!file->Write(buf->data(), buf_len, file_offset, io_callback,
- &completed)) {
- if (io_callback)
- io_callback->Discard();
- return net::ERR_CACHE_WRITE_FAILURE;
- }
-
- if (io_callback && completed)
- io_callback->Discard();
-
- if (io_callback)
- ReportIOTime(kWriteAsync1, start_async);
-
- ReportIOTime(kWrite, start);
- return (completed || callback.is_null()) ? buf_len : net::ERR_IO_PENDING;
+ if (operation)
+ operation->action = PENDING_DONE;
+ backend_->WriteData(this, address, actual_offset, buf, buf_len, callback);
+ return net::ERR_IO_PENDING;
}
// ------------------------------------------------------------------------
-bool EntryImpl::CreateDataBlock(int index, int size) {
+bool EntryImplV3::CreateDataBlock(int index, int size) {
DCHECK(index >= 0 && index < kNumStreams);
- Addr address(entry_.Data()->data_addr[index]);
+ Addr address(entry_->data_addr[index]);
if (!CreateBlock(size, &address))
return false;
- entry_.Data()->data_addr[index] = address.value();
- entry_.Store();
+ entry_->data_addr[index] = address.value();
return true;
}
-bool EntryImpl::CreateBlock(int size, Addr* address) {
+bool EntryImplV3::CreateBlock(int size, Addr* address) {
DCHECK(!address->is_initialized());
if (!backend_)
return false;
FileType file_type = Addr::RequiredFileType(size);
- if (EXTERNAL == file_type) {
- if (size > backend_->MaxFileSize())
- return false;
- if (!backend_->CreateExternalFile(address))
- return false;
- } else {
- int num_blocks = Addr::RequiredBlocks(size, file_type);
+ if (EXTERNAL != file_type) {
+ int num_blocks = (size + Addr::BlockSizeForFileType(file_type) - 1) /
+ Addr::BlockSizeForFileType(file_type);
- if (!backend_->CreateBlock(file_type, num_blocks, address))
- return false;
+ return backend_->CreateBlock(file_type, num_blocks, address);
}
+
+ if (size > backend_->MaxFileSize())
+ return false;
+
+ Addr block_address;
+ if (!backend_->CreateBlock(BLOCK_FILES, 1, &block_address))
+ return false;
+ *address = block_address.AsExternal();
+
+ scoped_refptr<net::IOBufferWithSize> buffer(
+ new net::IOBufferWithSize(2 * sizeof(uint32)));
+ memcpy(buffer->data(), &entry_->hash, buffer->size());
+
+ backend_->WriteData(this, block_address, 0, buffer, buffer->size(),
+ CompletionCallback());
return true;
}
@@ -987,47 +972,37 @@
// entry will be left dirty... and at some point it will be discarded; it is
// important that the entry doesn't keep a reference to this address, or we'll
// end up deleting the contents of |address| once again.
-void EntryImpl::DeleteData(Addr address, int index) {
+void EntryImplV3::DeleteData(Addr address) {
DCHECK(backend_);
if (!address.is_initialized())
return;
- if (address.is_separate_file()) {
- int failure = !DeleteCacheFile(backend_->GetFileName(address));
- CACHE_UMA(COUNTS, "DeleteFailed", 0, failure);
- if (failure) {
- LOG(ERROR) << "Failed to delete " <<
- backend_->GetFileName(address).value() << " from the cache.";
- }
- if (files_[index])
- files_[index] = NULL; // Releases the object.
- } else {
- backend_->DeleteBlock(address, true);
- }
+ backend_->Delete(this, address);
}
-void EntryImpl::UpdateRank(bool modified) {
+void EntryImplV3::UpdateRank(bool modified) {
if (!backend_)
return;
+ Time current = backend_->GetCurrentTime();
+ entry_->last_access_time = current.ToInternalValue();
+
+ if (modified)
+ entry_->last_modified_time = current.ToInternalValue();
+
if (!doomed_) {
- // Everything is handled by the backend.
backend_->UpdateRank(this, modified);
return;
}
-
- Time current = Time::Now();
- node_.Data()->last_used = current.ToInternalValue();
-
- if (modified)
- node_.Data()->last_modified = current.ToInternalValue();
}
-void EntryImpl::DeleteEntryData(bool everything) {
- DCHECK(doomed_ || !everything);
+bool EntryImplV3::DeleteEntryData() {
+ DCHECK(doomed_);
+ if (!backend_->ShouldDeleteNow(this))
+ return false;
if (GetEntryFlags() & PARENT_ENTRY) {
// We have some child entries that must go away.
- SparseControl::DeleteChildren(this);
+ SparseControlV3::DeleteChildren(this);
}
if (GetDataSize(0))
@@ -1035,37 +1010,23 @@
if (GetDataSize(1))
CACHE_UMA(COUNTS, "DeleteData", 0, GetDataSize(1));
for (int index = 0; index < kNumStreams; index++) {
- Addr address(entry_.Data()->data_addr[index]);
+ Addr address(entry_->data_addr[index]);
if (address.is_initialized()) {
- backend_->ModifyStorageSize(entry_.Data()->data_size[index] -
+ backend_->ModifyStorageSize(entry_->data_size[index] -
unreported_size_[index], 0);
- entry_.Data()->data_addr[index] = 0;
- entry_.Data()->data_size[index] = 0;
- entry_.Store();
- DeleteData(address, index);
+ entry_->data_addr[index] = 0;
+ entry_->data_size[index] = 0;
+ dirty_ = true;
+ //entry_.Store();
+ DeleteData(address);
}
}
- if (!everything)
- return;
-
- // Remove all traces of this entry.
- backend_->RemoveEntry(this);
-
// Note that at this point node_ and entry_ are just two blocks of data, and
// even if they reference each other, nobody should be referencing them.
- Addr address(entry_.Data()->long_key);
- DeleteData(address, kKeyFileIndex);
- backend_->ModifyStorageSize(entry_.Data()->key_len, 0);
-
- backend_->DeleteBlock(entry_.address(), true);
- entry_.Discard();
-
- if (!LeaveRankingsBehind()) {
- backend_->DeleteBlock(node_.address(), true);
- node_.Discard();
- }
+ backend_->Delete(this, address_);
+ return true;
}
// We keep a memory buffer for everything that ends up stored on a block file
@@ -1081,27 +1042,17 @@
// reuse it for the new data. Keep in mind that the normal use pattern is quite
// simple (write sequentially from the beginning), so we optimize for handling
// that case.
-bool EntryImpl::PrepareTarget(int index, int offset, int buf_len,
- bool truncate) {
+int EntryImplV3::PrepareTarget(int index, int offset, int buf_len,
+ bool truncate) {
if (truncate)
return HandleTruncation(index, offset, buf_len);
if (!offset && !buf_len)
- return true;
+ return net::OK;
- Addr address(entry_.Data()->data_addr[index]);
- if (address.is_initialized()) {
- if (address.is_block_file() && !MoveToLocalBuffer(index))
- return false;
+ if (!IsSimpleWrite(index, offset, buf_len))
+ return HandleOldData(index, offset, buf_len);
- if (!user_buffers_[index].get() && offset < kMaxBlockSize) {
- // We are about to create a buffer for the first 16KB, make sure that we
- // preserve existing data.
- if (!CopyToLocalBuffer(index))
- return false;
- }
- }
-
if (!user_buffers_[index].get())
user_buffers_[index].reset(new UserBuffer(backend_.get()));
@@ -1111,23 +1062,25 @@
// We get to this function with some data already stored. If there is a
// truncation that results on data stored internally, we'll explicitly
// handle the case here.
-bool EntryImpl::HandleTruncation(int index, int offset, int buf_len) {
- Addr address(entry_.Data()->data_addr[index]);
+int EntryImplV3::HandleTruncation(int index, int offset, int buf_len) {
+ Addr address(entry_->data_addr[index]);
- int current_size = entry_.Data()->data_size[index];
+ int current_size = entry_->data_size[index];
int new_size = offset + buf_len;
+ DCHECK_LT(new_size, current_size);
if (!new_size) {
// This is by far the most common scenario.
- backend_->ModifyStorageSize(current_size - unreported_size_[index], 0);
- entry_.Data()->data_addr[index] = 0;
- entry_.Data()->data_size[index] = 0;
+ backend_->ModifyStorageSize(current_size - unreported_size_[index], 0);//updatesize()
+ entry_->data_addr[index] = 0;
+ entry_->data_size[index] = 0;
unreported_size_[index] = 0;
- entry_.Store();
- DeleteData(address, index);
+ OnEntryModified();
+ //entry_->Store();
+ DeleteData(address);
user_buffers_[index].reset();
- return true;
+ return net::OK;
}
// We never postpone truncating a file, if there is one, but we may postpone
@@ -1140,21 +1093,24 @@
// Just truncate our buffer.
DCHECK_LT(new_size, user_buffers_[index]->End());
user_buffers_[index]->Truncate(new_size);
- return true;
+ return net::OK;
}
// Just discard our buffer.
- user_buffers_[index]->Reset();
+ user_buffers_[index].reset();
return PrepareBuffer(index, offset, buf_len);
}
// There is some overlap or we need to extend the file before the
// truncation.
- if (offset > user_buffers_[index]->Start())
- user_buffers_[index]->Truncate(new_size);
- UpdateSize(index, current_size, new_size);
- if (!Flush(index, 0))
- return false;
+ if (new_size > user_buffers_[index]->Start()) {
+ if (offset > user_buffers_[index]->Start())
+ user_buffers_[index]->Truncate(new_size);
+ UpdateSize(index, current_size, new_size);
+ int rv = Flush(index, 0);
+ if (rv != net::OK)
+ return rv;
+ }
user_buffers_[index].reset();
}
@@ -1162,80 +1118,96 @@
DCHECK(!user_buffers_[index].get());
DCHECK(address.is_initialized());
- if (new_size > kMaxBlockSize)
- return true; // Let the operation go directly to disk.
+ if (!IsSimpleWrite(index, offset, buf_len))
+ return net::OK; // Let the operation go directly to disk.
- return ImportSeparateFile(index, offset + buf_len);
+ if (address.is_separate_file())
+ backend_->Truncate(this, address, offset + buf_len);
+
+ if (!user_buffers_[index].get())
+ user_buffers_[index].reset(new UserBuffer(backend_.get()));
+
+ return PrepareBuffer(index, offset, buf_len);
}
-bool EntryImpl::CopyToLocalBuffer(int index) {
- Addr address(entry_.Data()->data_addr[index]);
- DCHECK(!user_buffers_[index].get());
- DCHECK(address.is_initialized());
+bool EntryImplV3::IsSimpleWrite(int index, int offset, int buf_len) {
+ Addr address(entry_->data_addr[index]);
+ if (!address.is_initialized())
+ return true;
- int len = std::min(entry_.Data()->data_size[index], kMaxBlockSize);
- user_buffers_[index].reset(new UserBuffer(backend_.get()));
- user_buffers_[index]->Write(len, NULL, 0);
+ if (address.is_block_file() && (offset + buf_len > kMaxBlockSize))// check limit
+ return false;
- File* file = GetBackingFile(address, index);
- int offset = 0;
+ if (!user_buffers_[index].get())
+ return true;
- if (address.is_block_file())
- offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;
+ if ((offset >= user_buffers_[index]->Start()) &&
+ (offset <= user_buffers_[index]->End())) {
+ return true;
+ }
- if (!file ||
- !file->Read(user_buffers_[index]->Data(), len, offset, NULL, NULL)) {
- user_buffers_[index].reset();
- return false;
- }
- return true;
+ return offset > entry_->data_size[index];
}
-bool EntryImpl::MoveToLocalBuffer(int index) {
- if (!CopyToLocalBuffer(index))
- return false;
+int EntryImplV3::HandleOldData(int index, int offset, int buf_len) {
+ Addr address(entry_->data_addr[index]);
+ DCHECK(address.is_initialized());
- Addr address(entry_.Data()->data_addr[index]);
- entry_.Data()->data_addr[index] = 0;
- entry_.Store();
- DeleteData(address, index);
+ if (address.is_block_file() && (offset + buf_len > kMaxBlockSize)) {// check limit
+ if (!GetAdjustedSize(index, offset) || !GetDataSize(index)) {
+ // There's nothing to save from the old data.
+ user_buffers_[index].reset();
+ DCHECK(!user_buffers_[kKeyIndex]);
+ DeleteData(address);
+ entry_->data_addr[kKeyIndex] = 0;
+ entry_->data_size[kKeyIndex] = 0;
+ WriteKey();
+ return PrepareBuffer(index, offset, buf_len);
+ }
+ // We have to move the data to a new file.
+ Addr new_address;
+ if (!CreateBlock(kMaxBlockSize * 2, &new_address))
+ return net::ERR_FAILED;
- // If we lose this entry we'll see it as zero sized.
- int len = entry_.Data()->data_size[index];
- backend_->ModifyStorageSize(len - unreported_size_[index], 0);
- unreported_size_[index] = len;
- return true;
-}
+ backend_->MoveData(this, address, new_address, entry_->data_size[index],
+ callback_);
+ entry_->data_addr[index] = new_address.value();
+ return net::ERR_IO_PENDING;
+ }
-bool EntryImpl::ImportSeparateFile(int index, int new_size) {
- if (entry_.Data()->data_size[index] > new_size)
- UpdateSize(index, entry_.Data()->data_size[index], new_size);
+ int rv = Flush(index, 0);
+ if (rv != net::OK)
+ return rv;
- return MoveToLocalBuffer(index);
+ user_buffers_[index].reset(); // Don't use a local buffer.
+ return net::OK;
}
-bool EntryImpl::PrepareBuffer(int index, int offset, int buf_len) {
+int EntryImplV3::PrepareBuffer(int index, int offset, int buf_len) {
DCHECK(user_buffers_[index].get());
+ int rv = net::OK;
if ((user_buffers_[index]->End() && offset > user_buffers_[index]->End()) ||
- offset > entry_.Data()->data_size[index]) {
+ offset > entry_->data_size[index]) {
// We are about to extend the buffer or the file (with zeros), so make sure
// that we are not overwriting anything.
- Addr address(entry_.Data()->data_addr[index]);
+ Addr address(entry_->data_addr[index]);
if (address.is_initialized() && address.is_separate_file()) {
- if (!Flush(index, 0))
- return false;
+ rv = Flush(index, 0);
+ if (rv != net::OK)
+ return rv;
// There is an actual file already, and we don't want to keep track of
// its length so we let this operation go straight to disk.
// The only case when a buffer is allowed to extend the file (as in fill
// with zeros before the start) is when there is no file yet to extend.
user_buffers_[index].reset();
- return true;
+ return rv;
}
}
if (!user_buffers_[index]->PreWrite(offset, buf_len)) {
- if (!Flush(index, offset + buf_len))
- return false;
+ rv = Flush(index, offset + buf_len);
+ if (rv != net::OK)
+ return rv;
// Lets try again.
if (offset > user_buffers_[index]->End() ||
@@ -1246,89 +1218,99 @@
user_buffers_[index].reset();
}
}
- return true;
+ return rv;
}
-bool EntryImpl::Flush(int index, int min_len) {
- Addr address(entry_.Data()->data_addr[index]);
- DCHECK(user_buffers_[index].get());
- DCHECK(!address.is_initialized() || address.is_separate_file());
+int EntryImplV3::Flush(int index, int min_len) {
+ Addr address(entry_->data_addr[index]);
+ if (!user_buffers_[index].get())
+ return net::OK;
+
+ //DCHECK(!address.is_initialized() || address.is_separate_file());
DVLOG(3) << "Flush";
- int size = std::max(entry_.Data()->data_size[index], min_len);
+ int size = std::max(entry_->data_size[index], min_len);
if (size && !address.is_initialized() && !CreateDataBlock(index, size))
- return false;
+ return net::ERR_FAILED;
- if (!entry_.Data()->data_size[index]) {
+ if (!entry_->data_size[index]) {
DCHECK(!user_buffers_[index]->Size());
- return true;
+ return net::OK;
}
- address.set_value(entry_.Data()->data_addr[index]);
+ address.set_value(entry_->data_addr[index]);
int len = user_buffers_[index]->Size();
int offset = user_buffers_[index]->Start();
if (!len && !offset)
- return true;
+ return net::OK;
- if (address.is_block_file()) {
- DCHECK_EQ(len, entry_.Data()->data_size[index]);
- DCHECK(!offset);
- offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;
+ if (!len) {
+ if (address.is_separate_file()) {
+ backend_->Truncate(this, address, offset);
+ return net::OK;
+ }
+ user_buffers_[index]->Rebase();
+ len = offset;
+ offset = 0;
}
- File* file = GetBackingFile(address, index);
- if (!file)
- return false;
-
- if (!file->Write(user_buffers_[index]->Data(), len, offset, NULL, NULL))
- return false;
- user_buffers_[index]->Reset();
-
- return true;
+ backend_->WriteData(this, address, offset, user_buffers_[index]->Get(),
+ len, callback_);
+ user_buffers_[index].reset();
+ return net::ERR_IO_PENDING;
}
-void EntryImpl::UpdateSize(int index, int old_size, int new_size) {
- if (entry_.Data()->data_size[index] == new_size)
+void EntryImplV3::UpdateSize(int index, int old_size, int new_size) {
+ if (entry_->data_size[index] == new_size)
return;
unreported_size_[index] += new_size - old_size;
- entry_.Data()->data_size[index] = new_size;
- entry_.set_modified();
+ entry_->data_size[index] = new_size;
+ OnEntryModified();
}
-int EntryImpl::InitSparseData() {
- if (sparse_.get())
- return net::OK;
+void EntryImplV3::WriteEntryData() {
+ CacheEntryBlockV3 entry_block;
+ entry_block.SetData(entry_.get());
+ entry_block.UpdateHash();
- // Use a local variable so that sparse_ never goes from 'valid' to NULL.
- scoped_ptr<SparseControl> sparse(new SparseControl(this));
- int result = sparse->Init();
- if (net::OK == result)
- sparse_.swap(sparse);
+ scoped_refptr<net::IOBufferWithSize> buffer(
+ new net::IOBufferWithSize(sizeof(EntryRecord)));
+ memcpy(buffer->data(), entry_.get(), buffer->size());
- return result;
+ backend_->WriteData(this, address_, 0, buffer, buffer->size(),
+ CompletionCallback());
}
-void EntryImpl::SetEntryFlags(uint32 flags) {
- entry_.Data()->flags |= flags;
- entry_.set_modified();
+void EntryImplV3::SetEntryFlags(uint32 flags) {
+ entry_->flags |= flags;
+ dirty_ = true;
}
-uint32 EntryImpl::GetEntryFlags() {
- return entry_.Data()->flags;
+uint32 EntryImplV3::GetEntryFlags() {
+ return entry_->flags;
}
-void EntryImpl::GetData(int index, char** buffer, Addr* address) {
+void EntryImplV3::OnEntryModified() {
+ if (modified_)
+ return;
+ DCHECK(!read_only_);
+ dirty_ = true;
+ modified_ = true;
+ if (backend_)
+ backend_->OnEntryModified(this);
+}
+
+void EntryImplV3::GetData(int index, scoped_refptr<IOBuffer>* buffer, Addr* address) {
DCHECK(backend_);
if (user_buffers_[index].get() && user_buffers_[index]->Size() &&
!user_buffers_[index]->Start()) {
// The data is already in memory, just copy it and we're done.
- int data_len = entry_.Data()->data_size[index];
+ int data_len = entry_->data_size[index];
if (data_len <= user_buffers_[index]->Size()) {
DCHECK(!user_buffers_[index]->Start());
- *buffer = new char[data_len];
- memcpy(*buffer, user_buffers_[index]->Data(), data_len);
+ *buffer = user_buffers_[index]->Get();
return;
}
}
@@ -1336,17 +1318,17 @@
// Bad news: we'd have to read the info from disk so instead we'll just tell
// the caller where to read from.
*buffer = NULL;
- address->set_value(entry_.Data()->data_addr[index]);
+ address->set_value(entry_->data_addr[index]);
if (address->is_initialized()) {
// Prevent us from deleting the block from the backing store.
- backend_->ModifyStorageSize(entry_.Data()->data_size[index] -
+ backend_->ModifyStorageSize(entry_->data_size[index] -
unreported_size_[index], 0);
- entry_.Data()->data_addr[index] = 0;
- entry_.Data()->data_size[index] = 0;
+ entry_->data_addr[index] = 0;
+ entry_->data_size[index] = 0;
}
}
-void EntryImpl::ReportIOTime(Operation op, const base::TimeTicks& start) {
+void EntryImplV3::ReportIOTime(Operation op, const base::TimeTicks& start) {
if (!backend_)
return;
@@ -1377,19 +1359,69 @@
}
}
-void EntryImpl::Log(const char* msg) {
- int dirty = 0;
- if (node_.HasData()) {
- dirty = node_.Data()->dirty;
- }
+void EntryImplV3::Log(const char* msg) {
+ Trace("%s 0x%p 0x%x", msg, reinterpret_cast<void*>(this), address_);
+ Trace(" data: 0x%x 0x%x", entry_->data_addr[0], entry_->data_addr[1]);
+ Trace(" doomed: %d", doomed_);
+}
- Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this),
- entry_.address().value(), node_.address().value());
+void EntryImplV3::OnIOComplete(int result) {
+ DCHECK_NE(result, net::ERR_IO_PENDING);
+ DCHECK(!pending_operations_.empty());
+ while (result != net::ERR_IO_PENDING) {
+ bool finished = false;
+ PendingOperation& next = pending_operations_.front();
+ switch (next.action) {
+ case PENDING_FLUSH:
+ if (result < 0)
+ finished = true;
+ next.action = PENDING_WRITE;
+ break;
+ case PENDING_READ:
+ result = ReadDataImpl(next.index, next.offset, next.buf,
+ next.buf_len, &next, callback_);
+ if (result != net::ERR_IO_PENDING)
+ finished = true;
+ break;
+ case PENDING_WRITE:
+ result = WriteDataImpl(next.index, next.offset, next.buf,
+ next.buf_len, &next, callback_,
+ next.truncate);
+ if (result != net::ERR_IO_PENDING)
+ finished = true;
+ break;
+ case PENDING_CLEANUP:
+ Cleanup();
+ finished = true;
+ break;
+ case PENDING_DONE:
+ finished = true;
+ break;
+ default: NOTREACHED();
+ }
+ if (finished) {
+ next.buf = NULL;
+ if (!next.callback.is_null())
+ next.callback.Run(result);
+ pending_operations_.pop();
- Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0],
- entry_.Data()->data_addr[1], entry_.Data()->long_key);
+ if (pending_operations_.empty()) {
+ if (dirty_ && HasOneRef()) {
+ // One of the pending operations modified this entry after the last
+ // Close... issue an extra cleanup.
+ Cleanup();
+ }
+ break;
+ }
- Trace(" doomed: %d 0x%x", doomed_, dirty);
+ // Cleanup may issue multiple flushes so there may be multiple pending
+ // callbacks already in flight. Make sure we wait for them.
+ next = pending_operations_.front();
+ DCHECK_NE(next.action, PENDING_FLUSH);
+ if (next.action == PENDING_DONE)
+ break;
+ }
+ }
}
} // namespace disk_cache
« no previous file with comments | « net/disk_cache/v3/entry_impl_v3.h ('k') | net/disk_cache/v3/eviction_v3.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698