From: Joao Eduardo Luis Date: Mon, 17 Sep 2012 17:08:05 +0000 (+0100) Subject: os: LevelDBStore: scrap init() and create open() and create_and_open() X-Git-Tag: v0.59~150^2~1^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=091fa826d9e3442f77112bc893f66f4d4ebb6723;p=ceph.git os: LevelDBStore: scrap init() and create open() and create_and_open() The init() function always implicitly created a new store if it was missing. This patches makes init() a private function accepting a bool that used to specify whether or not we want to create the store if it does not exists, and creates two functions: open() and create_and_open(). open() will fail if the store we are trying to open does not exist; create_and_open() maintains the same behavior as the previous behavior of init() and will create the store if it does not exist before opening it. Signed-off-by: Joao Eduardo Luis Reviewed-by: Samuel Just --- diff --git a/src/ceph_mon.cc b/src/ceph_mon.cc index 9ca75f27039d..51503e49ba6d 100644 --- a/src/ceph_mon.cc +++ b/src/ceph_mon.cc @@ -203,6 +203,8 @@ int main(int argc, const char **argv) // go MonitorDBStore store(g_conf->mon_data); + assert(!store.create_and_open(cerr)); + Monitor mon(g_ceph_context, g_conf->name.get_id(), &store, 0, &monmap); int r = mon.mkfs(osdmapbl); if (r < 0) { @@ -215,6 +217,7 @@ int main(int argc, const char **argv) } MonitorDBStore store(g_conf->mon_data); + assert(!store.open(std::cerr)); bufferlist magicbl; err = store.get(Monitor::MONITOR_NAME, "magic", magicbl); diff --git a/src/mon/MonitorDBStore.h b/src/mon/MonitorDBStore.h index d5cd3254ae00..7520499ab833 100644 --- a/src/mon/MonitorDBStore.h +++ b/src/mon/MonitorDBStore.h @@ -430,8 +430,15 @@ class MonitorDBStore db->submit_transaction_sync(dbt); } - MonitorDBStore(const string& path) : db(0) { + int open(ostream &out) { + return db->open(out); + } + + int create_and_open(ostream &out) { + return db->create_and_open(out); + } + MonitorDBStore(const string& path) : db(0) { string::const_reverse_iterator rit; int pos = 0; for (rit = path.rbegin(); rit != path.rend(); ++rit, ++pos) { @@ -440,18 +447,14 @@ class MonitorDBStore } ostringstream os; os << path.substr(0, path.size() - pos) << "/store.db"; - string full_path = os.str(); LevelDBStore *db_ptr = new LevelDBStore(full_path); if (!db_ptr) { - cerr << __func__ << " error initializing level db back storage in " - << full_path << std::endl; + std::cout << __func__ << " error initializing level db back storage in " + << full_path << std::endl; assert(0 != "MonitorDBStore: error initializing level db back storage"); } - cout << __func__ << " initializing back storage in " - << full_path << std::endl; - assert(!db_ptr->init(cerr)); db.reset(db_ptr); } MonitorDBStore(LevelDBStore *db_ptr) { diff --git a/src/os/FileStore.cc b/src/os/FileStore.cc index 1bab9c3c36d8..f180094abcad 100644 --- a/src/os/FileStore.cc +++ b/src/os/FileStore.cc @@ -1640,7 +1640,7 @@ int FileStore::mount() { LevelDBStore *omap_store = new LevelDBStore(omap_dir); stringstream err; - if (omap_store->init(err)) { + if (omap_store->create_and_open(err)) { delete omap_store; derr << "Error initializing leveldb: " << err.str() << dendl; ret = -1; diff --git a/src/os/LevelDBStore.cc b/src/os/LevelDBStore.cc index ec8fead966f2..3d94096f93e2 100644 --- a/src/os/LevelDBStore.cc +++ b/src/os/LevelDBStore.cc @@ -12,10 +12,10 @@ #include using std::string; -int LevelDBStore::init(ostream &out) +int LevelDBStore::init(ostream &out, bool create_if_missing) { leveldb::Options options; - options.create_if_missing = true; + options.create_if_missing = create_if_missing; leveldb::DB *_db; leveldb::Status status = leveldb::DB::Open(options, path, &_db); db.reset(_db); diff --git a/src/os/LevelDBStore.h b/src/os/LevelDBStore.h index 0fd9a11bced9..a867787a4d29 100644 --- a/src/os/LevelDBStore.h +++ b/src/os/LevelDBStore.h @@ -21,11 +21,20 @@ class LevelDBStore : public KeyValueDB { string path; boost::scoped_ptr db; + + int init(ostream &out, bool create_if_missing); + public: LevelDBStore(const string &path) : path(path) {} /// Opens underlying db - int init(ostream &out); + int open(ostream &out) { + return init(out, false); + } + /// Creates underlying db if missing and opens it + int create_and_open(ostream &out) { + return init(out, true); + } class LevelDBTransactionImpl : public KeyValueDB::TransactionImpl { public: diff --git a/src/test/ObjectMap/test_keyvaluedb_atomicity.cc b/src/test/ObjectMap/test_keyvaluedb_atomicity.cc index 8a25af6e5356..b19745723e9d 100644 --- a/src/test/ObjectMap/test_keyvaluedb_atomicity.cc +++ b/src/test/ObjectMap/test_keyvaluedb_atomicity.cc @@ -85,7 +85,7 @@ int main() { string strpath(path); std::cerr << "Using path: " << strpath << std::endl; LevelDBStore *store = new LevelDBStore(strpath); - assert(!store->init(std::cerr)); + assert(!store->create_and_open(std::cerr)); db.reset(store); verify(db.get()); diff --git a/src/test/ObjectMap/test_keyvaluedb_iterators.cc b/src/test/ObjectMap/test_keyvaluedb_iterators.cc index e5c9089916ca..f38275be98e6 100644 --- a/src/test/ObjectMap/test_keyvaluedb_iterators.cc +++ b/src/test/ObjectMap/test_keyvaluedb_iterators.cc @@ -38,7 +38,7 @@ public: assert(!store_path.empty()); LevelDBStore *db_ptr = new LevelDBStore(store_path); - assert(!db_ptr->init(std::cerr)); + assert(!db_ptr->create_and_open(std::cerr)); db.reset(db_ptr); mock.reset(new KeyValueDBMemory()); } diff --git a/src/test/ObjectMap/test_object_map.cc b/src/test/ObjectMap/test_object_map.cc index e536be3b8473..800443a84c3a 100644 --- a/src/test/ObjectMap/test_object_map.cc +++ b/src/test/ObjectMap/test_object_map.cc @@ -519,7 +519,7 @@ public: cerr << "using path " << strpath << std::endl;; LevelDBStore *store = new LevelDBStore(strpath); - assert(!store->init(cerr)); + assert(!store->create_and_open(cerr)); db.reset(new DBObjectMap(store)); tester.db = db.get(); diff --git a/src/test/filestore/test_idempotent.cc b/src/test/filestore/test_idempotent.cc index 6c1386181e1b..9feba3f9f6a9 100644 --- a/src/test/filestore/test_idempotent.cc +++ b/src/test/filestore/test_idempotent.cc @@ -64,7 +64,7 @@ int main(int argc, char **argv) { if (string(args[0]) == string("new")) start_new = true; LevelDBStore *_db = new LevelDBStore(db_path); - assert(!_db->init(std::cerr)); + assert(!_db->create_and_open(std::cerr)); boost::scoped_ptr db(_db); boost::scoped_ptr store(new FileStore(store_path, store_dev));