summaryrefslogtreecommitdiffstats
path: root/components/leveldb_proto
diff options
context:
space:
mode:
authorcjhopman <cjhopman@chromium.org>2014-11-19 16:30:06 -0800
committerCommit bot <commit-bot@chromium.org>2014-11-20 00:30:34 +0000
commit1aad174b259bc655e7b9e973fadfc79bc5c00e51 (patch)
tree0f5d45b5f7e5f75adf1a4a89bdcde683566c31a9 /components/leveldb_proto
parent14bcfc52eb12a1b08cca13198ba2b25a7156f2ce (diff)
downloadchromium_src-1aad174b259bc655e7b9e973fadfc79bc5c00e51.zip
chromium_src-1aad174b259bc655e7b9e973fadfc79bc5c00e51.tar.gz
chromium_src-1aad174b259bc655e7b9e973fadfc79bc5c00e51.tar.bz2
Make ProtoDatabase properly handle failed Init
When Init fails, further Save/Load calls should just fail (and not crash). This makes the underlying LevelDB properly handle the Init failed case and adds tests for that. Adds LevelDB::InitWithOptions to make it easy to have the Init fail in tests. Review URL: https://codereview.chromium.org/735823004 Cr-Commit-Position: refs/heads/master@{#304944}
Diffstat (limited to 'components/leveldb_proto')
-rw-r--r--components/leveldb_proto/leveldb_database.cc24
-rw-r--r--components/leveldb_proto/leveldb_database.h3
-rw-r--r--components/leveldb_proto/proto_database_impl_unittest.cc18
3 files changed, 39 insertions, 6 deletions
diff --git a/components/leveldb_proto/leveldb_database.cc b/components/leveldb_proto/leveldb_database.cc
index 65d68a9..45f220e 100644
--- a/components/leveldb_proto/leveldb_database.cc
+++ b/components/leveldb_proto/leveldb_database.cc
@@ -23,14 +23,13 @@ namespace leveldb_proto {
LevelDB::LevelDB() {}
-LevelDB::~LevelDB() { DFAKE_SCOPED_LOCK(thread_checker_); }
-
-bool LevelDB::Init(const base::FilePath& database_dir) {
+LevelDB::~LevelDB() {
DFAKE_SCOPED_LOCK(thread_checker_);
+}
- leveldb::Options options;
- options.create_if_missing = true;
- options.max_open_files = 0; // Use minimum.
+bool LevelDB::InitWithOptions(const base::FilePath& database_dir,
+ const leveldb::Options& options) {
+ DFAKE_SCOPED_LOCK(thread_checker_);
std::string path = database_dir.AsUTF8Unsafe();
@@ -52,9 +51,19 @@ bool LevelDB::Init(const base::FilePath& database_dir) {
return false;
}
+bool LevelDB::Init(const base::FilePath& database_dir) {
+ leveldb::Options options;
+ options.create_if_missing = true;
+ options.max_open_files = 0; // Use minimum.
+ return InitWithOptions(database_dir, options);
+}
+
bool LevelDB::Save(const base::StringPairs& entries_to_save,
const std::vector<std::string>& keys_to_remove) {
DFAKE_SCOPED_LOCK(thread_checker_);
+ if (!db_) {
+ return false;
+ }
leveldb::WriteBatch updates;
for (base::StringPairs::const_iterator it = entries_to_save.begin();
@@ -79,6 +88,9 @@ bool LevelDB::Save(const base::StringPairs& entries_to_save,
bool LevelDB::Load(std::vector<std::string>* entries) {
DFAKE_SCOPED_LOCK(thread_checker_);
+ if (!db_) {
+ return false;
+ }
leveldb::ReadOptions options;
scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options));
diff --git a/components/leveldb_proto/leveldb_database.h b/components/leveldb_proto/leveldb_database.h
index 187a687..faa8199 100644
--- a/components/leveldb_proto/leveldb_database.h
+++ b/components/leveldb_proto/leveldb_database.h
@@ -15,6 +15,7 @@
namespace leveldb {
class DB;
+struct Options;
} // namespace leveldb
namespace leveldb_proto {
@@ -27,6 +28,8 @@ class LevelDB {
LevelDB();
virtual ~LevelDB();
+ virtual bool InitWithOptions(const base::FilePath& database_dir,
+ const leveldb::Options& options);
virtual bool Init(const base::FilePath& database_dir);
virtual bool Save(const base::StringPairs& pairs_to_save,
const std::vector<std::string>& keys_to_remove);
diff --git a/components/leveldb_proto/proto_database_impl_unittest.cc b/components/leveldb_proto/proto_database_impl_unittest.cc
index 095b9d4..e1d3144 100644
--- a/components/leveldb_proto/proto_database_impl_unittest.cc
+++ b/components/leveldb_proto/proto_database_impl_unittest.cc
@@ -15,6 +15,7 @@
#include "components/leveldb_proto/testing/proto/test.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/leveldatabase/src/include/leveldb/options.h"
using base::MessageLoop;
using base::ScopedTempDir;
@@ -394,4 +395,21 @@ TEST(ProtoDatabaseImplLevelDBTest, TestDBCloseAndReopen) {
TestLevelDBSaveAndLoad(true);
}
+TEST(ProtoDatabaseImplLevelDBTest, TestDBInitFail) {
+ ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+
+ leveldb::Options options;
+ options.create_if_missing = false;
+ scoped_ptr<LevelDB> db(new LevelDB());
+
+ KeyValueVector save_entries;
+ std::vector<std::string> load_entries;
+ KeyVector remove_keys;
+
+ EXPECT_FALSE(db->InitWithOptions(temp_dir.path(), options));
+ EXPECT_FALSE(db->Load(&load_entries));
+ EXPECT_FALSE(db->Save(save_entries, remove_keys));
+}
+
} // namespace leveldb_proto