Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lean sync for servers without CONDSTORE #7375

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace OCA\Mail\Db;

use Horde_Imap_Client;
use InvalidArgumentException;
use OCA\Mail\Account;
use OCA\Mail\Address;
use OCA\Mail\AddressList;
Expand Down Expand Up @@ -429,6 +431,25 @@ public function updateBulk(Account $account, bool $permflagsEnabled, Message ...
return $messages;
}

/**
* @param int[] $uids
*/
public function updateFlagBulk(Mailbox $mailbox, string $flag, bool $set, array $uids): void {
$col = $this->hordeFlagToColumnName($flag);
$query = $this->db->getQueryBuilder();
$query->update($this->getTableName())
->set($col, $query->createNamedParameter($set, IQueryBuilder::PARAM_BOOL))
->where(
$query->expr()->eq('mailbox_id', $query->createNamedParameter($mailbox->getId(), IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
$query->expr()->eq($col, $query->createNamedParameter(!$set, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL),
$query->expr()->in('uid', $query->createParameter('uids'), IQueryBuilder::PARAM_INT_ARRAY),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if possible with all your fancy pancy supported databases

but an index like

('mailbox_id', 'uid') INCLUDING (flag1, flag2, flag3, flag4, flag5, flag6) would allow the filtering to happen mainly on the index level. Without going to the actual data.

);
foreach (array_chunk($uids, 1000) as $uidChunk) {
$query->setParameter('uids', $uidChunk, IQueryBuilder::PARAM_INT_ARRAY);
$affected = $query->executeStatement();
}
}

/**
* @param Account $account
* @param Message $message
Expand Down Expand Up @@ -987,6 +1008,26 @@ private function flagToColumnName(Flag $flag): string {
return "flag_$key";
}

private function hordeFlagToColumnName(string $flag): string {
$hordeToColumn = [
Horde_Imap_Client::FLAG_ANSWERED => 'flag_answered',
Horde_Imap_Client::FLAG_DELETED => 'flag_deleted',
Horde_Imap_Client::FLAG_DRAFT => 'flag_draft',
Horde_Imap_Client::FLAG_FLAGGED => 'flag_flagged',
Horde_Imap_Client::FLAG_SEEN => 'flag_seen',
Horde_Imap_Client::FLAG_FORWARDED => 'flag_forwarded',
Horde_Imap_Client::FLAG_JUNK => 'flag_junk',
Horde_Imap_Client::FLAG_NOTJUNK => 'flag_notjunk',
Horde_Imap_Client::FLAG_MDNSENT => 'flag_mdnsent',
];

if (!isset($hordeToColumn[$flag])) {
throw new InvalidArgumentException("Invalid flag " . $flag);
}

return $hordeToColumn[$flag];
}

/**
* @param Mailbox $mailbox
* @param int[] $uids
Expand Down
5 changes: 3 additions & 2 deletions lib/IMAP/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,10 @@ public function removeFlag(Horde_Imap_Client_Socket $client,
*/
public function getFlagged(Horde_Imap_Client_Socket $client,
Mailbox $mailbox,
string $flag): array {
string $flag,
bool $set = true): array {
$query = new Horde_Imap_Client_Search_Query();
$query->flag($flag, true);
$query->flag($flag, $set);
$messages = $client->search($mailbox->getName(), $query);
return $messages['match']->ids ?? [];
}
Expand Down
58 changes: 42 additions & 16 deletions lib/Service/Sync/ImapToDbSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,25 +408,51 @@ private function runPartialSync(Account $account,
$newOrVanished = !empty($newMessages);
}
if ($criteria & Horde_Imap_Client::SYNC_FLAGSUIDS) {
$response = $this->synchronizer->sync(
$client,
new Request(
$mailbox->getName(),
$mailbox->getSyncChangedToken(),
$uids
),
Horde_Imap_Client::SYNC_FLAGSUIDS
);
$perf->step('get changed messages via Horde');
$client->login();
if ($client->capability->isEnabled('CONDSTORE')) {
$response = $this->synchronizer->sync(
$client,
new Request(
$mailbox->getName(),
$mailbox->getSyncChangedToken(),
$uids
),
Horde_Imap_Client::SYNC_FLAGSUIDS
);
$perf->step('get changed messages via Horde');

$permflagsEnabled = $this->mailManager->isPermflagsEnabled($client, $account, $mailbox->getName());
$permflagsEnabled = $this->mailManager->isPermflagsEnabled($client, $account, $mailbox->getName());

foreach (array_chunk($response->getChangedMessages(), 500) as $chunk) {
$this->dbMapper->updateBulk($account, $permflagsEnabled, ...array_map(static function (IMAPMessage $imapMessage) use ($mailbox, $account) {
return $imapMessage->toDbMessage($mailbox->getId(), $account->getMailAccount());
}, $chunk));
foreach (array_chunk($response->getChangedMessages(), 500) as $chunk) {
$this->dbMapper->updateBulk($account, $permflagsEnabled, ...array_map(static function (IMAPMessage $imapMessage) use ($mailbox, $account) {
return $imapMessage->toDbMessage($mailbox->getId(), $account->getMailAccount());
}, $chunk));
}
$perf->step('persist changed messages');
} else {
$flags = [
Horde_Imap_Client::FLAG_ANSWERED,
Horde_Imap_Client::FLAG_DELETED,
Horde_Imap_Client::FLAG_DRAFT,
Horde_Imap_Client::FLAG_FLAGGED,
Horde_Imap_Client::FLAG_SEEN,
Horde_Imap_Client::FLAG_FORWARDED,
Horde_Imap_Client::FLAG_JUNK,
Horde_Imap_Client::FLAG_NOTJUNK,
Horde_Imap_Client::FLAG_MDNSENT,
];
foreach ($flags as $flag) {
foreach ([true, false] as $set) {
$uids = $this->imapMapper->getFlagged($client, $mailbox, $flag, $set);
$this->dbMapper->updateFlagBulk(
$mailbox,
$flag,
$set,
$uids,
);
}
}
}
$perf->step('persist changed messages');

// If a list of UIDs was *provided* (as opposed to loaded from the DB,
// we can not assume that all changes were detected, hence this is kinda
Expand Down