Index: net/disk_cache/flash/cache_entry.cc |
diff --git a/net/disk_cache/flash/cache_entry.cc b/net/disk_cache/flash/cache_entry.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..24cedd6f9ce3c6dc71dd732042800d5c87448f6b |
--- /dev/null |
+++ b/net/disk_cache/flash/cache_entry.cc |
@@ -0,0 +1,181 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/logging.h" |
+#include "net/base/io_buffer.h" |
+#include "net/base/net_errors.h" |
+#include "net/disk_cache/flash/cache_entry.h" |
+#include "net/disk_cache/flash/format.h" |
+#include "net/disk_cache/flash/log_structured_store.h" |
+ |
+namespace disk_cache { |
+ |
+CacheEntry::CacheEntry(LogStructuredStore* store) |
+ : store_(store), |
+ id_(-1), |
+ init_(false), |
+ closed_(false), |
+ modified_(false) { |
+ DCHECK(store); |
+ for (int i = 0; i < kFlashCacheEntryNumStreams; ++i) |
+ streams_[i].insync = true; |
+} |
+ |
+CacheEntry::CacheEntry(LogStructuredStore* store, int32 id) |
+ : store_(store), |
+ id_(id), |
+ init_(false), |
+ closed_(false), |
+ modified_(false) { |
+ DCHECK(store); |
+} |
+ |
+CacheEntry::~CacheEntry() { |
+ DCHECK(!init_ || closed_); |
+} |
+ |
+bool CacheEntry::Init() { |
+ DCHECK(!init_); |
+ |
+ if (!OnDisk()) { |
+ init_ = true; |
+ return true; |
+ } |
+ |
+ int32 stream_sizes[kFlashCacheEntryNumStreams]; |
+ COMPILE_ASSERT(sizeof(stream_sizes) == kFlashCacheEntryHeaderSize, |
+ invalid_cache_entry_header_size); |
+ |
+ if (!store_->OpenEntry(id_) || |
+ !store_->ReadData(id_, stream_sizes, kFlashCacheEntryHeaderSize, 0)) |
rvargas (doing something else)
2012/11/28 03:12:56
needs braces
agayev
2012/11/29 16:14:43
Done.
|
+ return false; |
+ |
+ for (int i = 0, offset = kFlashCacheEntryHeaderSize; |
+ i < kFlashCacheEntryNumStreams; ++i) { |
+ streams_[i].offset = offset; |
+ streams_[i].data.resize(stream_sizes[i]); |
+ offset += stream_sizes[i]; |
+ } |
+ |
+ init_ = true; |
+ return true; |
+} |
+ |
+bool CacheEntry::Close() { |
+ DCHECK(init_ && !closed_); |
+ if (OnDisk()) |
+ store_->CloseEntry(id_); |
+ |
+ if (!modified_) { |
+ closed_ = true; |
+ return true; |
+ } |
+ |
+ if (!Save()) |
+ return false; |
+ |
+ closed_ = true; |
+ return true; |
+} |
+ |
+int32 CacheEntry::id() const { |
+ DCHECK(init_); |
+ return id_; |
+} |
+ |
+int32 CacheEntry::GetDataSize(int index) const { |
+ DCHECK(init_&& ValidStream(index)); |
rvargas (doing something else)
2012/11/28 03:12:56
you should be careful here... it is not clear to m
agayev
2012/11/29 16:14:43
Done.
|
+ return streams_[index].data.size(); |
+} |
+ |
+int CacheEntry::ReadData(int index, int offset, net::IOBuffer* buf, |
+ int buf_len) { |
+ DCHECK(init_ && ValidStream(index)); |
rvargas (doing something else)
2012/11/28 03:12:56
same here
agayev
2012/11/29 16:14:43
Done.
|
+ const int stream_size = static_cast<int>(streams_[index].data.size()); |
rvargas (doing something else)
2012/11/28 03:12:56
drop the const and call GetDataSize
agayev
2012/11/29 16:14:43
Done. Why drop const?
rvargas (doing something else)
2012/11/29 20:32:36
Because it is a local variable that just stores th
|
+ |
+ if (offset >= stream_size || offset < 0 || buf_len == 0) |
+ return 0; |
+ if (offset + buf_len > stream_size) |
+ buf_len = stream_size - offset; |
+ |
+ if (OnDisk() && !LazyRead(index)) |
+ return net::ERR_FAILED; |
+ |
+ memcpy(buf->data(), &streams_[index].data[offset], buf_len); |
rvargas (doing something else)
2012/11/28 03:12:56
And extra copy here doesn't look right. What's the
agayev
2012/11/29 16:14:43
Right now we are not buffering in the Segment laye
rvargas (doing something else)
2012/11/29 20:32:36
Well... it is an extra buffer because what we real
|
+ return buf_len; |
+} |
+ |
+int CacheEntry::WriteData(int index, int offset, net::IOBuffer* buf, |
+ int buf_len) { |
+ DCHECK(init_ && !closed_ && ValidStream(index)); |
+ DCHECK(offset >= 0 && buf_len >= 0); |
rvargas (doing something else)
2012/11/28 03:12:56
same caveat about who's the caller
agayev
2012/11/29 16:14:43
Done.
rvargas (doing something else)
2012/12/05 00:21:01
done? does this come from the "user" ?
agayev
2012/12/05 16:53:19
Yes it does. HttpCache may call WriteData with an
|
+ if (offset + buf_len == 0) { // Truncate to 0. |
+ streams_[index].data.clear(); |
+ streams_[index].insync = true; |
+ } else if (offset == 0) { // Overwrite and truncate if necessary. |
+ streams_[index].data.resize(buf_len); |
+ streams_[index].insync = true; |
+ memcpy(&streams_[index].data[offset], buf->data(), buf_len); |
+ } else { // Append. |
+ DCHECK(offset == static_cast<int>(streams_[index].data.size())); |
+ streams_[index].data.resize(offset + buf_len); |
+ if (!LazyRead(index)) |
rvargas (doing something else)
2012/11/28 03:12:56
what for?
agayev
2012/11/29 16:14:43
The idea is to not to read a stream unless it is n
rvargas (doing something else)
2012/11/29 20:32:36
But we don't have to read the first part in order
|
+ return net::ERR_FAILED; |
+ memcpy(&streams_[index].data[offset], buf->data(), buf_len); |
+ } |
+ modified_ = true; |
+ return buf_len; |
+} |
+ |
+bool CacheEntry::OnDisk() const { |
+ return id_ != -1; |
+} |
+ |
+bool CacheEntry::ValidStream(int stream_index) const { |
+ DCHECK(init_); |
rvargas (doing something else)
2012/11/28 03:12:56
I'd remove the dchecks for init that you have when
agayev
2012/11/29 16:14:43
Done.
|
+ return stream_index >= 0 && stream_index < kFlashCacheEntryNumStreams; |
+} |
+ |
+int32 CacheEntry::Size() const { |
+ DCHECK(init_); |
+ int32 size = kFlashCacheEntryHeaderSize; |
+ for (int i = 0; i < kFlashCacheEntryNumStreams; ++i) |
+ size += streams_[i].data.size(); |
rvargas (doing something else)
2012/11/28 03:12:56
GetDataSize
agayev
2012/11/29 16:14:43
Done.
|
+ DCHECK(size > 0 && size <= kFlashSegmentFreeSpace); |
+ return size; |
+} |
+ |
+bool CacheEntry::Save() { |
+ DCHECK(init_ && !closed_); |
+ int32 stream_sizes[kFlashCacheEntryNumStreams]; |
+ COMPILE_ASSERT(sizeof(stream_sizes) == kFlashCacheEntryHeaderSize, |
+ invalid_cache_entry_header_size); |
+ |
+ for (int i = 0; i < kFlashCacheEntryNumStreams; ++i) { |
+ if (!LazyRead(i)) |
rvargas (doing something else)
2012/11/28 03:12:56
why?
agayev
2012/11/29 16:14:43
Say we open an existing entry and update stream 1
rvargas (doing something else)
2012/11/29 20:32:36
but that use case doesn't work with the rest of th
|
+ return false; |
+ stream_sizes[i] = streams_[i].data.size(); |
+ } |
+ |
+ if (!store_->CreateEntry(Size(), &id_)) |
+ return false; |
+ if (!store_->WriteData(stream_sizes, kFlashCacheEntryHeaderSize)) |
+ return false; |
+ for (int i = 0; i < kFlashCacheEntryNumStreams; ++i) |
+ if (!store_->WriteData(&streams_[i].data[0], streams_[i].data.size())) |
+ return false; |
+ store_->CloseEntry(id_); |
+ return true; |
+} |
+ |
+bool CacheEntry::LazyRead(int index) { |
+ DCHECK(init_ && ValidStream(index)); |
+ Stream& stream = streams_[index]; |
+ if (!stream.insync) |
+ stream.insync = store_->ReadData(id_, &stream.data[0], stream.data.size(), |
+ stream.offset); |
+ return stream.insync; |
+} |
+ |
+} // namespace disk_cache |