Skip to content

Commit

Permalink
Improved multi-folder search performance on sort order change or list…
Browse files Browse the repository at this point in the history
… page change

by proper index serialization and skipped redundant sorting
  • Loading branch information
alecpl committed Sep 11, 2015
1 parent 8e5c443 commit fef8537
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 40 deletions.
25 changes: 12 additions & 13 deletions program/lib/Roundcube/rcube_imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ protected function list_search_messages($folder, $page, $slice=0)

// gather messages from a multi-folder search
if ($this->search_set->multi) {
$page_size = $this->page_size;
$page_size = $this->page_size;
$sort_field = $this->sort_field;
$search_set = $this->search_set;

Expand All @@ -995,23 +995,26 @@ protected function list_search_messages($folder, $page, $slice=0)
$slice_length = min($page_size, $cnt - $from);

// fetch resultset headers, sort and slice them
if (!empty($sort_field)) {
if (!empty($sort_field) && $search_set->get_parameters('SORT') != $sort_field) {
$this->sort_field = null;
$this->page_size = 1000; // fetch up to 1000 matching messages per folder
$this->threading = false;
$this->page_size = 1000; // fetch up to 1000 matching messages per folder
$this->threading = false;

$a_msg_headers = array();
foreach ($search_set->sets as $resultset) {
if (!$resultset->is_empty()) {
$this->search_set = $resultset;
$this->search_set = $resultset;
$this->search_threads = $resultset instanceof rcube_result_thread;
$a_msg_headers = array_merge($a_msg_headers, $this->list_search_messages($resultset->get_parameters('MAILBOX'), 1));

$a_headers = $this->list_search_messages($resultset->get_parameters('MAILBOX'), 1);
$a_msg_headers = array_merge($a_msg_headers, $a_headers);
unset($a_headers);
}
}

// sort headers
if (!empty($a_msg_headers)) {
$a_msg_headers = $this->conn->sortHeaders($a_msg_headers, $sort_field, $this->sort_order);
$a_msg_headers = rcube_imap_generic::sortHeaders($a_msg_headers, $sort_field, $this->sort_order);
}

// store (sorted) message index
Expand Down Expand Up @@ -1045,7 +1048,7 @@ protected function list_search_messages($folder, $page, $slice=0)

// restore members
$this->sort_field = $sort_field;
$this->page_size = $page_size;
$this->page_size = $page_size;
$this->search_set = $search_set;

return $a_msg_headers;
Expand Down Expand Up @@ -1139,12 +1142,8 @@ protected function list_search_messages($folder, $page, $slice=0)
return array();
}

if (!$this->check_connection()) {
return array();
}

// if not already sorted
$a_msg_headers = $this->conn->sortHeaders(
$a_msg_headers = rcube_imap_generic::sortHeaders(
$a_msg_headers, $this->sort_field, $this->sort_order);

// only return the requested part of the set
Expand Down
7 changes: 4 additions & 3 deletions program/lib/Roundcube/rcube_result_index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ class rcube_result_index

protected $raw_data;
protected $mailbox;
protected $meta = array();
protected $meta = array();
protected $params = array();
protected $order = 'ASC';
protected $order = 'ASC';

const SEPARATOR_ELEMENT = ' ';


/**
* Object constructor.
*/
public function __construct($mailbox = null, $data = null)
public function __construct($mailbox = null, $data = null, $order = null)
{
$this->mailbox = $mailbox;
$this->order = $order == 'DESC' ? 'DESC' : 'ASC';
$this->init($data);
}

Expand Down
70 changes: 46 additions & 24 deletions program/lib/Roundcube/rcube_result_multifolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class rcube_result_multifolder
protected $meta = array();
protected $index = array();
protected $folders = array();
protected $sdata = array();
protected $order = 'ASC';
protected $sorting;

Expand Down Expand Up @@ -139,18 +140,13 @@ public function count_messages()
public function revert()
{
$this->order = $this->order == 'ASC' ? 'DESC' : 'ASC';
$this->index = array();
$this->index = array_reverse($this->index);

// revert order in all sub-sets
foreach ($this->sets as $set) {
if ($this->order != $set->get_parameters('ORDER')) {
$set->revert();
}

$folder = $set->get_parameters('MAILBOX');
$index = array_map(function($uid) use ($folder) { return $uid . '-' . $folder; }, $set->get());

$this->index = array_merge($this->index, $index);
}
}

Expand All @@ -175,7 +171,7 @@ public function exists($msgid, $get_index = false)
/**
* Filters data set. Removes elements listed in $ids list.
*
* @param array $ids List of IDs to remove.
* @param array $ids List of IDs to remove.
* @param string $folder IMAP folder
*/
public function filter($ids = array(), $folder = null)
Expand All @@ -193,9 +189,8 @@ public function filter($ids = array(), $folder = null)
/**
* Slices data set.
*
* @param $offset Offset (as for PHP's array_slice())
* @param $length Number of elements (as for PHP's array_slice())
*
* @param int $offset Offset (as for PHP's array_slice())
* @param int $length Number of elements (as for PHP's array_slice())
*/
public function slice($offset, $length)
{
Expand Down Expand Up @@ -226,9 +221,9 @@ public function get()
}

/**
* Return all messages in the result.
* Return all messages in the result in compressed form
*
* @return array List of message IDs
* @return string List of message IDs in compressed form
*/
public function get_compressed()
{
Expand All @@ -238,7 +233,7 @@ public function get_compressed()
/**
* Return result element at specified index
*
* @param int|string $index Element's index or "FIRST" or "LAST"
* @param int|string $index Element's index or "FIRST" or "LAST"
*
* @return int Element value
*/
Expand All @@ -255,7 +250,7 @@ public function get_element($idx)
* Returns response parameters, e.g. ESEARCH's MIN/MAX/COUNT/ALL/MODSEQ
* or internal data e.g. MAILBOX, ORDER
*
* @param string $param Parameter name
* @param string $param Parameter name
*
* @return array|string Response parameters or parameter value
*/
Expand All @@ -277,8 +272,9 @@ public function get_parameters($param=null)
/**
* Returns the stored result object for a particular folder
*
* @param string $folder Folder name
* @return false|obejct rcube_result_* instance of false if none found
* @param string $folder Folder name
*
* @return false|object rcube_result_* instance of false if none found
*/
public function get_set($folder)
{
Expand Down Expand Up @@ -306,21 +302,47 @@ protected function length()

public function __sleep()
{
return array('sets','folders','sorting','order');
$this->sdata = array('incomplete' => array(), 'error' => array());

foreach ($this->sets as $set) {
if ($set->incomplete) {
$this->sdata['incomplete'][] = $set->get_parameters('MAILBOX');
}
else if ($set->is_error()) {
$this->sdata['error'][] = $set->get_parameters('MAILBOX');
}
}

return array('sdata', 'index', 'folders', 'sorting', 'order');
}

public function __wakeup()
{
// restore index from saved result sets
$this->meta = array('count' => 0);
$this->meta = array('count' => count($this->index));
$this->incomplete = count($this->sdata['incomplete']) > 0;

// restore result sets from saved index
$data = array();
foreach ($this->index as $item) {
list($uid, $folder) = explode('-', $item, 2);
$data[$folder] .= ' ' . $uid;
}

foreach ($this->sets as $result) {
if ($result->count()) {
$this->append_result($result);
foreach ($this->folders as $folder) {
if (in_array($folder, $this->sdata['error'])) {
$data_str = null;
}
else if ($result->incomplete) {
$this->incomplete = true;
else {
$data_str = '* SORT' . $data[$folder];
}

$set = new rcube_result_index($folder, $data_str, strtoupper($this->order));

if (in_array($folder, $this->sdata['incomplete'])) {
$set->incomplete = true;
}

$this->sets[] = $set;
}
}
}

0 comments on commit fef8537

Please sign in to comment.