Skip to content

Commit

Permalink
Properly close databases
Browse files Browse the repository at this point in the history
Also implemented LibDB::close_db() which closes both the cursor and db.
  • Loading branch information
matejmatuska committed Sep 5, 2024
1 parent 6694d4f commit 6aec16a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ int main(int argc, char* argv[]){
DB_ * x = new Libdb();
if (!x->connect_database(src)) {
// error is printed in the connect_database function
delete x;
return 1;
delete x;
return 1;
}
DB_ *b;
if (lmdb)
Expand All @@ -67,15 +67,19 @@ int main(int argc, char* argv[]){
if (!b->create_database(dst)) {
std::cerr<<"Failed to create destination database\n";
delete x;
x->close_db();
delete b;
return 1;
}
if (!b->fill_database(x)){
std::cerr<<"database filling failed\n";
x->close_db();
b->close_db();
delete x;
delete b;
return 1;
}
x->close_db();
b->close_db();
delete x;
delete b;
Expand Down
3 changes: 2 additions & 1 deletion convert_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DB_ {
virtual bool fill_database(DB_ * old_database);
virtual void close_db();
virtual DBC * get_database();
virtual ~DB_();
virtual ~DB_();
};
/*
* Libdb class needs only open and read data from libdb database
Expand All @@ -54,6 +54,7 @@ class Libdb: public DB_ {
Libdb();
bool connect_database(std::string path);
DBC * get_database();
void close_db();
};
/*
* GDBM class provides API for GDBM, allowes to open and create and fill gdbm database
Expand Down
5 changes: 5 additions & 0 deletions db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ DBC * Libdb::get_database(){
return this->cursorp;
}

void Libdb::close_db() {
cursorp->close(cursorp);
db->close(db, 0);
}

GDBM_::GDBM_(){
database_type = DB_type::GDBM;
}
Expand Down

0 comments on commit 6aec16a

Please sign in to comment.