diff options
Diffstat (limited to 'lib/MaildirIndexer/Store.rakumod')
-rw-r--r-- | lib/MaildirIndexer/Store.rakumod | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/MaildirIndexer/Store.rakumod b/lib/MaildirIndexer/Store.rakumod new file mode 100644 index 0000000..502d60e --- /dev/null +++ b/lib/MaildirIndexer/Store.rakumod @@ -0,0 +1,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.path) or return; + my $email = parse-email($file,:headers-only) or return; + CATCH { warn $_ }; + $!lock.protect: { + .add-mail($email,$mailbox) for @!indices; + } + return; +} + +method del-file(IO:D $file --> Nil) { + 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 --> 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; +} |