Speedup MessageListByRequestIndexOrUids() by using the new FetchIterate()

This commit is contained in:
the-djmaze 2022-12-30 19:47:40 +01:00
parent fe718e81e9
commit c8deecb4aa
2 changed files with 28 additions and 23 deletions

View file

@ -32,7 +32,7 @@ trait Messages
* @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\Exceptions\*
*/
public function Fetch(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : array
public function FetchIterate(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : iterable
{
if (!\strlen(\trim($sIndexRange))) {
$this->writeLogException(new \InvalidArgumentException, \LOG_ERR);
@ -112,7 +112,7 @@ trait Messages
foreach ($this->yieldUntaggedResponses() as $oResponse) {
if (FetchResponse::isValidImapResponse($oResponse)) {
if (FetchResponse::hasUidAndSize($oResponse)) {
$aReturn[] = new FetchResponse($oResponse);
yield new FetchResponse($oResponse);
} else if ($this->oLogger) {
$this->oLogger->Write('Skipped Imap Response! ['.$oResponse.']', \LOG_NOTICE);
}
@ -121,7 +121,14 @@ trait Messages
} finally {
$this->aFetchCallbacks = array();
}
}
public function Fetch(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : array
{
$aReturn = array();
foreach ($this->FetchIterate($aInputFetchItems, $sIndexRange, $bIndexIsUid) as $oFetchResponse) {
$aReturn[] = $oFetchResponse;
}
return $aReturn;
}

View file

@ -479,7 +479,7 @@ class MailClient
protected function MessageListByRequestIndexOrUids(MessageCollection $oMessageCollection, SequenceSet $oRange, array &$aAllThreads = []) : void
{
if (\count($oRange)) {
$aFetchResponse = $this->oImapClient->Fetch(array(
$aFetchIterator = $this->oImapClient->FetchIterate(array(
FetchType::UID,
FetchType::RFC822_SIZE,
FetchType::INTERNALDATE,
@ -487,31 +487,29 @@ class MailClient
FetchType::BODYSTRUCTURE,
$this->getEnvelopeOrHeadersRequestString()
), (string) $oRange, $oRange->UID);
if (\count($aFetchResponse)) {
$aCollection = \array_fill_keys($oRange->getArrayCopy(), null);
foreach ($aFetchResponse as /* @var $oFetchResponseItem \MailSo\Imap\FetchResponse */ $oFetchResponseItem) {
$id = $oRange->UID
? $oFetchResponseItem->GetFetchValue(FetchType::UID)
: $oFetchResponseItem->oImapResponse->ResponseList[1];
$oMessage = Message::fromFetchResponse($oMessageCollection->FolderName, $oFetchResponseItem);
if ($oMessage) {
if ($aAllThreads) {
$iUid = $oMessage->Uid;
// Find thread and set it.
// Used by GUI to delete/move the whole thread or other features
foreach ($aAllThreads as $aMap) {
if (\in_array($iUid, $aMap)) {
$oMessage->SetThreads($aMap);
break;
}
// FETCH does not respond in the id order of the SequenceSet, so we prefill $aCollection for the right sort order.
$aCollection = \array_fill_keys($oRange->getArrayCopy(), null);
foreach ($aFetchIterator as /* @var $oFetchResponseItem \MailSo\Imap\FetchResponse */ $oFetchResponseItem) {
$id = $oRange->UID
? $oFetchResponseItem->GetFetchValue(FetchType::UID)
: $oFetchResponseItem->oImapResponse->ResponseList[1];
$oMessage = Message::fromFetchResponse($oMessageCollection->FolderName, $oFetchResponseItem);
if ($oMessage) {
if ($aAllThreads) {
$iUid = $oMessage->Uid;
// Find thread and set it.
// Used by GUI to delete/move the whole thread or other features
foreach ($aAllThreads as $aMap) {
if (\in_array($iUid, $aMap)) {
$oMessage->SetThreads($aMap);
break;
}
}
$aCollection[$id] = $oMessage;
}
$aCollection[$id] = $oMessage;
}
$oMessageCollection->exchangeArray(\array_values(\array_filter($aCollection)));
}
$oMessageCollection->exchangeArray(\array_values(\array_filter($aCollection)));
}
}