diff options
Diffstat (limited to 'lib/MaildirIndexer/Store.pm6')
-rw-r--r-- | lib/MaildirIndexer/Store.pm6 | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/MaildirIndexer/Store.pm6 b/lib/MaildirIndexer/Store.pm6 new file mode 100644 index 0000000..cbfcfa1 --- /dev/null +++ b/lib/MaildirIndexer/Store.pm6 @@ -0,0 +1,43 @@ +use v6.d.PREVIEW; +unit class MaildirIndexer::Store; +use MaildirIndexer::Parser; + +has %!id-for-file; +has %!mailboxes-for-id; +has $!lock = Lock.new; + +method dump() { + $!lock.protect: { + say "{.key} → {.value}" for %!id-for-file; + say "{.key} ⇒ {.value.perl}" for %!mailboxes-for-id; + } +} + +method add-file(IO $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); + }; + return; +} + +method del-file(IO $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); + } +} + } + return; +} + +sub mailbox-from-path(Str() $path) { + $path ~~ m{'/' (<-[/]>+?) '/' [cur|new|tmp] '/'} and return ~$/[0]; + return Nil; +} |