diff options
Diffstat (limited to 'lib/MaildirIndexer/Index/ByRef.rakumod')
-rw-r--r-- | lib/MaildirIndexer/Index/ByRef.rakumod | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/MaildirIndexer/Index/ByRef.rakumod b/lib/MaildirIndexer/Index/ByRef.rakumod new file mode 100644 index 0000000..d044272 --- /dev/null +++ b/lib/MaildirIndexer/Index/ByRef.rakumod @@ -0,0 +1,44 @@ +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 Array[Str] %!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, -> { + my $id = $email.message-id or return; + %!id-for-file{ $email.path } = $id; + %!mailboxes-for-id{ $id }.push($mailbox); + 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; + with %!mailboxes-for-id{ $id } { + with .grep($mailbox):k -> $pos { + .splice($pos,1); + } + } + 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 = .[*-1] } + } + } + return $result; +} |