diff options
author | peter <peter@chromium.org> | 2015-07-23 07:36:09 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-23 14:36:48 +0000 |
commit | 3617707ed710a4e88bcb4a11965d6c0296bf0309 (patch) | |
tree | c508890b69aa501210870b6c4bda560834c68566 /components/leveldb_proto | |
parent | 69a642f6b7eeeab3ef980b0d29965c25d4d3c46d (diff) | |
download | chromium_src-3617707ed710a4e88bcb4a11965d6c0296bf0309.zip chromium_src-3617707ed710a4e88bcb4a11965d6c0296bf0309.tar.gz chromium_src-3617707ed710a4e88bcb4a11965d6c0296bf0309.tar.bz2 |
Support in-memory databases in leveldb_proto
There is no requirement for LevelDB databases to live on disk - by
creating a memory environment they can live in memory (but are, of
course, ephemeral and entirely scoped to the instance).
This is useful for databases which might have to be created for
browser tests where mocking the database is undesirable.
R=cjhopman
BUG=
Review URL: https://codereview.chromium.org/1245703003
Cr-Commit-Position: refs/heads/master@{#340090}
Diffstat (limited to 'components/leveldb_proto')
-rw-r--r-- | components/leveldb_proto/leveldb_database.cc | 7 | ||||
-rw-r--r-- | components/leveldb_proto/leveldb_database.h | 5 | ||||
-rw-r--r-- | components/leveldb_proto/proto_database_impl_unittest.cc | 21 |
3 files changed, 33 insertions, 0 deletions
diff --git a/components/leveldb_proto/leveldb_database.cc b/components/leveldb_proto/leveldb_database.cc index b3e1a05..66b6987 100644 --- a/components/leveldb_proto/leveldb_database.cc +++ b/components/leveldb_proto/leveldb_database.cc @@ -13,7 +13,9 @@ #include "base/strings/string_split.h" #include "base/threading/thread_checker.h" #include "third_party/leveldatabase/env_chromium.h" +#include "third_party/leveldatabase/src/helpers/memenv/memenv.h" #include "third_party/leveldatabase/src/include/leveldb/db.h" +#include "third_party/leveldatabase/src/include/leveldb/env.h" #include "third_party/leveldatabase/src/include/leveldb/iterator.h" #include "third_party/leveldatabase/src/include/leveldb/options.h" #include "third_party/leveldatabase/src/include/leveldb/slice.h" @@ -57,6 +59,11 @@ bool LevelDB::Init(const base::FilePath& database_dir) { options.create_if_missing = true; options.max_open_files = 0; // Use minimum. options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; + if (database_dir.empty()) { + env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); + options.env = env_.get(); + } + return InitWithOptions(database_dir, options); } diff --git a/components/leveldb_proto/leveldb_database.h b/components/leveldb_proto/leveldb_database.h index faa8199..67d0707 100644 --- a/components/leveldb_proto/leveldb_database.h +++ b/components/leveldb_proto/leveldb_database.h @@ -15,6 +15,7 @@ namespace leveldb { class DB; +class Env; struct Options; } // namespace leveldb @@ -37,6 +38,10 @@ class LevelDB { private: DFAKE_MUTEX(thread_checker_); + + // The declaration order of these members matters: |db_| depends on |env_| and + // therefore has to be destructed first. + scoped_ptr<leveldb::Env> env_; scoped_ptr<leveldb::DB> db_; }; diff --git a/components/leveldb_proto/proto_database_impl_unittest.cc b/components/leveldb_proto/proto_database_impl_unittest.cc index b2fcd18..7ac19a5 100644 --- a/components/leveldb_proto/proto_database_impl_unittest.cc +++ b/components/leveldb_proto/proto_database_impl_unittest.cc @@ -412,4 +412,25 @@ TEST(ProtoDatabaseImplLevelDBTest, TestDBInitFail) { EXPECT_FALSE(db->Save(save_entries, remove_keys)); } +TEST(ProtoDatabaseImplLevelDBTest, TestMemoryDatabase) { + scoped_ptr<LevelDB> db(new LevelDB()); + + std::vector<std::string> load_entries; + + ASSERT_TRUE(db->Init(base::FilePath())); + + ASSERT_TRUE(db->Load(&load_entries)); + EXPECT_EQ(0u, load_entries.size()); + + KeyValueVector save_entries(1, std::make_pair("foo", "bar")); + KeyVector remove_keys; + + ASSERT_TRUE(db->Save(save_entries, remove_keys)); + + std::vector<std::string> second_load_entries; + + ASSERT_TRUE(db->Load(&second_load_entries)); + EXPECT_EQ(1u, second_load_entries.size()); +} + } // namespace leveldb_proto |