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

Unified Diff: components/data_reduction_proxy/core/browser/data_reduction_proxy_store_impl.cc

Issue 1173343009: LevelDB storage for data reduction proxy to store data usage stats. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 5 years, 6 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: components/data_reduction_proxy/core/browser/data_reduction_proxy_store_impl.cc
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_store_impl.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_store_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..78bfd039515a4c243d420b7dd45d90c3e42ff81d
--- /dev/null
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_store_impl.cc
@@ -0,0 +1,76 @@
+// Copyright 2015 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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_store_impl.h"
+
+#include "base/logging.h"
+#include "base/memory/ref_counted.h"
+#include "components/data_reduction_proxy/proto/store.pb.h"
+#include "third_party/leveldatabase/src/include/leveldb/db.h"
+#include "third_party/leveldatabase/src/include/leveldb/options.h"
+#include "third_party/leveldatabase/src/include/leveldb/status.h"
+#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
+
+namespace data_reduction_proxy {
+
+DataReductionProxyStoreImpl::DataReductionProxyStoreImpl(
+ const std::string& profile_path)
+ : profile_path_(profile_path) {
+ thread_checker_.DetachFromThread();
+}
+
+void DataReductionProxyStoreImpl::InitializeOnDBThread() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ leveldb::Options options;
+ options.create_if_missing = true;
+ std::string db_name = profile_path_ + "data_reduction_proxy_leveldb";
+ leveldb::DB* dbptr = nullptr;
+ leveldb::Status status = leveldb::DB::Open(options, db_name, &dbptr);
+ db_.reset(dbptr);
+ if (!status.ok()) {
+ LOG(FATAL) << "Failed to open Data Reduction Proxy DB: "
+ << status.ToString();
+ }
+}
+
+DataReductionProxyStore::Status LevelDbToDRPStoreStatus(
+ leveldb::Status leveldb_status) {
+ if (leveldb_status.ok())
+ return DataReductionProxyStore::Status::OK;
+ else if (leveldb_status.IsNotFound())
+ return DataReductionProxyStore::Status::NOT_FOUND;
+ else
+ return DataReductionProxyStore::Status::ERROR;
+}
+
+DataReductionProxyStore::Status DataReductionProxyStoreImpl::Get(
+ const std::string& key,
+ std::string& value) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ leveldb::ReadOptions read_options;
+ leveldb::Slice slice(key);
+ return LevelDbToDRPStoreStatus(db_->Get(read_options, slice, &value));
+}
+
+DataReductionProxyStore::Status DataReductionProxyStoreImpl::Put(
+ const std::map<std::string, std::string>* map) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ scoped_ptr<leveldb::WriteBatch> batch(new leveldb::WriteBatch());
+ for (const auto& iter : *map) {
+ leveldb::Slice key_slice(iter.first);
+ leveldb::Slice value_slice(iter.second);
+ batch->Put(key_slice, value_slice);
+ }
+
+ leveldb::WriteOptions write_options;
+ return LevelDbToDRPStoreStatus(db_->Write(write_options, batch.get()));
+}
+
+DataReductionProxyStoreImpl::~DataReductionProxyStoreImpl() {
+}
+
+} // namespace data_reduction_proxy

Powered by Google App Engine
This is Rietveld 408576698