瀏覽代碼

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

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1152 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 年之前
父節點
當前提交
b2fa746250
共有 2 個文件被更改,包括 72 次插入7 次删除
  1. 24 4
      addons/userrights/userrightsprofile.class.inc.php
  2. 48 3
      core/dbobject.class.php

+ 24 - 4
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()
 	{

+ 48 - 3
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 <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))