diff --git a/convert.cc b/convert.cc index 7ee9677..d34f84d 100755 --- a/convert.cc +++ b/convert.cc @@ -25,7 +25,7 @@ int main(int argc, char* argv[]){ {"src", required_argument, NULL, 's'}, {"lmdb", no_argument, NULL, 'l'}, {"help", no_argument, NULL, 'h'}, - {0} + {0, 0, 0, 0} }; int option_index = 0; while (1) { @@ -54,18 +54,28 @@ int main(int argc, char* argv[]){ DB_ * x = new Libdb(); - x->connect_database(src); + if (!x->connect_database(src)) { + // error is printed in the connect_database function + delete x; + return 1; + } DB_ *b; if (lmdb) b = new LMDB_(); else b = new GDBM_(); - b->create_database(dst); + if (!b->create_database(dst)) { + std::cerr<<"Failed to create destination database\n"; + delete x; + delete b; + return 1; + } if (!b->fill_database(x)){ std::cerr<<"database filling failed\n"; + delete x; + delete b; return 1; } - b->close_db(); delete x; delete b; return 0; diff --git a/convert_db.h b/convert_db.h index 7bc8dfa..9e8cf25 100644 --- a/convert_db.h +++ b/convert_db.h @@ -37,8 +37,9 @@ class DB_ { virtual bool create_database(std::string name); bool create_db(); virtual bool fill_database(DB_ * old_database); - virtual void close_db(); + virtual void close_db() = 0; virtual DBC * get_database(); + virtual ~DB_(); }; /* * Libdb class needs only open and read data from libdb database @@ -53,6 +54,8 @@ class Libdb: public DB_ { Libdb(); bool connect_database(std::string path); DBC * get_database(); + void close_db(); + ~Libdb(); }; /* * GDBM class provides API for GDBM, allowes to open and create and fill gdbm database @@ -69,6 +72,7 @@ class GDBM_: public DB_ { bool create_database(std::string name); bool fill_database(DB_ * old_database); void close_db(); + ~GDBM_(); }; //https://github.com/LMDB/lmdb/blob/mdb.master/libraries/liblmdb/mtest2.c //further inspiration @@ -86,5 +90,6 @@ class LMDB_: public DB_ { bool create_database(std::string name); bool fill_database(DB_ * old_database); void close_db(); + ~LMDB_(); }; diff --git a/db.cc b/db.cc index 932d517..ee8962c 100644 --- a/db.cc +++ b/db.cc @@ -1,10 +1,5 @@ #include "convert_db.h" - - -Libdb::Libdb(){ - database_type = DB_type::LIBDB; -}; bool DB_::connect_database(std::string){ return false; } @@ -21,6 +16,14 @@ DBC * DB_::get_database() { return NULL; } void DB_::close_db(){} + +DB_::~DB_() { +} + +Libdb::Libdb(){ + database_type = DB_type::LIBDB; +}; + bool Libdb::connect_database(std::string path){ DB * db; int status; @@ -48,6 +51,15 @@ DBC * Libdb::get_database(){ return this->cursorp; } +void Libdb::close_db() { + cursorp->close(cursorp); + db->close(db, 0); +} + +Libdb::~Libdb() { + close_db(); +} + GDBM_::GDBM_(){ database_type = DB_type::GDBM; } @@ -81,6 +93,8 @@ bool GDBM_::fill_database(DB_ * old_database){ if (status != 0) return false; } + free(data_db); + free(key_db); return true; } @@ -88,6 +102,10 @@ void GDBM_::close_db(){ gdbm_close(this->f); } +GDBM_::~GDBM_() { + close_db(); +} + LMDB_::LMDB_(){ database_type = DB_type::LMDB; @@ -161,4 +179,6 @@ void LMDB_::close_db(){ mdb_env_close(this->lmdb_database); } - +LMDB_::~LMDB_() { + close_db(); +}