use v6.d; unit class MaildirIndexer::Store; 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() { $!lock.protect: { .dump() for @!indices; } } method start() { for ^10 { start react { whenever $.file-channel -> $file { if $file.e && $file.f { self.add-file($file); } elsif !$file.e { self.del-file($file); } } } } } method add-file(IO:D $file) { my $email = parse-email($file,:headers-only); my $mailbox = mailbox-from-path($file.path) or return; $!lock.protect: { .add-mail($email,$mailbox) for @!indices; } return; } method del-file(IO:D $file) { my $mailbox = mailbox-from-path($file.path) or return; $!lock.protect: { .del-path($file,$mailbox) for @!indices; } return; } method mailbox-for-email(MaildirIndexer::Email:D $email) { for @!indices -> $index { with $index.mailbox-for-email($email) { return $_ }; } return Nil; } sub mailbox-from-path(Str() $path) { $path ~~ m{'/' (<-[/]>+?) '/' [cur|new|tmp] '/'} and return ~$/[0]; return Nil; }