summaryrefslogtreecommitdiff
path: root/lib/MaildirIndexer/Index/ByRef.rakumod
blob: f9fc4f9e6c50b7f37da8a2206816309a5a9b697e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 $emailStr: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 }.push($mailbox);
        return;
    }
}
 
method del-path(IO:D $fileStr: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 } {
            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;
}