1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-04-01 06:50:25 +03:00

Bug #3630: Showback uses a transaction for sqlite backends

This commit is contained in:
Carlos Martín 2015-02-24 15:34:11 +01:00
parent 8d00b74177
commit 3da52fe163
6 changed files with 83 additions and 5 deletions

View File

@ -75,6 +75,14 @@ public:
*/
void free_str(char * str);
/**
* Returns true if the syntax INSERT VALUES (data), (data), (data)
* is supported
*
* @return true if supported
*/
bool multiple_values_support();
private:
/**
@ -148,6 +156,8 @@ public:
char * escape_str(const string& str){return 0;};
void free_str(char * str){};
bool multiple_values_support(){return true};
};
#endif

View File

@ -55,6 +55,14 @@ public:
* @param str pointer to the str
*/
virtual void free_str(char * str) = 0;
/**
* Returns true if the syntax INSERT VALUES (data), (data), (data)
* is supported
*
* @return true if supported
*/
virtual bool multiple_values_support() = 0;
};
#endif /*SQL_DB_H_*/

View File

@ -73,6 +73,14 @@ public:
*/
void free_str(char * str);
/**
* Returns true if the syntax INSERT VALUES (data), (data), (data)
* is supported
*
* @return true if supported
*/
bool multiple_values_support();
private:
/**
* Fine-grain mutex for DB access

View File

@ -132,6 +132,13 @@ MySqlDB::~MySqlDB()
/* -------------------------------------------------------------------------- */
bool MySqlDB::multiple_values_support()
{
return true;
}
/* -------------------------------------------------------------------------- */
int MySqlDB::exec(ostringstream& cmd, Callbackable* obj, bool quiet)
{
int rc;

View File

@ -66,6 +66,16 @@ SqliteDB::~SqliteDB()
/* -------------------------------------------------------------------------- */
bool SqliteDB::multiple_values_support()
{
// Versions > 3.7.11 support multiple value inserts, but tests
// have ended in segfault. A transaction seems to perform better
//return SQLITE_VERSION_NUMBER >= 3007011;
return false;
}
/* -------------------------------------------------------------------------- */
int SqliteDB::exec(ostringstream& cmd, Callbackable* obj, bool quiet)
{
int rc;

View File

@ -654,6 +654,9 @@ int VirtualMachinePool::calculate_showback(
ostringstream oss;
ostringstream body;
char * sql_body;
string sql_cmd_start;
string sql_cmd_separator;
string sql_cmd_end;
tm tmp_tm;
int vid;
@ -859,9 +862,38 @@ int VirtualMachinePool::calculate_showback(
// Write to DB
if (db->multiple_values_support())
{
oss.str("");
oss << "REPLACE INTO " << VirtualMachine::showback_table
<< " ("<< VirtualMachine::showback_db_names <<") VALUES ";
sql_cmd_start = oss.str();
sql_cmd_separator = ",";
sql_cmd_end = "";
}
else
{
oss.str("");
oss << "BEGIN TRANSACTION; "
<< "REPLACE INTO " << VirtualMachine::showback_table
<< " ("<< VirtualMachine::showback_db_names <<") VALUES ";
sql_cmd_start = oss.str();
oss.str("");
oss << "; REPLACE INTO " << VirtualMachine::showback_table
<< " ("<< VirtualMachine::showback_db_names <<") VALUES ";
sql_cmd_separator = oss.str();
sql_cmd_end = "; COMMIT";
}
oss.str("");
oss << "REPLACE INTO " << VirtualMachine::showback_table
<< " ("<< VirtualMachine::showback_db_names <<") VALUES ";
int n_entries = 0;
@ -921,12 +953,11 @@ int VirtualMachinePool::calculate_showback(
if (n_entries == 0)
{
oss.str("");
oss << "REPLACE INTO " << VirtualMachine::showback_table
<< " ("<< VirtualMachine::showback_db_names <<") VALUES ";
oss << sql_cmd_start;
}
else
{
oss << ",";
oss << sql_cmd_separator;
}
oss << " (" << vm_it->first << ","
@ -941,6 +972,8 @@ int VirtualMachinePool::calculate_showback(
// To avoid the oss to grow indefinitely, flush contents
if (n_entries == 1000)
{
oss << sql_cmd_end;
rc = db->exec(oss);
if (rc != 0)
@ -970,6 +1003,8 @@ int VirtualMachinePool::calculate_showback(
if (n_entries > 0)
{
oss << sql_cmd_end;
rc = db->exec(oss);
if (rc != 0)