]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiStore.pm
doc: re-add missing 1.6 release notes
[public-inbox.git] / lib / PublicInbox / LeiStore.pm
index 553adbc8f84d4cdf4b79935330098ddce0d28203..a7d7d95326a15d624fcedfb682594ef73fef7743 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Local storage (cache/memo) for lei(1), suitable for personal/private
@@ -9,14 +9,13 @@
 package PublicInbox::LeiStore;
 use strict;
 use v5.10.1;
-use parent qw(PublicInbox::Lock);
-use PublicInbox::SearchIdx qw(crlf_adjust);
+use parent qw(PublicInbox::Lock PublicInbox::IPC);
 use PublicInbox::ExtSearchIdx;
 use PublicInbox::Import;
 use PublicInbox::InboxWritable;
 use PublicInbox::V2Writable;
-use PublicInbox::ContentHash qw(content_hash);
-use PublicInbox::MID qw(mids);
+use PublicInbox::ContentHash qw(content_hash content_digest);
+use PublicInbox::MID qw(mids mids_in);
 use PublicInbox::LeiSearch;
 use List::Util qw(max);
 
@@ -24,10 +23,7 @@ sub new {
        my (undef, $dir, $opt) = @_;
        my $eidx = PublicInbox::ExtSearchIdx->new($dir, $opt);
        my $self = bless { priv_eidx => $eidx }, __PACKAGE__;
-       if ($opt->{creat}) {
-               PublicInbox::SearchIdx::load_xapian_writable();
-               eidx_init($self);
-       }
+       eidx_init($self)->done if $opt->{creat};
        $self;
 }
 
@@ -78,8 +74,8 @@ sub importer {
        while (1) {
                my $latest = "$pfx/$max.git";
                my $old = -e $latest;
+               PublicInbox::Import::init_bare($latest);
                my $git = PublicInbox::Git->new($latest);
-               PublicInbox::Import::init_bare({ git => $git });
                $git->qx(qw(config core.sharedRepository 0600)) if !$old;
                my $packed_bytes = $git->packed_bytes;
                my $unpacked_bytes = $packed_bytes / $self->packing_factor;
@@ -107,14 +103,26 @@ sub eidx_init {
        $eidx;
 }
 
+# when a message has no Message-IDs at all, this is needed for
+# unsent Draft messages, at least
+sub _fake_mid_for ($$) {
+       my ($eml, $dig) = @_;
+       my $mids = mids_in($eml, qw(X-Alt-Message-ID Resent-Message-ID));
+       $eml->{-lei_fake_mid} =
+               $mids->[0] // PublicInbox::Import::digest2mid($dig, $eml);
+}
+
 sub _docids_for ($$) {
        my ($self, $eml) = @_;
        my %docids;
-       my $chash = content_hash($eml);
+       my $dig = content_digest($eml);
+       my $chash = $dig->clone->digest;
        my $eidx = eidx_init($self);
        my $oidx = $eidx->{oidx};
        my $im = $self->{im};
-       for my $mid (@{mids($eml)}) {
+       my $mids = mids($eml);
+       $mids->[0] //= _fake_mid_for($eml, $dig);
+       for my $mid (@$mids) {
                my ($id, $prev);
                while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
                        my $oid = $cur->{blob};
@@ -137,7 +145,7 @@ sub set_eml_keywords {
        my $eidx = eidx_init($self);
        my @docids = _docids_for($self, $eml);
        for my $docid (@docids) {
-               $eidx->idx_shard($docid)->shard_set_keywords($docid, @kw);
+               $eidx->idx_shard($docid)->ipc_do('set_keywords', $docid, @kw);
        }
        \@docids;
 }
@@ -147,7 +155,7 @@ sub add_eml_keywords {
        my $eidx = eidx_init($self);
        my @docids = _docids_for($self, $eml);
        for my $docid (@docids) {
-               $eidx->idx_shard($docid)->shard_add_keywords($docid, @kw);
+               $eidx->idx_shard($docid)->ipc_do('add_keywords', $docid, @kw);
        }
        \@docids;
 }
@@ -157,7 +165,7 @@ sub remove_eml_keywords {
        my $eidx = eidx_init($self);
        my @docids = _docids_for($self, $eml);
        for my $docid (@docids) {
-               $eidx->idx_shard($docid)->shard_remove_keywords($docid, @kw);
+               $eidx->idx_shard($docid)->ipc_do('remove_keywords', $docid, @kw)
        }
        \@docids;
 }
@@ -188,26 +196,31 @@ sub add_eml {
        my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
        my $im = $self->importer;
        $im->add($eml, undef, $smsg) or return; # duplicate returns undef
-       my $msgref = delete $smsg->{-raw_email};
-       $smsg->{bytes} = $smsg->{raw_bytes} + crlf_adjust($$msgref);
 
        local $self->{current_info} = $smsg->{blob};
        if (my @docids = _docids_for($self, $eml)) {
                for my $docid (@docids) {
                        my $idx = $eidx->idx_shard($docid);
                        $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
-                       $idx->shard_add_eidx_info($docid, '.', $eml); # List-Id
-                       $idx->shard_add_keywords($docid, @kw) if @kw;
+                       # add_eidx_info for List-Id
+                       $idx->ipc_do('add_eidx_info', $docid, '.', $eml);
+                       $idx->ipc_do('add_keywords', $docid, @kw) if @kw;
                }
+               \@docids;
        } else {
                $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
                $oidx->add_overview($eml, $smsg);
                $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
                my $idx = $eidx->idx_shard($smsg->{num});
-               $idx->index_raw($msgref, $eml, $smsg);
-               $idx->shard_add_keywords($smsg->{num}, @kw) if @kw;
+               $idx->index_eml($eml, $smsg);
+               $idx->ipc_do('add_keywords', $smsg->{num}, @kw) if @kw;
+               $smsg;
        }
-       $smsg->{blob}
+}
+
+sub set_eml {
+       my ($self, $eml, @kw) = @_;
+       add_eml($self, $eml, @kw) // set_eml_keywords($self, $eml, @kw);
 }
 
 sub done {