ソースを参照

N.1065 Fix performance issues.
ormLinkSet creates the objects on demand.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@4956 a333f486-631f-4898-b8df-5754b55c2be0

eespie 7 年 前
コミット
62c27a0032
1 ファイル変更26 行追加8 行削除
  1. 26 8
      core/ormlinkset.class.inc.php

+ 26 - 8
core/ormlinkset.class.inc.php

@@ -52,7 +52,7 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator
 	 * Object from the original set, minus the removed objects
 	 * Object from the original set, minus the removed objects
 	 * @var DBObject[] array of iObjectId => DBObject
 	 * @var DBObject[] array of iObjectId => DBObject
 	 */
 	 */
-	protected $aPreserved;
+	protected $aPreserved = array();
 
 
 	/**
 	/**
 	 * @var DBObject[] New items
 	 * @var DBObject[] New items
@@ -172,7 +172,7 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator
 		{
 		{
 			if ($this->oOriginalSet)
 			if ($this->oOriginalSet)
 			{
 			{
-				$this->aOriginalObjects = $this->oOriginalSet->ToArray();
+				$this->aOriginalObjects = $this->GetArrayOfIndex();
 				$this->aPreserved = $this->aOriginalObjects; // Copy (not effective until aPreserved gets modified)
 				$this->aPreserved = $this->aOriginalObjects; // Copy (not effective until aPreserved gets modified)
                 foreach ($this->aRemoved as $iObjectId)
                 foreach ($this->aRemoved as $iObjectId)
                 {
                 {
@@ -181,7 +181,7 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator
                         unset($this->aPreserved[$iObjectId]);
                         unset($this->aPreserved[$iObjectId]);
                     }
                     }
                 }
                 }
-                foreach ($this->aModified as $iObjectId)
+                foreach ($this->aModified as $iObjectId => $oLink)
                 {
                 {
                     if (array_key_exists($iObjectId, $this->aPreserved))
                     if (array_key_exists($iObjectId, $this->aPreserved))
                     {
                     {
@@ -199,6 +199,22 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator
 		}
 		}
 	}
 	}
 
 
+	/**
+	 * Note: After calling this method, the set cursor will be at the end of the set. You might want to rewind it.
+	 * @return array
+	 */
+	protected function GetArrayOfIndex()
+	{
+		$aRet = array();
+		$this->oOriginalSet->Rewind();
+		$iRow = 0;
+		while ($oObject = $this->oOriginalSet->Fetch())
+		{
+			$aRet[$oObject->GetKey()] = $iRow++;
+		}
+		return $aRet;
+	}
+
     /**
     /**
      * @param bool $bWithId
      * @param bool $bWithId
      * @return array
      * @return array
@@ -318,7 +334,9 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator
 		$iPreservedCount = count($this->aPreserved);
 		$iPreservedCount = count($this->aPreserved);
 		if ($this->iCursor < $iPreservedCount)
 		if ($this->iCursor < $iPreservedCount)
 		{
 		{
-			$oRet = current($this->aPreserved);
+			$iRet = current($this->aPreserved);
+			$this->oOriginalSet->Seek($iRet);
+			$oRet = $this->oOriginalSet->Fetch();
 		}
 		}
 		else
 		else
 		{
 		{
@@ -397,8 +415,6 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator
 	 */
 	 */
 	public function rewind()
 	public function rewind()
 	{
 	{
-		$this->LoadOriginalIds();
-
 		$this->iCursor = 0;
 		$this->iCursor = 0;
 		reset($this->aPreserved);
 		reset($this->aPreserved);
         reset($this->aAdded);
         reset($this->aAdded);
@@ -549,12 +565,15 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator
 
 
 		// Check for the existing links
 		// Check for the existing links
 		//
 		//
+		/** @var DBObject[] $aExistingLinks */
+		$aExistingLinks = array();
+		/** @var Int[] $aExistingRemote */
+		$aExistingRemote = array();
 		if (count($aCheckLinks) > 0)
 		if (count($aCheckLinks) > 0)
 		{
 		{
 			$oSearch = new DBObjectSearch($this->sClass);
 			$oSearch = new DBObjectSearch($this->sClass);
 			$oSearch->AddCondition('id', $aCheckLinks, 'IN');
 			$oSearch->AddCondition('id', $aCheckLinks, 'IN');
 			$oSet = new DBObjectSet($oSearch);
 			$oSet = new DBObjectSet($oSearch);
-			/** @var DBObject[] $aExistingLinks */
 			$aExistingLinks = $oSet->ToArray();
 			$aExistingLinks = $oSet->ToArray();
 		}
 		}
 
 
@@ -566,7 +585,6 @@ class ormLinkSet implements iDBObjectSetIterator, Iterator, SeekableIterator
 			$oSearch->AddCondition($sExtKeyToMe, $oHostObject->GetKey(), '=');
 			$oSearch->AddCondition($sExtKeyToMe, $oHostObject->GetKey(), '=');
 			$oSearch->AddCondition($sExtKeyToRemote, $aCheckRemote, 'IN');
 			$oSearch->AddCondition($sExtKeyToRemote, $aCheckRemote, 'IN');
 			$oSet = new DBObjectSet($oSearch);
 			$oSet = new DBObjectSet($oSearch);
-			/** @var Int[] $aExistingRemote */
 			$aExistingRemote = $oSet->GetColumnAsArray($sExtKeyToRemote);
 			$aExistingRemote = $oSet->GetColumnAsArray($sExtKeyToRemote);
 		}
 		}