1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

feature #407: LastOID is private for the pools. Minor changes in Group classes

This commit is contained in:
Ruben S. Montero 2011-05-21 02:06:29 +02:00
parent b0ddfd382c
commit e77a5f5b13
6 changed files with 105 additions and 93 deletions

View File

@ -59,9 +59,10 @@ private:
// Constructor
// *************************************************************************
Group(int id, int uid, const string& name);
Group(int id, int uid, const string& name):
PoolObjectSQL(id,name,uid,-1,table){};
virtual ~Group();
virtual ~Group(){};
// *************************************************************************
// DataBase implementation (Private)
@ -96,14 +97,29 @@ private:
* @param db pointer to the db
* @return 0 on success
*/
int insert(SqlDB *db, string& error_str);
int insert(SqlDB *db, string& error_str)
{
int rc;
rc = insert_replace(db, false);
if ( rc != 0 )
{
error_str = "Error inserting Group in DB.";
}
return rc;
}
/**
* Writes/updates the Group's data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
int update(SqlDB *db);
int update(SqlDB *db)
{
return insert_replace(db, true);
}
};
#endif /*GROUP_H_*/

View File

@ -30,14 +30,30 @@ public:
~GroupPool(){};
/* ---------------------------------------------------------------------- */
/* Constants r DB management */
/* ---------------------------------------------------------------------- */
/**
* Names for the default groups
* Default name for the oneadmin group
*/
static const string ONEADMIN_NAME;
/**
* Identifier for the oneadmin group
*/
static const int ONEADMIN_ID;
/**
* Default name for the users group
*/
static const string USERS_NAME;
static const int ONEADMIN_ID;
/**
* Identifier for the user group
*/
static const int USERS_ID;
/* ---------------------------------------------------------------------- */
/* Methods for DB management */
/* ---------------------------------------------------------------------- */

View File

@ -164,20 +164,30 @@ protected:
int dump(ostringstream& oss, const string& elem_name,
const char * table, const string& where);
/**
* Last object ID assigned to an object. It must be initialized by the
* target pool.
*/
int lastOID;
/* ---------------------------------------------------------------------- */
/* Interface to access the lastOID assigned by the pool */
/* ---------------------------------------------------------------------- */
/**
* Returns the value of the last identifier assigned by the pool
* Gets the value of the last identifier assigned by the pool
* @return the lastOID of the pool
*/
int get_lastOID()
{
return lastOID;
};
/**
* Sets the lastOID of the pool and updates the control database
* @param _lastOID for the pool
*/
void set_update_lastOID(int _lastOID)
{
lastOID = _lastOID;
update_lastOID();
};
/**
* Inserts the last oid into the pool_control table
*/
@ -185,25 +195,31 @@ protected:
private:
pthread_mutex_t mutex;
pthread_mutex_t mutex;
/**
* Max size for the pool, to control the memory footprint of the pool. This
* number MUST be greater than the max. number of objects that are
* accessed simultaneously.
*/
static const unsigned int MAX_POOL_SIZE;
static const unsigned int MAX_POOL_SIZE;
/**
* Last object ID assigned to an object. It must be initialized by the
* target pool.
*/
int lastOID;
/**
* Tablename for this pool
*/
string table;
string table;
/**
* The pool is implemented with a Map of SQL object pointers, using the
* OID as key.
*/
map<int,PoolObjectSQL *> pool;
map<int,PoolObjectSQL *> pool;
/**
* This is a name index for the pool map. The key is the name of the object
@ -221,7 +237,7 @@ private:
* OID queue to implement a FIFO-like replacement policy for the pool
* cache.
*/
queue<int> oid_queue;
queue<int> oid_queue;
/**
* Function to lock the pool

View File

@ -31,52 +31,10 @@ const char * Group::db_bootstrap = "CREATE TABLE IF NOT EXISTS group_pool ("
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, "
"UNIQUE(name))";
/* ************************************************************************ */
/* Group :: Constructor/Destructor */
/* Group :: Database Access Functions */
/* ************************************************************************ */
Group::Group(int id, int uid, const string& name):
PoolObjectSQL(id,name,uid,-1,table){};
Group::~Group(){};
/* ************************************************************************ */
/* Group :: Database Access Functions */
/* ************************************************************************ */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Group::insert(SqlDB *db, string& error_str)
{
int rc;
rc = insert_replace(db, false);
if ( rc != 0 )
{
error_str = "Error inserting Group in DB.";
}
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Group::update(SqlDB *db)
{
int rc;
rc = insert_replace(db, true);
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Group::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
@ -87,7 +45,7 @@ int Group::insert_replace(SqlDB *db, bool replace)
char * sql_name;
char * sql_xml;
// Update the Group
// Update the Group
sql_name = db->escape_str(name.c_str());
@ -103,7 +61,7 @@ int Group::insert_replace(SqlDB *db, bool replace)
goto error_body;
}
if(replace)
if ( replace )
{
oss << "REPLACE";
}
@ -133,9 +91,8 @@ error_name:
return -1;
}
/* ************************************************************************ */
/* Group :: Misc */
/* ************************************************************************ */
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
ostream& operator<<(ostream& os, Group& group)
{
@ -154,7 +111,7 @@ string& Group::to_xml(string& xml) const
ostringstream oss;
oss <<
"<GROUP>" <<
"<GROUP>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
"<NAME>" << name << "</NAME>" <<
@ -187,3 +144,7 @@ int Group::from_xml(const string& xml)
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */

View File

@ -20,61 +20,62 @@
#include <stdexcept>
/* -------------------------------------------------------------------------- */
/* There are two default groups boostrapped by the core: */
/* - oneadmin can not be removed */
/* - users to place regular users by default */
/* The first 100 group IDs are reserved for system groups. Regular ones start */
/* from ID 100 */
/* -------------------------------------------------------------------------- */
const string GroupPool::ONEADMIN_NAME = "oneadmin";
const int GroupPool::ONEADMIN_ID = 0;
const string GroupPool::USERS_NAME = "users";
const int GroupPool::USERS_ID = 1;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
GroupPool::GroupPool(SqlDB * db):PoolSQL(db, Group::table)
{
// lastOID is set in PoolSQL::init_cb
if (get_lastOID() == -1)
ostringstream oss;
string error_str;
if (get_lastOID() == -1) //lastOID is set in PoolSQL::init_cb
{
int rc;
Group * group;
string error_str;
// Build the default groups
// Build the default oneadmins & users group
group = new Group(ONEADMIN_ID, 0, ONEADMIN_NAME);
// Insert the Object in the pool
rc = PoolSQL::allocate(group, error_str);
if(rc < 0)
if( rc < 0 )
{
ostringstream oss;
oss << "Error trying to create default group: " << error_str;
NebulaLog::log("GROUP",Log::ERROR,oss);
throw runtime_error(oss.str());
goto error_groups;
}
group = new Group(USERS_ID, 0, USERS_NAME);
// Insert the Object in the pool
rc = PoolSQL::allocate(group, error_str);
if(rc < 0)
{
ostringstream oss;
oss << "Error trying to create default group: " << error_str;
NebulaLog::log("GROUP",Log::ERROR,oss);
throw runtime_error(oss.str());
goto error_groups;
}
// First 100 group Ids are reserved for system groups.
// Regular ones start from ID 100
lastOID=99;
update_lastOID();
set_update_lastOID(99);
}
return;
error_groups:
oss << "Error trying to create default group: " << error_str;
NebulaLog::log("GROUP",Log::ERROR,oss);
throw runtime_error(oss.str());
}
/* -------------------------------------------------------------------------- */
@ -106,7 +107,6 @@ int GroupPool::allocate(int uid, string name, int * oid, string& error_str)
return *oid;
error_name:
oss << "NAME cannot be empty.";
goto error_common;
@ -132,8 +132,7 @@ int GroupPool::drop(Group * group)
if( group->get_oid() < 100 )
{
NebulaLog::log("GROUP",Log::ERROR,
"Groups with ID less than 100 cannot be deleted.");
"System Groups (ID < 100) cannot be deleted.");
return -1;
}

View File

@ -140,12 +140,16 @@ int PoolSQL::allocate(
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void PoolSQL::update_lastOID()
{
// db->escape_str is not used for 'table' since its name can't be set in
// any way by the user, it is hardcoded.
ostringstream oss;
oss << "REPLACE INTO pool_control (tablename, last_oid) VALUES ("
<< "'" << table << "',"
<< lastOID << ")";