Commit 57c48fd5 authored by Carla Guillen's avatar Carla Guillen
Browse files

Making PerSystDB singleton, and fixing a bug

parent 471fd55a
......@@ -71,12 +71,7 @@ void PerSystDB::print_error(){
LOG(error) << "Error(" << mysql_errno(_mysql) << ") [" << mysql_sqlstate(_mysql) << "] \""<< mysql_error(_mysql) << "\"" ;
}
PerSystDB::PerSystDB(): _mysql(NULL), _rotation(EVERY_MONTH), _every_x_days(0), _internal_connection(false), _end_aggregate_timestamp(0) {
}
PerSystDB::PerSystDB(MYSQL *mysql, Rotation_t rotation, unsigned int every_x_days) :
_mysql(mysql), _rotation(rotation), _every_x_days(every_x_days), _internal_connection(
false), _end_aggregate_timestamp(0) {
PerSystDB::PerSystDB(): _mysql(NULL), _rotation(EVERY_MONTH), _every_x_days(0), _end_aggregate_timestamp(0) {
}
PerSystDB * PerSystDB::getInstance(){
......@@ -107,25 +102,9 @@ bool PerSystDB::finalizeConnection(){
return true;
}
PerSystDB::PerSystDB(const std::string & host, const std::string & user,
const std::string & password, const std::string & database_name,
int port, Rotation_t rotation, unsigned int every_x_days) :
_rotation(rotation), _every_x_days(every_x_days), _internal_connection(
true), _end_aggregate_timestamp(0) {
_mysql = mysql_init(NULL);
if(!mysql_real_connect(_mysql, host.c_str(), user.c_str(), password.c_str(), database_name.c_str(), port, NULL, 0)){
print_error();
} else {
LOG(debug) << "Internal Connection: Successfully connected to mariadb";
}
}
PerSystDB::~PerSystDB(){
if(_internal_connection){
mysql_close(_mysql);
LOG(debug) << "Internal Connection: Disconnected from mariadb";
}
}
bool PerSystDB::getDBJobIDs(std::vector<std::string> & job_id_strings, std::map<std::string, std::string>& job_id_map) {
......@@ -163,7 +142,7 @@ bool PerSystDB::getCurrentSuffixAggregateTable(std::string & suffix){
if(_end_aggregate_timestamp){
auto now_uts = getTimestamp();
if(now_uts < _end_aggregate_timestamp) { //suffix found, don't do anything
suffix = _current_table_suffix;
suffix = _current_table_suffix;
return true;
}
}
......@@ -187,6 +166,7 @@ bool PerSystDB::getCurrentSuffixAggregateTable(std::string & suffix){
while((row = result.fetch_row())){
if(row[0] && row[1]){
suffix = std::string(row[0]);
_current_table_suffix = suffix;
std::string row1(row[1]);
_end_aggregate_timestamp = std::stoull(row1) * 1e9;
return true;
......@@ -266,7 +246,7 @@ bool PerSystDB::createNewAggregate(std::string& new_suffix){
auto today_iso = to_iso_extended_string(today);
end_timestamp = today_iso + " 00:00:00";
}
int new_suff = std::stoi(last_suffix) + 1;
int new_suff = std::stoi(last_suffix) + 1;
std::string new_begin_timestamp, new_end_timestamp;
getNewDates(end_timestamp, new_begin_timestamp, new_end_timestamp);
......@@ -293,6 +273,7 @@ bool PerSystDB::createNewAggregate(std::string& new_suffix){
return false;
}
new_suffix = std::to_string(new_suff);
_current_table_suffix = new_suffix;
return true;
}
......@@ -322,8 +303,7 @@ bool PerSystDB::updateJobsLastSuffix(std::map<std::string, std::string>& job_map
bool PerSystDB::getTableSuffix(std::string & table_suffix){
if(!getCurrentSuffixAggregateTable(table_suffix) &&
!createNewAggregate(table_suffix)){
if(!getCurrentSuffixAggregateTable(table_suffix) && !createNewAggregate(table_suffix)){
return false;
}
return true;
......
......@@ -59,7 +59,6 @@ protected:
MYSQL *_mysql;
Rotation_t _rotation;
unsigned int _every_x_days; //ignored except when EVERY_XDAYS is chosen
bool _internal_connection;
unsigned long long _end_aggregate_timestamp;
std::string _current_table_suffix;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
......@@ -67,18 +66,16 @@ protected:
void print_error();
static PerSystDB * instance;
static std::mutex mut;
virtual ~PerSystDB();
bool getCurrentSuffixAggregateTable(std::string & new_suffix);
bool createNewAggregate(std::string& new_suffix);
public:
PerSystDB(); //Used for later initialization of mysql
PerSystDB();
bool initializeConnection(const std::string & host,
const std::string & user, const std::string & password,
const std::string & database_name, Rotation_t rotation, int port =3306, unsigned int every_x_days = 0);
bool finalizeConnection();
PerSystDB(MYSQL *mysql, Rotation_t rotation, unsigned int every_x_days=0);
PerSystDB(const std::string & host, const std::string & user,
const std::string & password, const std::string & database_name,
int port, Rotation_t rotation, unsigned int every_x_days = 0);
virtual ~PerSystDB();
/**
* Check if job_id (db) exist. If map empty it doesn't exist/job not found is not yet on accounting.
......@@ -86,9 +83,7 @@ public:
*/
bool getDBJobIDs(std::vector<std::string> & job_id_strings, std::map<std::string, std::string>& job_id_map);
bool getCurrentSuffixAggregateTable(std::string & new_suffix);
bool insertIntoJob(const std::string& job_id_string, unsigned long long uid, int & job_id_db, const std::string & suffix);
bool createNewAggregate(std::string& new_suffix);
void getNewDates(const std::string& last_end_timestamp, std::string & begin_timestamp, std::string & end_timestamp);
bool insertInAggregateTable(const std::string& suffix, Aggregate_info_t & agg_info);
bool updateJobsLastSuffix(std::map<std::string, std::string>& job_map, std::string & suffix);
......
......@@ -177,15 +177,15 @@ void PerSystSqlOperator::compute(U_Ptr unit, qeJobData& jobData) {
// handle jobs which are not present
for(auto &job_id_string : job_ids ){
auto search = job_map.find(job_id_string);
if(search == job_map.end()){ //Not found
int job_id_db;
if(_persystdb->insertIntoJob(job_id_string, jobData.userId, job_id_db, table_suffix)){
agg_info.job_id_db = std::to_string(job_id_db);
} else {
continue;
}
}
auto search = job_map.find(job_id_string);
if(search == job_map.end()){ //Not found
int job_id_db;
if(_persystdb->insertIntoJob(job_id_string, jobData.userId, job_id_db, table_suffix)){
agg_info.job_id_db = std::to_string(job_id_db);
} else {
continue;
}
}
}
agg_info.timestamp = (my_timestamp/1e9);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment