diff options
Diffstat (limited to 'lib/MaildirIndexer/Store.pm6')
-rw-r--r-- | lib/MaildirIndexer/Store.pm6 | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/lib/MaildirIndexer/Store.pm6 b/lib/MaildirIndexer/Store.pm6 index e54fa8f..5c95250 100644 --- a/lib/MaildirIndexer/Store.pm6 +++ b/lib/MaildirIndexer/Store.pm6 @@ -1,45 +1,58 @@ -use v6.d.PREVIEW; +use v6.d; unit class MaildirIndexer::Store; +use MaildirIndexer::Index; +use MaildirIndexer::ScanDir; use MaildirIndexer::Parser; -has %!id-for-file; -has %!mailboxes-for-id; -has $!lock = Lock.new; +has Lock $!lock .= new; +has MaildirIndexer::Index @.indices is required; +has Str $.maildir is required; +has Int $.workers = 10; method dump() { $!lock.protect: { - say "{.key} → {.value}" for %!id-for-file; - say "{.key} ⇒ {.value.perl}" for %!mailboxes-for-id; + .dump() for @!indices; } } -method add-file(IO $file) { +method start() { + my $file-supply = scan-dir($.maildir); + my $file-channel = $file-supply.Channel; + + 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 $id = $email.message-id or return; my $mailbox = mailbox-from-path($file.path) or return; $!lock.protect: { - %!id-for-file{ $file.path } = $id; - %!mailboxes-for-id{ $id }.push($mailbox); - }; + .add-mail($email,$mailbox) for @!indices; + } return; } -method del-file(IO $file) { +method del-file(IO:D $file) { my $mailbox = mailbox-from-path($file.path) or return; $!lock.protect: { - my $id = %!id-for-file{ $file.path }:delete; - with %!mailboxes-for-id{ $id } { - with .grep($mailbox):k -> $pos { - .splice($pos,1); - } - } + .del-path($file,$mailbox) for @!indices; } return; } -method mailbox-for-email(MaildirIndexer::Email $email) { - for |$email.refs() -> $ref { - with %!mailboxes-for-id{$ref} { return .[*-1] } +method mailbox-for-email(MaildirIndexer::Email:D $email) { + for @!indices -> $index { + with $index.mailbox-for-email($email) { return $_ }; } return Nil; } |