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

Side by Side Diff: net/disk_cache/v3/entry_impl_v3.cc

Issue 121643003: Reorganize net/disk_cache into backend specific directories. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase & remediate Created 6 years, 10 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
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/disk_cache/v3/entry_impl_v3.h"
6
7 #include "base/hash.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_util.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/disk_cache/bitmap.h"
14 #include "net/disk_cache/cache_util.h"
15 #include "net/disk_cache/net_log_parameters.h"
16 #include "net/disk_cache/v3/backend_impl_v3.h"
17 #include "net/disk_cache/v3/disk_format_v3.h"
18 // #include "net/disk_cache/v3/sparse_control_v3.h"
19
20 // Define BLOCKFILE_BACKEND_IMPL_OBJ to be a disk_cache::BackendImpl* in order
21 // to use the CACHE_UMA histogram macro.
22 #define BLOCKFILE_BACKEND_IMPL_OBJ backend_
23 #include "net/disk_cache/v3/histogram_macros.h"
24
25 using base::Time;
26 using base::TimeDelta;
27 using base::TimeTicks;
28
29 namespace {
30
31 const int kMaxBufferSize = 1024 * 1024; // 1 MB.
32
33 } // namespace
34
35 namespace disk_cache {
36
37 typedef StorageBlock<EntryRecord> CacheEntryBlockV3;
38 typedef StorageBlock<ShortEntryRecord> CacheShortEntryBlock;
39
40 // This class handles individual memory buffers that store data before it is
41 // sent to disk. The buffer can start at any offset, but if we try to write to
42 // anywhere in the first 16KB of the file (kMaxBlockSize), we set the offset to
43 // zero. The buffer grows up to a size determined by the backend, to keep the
44 // total memory used under control.
45 class EntryImplV3::UserBuffer {
46 public:
47 explicit UserBuffer(BackendImplV3* backend)
48 : backend_(backend->GetWeakPtr()), offset_(0), grow_allowed_(true) {
49 buffer_.reserve(kMaxBlockSize);
50 }
51 ~UserBuffer() {
52 if (backend_)
53 backend_->BufferDeleted(capacity() - kMaxBlockSize);
54 }
55
56 // Returns true if we can handle writing |len| bytes to |offset|.
57 bool PreWrite(int offset, int len);
58
59 // Truncates the buffer to |offset| bytes.
60 void Truncate(int offset);
61
62 // Writes |len| bytes from |buf| at the given |offset|.
63 void Write(int offset, IOBuffer* buf, int len);
64
65 // Returns true if we can read |len| bytes from |offset|, given that the
66 // actual file has |eof| bytes stored. Note that the number of bytes to read
67 // may be modified by this method even though it returns false: that means we
68 // should do a smaller read from disk.
69 bool PreRead(int eof, int offset, int* len);
70
71 // Read |len| bytes from |buf| at the given |offset|.
72 int Read(int offset, IOBuffer* buf, int len);
73
74 // Prepare this buffer for reuse.
75 void Reset();
76
77 char* Data() { return buffer_.size() ? &buffer_[0] : NULL; }
78 int Size() { return static_cast<int>(buffer_.size()); }
79 int Start() { return offset_; }
80 int End() { return offset_ + Size(); }
81
82 private:
83 int capacity() { return static_cast<int>(buffer_.capacity()); }
84 bool GrowBuffer(int required, int limit);
85
86 base::WeakPtr<BackendImplV3> backend_;
87 int offset_;
88 std::vector<char> buffer_;
89 bool grow_allowed_;
90 DISALLOW_COPY_AND_ASSIGN(UserBuffer);
91 };
92
93 bool EntryImplV3::UserBuffer::PreWrite(int offset, int len) {
94 DCHECK_GE(offset, 0);
95 DCHECK_GE(len, 0);
96 DCHECK_GE(offset + len, 0);
97
98 // We don't want to write before our current start.
99 if (offset < offset_)
100 return false;
101
102 // Lets get the common case out of the way.
103 if (offset + len <= capacity())
104 return true;
105
106 // If we are writing to the first 16K (kMaxBlockSize), we want to keep the
107 // buffer offset_ at 0.
108 if (!Size() && offset > kMaxBlockSize)
109 return GrowBuffer(len, kMaxBufferSize);
110
111 int required = offset - offset_ + len;
112 return GrowBuffer(required, kMaxBufferSize * 6 / 5);
113 }
114
115 void EntryImplV3::UserBuffer::Truncate(int offset) {
116 DCHECK_GE(offset, 0);
117 DCHECK_GE(offset, offset_);
118 DVLOG(3) << "Buffer truncate at " << offset << " current " << offset_;
119
120 offset -= offset_;
121 if (Size() >= offset)
122 buffer_.resize(offset);
123 }
124
125 void EntryImplV3::UserBuffer::Write(int offset, IOBuffer* buf, int len) {
126 DCHECK_GE(offset, 0);
127 DCHECK_GE(len, 0);
128 DCHECK_GE(offset + len, 0);
129 DCHECK_GE(offset, offset_);
130 DVLOG(3) << "Buffer write at " << offset << " current " << offset_;
131
132 if (!Size() && offset > kMaxBlockSize)
133 offset_ = offset;
134
135 offset -= offset_;
136
137 if (offset > Size())
138 buffer_.resize(offset);
139
140 if (!len)
141 return;
142
143 char* buffer = buf->data();
144 int valid_len = Size() - offset;
145 int copy_len = std::min(valid_len, len);
146 if (copy_len) {
147 memcpy(&buffer_[offset], buffer, copy_len);
148 len -= copy_len;
149 buffer += copy_len;
150 }
151 if (!len)
152 return;
153
154 buffer_.insert(buffer_.end(), buffer, buffer + len);
155 }
156
157 bool EntryImplV3::UserBuffer::PreRead(int eof, int offset, int* len) {
158 DCHECK_GE(offset, 0);
159 DCHECK_GT(*len, 0);
160
161 if (offset < offset_) {
162 // We are reading before this buffer.
163 if (offset >= eof)
164 return true;
165
166 // If the read overlaps with the buffer, change its length so that there is
167 // no overlap.
168 *len = std::min(*len, offset_ - offset);
169 *len = std::min(*len, eof - offset);
170
171 // We should read from disk.
172 return false;
173 }
174
175 if (!Size())
176 return false;
177
178 // See if we can fulfill the first part of the operation.
179 return (offset - offset_ < Size());
180 }
181
182 int EntryImplV3::UserBuffer::Read(int offset, IOBuffer* buf, int len) {
183 DCHECK_GE(offset, 0);
184 DCHECK_GT(len, 0);
185 DCHECK(Size() || offset < offset_);
186
187 int clean_bytes = 0;
188 if (offset < offset_) {
189 // We don't have a file so lets fill the first part with 0.
190 clean_bytes = std::min(offset_ - offset, len);
191 memset(buf->data(), 0, clean_bytes);
192 if (len == clean_bytes)
193 return len;
194 offset = offset_;
195 len -= clean_bytes;
196 }
197
198 int start = offset - offset_;
199 int available = Size() - start;
200 DCHECK_GE(start, 0);
201 DCHECK_GE(available, 0);
202 len = std::min(len, available);
203 memcpy(buf->data() + clean_bytes, &buffer_[start], len);
204 return len + clean_bytes;
205 }
206
207 void EntryImplV3::UserBuffer::Reset() {
208 if (!grow_allowed_) {
209 if (backend_)
210 backend_->BufferDeleted(capacity() - kMaxBlockSize);
211 grow_allowed_ = true;
212 std::vector<char> tmp;
213 buffer_.swap(tmp);
214 buffer_.reserve(kMaxBlockSize);
215 }
216 offset_ = 0;
217 buffer_.clear();
218 }
219
220 bool EntryImplV3::UserBuffer::GrowBuffer(int required, int limit) {
221 DCHECK_GE(required, 0);
222 int current_size = capacity();
223 if (required <= current_size)
224 return true;
225
226 if (required > limit)
227 return false;
228
229 if (!backend_)
230 return false;
231
232 int to_add = std::max(required - current_size, kMaxBlockSize * 4);
233 to_add = std::max(current_size, to_add);
234 required = std::min(current_size + to_add, limit);
235
236 grow_allowed_ = backend_->IsAllocAllowed(current_size, required);
237 if (!grow_allowed_)
238 return false;
239
240 DVLOG(3) << "Buffer grow to " << required;
241
242 buffer_.reserve(required);
243 return true;
244 }
245
246 // ------------------------------------------------------------------------
247
248 EntryImplV3::EntryImplV3(BackendImplV3* backend, Addr address, bool read_only)
249 : backend_(backend->GetWeakPtr()),
250 address_(address),
251 doomed_(false),
252 read_only_(read_only),
253 dirty_(true),
254 modified_(false) {
255 for (int i = 0; i < kNumStreams; i++) {
256 unreported_size_[i] = 0;
257 }
258 }
259
260 #if defined(V3_NOT_JUST_YET_READY)
261
262 bool EntryImplV3::CreateEntry(Addr node_address, const std::string& key,
263 uint32 hash) {
264 Trace("Create entry In");
265 EntryStore* entry_store = entry_.Data();
266 RankingsNode* node = node_.Data();
267 memset(entry_store, 0, sizeof(EntryStore) * entry_.address().num_blocks());
268 memset(node, 0, sizeof(RankingsNode));
269 if (!node_.LazyInit(backend_->File(node_address), node_address))
270 return false;
271
272 entry_store->rankings_node = node_address.value();
273 node->contents = entry_.address().value();
274
275 entry_store->hash = hash;
276 entry_store->creation_time = Time::Now().ToInternalValue();
277 entry_store->key_len = static_cast<int32>(key.size());
278 if (entry_store->key_len > kMaxInternalKeyLength) {
279 Addr address(0);
280 if (!CreateBlock(entry_store->key_len + 1, &address))
281 return false;
282
283 entry_store->long_key = address.value();
284 File* key_file = GetBackingFile(address, kKeyFileIndex);
285 key_ = key;
286
287 size_t offset = 0;
288 if (address.is_block_file())
289 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;
290
291 if (!key_file || !key_file->Write(key.data(), key.size(), offset)) {
292 DeleteData(address, kKeyFileIndex);
293 return false;
294 }
295
296 if (address.is_separate_file())
297 key_file->SetLength(key.size() + 1);
298 } else {
299 memcpy(entry_store->key, key.data(), key.size());
300 entry_store->key[key.size()] = '\0';
301 }
302 backend_->ModifyStorageSize(0, static_cast<int32>(key.size()));
303 CACHE_UMA(COUNTS, "KeySize", 0, static_cast<int32>(key.size()));
304 node->dirty = backend_->GetCurrentEntryId();
305 Log("Create Entry ");
306 return true;
307 }
308
309 uint32 EntryImplV3::GetHash() {
310 return entry_.Data()->hash;
311 }
312
313 bool EntryImplV3::IsSameEntry(const std::string& key, uint32 hash) {
314 if (entry_.Data()->hash != hash ||
315 static_cast<size_t>(entry_.Data()->key_len) != key.size())
316 return false;
317
318 return (key.compare(GetKey()) == 0);
319 }
320
321 void EntryImplV3::InternalDoom() {
322 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM);
323 DCHECK(node_.HasData());
324 if (!node_.Data()->dirty) {
325 node_.Data()->dirty = backend_->GetCurrentEntryId();
326 node_.Store();
327 }
328 doomed_ = true;
329 }
330
331 // This only includes checks that relate to the first block of the entry (the
332 // first 256 bytes), and values that should be set from the entry creation.
333 // Basically, even if there is something wrong with this entry, we want to see
334 // if it is possible to load the rankings node and delete them together.
335 bool EntryImplV3::SanityCheck() {
336 if (!entry_.VerifyHash())
337 return false;
338
339 EntryStore* stored = entry_.Data();
340 if (!stored->rankings_node || stored->key_len <= 0)
341 return false;
342
343 if (stored->reuse_count < 0 || stored->refetch_count < 0)
344 return false;
345
346 Addr rankings_addr(stored->rankings_node);
347 if (!rankings_addr.SanityCheckForRankings())
348 return false;
349
350 Addr next_addr(stored->next);
351 if (next_addr.is_initialized() && !next_addr.SanityCheckForEntry()) {
352 STRESS_NOTREACHED();
353 return false;
354 }
355 STRESS_DCHECK(next_addr.value() != entry_.address().value());
356
357 if (stored->state > ENTRY_DOOMED || stored->state < ENTRY_NORMAL)
358 return false;
359
360 Addr key_addr(stored->long_key);
361 if ((stored->key_len <= kMaxInternalKeyLength && key_addr.is_initialized()) ||
362 (stored->key_len > kMaxInternalKeyLength && !key_addr.is_initialized()))
363 return false;
364
365 if (!key_addr.SanityCheck())
366 return false;
367
368 if (key_addr.is_initialized() &&
369 ((stored->key_len < kMaxBlockSize && key_addr.is_separate_file()) ||
370 (stored->key_len >= kMaxBlockSize && key_addr.is_block_file())))
371 return false;
372
373 int num_blocks = NumBlocksForEntry(stored->key_len);
374 if (entry_.address().num_blocks() != num_blocks)
375 return false;
376
377 return true;
378 }
379
380 bool EntryImplV3::DataSanityCheck() {
381 EntryStore* stored = entry_.Data();
382 Addr key_addr(stored->long_key);
383
384 // The key must be NULL terminated.
385 if (!key_addr.is_initialized() && stored->key[stored->key_len])
386 return false;
387
388 if (stored->hash != base::Hash(GetKey()))
389 return false;
390
391 for (int i = 0; i < kNumStreams; i++) {
392 Addr data_addr(stored->data_addr[i]);
393 int data_size = stored->data_size[i];
394 if (data_size < 0)
395 return false;
396 if (!data_size && data_addr.is_initialized())
397 return false;
398 if (!data_addr.SanityCheck())
399 return false;
400 if (!data_size)
401 continue;
402 if (data_size <= kMaxBlockSize && data_addr.is_separate_file())
403 return false;
404 if (data_size > kMaxBlockSize && data_addr.is_block_file())
405 return false;
406 }
407 return true;
408 }
409
410 void EntryImplV3::FixForDelete() {
411 EntryStore* stored = entry_.Data();
412 Addr key_addr(stored->long_key);
413
414 if (!key_addr.is_initialized())
415 stored->key[stored->key_len] = '\0';
416
417 for (int i = 0; i < kNumStreams; i++) {
418 Addr data_addr(stored->data_addr[i]);
419 int data_size = stored->data_size[i];
420 if (data_addr.is_initialized()) {
421 if ((data_size <= kMaxBlockSize && data_addr.is_separate_file()) ||
422 (data_size > kMaxBlockSize && data_addr.is_block_file()) ||
423 !data_addr.SanityCheck()) {
424 STRESS_NOTREACHED();
425 // The address is weird so don't attempt to delete it.
426 stored->data_addr[i] = 0;
427 // In general, trust the stored size as it should be in sync with the
428 // total size tracked by the backend.
429 }
430 }
431 if (data_size < 0)
432 stored->data_size[i] = 0;
433 }
434 entry_.Store();
435 }
436
437 void EntryImplV3::SetTimes(base::Time last_used, base::Time last_modified) {
438 node_.Data()->last_used = last_used.ToInternalValue();
439 node_.Data()->last_modified = last_modified.ToInternalValue();
440 node_.set_modified();
441 }
442
443 void EntryImplV3::BeginLogging(net::NetLog* net_log, bool created) {
444 DCHECK(!net_log_.net_log());
445 net_log_ = net::BoundNetLog::Make(
446 net_log, net::NetLog::SOURCE_DISK_CACHE_ENTRY);
447 net_log_.BeginEvent(
448 net::NetLog::TYPE_DISK_CACHE_ENTRY_IMPL,
449 CreateNetLogEntryCreationCallback(this, created));
450 }
451
452 const net::BoundNetLog& EntryImplV3::net_log() const {
453 return net_log_;
454 }
455
456 // ------------------------------------------------------------------------
457
458 void EntryImplV3::Doom() {
459 if (background_queue_)
460 background_queue_->DoomEntryImpl(this);
461 }
462
463 void EntryImplV3::DoomImpl() {
464 if (doomed_ || !backend_)
465 return;
466
467 SetPointerForInvalidEntry(backend_->GetCurrentEntryId());
468 backend_->InternalDoomEntry(this);
469 }
470
471 void EntryImplV3::Close() {
472 if (background_queue_)
473 background_queue_->CloseEntryImpl(this);
474 }
475
476 std::string EntryImplV3::GetKey() const {
477 CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_);
478 int key_len = entry->Data()->key_len;
479 if (key_len <= kMaxInternalKeyLength)
480 return std::string(entry->Data()->key);
481
482 // We keep a copy of the key so that we can always return it, even if the
483 // backend is disabled.
484 if (!key_.empty())
485 return key_;
486
487 Addr address(entry->Data()->long_key);
488 DCHECK(address.is_initialized());
489 size_t offset = 0;
490 if (address.is_block_file())
491 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;
492
493 COMPILE_ASSERT(kNumStreams == kKeyFileIndex, invalid_key_index);
494 File* key_file = const_cast<EntryImpl*>(this)->GetBackingFile(address,
495 kKeyFileIndex);
496 if (!key_file)
497 return std::string();
498
499 ++key_len; // We store a trailing \0 on disk that we read back below.
500 if (!offset && key_file->GetLength() != static_cast<size_t>(key_len))
501 return std::string();
502
503 if (!key_file->Read(WriteInto(&key_, key_len), key_len, offset))
504 key_.clear();
505 return key_;
506 }
507
508 Time EntryImplV3::GetLastUsed() const {
509 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_);
510 return Time::FromInternalValue(node->Data()->last_used);
511 }
512
513 Time EntryImplV3::GetLastModified() const {
514 CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_);
515 return Time::FromInternalValue(node->Data()->last_modified);
516 }
517
518 int32 EntryImplV3::GetDataSize(int index) const {
519 if (index < 0 || index >= kNumStreams)
520 return 0;
521
522 CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_);
523 return entry->Data()->data_size[index];
524 }
525
526 int EntryImplV3::ReadData(int index, int offset, IOBuffer* buf, int buf_len,
527 const CompletionCallback& callback) {
528 if (callback.is_null())
529 return ReadDataImpl(index, offset, buf, buf_len, callback);
530
531 DCHECK(node_.Data()->dirty || read_only_);
532 if (index < 0 || index >= kNumStreams)
533 return net::ERR_INVALID_ARGUMENT;
534
535 int entry_size = entry_.Data()->data_size[index];
536 if (offset >= entry_size || offset < 0 || !buf_len)
537 return 0;
538
539 if (buf_len < 0)
540 return net::ERR_INVALID_ARGUMENT;
541
542 if (!background_queue_)
543 return net::ERR_UNEXPECTED;
544
545 background_queue_->ReadData(this, index, offset, buf, buf_len, callback);
546 return net::ERR_IO_PENDING;
547 }
548
549 int EntryImpl::ReadDataImpl(int index, int offset, IOBuffer* buf, int buf_len,
550 const CompletionCallback& callback) {
551 if (net_log_.IsLoggingAllEvents()) {
552 net_log_.BeginEvent(
553 net::NetLog::TYPE_ENTRY_READ_DATA,
554 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false));
555 }
556
557 int result = InternalReadData(index, offset, buf, buf_len, callback);
558
559 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) {
560 net_log_.EndEvent(
561 net::NetLog::TYPE_ENTRY_READ_DATA,
562 CreateNetLogReadWriteCompleteCallback(result));
563 }
564 return result;
565 }
566
567 int EntryImplV3::WriteData(int index, int offset, IOBuffer* buf, int buf_len,
568 const CompletionCallback& callback, bool truncate) {
569 if (callback.is_null())
570 return WriteDataImpl(index, offset, buf, buf_len, callback, truncate);
571
572 DCHECK(node_.Data()->dirty || read_only_);
573 if (index < 0 || index >= kNumStreams)
574 return net::ERR_INVALID_ARGUMENT;
575
576 if (offset < 0 || buf_len < 0)
577 return net::ERR_INVALID_ARGUMENT;
578
579 if (!background_queue_)
580 return net::ERR_UNEXPECTED;
581
582 background_queue_->WriteData(this, index, offset, buf, buf_len, truncate,
583 callback);
584 return net::ERR_IO_PENDING;
585 }
586
587 int EntryImpl::WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len,
588 const CompletionCallback& callback,
589 bool truncate) {
590 if (net_log_.IsLoggingAllEvents()) {
591 net_log_.BeginEvent(
592 net::NetLog::TYPE_ENTRY_WRITE_DATA,
593 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate));
594 }
595
596 int result = InternalWriteData(index, offset, buf, buf_len, callback,
597 truncate);
598
599 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) {
600 net_log_.EndEvent(
601 net::NetLog::TYPE_ENTRY_WRITE_DATA,
602 CreateNetLogReadWriteCompleteCallback(result));
603 }
604 return result;
605 }
606
607 int EntryImplV3::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
608 const CompletionCallback& callback) {
609 if (callback.is_null())
610 return ReadSparseDataImpl(offset, buf, buf_len, callback);
611
612 if (!background_queue_)
613 return net::ERR_UNEXPECTED;
614
615 background_queue_->ReadSparseData(this, offset, buf, buf_len, callback);
616 return net::ERR_IO_PENDING;
617 }
618
619 int EntryImpl::ReadSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len,
620 const CompletionCallback& callback) {
621 DCHECK(node_.Data()->dirty || read_only_);
622 int result = InitSparseData();
623 if (net::OK != result)
624 return result;
625
626 TimeTicks start = TimeTicks::Now();
627 result = sparse_->StartIO(SparseControl::kReadOperation, offset, buf, buf_len,
628 callback);
629 ReportIOTime(kSparseRead, start);
630 return result;
631 }
632
633 int EntryImplV3::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
634 const CompletionCallback& callback) {
635 if (callback.is_null())
636 return WriteSparseDataImpl(offset, buf, buf_len, callback);
637
638 if (!background_queue_)
639 return net::ERR_UNEXPECTED;
640
641 background_queue_->WriteSparseData(this, offset, buf, buf_len, callback);
642 return net::ERR_IO_PENDING;
643 }
644
645 int EntryImpl::WriteSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len,
646 const CompletionCallback& callback) {
647 DCHECK(node_.Data()->dirty || read_only_);
648 int result = InitSparseData();
649 if (net::OK != result)
650 return result;
651
652 TimeTicks start = TimeTicks::Now();
653 result = sparse_->StartIO(SparseControl::kWriteOperation, offset, buf,
654 buf_len, callback);
655 ReportIOTime(kSparseWrite, start);
656 return result;
657 }
658
659 int EntryImplV3::GetAvailableRange(int64 offset, int len, int64* start,
660 const CompletionCallback& callback) {
661 if (!background_queue_)
662 return net::ERR_UNEXPECTED;
663
664 background_queue_->GetAvailableRange(this, offset, len, start, callback);
665 return net::ERR_IO_PENDING;
666 }
667
668 int EntryImpl::GetAvailableRangeImpl(int64 offset, int len, int64* start) {
669 int result = InitSparseData();
670 if (net::OK != result)
671 return result;
672
673 return sparse_->GetAvailableRange(offset, len, start);
674 }
675
676 bool EntryImplV3::CouldBeSparse() const {
677 if (sparse_.get())
678 return true;
679
680 scoped_ptr<SparseControl> sparse;
681 sparse.reset(new SparseControl(const_cast<EntryImpl*>(this)));
682 return sparse->CouldBeSparse();
683 }
684
685 void EntryImplV3::CancelSparseIO() {
686 if (background_queue_)
687 background_queue_->CancelSparseIO(this);
688 }
689
690 void EntryImplV3::CancelSparseIOImpl() {
691 if (!sparse_.get())
692 return;
693
694 sparse_->CancelIO();
695 }
696
697 int EntryImplV3::ReadyForSparseIO(const CompletionCallback& callback) {
698 if (!sparse_.get())
699 return net::OK;
700
701 if (!background_queue_)
702 return net::ERR_UNEXPECTED;
703
704 background_queue_->ReadyForSparseIO(this, callback);
705 return net::ERR_IO_PENDING;
706 }
707
708 int EntryImplV3::ReadyForSparseIOImpl(const CompletionCallback& callback) {
709 DCHECK(sparse_.get());
710 return sparse_->ReadyToUse(callback);
711 }
712
713 // ------------------------------------------------------------------------
714
715 // When an entry is deleted from the cache, we clean up all the data associated
716 // with it for two reasons: to simplify the reuse of the block (we know that any
717 // unused block is filled with zeros), and to simplify the handling of write /
718 // read partial information from an entry (don't have to worry about returning
719 // data related to a previous cache entry because the range was not fully
720 // written before).
721 EntryImplV3::~EntryImplV3() {
722 if (!backend_) {
723 entry_.clear_modified();
724 node_.clear_modified();
725 return;
726 }
727 Log("~EntryImpl in");
728
729 // Save the sparse info to disk. This will generate IO for this entry and
730 // maybe for a child entry, so it is important to do it before deleting this
731 // entry.
732 sparse_.reset();
733
734 // Remove this entry from the list of open entries.
735 backend_->OnEntryDestroyBegin(entry_.address());
736
737 if (doomed_) {
738 DeleteEntryData(true);
739 } else {
740 #if defined(NET_BUILD_STRESS_CACHE)
741 SanityCheck();
742 #endif
743 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_CLOSE);
744 bool ret = true;
745 for (int index = 0; index < kNumStreams; index++) {
746 if (user_buffers_[index].get()) {
747 if (!(ret = Flush(index, 0)))
748 LOG(ERROR) << "Failed to save user data";
749 }
750 if (unreported_size_[index]) {
751 backend_->ModifyStorageSize(
752 entry_.Data()->data_size[index] - unreported_size_[index],
753 entry_.Data()->data_size[index]);
754 }
755 }
756
757 if (!ret) {
758 // There was a failure writing the actual data. Mark the entry as dirty.
759 int current_id = backend_->GetCurrentEntryId();
760 node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1;
761 node_.Store();
762 } else if (node_.HasData() && !dirty_ && node_.Data()->dirty) {
763 node_.Data()->dirty = 0;
764 node_.Store();
765 }
766 }
767
768 Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this));
769 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_ENTRY_IMPL);
770 backend_->OnEntryDestroyEnd();
771 }
772
773 int EntryImpl::InternalReadData(int index, int offset,
774 IOBuffer* buf, int buf_len,
775 const CompletionCallback& callback) {
776 DCHECK(node_.Data()->dirty || read_only_);
777 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len;
778 if (index < 0 || index >= kNumStreams)
779 return net::ERR_INVALID_ARGUMENT;
780
781 int entry_size = entry_.Data()->data_size[index];
782 if (offset >= entry_size || offset < 0 || !buf_len)
783 return 0;
784
785 if (buf_len < 0)
786 return net::ERR_INVALID_ARGUMENT;
787
788 if (!backend_)
789 return net::ERR_UNEXPECTED;
790
791 TimeTicks start = TimeTicks::Now();
792
793 if (offset + buf_len > entry_size)
794 buf_len = entry_size - offset;
795
796 UpdateRank(false);
797
798 backend_->OnEvent(Stats::READ_DATA);
799 backend_->OnRead(buf_len);
800
801 Addr address(entry_.Data()->data_addr[index]);
802 int eof = address.is_initialized() ? entry_size : 0;
803 if (user_buffers_[index].get() &&
804 user_buffers_[index]->PreRead(eof, offset, &buf_len)) {
805 // Complete the operation locally.
806 buf_len = user_buffers_[index]->Read(offset, buf, buf_len);
807 ReportIOTime(kRead, start);
808 return buf_len;
809 }
810
811 address.set_value(entry_.Data()->data_addr[index]);
812 DCHECK(address.is_initialized());
813 if (!address.is_initialized()) {
814 DoomImpl();
815 return net::ERR_FAILED;
816 }
817
818 File* file = GetBackingFile(address, index);
819 if (!file) {
820 DoomImpl();
821 LOG(ERROR) << "No file for " << std::hex << address.value();
822 return net::ERR_FILE_NOT_FOUND;
823 }
824
825 size_t file_offset = offset;
826 if (address.is_block_file()) {
827 DCHECK_LE(offset + buf_len, kMaxBlockSize);
828 file_offset += address.start_block() * address.BlockSize() +
829 kBlockHeaderSize;
830 }
831
832 SyncCallback* io_callback = NULL;
833 if (!callback.is_null()) {
834 io_callback = new SyncCallback(this, buf, callback,
835 net::NetLog::TYPE_ENTRY_READ_DATA);
836 }
837
838 TimeTicks start_async = TimeTicks::Now();
839
840 bool completed;
841 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) {
842 if (io_callback)
843 io_callback->Discard();
844 DoomImpl();
845 return net::ERR_CACHE_READ_FAILURE;
846 }
847
848 if (io_callback && completed)
849 io_callback->Discard();
850
851 if (io_callback)
852 ReportIOTime(kReadAsync1, start_async);
853
854 ReportIOTime(kRead, start);
855 return (completed || callback.is_null()) ? buf_len : net::ERR_IO_PENDING;
856 }
857
858 int EntryImpl::InternalWriteData(int index, int offset,
859 IOBuffer* buf, int buf_len,
860 const CompletionCallback& callback,
861 bool truncate) {
862 DCHECK(node_.Data()->dirty || read_only_);
863 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len;
864 if (index < 0 || index >= kNumStreams)
865 return net::ERR_INVALID_ARGUMENT;
866
867 if (offset < 0 || buf_len < 0)
868 return net::ERR_INVALID_ARGUMENT;
869
870 if (!backend_)
871 return net::ERR_UNEXPECTED;
872
873 int max_file_size = backend_->MaxFileSize();
874
875 // offset or buf_len could be negative numbers.
876 if (offset > max_file_size || buf_len > max_file_size ||
877 offset + buf_len > max_file_size) {
878 int size = offset + buf_len;
879 if (size <= max_file_size)
880 size = kint32max;
881 backend_->TooMuchStorageRequested(size);
882 return net::ERR_FAILED;
883 }
884
885 TimeTicks start = TimeTicks::Now();
886
887 // Read the size at this point (it may change inside prepare).
888 int entry_size = entry_.Data()->data_size[index];
889 bool extending = entry_size < offset + buf_len;
890 truncate = truncate && entry_size > offset + buf_len;
891 Trace("To PrepareTarget 0x%x", entry_.address().value());
892 if (!PrepareTarget(index, offset, buf_len, truncate))
893 return net::ERR_FAILED;
894
895 Trace("From PrepareTarget 0x%x", entry_.address().value());
896 if (extending || truncate)
897 UpdateSize(index, entry_size, offset + buf_len);
898
899 UpdateRank(true);
900
901 backend_->OnEvent(Stats::WRITE_DATA);
902 backend_->OnWrite(buf_len);
903
904 if (user_buffers_[index].get()) {
905 // Complete the operation locally.
906 user_buffers_[index]->Write(offset, buf, buf_len);
907 ReportIOTime(kWrite, start);
908 return buf_len;
909 }
910
911 Addr address(entry_.Data()->data_addr[index]);
912 if (offset + buf_len == 0) {
913 if (truncate) {
914 DCHECK(!address.is_initialized());
915 }
916 return 0;
917 }
918
919 File* file = GetBackingFile(address, index);
920 if (!file)
921 return net::ERR_FILE_NOT_FOUND;
922
923 size_t file_offset = offset;
924 if (address.is_block_file()) {
925 DCHECK_LE(offset + buf_len, kMaxBlockSize);
926 file_offset += address.start_block() * address.BlockSize() +
927 kBlockHeaderSize;
928 } else if (truncate || (extending && !buf_len)) {
929 if (!file->SetLength(offset + buf_len))
930 return net::ERR_FAILED;
931 }
932
933 if (!buf_len)
934 return 0;
935
936 SyncCallback* io_callback = NULL;
937 if (!callback.is_null()) {
938 io_callback = new SyncCallback(this, buf, callback,
939 net::NetLog::TYPE_ENTRY_WRITE_DATA);
940 }
941
942 TimeTicks start_async = TimeTicks::Now();
943
944 bool completed;
945 if (!file->Write(buf->data(), buf_len, file_offset, io_callback,
946 &completed)) {
947 if (io_callback)
948 io_callback->Discard();
949 return net::ERR_CACHE_WRITE_FAILURE;
950 }
951
952 if (io_callback && completed)
953 io_callback->Discard();
954
955 if (io_callback)
956 ReportIOTime(kWriteAsync1, start_async);
957
958 ReportIOTime(kWrite, start);
959 return (completed || callback.is_null()) ? buf_len : net::ERR_IO_PENDING;
960 }
961
962 // ------------------------------------------------------------------------
963
964 bool EntryImpl::CreateDataBlock(int index, int size) {
965 DCHECK(index >= 0 && index < kNumStreams);
966
967 Addr address(entry_.Data()->data_addr[index]);
968 if (!CreateBlock(size, &address))
969 return false;
970
971 entry_.Data()->data_addr[index] = address.value();
972 entry_.Store();
973 return true;
974 }
975
976 bool EntryImpl::CreateBlock(int size, Addr* address) {
977 DCHECK(!address->is_initialized());
978 if (!backend_)
979 return false;
980
981 FileType file_type = Addr::RequiredFileType(size);
982 if (EXTERNAL == file_type) {
983 if (size > backend_->MaxFileSize())
984 return false;
985 if (!backend_->CreateExternalFile(address))
986 return false;
987 } else {
988 int num_blocks = Addr::RequiredBlocks(size, file_type);
989
990 if (!backend_->CreateBlock(file_type, num_blocks, address))
991 return false;
992 }
993 return true;
994 }
995
996 // Note that this method may end up modifying a block file so upon return the
997 // involved block will be free, and could be reused for something else. If there
998 // is a crash after that point (and maybe before returning to the caller), the
999 // entry will be left dirty... and at some point it will be discarded; it is
1000 // important that the entry doesn't keep a reference to this address, or we'll
1001 // end up deleting the contents of |address| once again.
1002 void EntryImpl::DeleteData(Addr address, int index) {
1003 DCHECK(backend_);
1004 if (!address.is_initialized())
1005 return;
1006 if (address.is_separate_file()) {
1007 int failure = !DeleteCacheFile(backend_->GetFileName(address));
1008 CACHE_UMA(COUNTS, "DeleteFailed", 0, failure);
1009 if (failure) {
1010 LOG(ERROR) << "Failed to delete " <<
1011 backend_->GetFileName(address).value() << " from the cache.";
1012 }
1013 if (files_[index])
1014 files_[index] = NULL; // Releases the object.
1015 } else {
1016 backend_->DeleteBlock(address, true);
1017 }
1018 }
1019
1020 void EntryImpl::UpdateRank(bool modified) {
1021 if (!backend_)
1022 return;
1023
1024 if (!doomed_) {
1025 // Everything is handled by the backend.
1026 backend_->UpdateRank(this, modified);
1027 return;
1028 }
1029
1030 Time current = Time::Now();
1031 node_.Data()->last_used = current.ToInternalValue();
1032
1033 if (modified)
1034 node_.Data()->last_modified = current.ToInternalValue();
1035 }
1036
1037 void EntryImpl::DeleteEntryData(bool everything) {
1038 DCHECK(doomed_ || !everything);
1039
1040 if (GetEntryFlags() & PARENT_ENTRY) {
1041 // We have some child entries that must go away.
1042 SparseControl::DeleteChildren(this);
1043 }
1044
1045 if (GetDataSize(0))
1046 CACHE_UMA(COUNTS, "DeleteHeader", 0, GetDataSize(0));
1047 if (GetDataSize(1))
1048 CACHE_UMA(COUNTS, "DeleteData", 0, GetDataSize(1));
1049 for (int index = 0; index < kNumStreams; index++) {
1050 Addr address(entry_.Data()->data_addr[index]);
1051 if (address.is_initialized()) {
1052 backend_->ModifyStorageSize(entry_.Data()->data_size[index] -
1053 unreported_size_[index], 0);
1054 entry_.Data()->data_addr[index] = 0;
1055 entry_.Data()->data_size[index] = 0;
1056 entry_.Store();
1057 DeleteData(address, index);
1058 }
1059 }
1060
1061 if (!everything)
1062 return;
1063
1064 // Remove all traces of this entry.
1065 backend_->RemoveEntry(this);
1066
1067 // Note that at this point node_ and entry_ are just two blocks of data, and
1068 // even if they reference each other, nobody should be referencing them.
1069
1070 Addr address(entry_.Data()->long_key);
1071 DeleteData(address, kKeyFileIndex);
1072 backend_->ModifyStorageSize(entry_.Data()->key_len, 0);
1073
1074 backend_->DeleteBlock(entry_.address(), true);
1075 entry_.Discard();
1076
1077 if (!LeaveRankingsBehind()) {
1078 backend_->DeleteBlock(node_.address(), true);
1079 node_.Discard();
1080 }
1081 }
1082
1083 // We keep a memory buffer for everything that ends up stored on a block file
1084 // (because we don't know yet the final data size), and for some of the data
1085 // that end up on external files. This function will initialize that memory
1086 // buffer and / or the files needed to store the data.
1087 //
1088 // In general, a buffer may overlap data already stored on disk, and in that
1089 // case, the contents of the buffer are the most accurate. It may also extend
1090 // the file, but we don't want to read from disk just to keep the buffer up to
1091 // date. This means that as soon as there is a chance to get confused about what
1092 // is the most recent version of some part of a file, we'll flush the buffer and
1093 // reuse it for the new data. Keep in mind that the normal use pattern is quite
1094 // simple (write sequentially from the beginning), so we optimize for handling
1095 // that case.
1096 bool EntryImpl::PrepareTarget(int index, int offset, int buf_len,
1097 bool truncate) {
1098 if (truncate)
1099 return HandleTruncation(index, offset, buf_len);
1100
1101 if (!offset && !buf_len)
1102 return true;
1103
1104 Addr address(entry_.Data()->data_addr[index]);
1105 if (address.is_initialized()) {
1106 if (address.is_block_file() && !MoveToLocalBuffer(index))
1107 return false;
1108
1109 if (!user_buffers_[index].get() && offset < kMaxBlockSize) {
1110 // We are about to create a buffer for the first 16KB, make sure that we
1111 // preserve existing data.
1112 if (!CopyToLocalBuffer(index))
1113 return false;
1114 }
1115 }
1116
1117 if (!user_buffers_[index].get())
1118 user_buffers_[index].reset(new UserBuffer(backend_.get()));
1119
1120 return PrepareBuffer(index, offset, buf_len);
1121 }
1122
1123 // We get to this function with some data already stored. If there is a
1124 // truncation that results on data stored internally, we'll explicitly
1125 // handle the case here.
1126 bool EntryImpl::HandleTruncation(int index, int offset, int buf_len) {
1127 Addr address(entry_.Data()->data_addr[index]);
1128
1129 int current_size = entry_.Data()->data_size[index];
1130 int new_size = offset + buf_len;
1131
1132 if (!new_size) {
1133 // This is by far the most common scenario.
1134 backend_->ModifyStorageSize(current_size - unreported_size_[index], 0);
1135 entry_.Data()->data_addr[index] = 0;
1136 entry_.Data()->data_size[index] = 0;
1137 unreported_size_[index] = 0;
1138 entry_.Store();
1139 DeleteData(address, index);
1140
1141 user_buffers_[index].reset();
1142 return true;
1143 }
1144
1145 // We never postpone truncating a file, if there is one, but we may postpone
1146 // telling the backend about the size reduction.
1147 if (user_buffers_[index].get()) {
1148 DCHECK_GE(current_size, user_buffers_[index]->Start());
1149 if (!address.is_initialized()) {
1150 // There is no overlap between the buffer and disk.
1151 if (new_size > user_buffers_[index]->Start()) {
1152 // Just truncate our buffer.
1153 DCHECK_LT(new_size, user_buffers_[index]->End());
1154 user_buffers_[index]->Truncate(new_size);
1155 return true;
1156 }
1157
1158 // Just discard our buffer.
1159 user_buffers_[index]->Reset();
1160 return PrepareBuffer(index, offset, buf_len);
1161 }
1162
1163 // There is some overlap or we need to extend the file before the
1164 // truncation.
1165 if (offset > user_buffers_[index]->Start())
1166 user_buffers_[index]->Truncate(new_size);
1167 UpdateSize(index, current_size, new_size);
1168 if (!Flush(index, 0))
1169 return false;
1170 user_buffers_[index].reset();
1171 }
1172
1173 // We have data somewhere, and it is not in a buffer.
1174 DCHECK(!user_buffers_[index].get());
1175 DCHECK(address.is_initialized());
1176
1177 if (new_size > kMaxBlockSize)
1178 return true; // Let the operation go directly to disk.
1179
1180 return ImportSeparateFile(index, offset + buf_len);
1181 }
1182
1183 bool EntryImpl::CopyToLocalBuffer(int index) {
1184 Addr address(entry_.Data()->data_addr[index]);
1185 DCHECK(!user_buffers_[index].get());
1186 DCHECK(address.is_initialized());
1187
1188 int len = std::min(entry_.Data()->data_size[index], kMaxBlockSize);
1189 user_buffers_[index].reset(new UserBuffer(backend_.get()));
1190 user_buffers_[index]->Write(len, NULL, 0);
1191
1192 File* file = GetBackingFile(address, index);
1193 int offset = 0;
1194
1195 if (address.is_block_file())
1196 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;
1197
1198 if (!file ||
1199 !file->Read(user_buffers_[index]->Data(), len, offset, NULL, NULL)) {
1200 user_buffers_[index].reset();
1201 return false;
1202 }
1203 return true;
1204 }
1205
1206 bool EntryImpl::MoveToLocalBuffer(int index) {
1207 if (!CopyToLocalBuffer(index))
1208 return false;
1209
1210 Addr address(entry_.Data()->data_addr[index]);
1211 entry_.Data()->data_addr[index] = 0;
1212 entry_.Store();
1213 DeleteData(address, index);
1214
1215 // If we lose this entry we'll see it as zero sized.
1216 int len = entry_.Data()->data_size[index];
1217 backend_->ModifyStorageSize(len - unreported_size_[index], 0);
1218 unreported_size_[index] = len;
1219 return true;
1220 }
1221
1222 bool EntryImpl::ImportSeparateFile(int index, int new_size) {
1223 if (entry_.Data()->data_size[index] > new_size)
1224 UpdateSize(index, entry_.Data()->data_size[index], new_size);
1225
1226 return MoveToLocalBuffer(index);
1227 }
1228
1229 bool EntryImpl::PrepareBuffer(int index, int offset, int buf_len) {
1230 DCHECK(user_buffers_[index].get());
1231 if ((user_buffers_[index]->End() && offset > user_buffers_[index]->End()) ||
1232 offset > entry_.Data()->data_size[index]) {
1233 // We are about to extend the buffer or the file (with zeros), so make sure
1234 // that we are not overwriting anything.
1235 Addr address(entry_.Data()->data_addr[index]);
1236 if (address.is_initialized() && address.is_separate_file()) {
1237 if (!Flush(index, 0))
1238 return false;
1239 // There is an actual file already, and we don't want to keep track of
1240 // its length so we let this operation go straight to disk.
1241 // The only case when a buffer is allowed to extend the file (as in fill
1242 // with zeros before the start) is when there is no file yet to extend.
1243 user_buffers_[index].reset();
1244 return true;
1245 }
1246 }
1247
1248 if (!user_buffers_[index]->PreWrite(offset, buf_len)) {
1249 if (!Flush(index, offset + buf_len))
1250 return false;
1251
1252 // Lets try again.
1253 if (offset > user_buffers_[index]->End() ||
1254 !user_buffers_[index]->PreWrite(offset, buf_len)) {
1255 // We cannot complete the operation with a buffer.
1256 DCHECK(!user_buffers_[index]->Size());
1257 DCHECK(!user_buffers_[index]->Start());
1258 user_buffers_[index].reset();
1259 }
1260 }
1261 return true;
1262 }
1263
1264 bool EntryImpl::Flush(int index, int min_len) {
1265 Addr address(entry_.Data()->data_addr[index]);
1266 DCHECK(user_buffers_[index].get());
1267 DCHECK(!address.is_initialized() || address.is_separate_file());
1268 DVLOG(3) << "Flush";
1269
1270 int size = std::max(entry_.Data()->data_size[index], min_len);
1271 if (size && !address.is_initialized() && !CreateDataBlock(index, size))
1272 return false;
1273
1274 if (!entry_.Data()->data_size[index]) {
1275 DCHECK(!user_buffers_[index]->Size());
1276 return true;
1277 }
1278
1279 address.set_value(entry_.Data()->data_addr[index]);
1280
1281 int len = user_buffers_[index]->Size();
1282 int offset = user_buffers_[index]->Start();
1283 if (!len && !offset)
1284 return true;
1285
1286 if (address.is_block_file()) {
1287 DCHECK_EQ(len, entry_.Data()->data_size[index]);
1288 DCHECK(!offset);
1289 offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;
1290 }
1291
1292 File* file = GetBackingFile(address, index);
1293 if (!file)
1294 return false;
1295
1296 if (!file->Write(user_buffers_[index]->Data(), len, offset, NULL, NULL))
1297 return false;
1298 user_buffers_[index]->Reset();
1299
1300 return true;
1301 }
1302
1303 void EntryImpl::UpdateSize(int index, int old_size, int new_size) {
1304 if (entry_.Data()->data_size[index] == new_size)
1305 return;
1306
1307 unreported_size_[index] += new_size - old_size;
1308 entry_.Data()->data_size[index] = new_size;
1309 entry_.set_modified();
1310 }
1311
1312 int EntryImpl::InitSparseData() {
1313 if (sparse_.get())
1314 return net::OK;
1315
1316 // Use a local variable so that sparse_ never goes from 'valid' to NULL.
1317 scoped_ptr<SparseControl> sparse(new SparseControl(this));
1318 int result = sparse->Init();
1319 if (net::OK == result)
1320 sparse_.swap(sparse);
1321
1322 return result;
1323 }
1324
1325 void EntryImpl::SetEntryFlags(uint32 flags) {
1326 entry_.Data()->flags |= flags;
1327 entry_.set_modified();
1328 }
1329
1330 uint32 EntryImpl::GetEntryFlags() {
1331 return entry_.Data()->flags;
1332 }
1333
1334 void EntryImpl::GetData(int index, char** buffer, Addr* address) {
1335 DCHECK(backend_);
1336 if (user_buffers_[index].get() && user_buffers_[index]->Size() &&
1337 !user_buffers_[index]->Start()) {
1338 // The data is already in memory, just copy it and we're done.
1339 int data_len = entry_.Data()->data_size[index];
1340 if (data_len <= user_buffers_[index]->Size()) {
1341 DCHECK(!user_buffers_[index]->Start());
1342 *buffer = new char[data_len];
1343 memcpy(*buffer, user_buffers_[index]->Data(), data_len);
1344 return;
1345 }
1346 }
1347
1348 // Bad news: we'd have to read the info from disk so instead we'll just tell
1349 // the caller where to read from.
1350 *buffer = NULL;
1351 address->set_value(entry_.Data()->data_addr[index]);
1352 if (address->is_initialized()) {
1353 // Prevent us from deleting the block from the backing store.
1354 backend_->ModifyStorageSize(entry_.Data()->data_size[index] -
1355 unreported_size_[index], 0);
1356 entry_.Data()->data_addr[index] = 0;
1357 entry_.Data()->data_size[index] = 0;
1358 }
1359 }
1360
1361 #endif // defined(V3_NOT_JUST_YET_READY).
1362
1363 void EntryImplV3::ReportIOTime(Operation op, const base::TimeTicks& start) {
1364 if (!backend_)
1365 return;
1366
1367 switch (op) {
1368 case kRead:
1369 CACHE_UMA(AGE_MS, "ReadTime", start);
1370 break;
1371 case kWrite:
1372 CACHE_UMA(AGE_MS, "WriteTime", start);
1373 break;
1374 case kSparseRead:
1375 CACHE_UMA(AGE_MS, "SparseReadTime", start);
1376 break;
1377 case kSparseWrite:
1378 CACHE_UMA(AGE_MS, "SparseWriteTime", start);
1379 break;
1380 case kAsyncIO:
1381 CACHE_UMA(AGE_MS, "AsyncIOTime", start);
1382 break;
1383 case kReadAsync1:
1384 CACHE_UMA(AGE_MS, "AsyncReadDispatchTime", start);
1385 break;
1386 case kWriteAsync1:
1387 CACHE_UMA(AGE_MS, "AsyncWriteDispatchTime", start);
1388 break;
1389 default:
1390 NOTREACHED();
1391 }
1392 }
1393
1394 void EntryImplV3::Log(const char* msg) {
1395 Trace("%s 0x%p 0x%x", msg, reinterpret_cast<void*>(this), address_);
1396 Trace(" data: 0x%x 0x%x", entry_->data_addr[0], entry_->data_addr[1]);
1397 Trace(" doomed: %d", doomed_);
1398 }
1399
1400 void EntryImplV3::Doom() {
1401 NOTIMPLEMENTED();
1402 }
1403
1404 void EntryImplV3::Close() {
1405 NOTIMPLEMENTED();
1406 }
1407
1408 std::string EntryImplV3::GetKey() const {
1409 return std::string();
1410 }
1411
1412 Time EntryImplV3::GetLastUsed() const {
1413 return Time();
1414 }
1415
1416 Time EntryImplV3::GetLastModified() const {
1417 return Time();
1418 }
1419
1420 int32 EntryImplV3::GetDataSize(int index) const {
1421 return 0;
1422 }
1423
1424 int EntryImplV3::ReadData(int index, int offset, IOBuffer* buf, int buf_len,
1425 const CompletionCallback& callback) {
1426 return net::ERR_FAILED;
1427 }
1428
1429 int EntryImplV3::WriteData(int index, int offset, IOBuffer* buf, int buf_len,
1430 const CompletionCallback& callback, bool truncate) {
1431 return net::ERR_FAILED;
1432 }
1433
1434 int EntryImplV3::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len,
1435 const CompletionCallback& callback) {
1436 return net::ERR_FAILED;
1437 }
1438
1439 int EntryImplV3::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len,
1440 const CompletionCallback& callback) {
1441 return net::ERR_FAILED;
1442 }
1443
1444 int EntryImplV3::GetAvailableRange(int64 offset, int len, int64* start,
1445 const CompletionCallback& callback) {
1446 return net::ERR_FAILED;
1447 }
1448
1449 bool EntryImplV3::CouldBeSparse() const {
1450 return false;
1451 }
1452
1453 void EntryImplV3::CancelSparseIO() {
1454 NOTIMPLEMENTED();
1455 }
1456
1457 int EntryImplV3::ReadyForSparseIO(const CompletionCallback& callback) {
1458 return net::ERR_FAILED;
1459 }
1460
1461 EntryImplV3::~EntryImplV3() {
1462 NOTIMPLEMENTED();
1463 }
1464
1465 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698