Keep track of dirty prefs so we don't have to iterate through potentially large array...
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 15 Oct 2010 22:12:29 +0000 (16:12 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 19 Oct 2010 17:07:48 +0000 (11:07 -0600)
framework/Prefs/lib/Horde/Prefs.php
framework/Prefs/lib/Horde/Prefs/File.php
framework/Prefs/lib/Horde/Prefs/Imsp.php
framework/Prefs/lib/Horde/Prefs/KolabImap.php
framework/Prefs/lib/Horde/Prefs/Ldap.php
framework/Prefs/lib/Horde/Prefs/Sql.php

index ef50b70..cfc9a6d 100644 (file)
@@ -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.
      *
index 561b424..5a58004 100644 (file)
@@ -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;
     }
 
 }
index 3ffebab..e3d5efc 100644 (file)
@@ -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) {
index ac99fbb..3e0a5eb 100644 (file)
@@ -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;
             }
index 950f322..b2cf3d3 100644 (file)
@@ -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;
             }
index d8416c9..d853d9d 100644 (file)
@@ -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) {