Skip to content

Commit

Permalink
Close databases in destructors
Browse files Browse the repository at this point in the history
  • Loading branch information
matejmatuska committed Sep 5, 2024
1 parent 6aec16a commit 480baa2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
5 changes: 0 additions & 5 deletions convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,15 @@ 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;
return 0;
Expand Down
5 changes: 4 additions & 1 deletion convert_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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_();
};
Expand All @@ -55,6 +55,7 @@ class Libdb: public DB_ {
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
Expand All @@ -71,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
Expand All @@ -88,5 +90,6 @@ class LMDB_: public DB_ {
bool create_database(std::string name);
bool fill_database(DB_ * old_database);
void close_db();
~LMDB_();
};

21 changes: 15 additions & 6 deletions db.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#include "convert_db.h"



Libdb::Libdb(){
database_type = DB_type::LIBDB;
};
bool DB_::connect_database(std::string){
return false;
}
Expand All @@ -25,6 +20,10 @@ void DB_::close_db(){}
DB_::~DB_() {
}

Libdb::Libdb(){
database_type = DB_type::LIBDB;
};

bool Libdb::connect_database(std::string path){
DB * db;
int status;
Expand Down Expand Up @@ -57,6 +56,10 @@ void Libdb::close_db() {
db->close(db, 0);
}

Libdb::~Libdb() {
close_db();
}

GDBM_::GDBM_(){
database_type = DB_type::GDBM;
}
Expand Down Expand Up @@ -97,6 +100,10 @@ void GDBM_::close_db(){
gdbm_close(this->f);
}

GDBM_::~GDBM_() {
close_db();
}

LMDB_::LMDB_(){
database_type = DB_type::LMDB;

Expand Down Expand Up @@ -170,4 +177,6 @@ void LMDB_::close_db(){
mdb_env_close(this->lmdb_database);
}


LMDB_::~LMDB_() {
close_db();
}

0 comments on commit 480baa2

Please sign in to comment.