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;
}