From: Michael M Slusarz Date: Fri, 15 Oct 2010 22:12:29 +0000 (-0600) Subject: Keep track of dirty prefs so we don't have to iterate through potentially large array... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=5c3919542a4c64907d06f26a3747b9ee9da89864;p=horde.git Keep track of dirty prefs so we don't have to iterate through potentially large array at end of every access --- diff --git a/framework/Prefs/lib/Horde/Prefs.php b/framework/Prefs/lib/Horde/Prefs.php index ef50b7024..cfc9a6dda 100644 --- a/framework/Prefs/lib/Horde/Prefs.php +++ b/framework/Prefs/lib/Horde/Prefs.php @@ -106,6 +106,13 @@ class Horde_Prefs implements ArrayAccess protected $_dict; /** + * List of dirty prefs. + * + * @var array + */ + protected $_dirty = array(); + + /** * Attempts to return a concrete instance based on $driver. * * @param string $driver Either a driver name, or the full class name to @@ -236,7 +243,7 @@ class Horde_Prefs implements ArrayAccess { // FIXME not updated yet - not removed from backend. $scope = $this->_getPreferenceScope($pref); - unset($this->_prefs[$pref]); + unset($this->_dirty[$scope][$pref], $this->_prefs[$pref]); $this->_cache->clear($scope, $pref); } @@ -436,6 +443,12 @@ class Horde_Prefs implements ArrayAccess */ public function setDirty($pref, $bool) { + if ($bool) { + $this->_dirty[$this->_scope][$pref] = $this->_prefs[$pref]; + } else { + unset($this->_dirty[$this->_scope][$pref]); + } + $this->_setMask($pref, $bool, self::DIRTY); } @@ -621,7 +634,7 @@ class Horde_Prefs implements ArrayAccess /* Perform a Horde-wide cleanup? */ if ($all) { /* Destroy the contents of the preferences hash. */ - $this->_prefs = array(); + $this->_dirty = $this->_prefs = array(); /* Destroy the contents of the preferences cache. */ $this->_cache->clear(); @@ -668,28 +681,6 @@ class Horde_Prefs implements ArrayAccess } /** - * Return all "dirty" preferences across all scopes. - * - * @return array The values for all dirty preferences, in a - * multi-dimensional array of scope => pref name => - * pref values. - */ - protected function _dirtyPrefs() - { - $dirty_prefs = array(); - - foreach ($this->_scopes as $scope => $prefs) { - foreach ($prefs as $pref_name => $pref) { - if (isset($pref['m']) && ($pref['m'] & self::DIRTY)) { - $dirty_prefs[$scope][$pref_name] = $pref; - } - } - } - - return $dirty_prefs; - } - - /** * Populates the $_scopes hash with new entries and externally * defined default values. * diff --git a/framework/Prefs/lib/Horde/Prefs/File.php b/framework/Prefs/lib/Horde/Prefs/File.php index 561b424cc..5a58004d7 100644 --- a/framework/Prefs/lib/Horde/Prefs/File.php +++ b/framework/Prefs/lib/Horde/Prefs/File.php @@ -174,20 +174,12 @@ class Horde_Prefs_File extends Horde_Prefs_Base /** * Stores preferences in the current session. * - * @return boolean True on success. * @throws Horde_Prefs_Exception */ public function store() { - if (is_null($this->_dirname)) { - return false; - } - - // Get the list of preferences that have changed. If there are - // none, no need to hit the backend. - $dirty_prefs = $this->_dirtyPrefs(); - if (!$dirty_prefs) { - return true; + if (is_null($this->_dirname) || empty($this->_dirty)) { + return; } // Read in all existing preferences, if any. @@ -197,7 +189,7 @@ class Horde_Prefs_File extends Horde_Prefs_Base } // Update all values from dirty scope - foreach ($dirty_prefs as $scope => $prefs) { + foreach ($this->_dirty as $scope => $prefs) { foreach ($prefs as $name => $pref) { // Don't store locked preferences. if (!($this->_scopes[$scope][$name]['m'] & self::LOCKED)) { @@ -212,8 +204,6 @@ class Horde_Prefs_File extends Horde_Prefs_Base if ($this->_writeCache() == false) { throw new Horde_Prefs_Exception(sprintf('Write of preferences to %s failed', $this->_filename)); } - - return true; } } diff --git a/framework/Prefs/lib/Horde/Prefs/Imsp.php b/framework/Prefs/lib/Horde/Prefs/Imsp.php index 3ffebab71..e3d5efc3c 100644 --- a/framework/Prefs/lib/Horde/Prefs/Imsp.php +++ b/framework/Prefs/lib/Horde/Prefs/Imsp.php @@ -69,14 +69,13 @@ class Horde_Prefs_Imsp extends Horde_Prefs_Base { // Get the list of preferences that have changed. If there are // none, no need to hit the backend. - $dirty_prefs = $this->_dirtyPrefs(); - if (!$dirty_prefs) { + if (empty($this->_dirty)) { return; } $this->_connect(); - foreach ($dirty_prefs as $scope => $prefs) { + foreach ($this->_dirty as $scope => $prefs) { $updated = array(); foreach ($prefs as $name => $pref) { diff --git a/framework/Prefs/lib/Horde/Prefs/KolabImap.php b/framework/Prefs/lib/Horde/Prefs/KolabImap.php index ac99fbb21..3e0a5eb18 100644 --- a/framework/Prefs/lib/Horde/Prefs/KolabImap.php +++ b/framework/Prefs/lib/Horde/Prefs/KolabImap.php @@ -148,11 +148,9 @@ class Horde_Prefs_KolabImap extends Horde_Prefs_Base { // Get the list of preferences that have changed. If there are // none, no need to hit the backend. - $dirty_prefs = $this->_dirtyPrefs(); - if (!$dirty_prefs) { + if (empty($this->_dirty)) { return; } - $dirty_scopes = array_keys($dirty_prefs); $this->_connect(); @@ -161,7 +159,7 @@ class Horde_Prefs_KolabImap extends Horde_Prefs_Base // all of the values of a multi-value entry wholesale, we // can't just pick out the dirty preferences; we must update // every scope that has dirty preferences. - foreach ($dirty_scopes as $scope) { + foreach (array_keys($this->_dirty) as $scope) { $new_values = array(); foreach ($this->_scopes[$scope] as $name => $pref) { // Don't store locked preferences. @@ -200,7 +198,7 @@ class Horde_Prefs_KolabImap extends Horde_Prefs_Base } // Clean the preferences since they were just saved. - foreach ($dirty_prefs as $scope => $prefs) { + foreach ($this->_dirty as $scope => $prefs) { foreach ($prefs as $name => $pref) { $this->_scopes[$scope][$name]['m'] &= ~self::DIRTY; } diff --git a/framework/Prefs/lib/Horde/Prefs/Ldap.php b/framework/Prefs/lib/Horde/Prefs/Ldap.php index 950f32259..b2cf3d3b3 100644 --- a/framework/Prefs/lib/Horde/Prefs/Ldap.php +++ b/framework/Prefs/lib/Horde/Prefs/Ldap.php @@ -319,11 +319,9 @@ class Horde_Prefs_Ldap extends Horde_Prefs { // Get the list of preferences that have changed. If there are // none, no need to hit the backend. - $dirty_prefs = $this->_dirtyPrefs(); - if (!$dirty_prefs) { + if (empty($this->_dirty)) { return; } - $dirty_scopes = array_keys($dirty_prefs); $this->_connect(); @@ -333,7 +331,7 @@ class Horde_Prefs_Ldap extends Horde_Prefs // can't just pick out the dirty preferences; we must update // every scope that has dirty preferences. $new_values = array(); - foreach ($dirty_scopes as $scope) { + foreach (array_keys($this->_dirty) as $scope) { foreach ($this->_scopes[$scope] as $name => $pref) { // Don't store locked preferences. if (!($pref['m'] & self::LOCKED)) { @@ -397,7 +395,7 @@ class Horde_Prefs_Ldap extends Horde_Prefs } // Clean the preferences since they were just saved. - foreach ($dirty_prefs as $scope => $prefs) { + foreach ($this->_dirty as $scope => $prefs) { foreach ($prefs as $name => $pref) { $this->_scopes[$scope][$name]['m'] &= ~self::DIRTY; } diff --git a/framework/Prefs/lib/Horde/Prefs/Sql.php b/framework/Prefs/lib/Horde/Prefs/Sql.php index d8416c991..d853d9d2f 100644 --- a/framework/Prefs/lib/Horde/Prefs/Sql.php +++ b/framework/Prefs/lib/Horde/Prefs/Sql.php @@ -113,15 +113,9 @@ class Horde_Prefs_Sql extends Horde_Prefs */ public function store() { - // Get the list of preferences that have changed. If there are - // none, no need to hit the backend. - if (!($dirty_prefs = $this->_dirtyPrefs())) { - return; - } - // For each preference, check for an existing table row and // update it if it's there, or create a new one if it's not. - foreach ($dirty_prefs as $scope => $prefs) { + foreach ($this->_dirty as $scope => $prefs) { $updated = array(); foreach ($prefs as $name => $pref) {