summaryrefslogtreecommitdiff
path: root/lib/MaildirIndexer/Store.pm6
blob: 502d60e17e6d46b52204636d5511663d23366664 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use v6.d;
unit class MaildirIndexer::Store;
use MaildirIndexer::LogTimelineSchema;
use MaildirIndexer::Index;
use MaildirIndexer::Parser;
 
has Lock $!lock .= new;
has MaildirIndexer::Index @.indices is required;
has Channel $.file-channel is required;
has Int $.workers = 10;
 
method dump(--> Nil{
    $!lock.protect: {
        .dump() for @!indices;
    }
}
 
method start(--> Nil{
    for ^$.workers {
        start react {
            CATCH { warn $_ };
            whenever $.file-channel -> $file {
                if $file.e && $file.f {
                    MaildirIndexer::LogTimelineSchema::Store::Add.log: :file($file.path), -> {
                        self.add-file($file);
                    }
                }
                elsif !$file.e {
                    MaildirIndexer::LogTimelineSchema::Store::Rm.log: :file($file.path), -> {
                        self.del-file($file);
                    }
                }
            }
        }
    }
}
 
method add-file(IO:D $file --> Nil{
    my $mailbox = mailbox-from-path($file.pathor return;
    my $email = parse-email($file,:headers-onlyor return;
    CATCH { warn $_ };
    $!lock.protect: {
        .add-mail($email,$mailboxfor @!indices;
    }
    return;
}
 
method del-file(IO:D $file --> Nil{
    my $mailbox = mailbox-from-path($file.pathor return;
    $!lock.protect: {
        .del-path($file,$mailboxfor @!indices;
    }
    return;
}
 
method mailbox-for-email(MaildirIndexer::Email:D $email --> Str{
    my Str $result;
    MaildirIndexer::LogTimelineSchema::Store::Find.log: {
        for @!indices -> $index {
            with $index.mailbox-for-email($email{ $result = $_last };
        }
    }
    return $result;
}
 
sub mailbox-from-path(Str() $path --> Str{
    $path ~~ m{'/' (<-[/]>+?'/' [cur|new|tmp] '/'} and return ~$/[0];
    return Nil;
}