From d4bfa4c48f4a5007a5be2e559ab96933b55c59f9 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Tue, 29 Mar 2011 14:28:47 +0000 Subject: [PATCH] Optimization in the setup: 10 queries to insert the 1500 action grant records SVN:trunk[1152] --- .../userrightsprofile.class.inc.php | 28 ++++++++-- core/dbobject.class.php | 51 +++++++++++++++++-- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/addons/userrights/userrightsprofile.class.inc.php b/addons/userrights/userrightsprofile.class.inc.php index c46a3db96..c6c2091bb 100644 --- a/addons/userrights/userrightsprofile.class.inc.php +++ b/addons/userrights/userrightsprofile.class.inc.php @@ -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() { diff --git a/core/dbobject.class.php b/core/dbobject.class.php index 79253af2d..a7642e51e 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -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 )) + private static $m_aBulkInsertCols = array(); // class => array of ('table' => array of ) + 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))