feat: track cache changes with sequence numbers and tombstones

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-10-07 21:21:14 -04:00
parent c20dde6633
commit eea9b5630a
10 changed files with 511 additions and 46 deletions
+149 -9
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace KTXT\ProviderImap\Tests\Unit;
use Generator;
use KTXF\Resource\Delta\Delta;
use KTXF\Resource\Filter\IFilter;
use KTXF\Resource\Sort\ISort;
use KTXC\Db\DataStore;
@@ -15,6 +16,7 @@ use KTXM\ProviderImap\Providers\CollectionResource;
use KTXM\ProviderImap\Providers\EntityResource;
use KTXM\ProviderImap\Providers\Service;
use KTXM\ProviderImap\Service\Cache\HarmonizationService;
use KTXM\ProviderImap\Service\Cache\MessageDeltaService;
use KTXM\ProviderImap\Service\Cache\MessageIngestor;
use KTXM\ProviderImap\Service\Live\LiveMailService;
use KTXM\ProviderImap\Stores\MailboxStore;
@@ -29,6 +31,7 @@ final class MessageHarmonizationTest extends TestCase
private FakeMessageFileStore $files;
private MessageHarmonizationLiveStub $live;
private HarmonizationService $harmonizer;
private MessageDeltaService $deltas;
protected function setUp(): void
{
@@ -46,8 +49,9 @@ final class MessageHarmonizationTest extends TestCase
$this->mailboxes,
$this->messages,
$this->files,
new MessageIngestor($this->files, $this->messages),
new MessageIngestor($this->files, $this->messages, $this->mailboxes),
))->for($service, $this->live);
$this->deltas = new MessageDeltaService($this->mailboxes, $this->messages);
$this->mailboxes->upsert('tenant', 'svc', (new CollectionResource('imap', 'svc'))->fromImap(new Mailbox('INBOX', '/', [])));
}
@@ -159,6 +163,87 @@ final class MessageHarmonizationTest extends TestCase
$this->assertNull($this->mailboxes->lockOwner);
}
public function testEmptySignatureReturnsTheCurrentSignature(): void
{
$this->live->flags = [1 => [], 2 => [], 3 => []];
$this->harmonizer->harmonizeMessages('INBOX');
$delta = $this->deltas->delta('svc', 'INBOX', '');
$this->assertSame('7:3', $delta->signature);
$this->assertSame([[], [], []], $this->changeLists($delta));
}
public function testEmptySignatureWhileIncompleteStartsFromZero(): void
{
$this->live->flags = [1 => [], 2 => []];
$this->live->unfetchable = [2];
$this->harmonizer->harmonizeMessages('INBOX');
$delta = $this->deltas->delta('svc', 'INBOX', '');
$this->assertSame('7:0', $delta->signature);
$this->assertSame([['1'], [], []], $this->changeLists($this->deltas->delta('svc', 'INBOX', '7:0')));
}
public function testDeltaReportsAdditionsModificationsAndDeletions(): void
{
$this->live->flags = [1 => [], 2 => [], 3 => []];
$this->harmonizer->harmonizeMessages('INBOX');
$signature = $this->deltas->delta('svc', 'INBOX', '')->signature;
$this->live->flags = [2 => ['\\Seen'], 3 => [], 4 => []];
$this->harmonizer->harmonizeMessages('INBOX');
$delta = $this->deltas->delta('svc', 'INBOX', $signature);
$this->assertSame([['4'], ['2'], ['1']], $this->changeLists($delta));
$this->assertSame([[], [], []], $this->changeLists($this->deltas->delta('svc', 'INBOX', $delta->signature)));
}
public function testMessagesAddedSinceTheSignatureAreOnlyAdditions(): void
{
$this->live->flags = [1 => []];
$this->harmonizer->harmonizeMessages('INBOX');
$signature = $this->deltas->delta('svc', 'INBOX', '')->signature;
$this->live->flags = [1 => [], 2 => [], 3 => []];
$this->harmonizer->harmonizeMessages('INBOX');
$this->live->flags = [1 => [], 2 => ['\\Flagged']];
$this->harmonizer->harmonizeMessages('INBOX');
// 2: added then flagged -> addition; 3: added then expunged -> not reported
$this->assertSame([['2'], [], []], $this->changeLists($this->deltas->delta('svc', 'INBOX', $signature)));
}
public function testSignatureThatCannotBeAnsweredIsReset(): void
{
$this->live->flags = [1 => [], 2 => []];
$this->harmonizer->harmonizeMessages('INBOX');
$old = $this->deltas->delta('svc', 'INBOX', '')->signature;
$this->assertSame([['1', '2'], [], []], $this->changeLists($this->deltas->delta('svc', 'INBOX', 'garbage')));
$this->assertSame([['1', '2'], [], []], $this->changeLists($this->deltas->delta('svc', 'INBOX', '7:999')));
$this->mailboxes->updateState('svc', 'INBOX', ['purgedSeq' => 2]);
$this->assertSame([['1', '2'], [], []], $this->changeLists($this->deltas->delta('svc', 'INBOX', '7:1')));
$this->live->uidValidity = 8;
$this->live->flags = [5 => []];
$this->harmonizer->harmonizeMessages('INBOX');
$delta = $this->deltas->delta('svc', 'INBOX', $old);
$this->assertSame([['5'], [], []], $this->changeLists($delta));
$this->assertStringStartsWith('8:', $delta->signature);
}
public function testMailboxThatIsNotCachedHasAnEmptyDelta(): void
{
$delta = $this->deltas->delta('svc', 'Unknown', '7:1');
$this->assertSame('', $delta->signature);
$this->assertSame([[], [], []], $this->changeLists($delta));
}
public function testLockIsReleasedWhenTheRunFails(): void
{
$this->live->uidValidity = null;
@@ -171,6 +256,18 @@ final class MessageHarmonizationTest extends TestCase
$this->assertNull($this->mailboxes->lockOwner);
}
/**
* @return array{0: string[], 1: string[], 2: string[]}
*/
private function changeLists(Delta $delta): array
{
return [
json_decode(json_encode($delta->additions), true),
json_decode(json_encode($delta->modifications), true),
json_decode(json_encode($delta->deletions), true),
];
}
}
final class MessageHarmonizationLiveStub extends LiveMailService
@@ -272,37 +369,80 @@ final class FakeMailboxStore extends MailboxStore
$this->lockOwner = null;
}
}
public function reserveSequence(string $serviceId, string $name, int $count = 1): int
{
$this->documents[$name]['changeSeq'] += $count;
return $this->documents[$name]['changeSeq'] - $count + 1;
}
public function state(string $serviceId, string $name): array
{
return array_replace(self::STATE_DEFAULTS, array_intersect_key($this->documents[$name] ?? [], self::STATE_DEFAULTS));
}
}
final class FakeMessageStore extends MessageStore
{
/** @var array<int, array<int, list<string>>> flags by UIDVALIDITY and UID */
/** @var array<int, array<int, array{flags: list<string>, addedSeq: int, modSeq: int, removedSeq: ?int}>> by UIDVALIDITY and UID */
private array $documents = [];
public function upsert(string $tenantId, string $serviceId, int $uidValidity, EntityResource $entity): void
public function upsert(string $tenantId, string $serviceId, int $uidValidity, EntityResource $entity, int $seq): void
{
$meta = $entity->toCacheMeta();
$this->documents[$uidValidity][$meta['uid']] = $meta['flags'];
$existing = $this->documents[$uidValidity][$meta['uid']] ?? null;
$this->documents[$uidValidity][$meta['uid']] = [
'flags' => $meta['flags'],
'addedSeq' => $existing['addedSeq'] ?? $seq,
'modSeq' => $seq,
'removedSeq' => null,
];
ksort($this->documents[$uidValidity]);
}
public function flags(string $serviceId, string $mailbox, int $uidValidity): array
{
return $this->documents[$uidValidity] ?? [];
$flags = [];
foreach ($this->documents[$uidValidity] ?? [] as $uid => $document) {
if ($document['removedSeq'] === null) {
$flags[$uid] = $document['flags'];
}
}
return $flags;
}
public function updateFlags(string $serviceId, string $mailbox, int $uidValidity, int $uid, array $flags): void
public function updateFlags(string $serviceId, string $mailbox, int $uidValidity, int $uid, array $flags, int $seq): void
{
$this->documents[$uidValidity][$uid] = array_values($flags);
$this->documents[$uidValidity][$uid]['flags'] = array_values($flags);
$this->documents[$uidValidity][$uid]['modSeq'] = $seq;
}
public function delete(string $serviceId, string $mailbox, int $uidValidity, int ...$uids): void
public function tombstone(string $serviceId, string $mailbox, int $uidValidity, int $seq, int ...$uids): void
{
foreach ($uids as $uid) {
unset($this->documents[$uidValidity][$uid]);
$this->documents[$uidValidity][$uid]['removedSeq'] = $seq;
$this->documents[$uidValidity][$uid]['modSeq'] = $seq;
$this->documents[$uidValidity][$uid]['flags'] = [];
}
}
public function changes(string $serviceId, string $mailbox, int $uidValidity, int $since): array
{
$changes = ['additions' => [], 'modifications' => [], 'deletions' => []];
foreach ($this->documents[$uidValidity] ?? [] as $uid => $document) {
if ($document['removedSeq'] !== null) {
if ($document['addedSeq'] <= $since && $document['removedSeq'] > $since) {
$changes['deletions'][] = $uid;
}
} elseif ($document['addedSeq'] > $since) {
$changes['additions'][] = $uid;
} elseif ($document['modSeq'] > $since) {
$changes['modifications'][] = $uid;
}
}
return $changes;
}
public function deleteByMailbox(string $serviceId, string $mailbox): void
{
$this->documents = [];