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

Unified Diff: base/prefs/leveldb_pref_store.cc

Issue 169323003: Implementation of leveldb-backed PrefStore (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 side-by-side diff with in-line comments
Download patch
Index: base/prefs/leveldb_pref_store.cc
diff --git a/base/prefs/leveldb_pref_store.cc b/base/prefs/leveldb_pref_store.cc
new file mode 100644
index 0000000000000000000000000000000000000000..da787e449d951128270258fa7004761bbb7bfc09
--- /dev/null
+++ b/base/prefs/leveldb_pref_store.cc
@@ -0,0 +1,167 @@
+// 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/prefs/leveldb_pref_store.h"
+
+#include <algorithm>
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 unused?
+#include <set>
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 unused?
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/file_util.h"
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 unused?
dgrogan 2014/02/25 03:32:08 Needed, but the rest were not.
+#include "base/json/json_file_value_serializer.h"
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 unused?
+#include "base/json/json_string_value_serializer.h"
+#include "base/location.h"
+#include "base/memory/ref_counted.h"
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 unused?
+#include "base/message_loop/message_loop_proxy.h"
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 Unused?
+#include "base/prefs/pref_filter.h"
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 unused?
+#include "base/sequenced_task_runner.h"
+#include "base/threading/sequenced_worker_pool.h"
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 unused?
+#include "base/values.h"
+#include "third_party/leveldatabase/src/include/leveldb/db.h"
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 Also in .h, but that might be able to use a forwar
+
+LevelDBPrefStore::LevelDBPrefStore(
+ const base::FilePath& filename,
+ base::SequencedTaskRunner* sequenced_task_runner)
+ : path_(filename),
+ sequenced_task_runner_(sequenced_task_runner),
+ read_only_(false),
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 What's the use case for this?
+ initialized_(false),
+ read_error_(PREF_READ_ERROR_OTHER) {}
+
+bool LevelDBPrefStore::GetValue(const std::string& key,
+ const base::Value** result) const {
+ const base::Value* tmp = NULL;
+ if (!prefs_.GetValue(key, &tmp)) {
+ return false;
+ }
+
+ if (result)
+ *result = tmp;
+ return true;
+}
+
+// Callers of GetMutableValue have to call ReportValueChanged in order for the
+// new value to be persisted.
+bool LevelDBPrefStore::GetMutableValue(const std::string& key,
+ base::Value** result) {
+ return prefs_.GetValue(key, result);
+}
+
+void LevelDBPrefStore::AddObserver(PrefStore::Observer* observer) {
+ observers_.AddObserver(observer);
+}
+
+void LevelDBPrefStore::RemoveObserver(PrefStore::Observer* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+bool LevelDBPrefStore::HasObservers() const {
+ return observers_.might_have_observers();
+}
+
+bool LevelDBPrefStore::IsInitializationComplete() const { return initialized_; }
+
+void LevelDBPrefStore::PersistOnFileThread(const std::string& key,
+ const std::string& value) {
+ DCHECK(sequenced_task_runner_->RunsTasksOnCurrentThread());
+
+ leveldb::Status status = db_->Put(leveldb::WriteOptions(), key, value);
+ DCHECK(status.ok());
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 Is this OK? What kind of errors can happen here? E
dgrogan 2014/02/25 03:32:08 The corresponding error in JSONPrefStore is ignore
+}
+
+void LevelDBPrefStore::PersistFromUIThread(const std::string& key,
+ base::Value* value) {
+ if (read_only_)
+ return;
+ std::string value_string;
+ JSONStringValueSerializer serializer(&value_string);
+ DCHECK(serializer.Serialize(*value));
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 Please no production code in DCHECK, it won't get
dgrogan 2014/02/25 03:32:08 Done.
+
+ sequenced_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &LevelDBPrefStore::PersistOnFileThread, this, key, value_string));
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 How do you make sure |this| is still alive when Pe
dgrogan 2014/02/25 03:32:08 If I'm reading https://code.google.com/p/chromium/
Mattias Nissler (ping if slow) 2014/02/25 10:51:44 Note that PrefStore inherits from base::RefCounted
dgrogan 2014/02/26 07:10:47 Wouldn't this just move the problem to the interna
Mattias Nissler (ping if slow) 2014/02/26 08:35:00 The difference is that you can destroy that object
+}
+
+void LevelDBPrefStore::SetValue(const std::string& key, base::Value* value) {
+ DCHECK(value);
+ scoped_ptr<base::Value> new_value(value);
+ base::Value* old_value = NULL;
+ bool found = prefs_.GetValue(key, &old_value);
+ if (!found || !value->Equals(old_value)) {
+ PersistFromUIThread(key, new_value.get());
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 JSONPrefStore batches writes and hits the file onl
dgrogan 2014/02/25 03:32:08 LevelDB does not do this internally. I thought one
Mattias Nissler (ping if slow) 2014/02/25 10:51:44 OK, are you planning to run performance tests on t
dgrogan 2014/03/05 03:06:57 Yes. Note that I don't yet know how, but it's on m
+ prefs_.SetValue(key, new_value.release());
+ }
+}
+
+void LevelDBPrefStore::SetValueSilently(const std::string& key,
+ base::Value* value) {
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 implement?
+}
+
+void LevelDBPrefStore::RemoveValue(const std::string& key) {
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 implement?
+}
+
+bool LevelDBPrefStore::ReadOnly() const { return read_only_; }
+
+PersistentPrefStore::PrefReadError LevelDBPrefStore::GetReadError() const {
+ return read_error_;
+}
+
+PersistentPrefStore::PrefReadError LevelDBPrefStore::ReadPrefs() {
+ DCHECK(!db_);
+ if (path_.empty()) {
+ OnStorageRead(PREF_READ_ERROR_FILE_NOT_SPECIFIED, false);
+ return PREF_READ_ERROR_FILE_NOT_SPECIFIED;
+ }
+
+ bool no_dir = !base::PathExists(path_.DirName());
+
+ leveldb::DB* db;
+ leveldb::Options options;
+ options.create_if_missing = true;
+ leveldb::Status status =
+ leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db);
+ if (!status.ok()) {
+ // TODO(dgrogan): Add error histogram and call RepairDB.
+ OnStorageRead(PREF_READ_ERROR_FILE_OTHER, no_dir);
+ return PREF_READ_ERROR_FILE_OTHER;
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 Is it possible to get some more information about
dgrogan 2014/03/05 03:06:57 Done.
+ }
+ db_.reset(db);
+
+ scoped_ptr<leveldb::Iterator> it(db->NewIterator(leveldb::ReadOptions()));
+ for (it->SeekToFirst(); it->Valid(); it->Next()) {
+ const std::string value_string = it->value().ToString();
+ JSONStringValueSerializer deserializer(value_string);
+ std::string error_message;
+ int error_code;
+ base::Value* json_value =
+ deserializer.Deserialize(&error_code, &error_message);
+ DCHECK(json_value) << error_message;
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 This should probably not be a DCHECK (unless level
dgrogan 2014/02/25 03:32:08 Done.
+ prefs_.SetValue(it->key().ToString(), json_value);
+ }
+
+ PersistentPrefStore::PrefReadError error = PREF_READ_ERROR_NONE;
+ if (!it->status().ok()) {
+ // TODO(dgrogan): Add error histogram?
+ error = PREF_READ_ERROR_FILE_OTHER;
+ }
+ OnStorageRead(error, no_dir);
+ return error;
+}
+
+void LevelDBPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 implement?
+}
+
+void LevelDBPrefStore::CommitPendingWrite() {
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 If this is not needed, please add a comment explai
dgrogan 2014/02/25 03:32:08 Done.
+}
+
+void LevelDBPrefStore::ReportValueChanged(const std::string& key) {
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 implement?
+}
+
+void LevelDBPrefStore::OnStorageRead(PersistentPrefStore::PrefReadError error,
+ bool no_dir) {
Mattias Nissler (ping if slow) 2014/02/24 09:00:40 implement?
+}
+
+LevelDBPrefStore::~LevelDBPrefStore() {}

Powered by Google App Engine
This is Rietveld 408576698