use v6.d; use MaildirIndexer::Index; unit class MaildirIndexer::Index::ByRef does MaildirIndexer::Index; use MaildirIndexer::LogTimelineSchema; use MaildirIndexer::Email; has Str %!id-for-file; has SetHash %!mailboxes-for-id; method dump() { say "{.key} → {.value}" for %!id-for-file; say "{.key} ⇒ {.value.perl}" for %!mailboxes-for-id; } method add-mail(MaildirIndexer::Email:D $email, Str:D $mailbox --> Nil) { MaildirIndexer::LogTimelineSchema::Index::Add.log: :class('ByRef'),:$mailbox, -> { # ignore adding the same file twice, files in maildirs are # immutable return if %!id-for-file{ $email.path }:exists; my $id = $email.message-id or return; %!id-for-file{ $email.path } = $id; %!mailboxes-for-id{ $id }{$mailbox}=1; return; } } method del-path(IO:D $file, Str:D $mailbox --> Nil) { MaildirIndexer::LogTimelineSchema::Index::Rm.log: :class('ByRef'),:$mailbox, -> { my $id = %!id-for-file{ $file.path }:delete or return; with %!mailboxes-for-id{ $id } -> $boxes { $boxes{$mailbox}:delete; } return; } } method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { my Str $result; MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByRef'), -> { for |$email.refs() -> $ref { with %!mailboxes-for-id{$ref} { $result = .keys.sort.[0] } } } return $result; }