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

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

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

Powered by Google App Engine
This is Rietveld 408576698