MFB: Speed up browsing of long books by more than a magnitude.
authorJan Schneider <jan@horde.org>
Mon, 16 Aug 2010 22:12:01 +0000 (00:12 +0200)
committerJan Schneider <jan@horde.org>
Mon, 16 Aug 2010 22:12:01 +0000 (00:12 +0200)
turba/docs/CHANGES
turba/lib/Driver.php
turba/lib/List.php
turba/lib/Object.php

index be97b31..365ca2c 100644 (file)
@@ -13,7 +13,7 @@ v3.0-git
 v2.3.5-cvs
 ----------
 
-
+[jan] Speed up browsing of long address books.
 
 
 ------
index 062851b..61e08b8 100644 (file)
@@ -423,32 +423,26 @@ class Turba_Driver
     }
 
     /**
-     * Translates an array of hashes from being keyed on driver-specific
-     * fields to being keyed on the generalized Turba attributes. The
-     * translation is based on the contents of $this->map.
+     * Translates a hash from being keyed on driver-specific fields to being
+     * keyed on the generalized Turba attributes. The translation is based on
+     * the contents of $this->map.
      *
-     * @param array $objects  Array of hashes using driver-specific keys.
+     * @param array $entry  A hash using driver-specific keys.
      *
-     * @return array  Translated version of $objects.
+     * @return array  Translated version of $entry.
      */
-    function toTurbaKeys($objects)
+    function toTurbaKeys($entry)
     {
-        $attributes = array();
-        foreach ($objects as $entry) {
-            $new_entry = array();
-
-            foreach ($this->map as $key => $val) {
-                if (!is_array($val)) {
-                    $new_entry[$key] = null;
-                    if (isset($entry[$val]) && strlen($entry[$val])) {
-                        $new_entry[$key] = trim($entry[$val]);
-                    }
+        $new_entry = array();
+        foreach ($this->map as $key => $val) {
+            if (!is_array($val)) {
+                $new_entry[$key] = null;
+                if (isset($entry[$val]) && strlen($entry[$val])) {
+                    $new_entry[$key] = trim($entry[$val]);
                 }
             }
-
-            $attributes[] = $new_entry;
         }
-        return $attributes;
+        return $new_entry;
     }
 
     /**
@@ -557,12 +551,12 @@ class Turba_Driver
      */
     function _toTurbaObjects($objects, $sort_order = null)
     {
-        /* Translate the driver-specific fields in the result back to the more
-         * generalized common Turba attributes using the map. */
-        $objects = $this->toTurbaKeys($objects);
-
         $list = new Turba_List();
         foreach ($objects as $object) {
+            /* Translate the driver-specific fields in the result back to the
+             * more generalized common Turba attributes using the map. */
+            $object = $this->toTurbaKeys($object);
+
             $done = false;
             if (!empty($object['__type']) &&
                 ucwords($object['__type']) != 'Object') {
@@ -721,8 +715,8 @@ class Turba_Driver
         }
 
         $results = array();
-        $objects = $this->toTurbaKeys($objects);
         foreach ($objects as $object) {
+            $object = $this->toTurbaKeys($object);
             $done = false;
             if (!empty($object['__type']) &&
                 ucwords($object['__type']) != 'Object') {
index aef8dd3..564cc76 100644 (file)
@@ -147,7 +147,7 @@ class Turba_List {
      *
      * @return integer  Comparison of the two field values.
      */
-    function cmp($a, $b)
+    function cmp(&$a, &$b)
     {
         require TURBA_BASE . '/config/attributes.php';
         foreach ($this->_usortCriteria as $field) {
@@ -165,8 +165,7 @@ class Turba_List {
             }
 
             $method = 'cmp_' . $usortType;
-            $result = $this->$method($a->getValue($field['field']),
-                                     $b->getValue($field['field']));
+            $result = $this->$method($a, $b, $field['field']);
             if (!$field['ascending']) {
                 $result = -$result;
             }
@@ -177,18 +176,22 @@ class Turba_List {
         return 0;
     }
 
-    function cmp_text($a, $b)
+    function cmp_text(&$a, &$b, $field)
     {
-        $acmp = Horde_String::lower($a, true);
-        $bcmp = Horde_String::lower($b, true);
+        if (!isset($a->sortValue[$field])) {
+            $a->sortValue[$field] = Horde_String::lower($a->getValue($field), true);
+        }
+        if (!isset($b->sortValue[$field])) {
+            $b->sortValue[$field] = Horde_String::lower($b->getValue($field), true);
+        }
 
         // Use strcoll for locale-safe comparisons.
-        return strcoll($acmp, $bcmp);
+        return strcoll($a->sortValue[$field], $b->sortValue[$field]);
     }
 
-    function cmp_int($a, $b)
+    function cmp_int($a, $b, $field)
     {
-        return ($a > $b) ? 1 : -1;
+        return ($a->getValue($field) > $b->getValue($field)) ? 1 : -1;
     }
 
 }
index 37c39d0..68c3dc4 100644 (file)
@@ -31,6 +31,13 @@ class Turba_Object {
     var $_vfs;
 
     /**
+     * Keeps the normalized values of sort columns.
+     *
+     * @var array
+     */
+    var $sortValue = array();
+
+    /**
      * Constructs a new Turba_Object object.
      *
      * @param Turba_Driver $driver  The source that this object came from.