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

Unified Diff: net/disk_cache/very_simple/very_simple_synchronous_entry.cc

Issue 12192005: Add new simple disk cache backend. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase to trunk (::FilePath --> base::FilePath) Created 7 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: net/disk_cache/very_simple/very_simple_synchronous_entry.cc
diff --git a/net/disk_cache/very_simple/very_simple_synchronous_entry.cc b/net/disk_cache/very_simple/very_simple_synchronous_entry.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8a3ac4372b3fcb9c862d30bbc9588db3b7b7c432
--- /dev/null
+++ b/net/disk_cache/very_simple/very_simple_synchronous_entry.cc
@@ -0,0 +1,324 @@
+// Copyright (c) 2013 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 "net/disk_cache/very_simple/very_simple_synchronous_entry.h"
+
+#include <algorithm>
+#include <cstring>
+
+#include "base/basictypes.h"
+#include "base/file_util.h"
+#include "base/location.h"
+#include "base/message_loop_proxy.h"
+#include "base/sha1.h"
+#include "base/stringprintf.h"
+#include "base/task_runner.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+#include "net/disk_cache/very_simple/very_simple_disk_format.h"
+#include "third_party/smhasher/src/MurmurHash3.h"
+
+using base::Bind;
+using base::ClosePlatformFile;
+using base::GetPlatformFileInfo;
+using base::PlatformFileError;
+using base::PlatformFileInfo;
+using base::PLATFORM_FILE_CREATE_ALWAYS;
+using base::PLATFORM_FILE_OK;
+using base::PLATFORM_FILE_OPEN;
+using base::PLATFORM_FILE_READ;
+using base::PLATFORM_FILE_WRITE;
+using base::ReadPlatformFile;
+using base::TaskRunner;
+using base::Time;
+using base::TruncatePlatformFile;
+using base::WritePlatformFile;
+
+namespace {
+
+std::string GetFilenameForKeyAndIndex(const std::string& key, int index) {
+ const std::string sha_hash = base::SHA1HashString(key);
+ return StringPrintf("%02x%02x%02x%02x%02x_%1d",
+ implicit_cast<unsigned char>(sha_hash[0]),
+ implicit_cast<unsigned char>(sha_hash[1]),
+ implicit_cast<unsigned char>(sha_hash[2]),
+ implicit_cast<unsigned char>(sha_hash[3]),
+ implicit_cast<unsigned char>(sha_hash[4]), index);
+}
+
+int32 DataSizeFromKeyAndFileSize(size_t key_size, int64 file_size) {
+ return file_size - key_size - sizeof(disk_cache::VerySimpleFileHeader);
+}
+
+int64 FileOffsetFromDataOffset(size_t key_size, int offset) {
+ const int64 headers_size = sizeof(disk_cache::VerySimpleFileHeader) +
+ key_size;
+ return headers_size + offset;
+}
+
+} // namespace
+
+namespace disk_cache {
+
+// static
+void VerySimpleSynchronousEntry::OpenEntry(
+ const FilePath& path,
+ const std::string& key,
+ const scoped_refptr<TaskRunner>& callback_runner,
+ const SynchronousEntryCallback& callback) {
+ VerySimpleSynchronousEntry* sync_entry =
+ new VerySimpleSynchronousEntry(callback_runner, path, key);
+
+ if (!sync_entry->InitializeForOpen()) {
+ delete sync_entry;
+ callback_runner->PostTask(FROM_HERE,
+ Bind(callback,
+ static_cast<VerySimpleSynchronousEntry*>(
+ NULL),
+ net::ERR_FAILED));
+ return;
+ }
+ callback_runner->PostTask(FROM_HERE, Bind(callback, sync_entry, net::OK));
rvargas (doing something else) 2013/02/06 03:28:40 nit: merge the two PostTasks?
gavinp 2013/02/08 23:17:51 Done.
+}
+
+// static
+void VerySimpleSynchronousEntry::CreateEntry(
+ const FilePath& path,
+ const std::string& key,
+ const scoped_refptr<TaskRunner>& callback_runner,
+ const SynchronousEntryCallback& callback) {
+ VerySimpleSynchronousEntry* sync_entry =
+ new VerySimpleSynchronousEntry(callback_runner, path, key);
+
+ if (!sync_entry->InitializeForCreate()) {
+ delete sync_entry;
+ callback_runner->PostTask(FROM_HERE,
+ Bind(callback,
+ static_cast<VerySimpleSynchronousEntry*>(
+ NULL),
+ net::ERR_FAILED));
+ return;
+ }
+ callback_runner->PostTask(FROM_HERE, Bind(callback, sync_entry, net::OK));
+}
+
+// static
+void VerySimpleSynchronousEntry::DoomEntry(
+ const FilePath& path,
+ const std::string& key,
+ scoped_refptr<TaskRunner> callback_runner,
+ const net::CompletionCallback& callback) {
+ for (int i = 0; i < kIndexCount; ++i) {
+ bool delete_result =
+ file_util::Delete(path.AppendASCII(GetFilenameForKeyAndIndex(key, i)),
rvargas (doing something else) 2013/02/06 03:28:40 How is Read and Write expected to work after this?
gavinp 2013/02/08 23:17:51 They are not.
pasko-google - do not use 2013/02/11 13:59:25 I think Ricardo refers to when we have pending Rea
gavinp 2013/02/11 17:55:50 Aha, yes. And we should support that, and we don't
+ false);
+ DCHECK(delete_result);
+ }
+ if (!callback.is_null())
+ callback_runner->PostTask(FROM_HERE, Bind(callback, net::OK));
+}
+
+void VerySimpleSynchronousEntry::DoomAndClose() {
+ scoped_refptr<TaskRunner> callback_runner = callback_runner_;
+ FilePath path = path_;
+ std::string key = key_;
+
+ Close();
+ // |this| is now deleted.
+
+ DoomEntry(path, key, callback_runner, net::CompletionCallback());
+}
+
+void VerySimpleSynchronousEntry::Close() {
+ for (int i = 0; i < kIndexCount; ++i) {
+ bool result = ClosePlatformFile(files_[i]);
+ DCHECK(result);
+ }
+ delete this;
+}
+
+void VerySimpleSynchronousEntry::ReadData(
+ int index,
+ int offset,
+ net::IOBuffer* buf,
+ int buf_len,
+ const SynchronousEntryCallback& callback) {
+ DCHECK(initialized_);
+ if (status_[index].mode != EntryStatus::ENTRY_READER)
+ status_[index].data_offset = 0;
+ DCHECK_EQ(status_[index].data_offset, offset);
+ status_[index].mode = EntryStatus::ENTRY_READER;
+
+ int64 file_offset = FileOffsetFromDataOffset(key_.size(), offset);
+ int bytes_read = ReadPlatformFile(files_[index], file_offset,
+ buf->data(), buf_len);
+ if (bytes_read > 0) {
+ last_used_ = Time::Now();
+ status_[index].data_offset += bytes_read;
+ }
+ int result = (bytes_read >= 0) ? bytes_read : net::ERR_FAILED;
+ callback_runner_->PostTask(FROM_HERE, Bind(callback, this, result));
+}
+
+void VerySimpleSynchronousEntry::WriteData(
+ int index,
+ int offset,
+ net::IOBuffer* buf,
+ int buf_len,
+ const SynchronousEntryCallback& callback,
+ bool truncate) {
+ DCHECK(initialized_);
+ if (status_[index].mode != EntryStatus::ENTRY_WRITER)
+ status_[index].data_offset = 0;
+ DCHECK_EQ(status_[index].data_offset, offset);
+ status_[index].mode = EntryStatus::ENTRY_WRITER;
+
+ int64 file_offset = FileOffsetFromDataOffset(key_.size(), offset);
+ if (buf_len > 0) {
+ if (WritePlatformFile(files_[index], file_offset, buf->data(), buf_len) !=
+ buf_len) {
+ callback_runner_->PostTask(FROM_HERE,
+ Bind(callback, this, net::ERR_FAILED));
+ return;
+ }
+ data_size_[index] = std::max(data_size_[index], offset + buf_len);
+ status_[index].data_offset += buf_len;
+ }
+ if (truncate) {
+ data_size_[index] = offset + buf_len;
+ if (!TruncatePlatformFile(files_[index], file_offset + buf_len)) {
+ callback_runner_->PostTask(FROM_HERE,
+ Bind(callback, this, net::ERR_FAILED));
+ return;
+ }
+ }
+ last_modified_ = Time::Now();
+ callback_runner_->PostTask(FROM_HERE, Bind(callback, this, buf_len));
+}
+
+VerySimpleSynchronousEntry::EntryStatus::EntryStatus()
+ : mode(ENTRY_UNINITIALIZED),
+ data_offset(0) {
+}
+
+VerySimpleSynchronousEntry::VerySimpleSynchronousEntry(
+ const scoped_refptr<TaskRunner>& callback_runner,
+ const FilePath& path,
+ const std::string& key) : callback_runner_(callback_runner),
+ path_(path),
+ key_(key),
+ initialized_(false) {
+}
+
+VerySimpleSynchronousEntry::~VerySimpleSynchronousEntry() {
+}
+
+bool VerySimpleSynchronousEntry::OpenOrCreateFiles(bool create) {
+ for (int i = 0; i < kIndexCount; ++i) {
+ FilePath filename = path_.AppendASCII(GetFilenameForKeyAndIndex(key_, i));
+ int flags = PLATFORM_FILE_READ | PLATFORM_FILE_WRITE;
+ if (create)
+ flags = flags | PLATFORM_FILE_CREATE_ALWAYS;
+ else
+ flags = flags | PLATFORM_FILE_OPEN;
+ PlatformFileError error;
+ files_[i] = CreatePlatformFile(filename, flags, NULL, &error);
+ if (error != PLATFORM_FILE_OK) {
+ while (--i >= 0) {
+ bool did_close = ClosePlatformFile(files_[i]);
+ DCHECK(did_close);
+ }
+ return false;
+ }
+ }
+
+ for (int i = 0; i < kIndexCount; ++i) {
+ PlatformFileInfo file_info;
+ if (!GetPlatformFileInfo(files_[i], &file_info))
+ NOTREACHED();
+ last_used_ = std::max(last_used_, file_info.last_accessed);
+ last_modified_ = std::max(last_modified_, file_info.last_modified);
+ data_size_[i] = DataSizeFromKeyAndFileSize(key_.size(), file_info.size);
+ }
+
+ return true;
+}
+
+bool VerySimpleSynchronousEntry::InitializeForOpen() {
+ DCHECK(!initialized_);
+ if (!OpenOrCreateFiles(false))
+ return false;
+
+ VLOG(8) << "VerySimpleSynchronousEntry::InitializeForOpen";
rvargas (doing something else) 2013/02/06 03:28:40 nit: general tracing is bad
gavinp 2013/02/08 23:17:51 Done.
+ for (int i = 0; i < kIndexCount; ++i) {
+ VerySimpleFileHeader header;
+ if (ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header),
+ sizeof(header)) != sizeof(header)) {
+ VLOG(9) << " no header";
rvargas (doing something else) 2013/02/06 03:28:40 ... and logging on errors is good.
gavinp 2013/02/08 23:17:51 Done.
+ return false;
+ }
+ if (header.initial_magic_number != kVerySimpleInitialMagicNumber ||
+ header.version != kVerySimpleVersion) {
+ VLOG(9) << " no magic number";
+ return false;
+ }
+
+ char key[1024];
+ DCHECK_LE(header.key_length, sizeof(key));
+ if (ReadPlatformFile(files_[i], sizeof(header), key, header.key_length) !=
+ implicit_cast<int>(header.key_length)) {
+ VLOG(9) << " no key";
+ return false;
+ }
+ if (std::memcmp(static_cast<const void*>(key_.data()),
+ static_cast<const void*>(key),
+ key_.size()) != 0) {
+ VLOG(9) << " bad key";
+ return false;
+ }
+
+ uint32 check_murmur3;
+ MurmurHash3_x86_32(static_cast<void*>(key), header.key_length, 0,
+ &check_murmur3);
+ if (check_murmur3 != header.key_murmur3) {
+ VLOG(9) << " bad key hash";
+ return false;
+ }
+ }
+
+ VLOG(9) << "success we openned it!";
+ initialized_ = true;
+ return true;
+}
+
+bool VerySimpleSynchronousEntry::InitializeForCreate() {
+ DCHECK(!initialized_);
+ if (!OpenOrCreateFiles(true))
+ return false;
+
+ for (int i = 0; i < kIndexCount; ++i) {
+ VerySimpleFileHeader header;
+ header.initial_magic_number = kVerySimpleInitialMagicNumber;
+ header.version = kVerySimpleVersion;
+
+ header.key_length = key_.size();
+ MurmurHash3_x86_32(static_cast<const void*>(key_.data()), key_.size(), 0,
+ static_cast<void*>(&header.key_murmur3));
+
+ if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header),
+ sizeof(header)) != sizeof(header)) {
+ return false;
+ }
+
+ if (WritePlatformFile(files_[i], sizeof(header), key_.data(),
+ key_.size()) != implicit_cast<int>(key_.size())) {
+ return false;
+ }
+ }
+
+ initialized_ = true;
+ return true;
+}
+
+} // namespace disk_cache

Powered by Google App Engine
This is Rietveld 408576698