Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check return values when connecting to/creating a database #2

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
matejmatuska marked this conversation as resolved.
Show resolved Hide resolved
return 1;
}
b->close_db();
delete x;
delete b;
return 0;
Expand Down
7 changes: 6 additions & 1 deletion convert_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -86,5 +90,6 @@ class LMDB_: public DB_ {
bool create_database(std::string name);
bool fill_database(DB_ * old_database);
void close_db();
~LMDB_();
};

32 changes: 26 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 @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -81,13 +93,19 @@ bool GDBM_::fill_database(DB_ * old_database){
if (status != 0)
return false;
}
free(data_db);
free(key_db);
return true;
}

void GDBM_::close_db(){
gdbm_close(this->f);
}

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

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

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


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