From 411743cf2a625c2025ff85de32f2403858355a40 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Tue, 17 Aug 2010 00:12:01 +0200 Subject: [PATCH] MFB: Speed up browsing of long books by more than a magnitude. --- turba/docs/CHANGES | 2 +- turba/lib/Driver.php | 42 ++++++++++++++++++------------------------ turba/lib/List.php | 21 ++++++++++++--------- turba/lib/Object.php | 7 +++++++ 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/turba/docs/CHANGES b/turba/docs/CHANGES index be97b316e..365ca2cc6 100644 --- a/turba/docs/CHANGES +++ b/turba/docs/CHANGES @@ -13,7 +13,7 @@ v3.0-git v2.3.5-cvs ---------- - +[jan] Speed up browsing of long address books. ------ diff --git a/turba/lib/Driver.php b/turba/lib/Driver.php index 062851bcb..61e08b8fb 100644 --- a/turba/lib/Driver.php +++ b/turba/lib/Driver.php @@ -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') { diff --git a/turba/lib/List.php b/turba/lib/List.php index aef8dd3cb..564cc765b 100644 --- a/turba/lib/List.php +++ b/turba/lib/List.php @@ -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; } } diff --git a/turba/lib/Object.php b/turba/lib/Object.php index 37c39d0f3..68c3dc47e 100644 --- a/turba/lib/Object.php +++ b/turba/lib/Object.php @@ -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. -- 2.11.0