From 0e1a364736a449b0260e7fc96768c2e2c7b2b913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Constantino=20V=C3=A1zquez=20Blanco?= Date: Wed, 28 Apr 2010 15:00:07 +0200 Subject: [PATCH] feature #206: Isolation of INSERT and REPLACE SQL queries for Host objects. --- include/Host.h | 8 ++++ include/HostShare.h | 8 ++++ include/TemplateSQL.h | 8 ++++ src/host/Host.cc | 92 ++++++++++++++++++++++++++----------- src/host/HostShare.cc | 44 ++++++++++++------ src/template/TemplateSQL.cc | 34 +++++++++++--- 6 files changed, 148 insertions(+), 46 deletions(-) diff --git a/include/Host.h b/include/Host.h index 1a14fa0614..cc9630bb71 100644 --- a/include/Host.h +++ b/include/Host.h @@ -447,6 +447,14 @@ private: // DataBase implementation (Private) // ************************************************************************* + /** + * Execute an INSERT or REPLACE Sql query. + * @param db The SQL DB + * @param replace Execute an INSERT or a REPLACE + * @return 0 one success + */ + int insert_replace(SqlDB *db, bool replace); + /** * Callback function to unmarshall a Host object (Host::select) * @param num the number of columns read from the DB diff --git a/include/HostShare.h b/include/HostShare.h index 2eba534f90..419d487a56 100644 --- a/include/HostShare.h +++ b/include/HostShare.h @@ -197,6 +197,14 @@ private: * @return 0 on success */ int drop(SqlDB * db); + + /** + * Execute an INSERT or REPLACE Sql query. + * @param db The SQL DB + * @param replace Execute an INSERT or a REPLACE + * @return 0 one success + */ + int insert_replace(SqlDB *db, bool replace); /** * Callback function to unmarshall a HostShare object (HostShare::select) diff --git a/include/TemplateSQL.h b/include/TemplateSQL.h index 2c07652cb7..912d20b735 100644 --- a/include/TemplateSQL.h +++ b/include/TemplateSQL.h @@ -85,6 +85,14 @@ protected: * @param db pointer to the database. */ int drop(SqlDB *db); + + /** + * Execute an INSERT or REPLACE Sql query. + * @param db The SQL DB + * @param replace Execute an INSERT or a REPLACE + * @return 0 one success + */ + int insert_replace(SqlDB *db, bool replace); /** * Removes a template attribute from the DB. If there are multiple diff --git a/src/host/Host.cc b/src/host/Host.cc index a7bd5c62c9..cb83fe7f57 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -146,18 +146,32 @@ int Host::insert(SqlDB *db) map::iterator iter; // Set up the template ID, to insert it - if ( host_template.id == -1 ) { host_template.id = oid; } // Set up the share ID, to insert it - if ( host_share.hsid == -1 ) { host_share.hsid = oid; } + + // Update the Template + rc = host_template.insert(db); + + if ( rc != 0 ) + { + return rc; + } + + // Update the HostShare + rc = host_share.insert(db); + + if ( rc != 0 ) + { + return rc; + } //Insert the Host and its template rc = insert_replace(db, false); @@ -170,10 +184,45 @@ int Host::insert(SqlDB *db) return 0; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ int Host::update(SqlDB *db) +{ + int rc; + + // Update the Template + rc = host_template.update(db); + + if ( rc != 0 ) + { + return rc; + } + + // Update the HostShare + rc = host_share.update(db); + + if ( rc != 0 ) + { + return rc; + } + + rc = insert_replace(db, true); + + if ( rc != 0 ) + { + return rc; + } + + return 0; + + +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int Host::insert_replace(SqlDB *db, bool replace) { ostringstream oss; @@ -183,26 +232,8 @@ int Host::update(SqlDB *db) char * sql_im_mad_name; char * sql_tm_mad_name; char * sql_vmm_mad_name; - - // Update the Template - - rc = host_template.update(db); - - if ( rc != 0 ) - { - return rc; - } - - // Update the HostShare - - rc = host_share.update(db); - - if ( rc != 0 ) - { - return rc; - } - - // Update the Host + + // Update the Host sql_hostname = db->escape_str(hostname.c_str()); @@ -231,10 +262,19 @@ int Host::update(SqlDB *db) { goto error_vmm; } + + if(replace) + { + oss << "REPLACE"; + } + else + { + oss << "INSERT"; + } - // Construct the SQL statement to Insert or Replace (effectively, update) + // Construct the SQL statement to Insert or Replace - oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES (" + oss <<" INTO "<< table <<" "<< db_names <<" VALUES (" << oid << "," << "'" << sql_hostname << "'," << state << "," diff --git a/src/host/HostShare.cc b/src/host/HostShare.cc index b22910dfee..12c715280f 100644 --- a/src/host/HostShare.cc +++ b/src/host/HostShare.cc @@ -192,39 +192,55 @@ int HostShare::select(SqlDB * db) return rc; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ int HostShare::insert(SqlDB * db) { int rc; - rc = update(db); + rc = insert_replace(db, false); - if ( rc != 0 ) - { - return rc; - } - - return 0; + return rc; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ int HostShare::update(SqlDB * db) { - ostringstream oss; int rc; - oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES (" + rc = insert_replace(db, true); + + return rc; +} + +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ + +int HostShare::insert_replace(SqlDB *db, bool replace) +{ + ostringstream oss; + int rc; + + if(replace) + { + oss << "REPLACE"; + } + else + { + oss << "INSERT"; + } + + oss << " INTO " << table << " "<< db_names <<" VALUES (" << hsid << "," << disk_usage <<","<< mem_usage <<","<< cpu_usage<< "," << max_disk <<","<< max_mem <<","<< max_cpu << "," << free_disk <<","<< free_mem <<","<< free_cpu << "," << used_disk <<","<< used_mem <<","<< used_cpu << "," << running_vms<< ")"; - + rc = db->exec(oss); return rc; diff --git a/src/template/TemplateSQL.cc b/src/template/TemplateSQL.cc index b4e6fadb17..701cc7e0c1 100644 --- a/src/template/TemplateSQL.cc +++ b/src/template/TemplateSQL.cc @@ -66,14 +66,25 @@ int TemplateSQL::insert(SqlDB * db) return -1; } - rc = update(db); + rc = insert_replace(db, false); return rc; } -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------ */ int TemplateSQL::update(SqlDB * db) +{ + int rc; + + rc = insert_replace(db, true); + + return rc; +} + +/* ------------------------------------------------------------------------ */ + +int TemplateSQL::insert_replace(SqlDB *db, bool replace) { multimap::iterator it; ostringstream oss; @@ -82,7 +93,9 @@ int TemplateSQL::update(SqlDB * db) char * sql_attr; Attribute::AttributeType atype; - for(it=attributes.begin(),oss.str("");it!=attributes.end();it++,oss.str("")) + for(it=attributes.begin(),oss.str(""); + it!=attributes.end(); + it++,oss.str("")) { if ( it->second == 0 ) { @@ -105,8 +118,17 @@ int TemplateSQL::update(SqlDB * db) { continue; } + + if(replace) + { + oss << "REPLACE"; + } + else + { + oss << "INSERT"; + } - oss << "INSERT OR REPLACE INTO " << table << " " << db_names + oss << " INTO " << table << " " << db_names << " VALUES (" << id << ",'" << it->first << "',"<< atype <<",'" << sql_attr << "')"; @@ -129,8 +151,8 @@ error_sql: } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------ */ +/* ------------------------------------------------------------------------ */ int TemplateSQL::select_cb(void *nil, int num, char **values, char **names) {