1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-26 10:03:37 +03:00

feature #206: Isolation of INSERT and REPLACE SQL queries for Host objects.

This commit is contained in:
Constantino Vázquez Blanco 2010-04-28 15:00:07 +02:00
parent 5cf9d453e1
commit 0e1a364736
6 changed files with 148 additions and 46 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -146,18 +146,32 @@ int Host::insert(SqlDB *db)
map<int,HostShare *>::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 << ","

View File

@ -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;

View File

@ -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<string,Attribute *>::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)
{