Optimization in the setup: 10 queries to insert the 1500 action grant records

SVN:trunk[1152]
This commit is contained in:
Romain Quetiez
2011-03-29 14:28:47 +00:00
parent 30a1528a28
commit d4bfa4c48f
2 changed files with 72 additions and 7 deletions

View File

@@ -27,7 +27,27 @@
define('ADMIN_PROFILE_NAME', 'Administrator');
define('PORTAL_PROFILE_NAME', 'Portal user');
class UserRightsBaseClass extends cmdbAbstractObject
class UserRightsBaseClassGUI extends cmdbAbstractObject
{
// Whenever something changes, reload the privileges
protected function AfterInsert()
{
UserRights::FlushPrivileges();
}
protected function AfterUpdate()
{
UserRights::FlushPrivileges();
}
protected function AfterDelete()
{
UserRights::FlushPrivileges();
}
}
class UserRightsBaseClass extends DBObject
{
// Whenever something changes, reload the privileges
@@ -50,7 +70,7 @@ class UserRightsBaseClass extends cmdbAbstractObject
class URP_Profiles extends UserRightsBaseClass
class URP_Profiles extends UserRightsBaseClassGUI
{
public static function Init()
{
@@ -222,7 +242,7 @@ class URP_Profiles extends UserRightsBaseClass
class URP_UserProfile extends UserRightsBaseClass
class URP_UserProfile extends UserRightsBaseClassGUI
{
public static function Init()
{
@@ -262,7 +282,7 @@ class URP_UserProfile extends UserRightsBaseClass
}
}
class URP_UserOrg extends UserRightsBaseClass
class URP_UserOrg extends UserRightsBaseClassGUI
{
public static function Init()
{

View File

@@ -34,6 +34,10 @@ abstract class DBObject
{
private static $m_aMemoryObjectsByClass = array();
private static $m_aBulkInsertItems = array(); // class => array of ('table' => array of (array of <sql_value>))
private static $m_aBulkInsertCols = array(); // class => array of ('table' => array of <sql_column>)
private static $m_bBulkInsert = false;
private $m_bIsInDB = false; // true IIF the object is mapped to a DB record
private $m_iKey = null;
private $m_aCurrValues = array();
@@ -943,6 +947,35 @@ abstract class DBObject
}
}
// Note: this is experimental - it was designed to speed up the setup of iTop
// Known limitations:
// - does not work with multi-table classes (issue with the unique id to maintain in several tables)
// - the id of the object is not updated
static public final function BulkInsertStart()
{
self::$m_bBulkInsert = true;
}
static public final function BulkInsertFlush()
{
if (!self::$m_bBulkInsert) return;
foreach(self::$m_aBulkInsertCols as $sClass => $aTables)
{
foreach ($aTables as $sTable => $sColumns)
{
$sValues = implode(', ', self::$m_aBulkInsertItems[$sClass][$sTable]);
$sInsertSQL = "INSERT INTO `$sTable` ($sColumns) VALUES $sValues";
$iNewKey = CMDBSource::InsertInto($sInsertSQL);
}
}
// Reset
self::$m_aBulkInsertItems = array();
self::$m_aBulkInsertCols = array();
self::$m_bBulkInsert = false;
}
private function DBInsertSingleTable($sTableClass)
{
$sTable = MetaModel::DBGetTable($sTableClass);
@@ -976,15 +1009,27 @@ abstract class DBObject
if (count($aValuesToWrite) == 0) return false;
$sInsertSQL = "INSERT INTO `$sTable` (".join(",", $aFieldsToWrite).") VALUES (".join(", ", $aValuesToWrite).")";
if (MetaModel::DBIsReadOnly())
{
$iNewKey = -1;
}
else
{
$iNewKey = CMDBSource::InsertInto($sInsertSQL);
if (self::$m_bBulkInsert)
{
if (!isset(self::$m_aBulkInsertCols[$sClass][$sTable]))
{
self::$m_aBulkInsertCols[$sClass][$sTable] = implode(', ', $aFieldsToWrite);
}
self::$m_aBulkInsertItems[$sClass][$sTable][] = '('.implode (', ', $aValuesToWrite).')';
$iNewKey = 999999; // TODO - compute next id....
}
else
{
$sInsertSQL = "INSERT INTO `$sTable` (".join(",", $aFieldsToWrite).") VALUES (".join(", ", $aValuesToWrite).")";
$iNewKey = CMDBSource::InsertInto($sInsertSQL);
}
}
// Note that it is possible to have a key defined here, and the autoincrement expected, this is acceptable in a non root class
if (empty($this->m_iKey))