]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
os: LevelDBStore: scrap init() and create open() and create_and_open()
authorJoao Eduardo Luis <joao.luis@inktank.com>
Mon, 17 Sep 2012 17:08:05 +0000 (18:08 +0100)
committerJoao Eduardo Luis <joao.luis@inktank.com>
Thu, 21 Feb 2013 18:02:22 +0000 (18:02 +0000)
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 <joao.luis@inktank.com>
Reviewed-by: Samuel Just <sam.just@inktank.com>
src/ceph_mon.cc
src/mon/MonitorDBStore.h
src/os/FileStore.cc
src/os/LevelDBStore.cc
src/os/LevelDBStore.h
src/test/ObjectMap/test_keyvaluedb_atomicity.cc
src/test/ObjectMap/test_keyvaluedb_iterators.cc
src/test/ObjectMap/test_object_map.cc
src/test/filestore/test_idempotent.cc

index 9ca75f27039d3615c8aa5c1bec20857f47cc45e4..51503e49ba6d89f1dc0dfbdf90d6d0c70f173db5 100644 (file)
@@ -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);
index d5cd3254ae0037869abe3959817c1e4e108f637d..7520499ab833b14cdd9f901d56a3944e03c33b78 100644 (file)
@@ -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) {
index 1bab9c3c36d897a99bf50d96e83227344d4e0e79..f180094abcad5b7545d474eb40d07c41454d1057 100644 (file)
@@ -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;
index ec8fead966f2336e6bb621e0cfd0a627984c0464..3d94096f93e272a219081991a5b32fd4fc8d7bff 100644 (file)
 #include <errno.h>
 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);
index 0fd9a11bced9488fd277ab8a30cf1cd6f1346b87..a867787a4d29ac069929bad718357b9424ca9f44 100644 (file)
 class LevelDBStore : public KeyValueDB {
   string path;
   boost::scoped_ptr<leveldb::DB> 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:
index 8a25af6e53564867997c063a273447ffe533ba80..b19745723e9d2270a82366d862b82e0e9a37a425 100644 (file)
@@ -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());
index e5c9089916ca78b271f42ecde47d1aae43706b17..f38275be98e69ddac9d5904ea5a9ba75d3f87a65 100644 (file)
@@ -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());
   }
index e536be3b847369e1761fd31f6bf86882d6237d2c..800443a84c3a9aaf6c02947212780df31097f5e4 100644 (file)
@@ -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();
index 6c1386181e1bb3ea370a5b1052827c6d814f2218..9feba3f9f6a9f76031aa190e11af718318d8a137 100644 (file)
@@ -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<KeyValueDB> db(_db);
   boost::scoped_ptr<ObjectStore> store(new FileStore(store_path, store_dev));