]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
index: allow search/lookups on X-Alt-Message-ID
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index 8810fe76450dde64728d715e4e63f14b3c6bb741..b2d71a1f88b462493d3db527c609d00f4e16b31b 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # based on notmuch, but with no concept of folders, files or flags
 #
@@ -12,14 +12,13 @@ use warnings;
 use base qw(PublicInbox::Search PublicInbox::Lock);
 use PublicInbox::MIME;
 use PublicInbox::InboxWritable;
-use PublicInbox::MID qw/mid_clean id_compress mid_mime mids/;
+use PublicInbox::MID qw/mid_clean id_compress mid_mime mids_for_index/;
 use PublicInbox::MsgIter;
 use Carp qw(croak);
 use POSIX qw(strftime);
 use PublicInbox::OverIdx;
 use PublicInbox::Spawn qw(spawn);
-require PublicInbox::Git;
-use Compress::Zlib qw(compress);
+use PublicInbox::Git qw(git_unquote);
 
 use constant {
        BATCH_BYTES => defined($ENV{XAPIAN_FLUSH_THRESHOLD}) ?
@@ -29,57 +28,28 @@ use constant {
 
 my $xapianlevels = qr/\A(?:full|medium)\z/;
 
-my %GIT_ESC = (
-       a => "\a",
-       b => "\b",
-       f => "\f",
-       n => "\n",
-       r => "\r",
-       t => "\t",
-       v => "\013",
-);
-
-sub git_unquote ($) {
-       my ($s) = @_;
-       return $s unless ($s =~ /\A"(.*)"\z/);
-       $s = $1;
-       $s =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g;
-       $s =~ s/\\([0-7]{1,3})/chr(oct($1))/ge;
-       $s;
-}
-
 sub new {
-       my ($class, $ibx, $creat, $part) = @_;
+       my ($class, $ibx, $creat, $shard) = @_;
+       ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
        my $levels = qr/\A(?:full|medium|basic)\z/;
-       my $mainrepo = $ibx; # for "public-inbox-index" w/o entry in config
-       my $git_dir = $mainrepo;
-       my ($altid, $git);
-       my $version = 1;
+       my $inboxdir = $ibx->{inboxdir};
+       my $version = $ibx->{version} || 1;
        my $indexlevel = 'full';
-       if (ref $ibx) {
-               $mainrepo = $ibx->{mainrepo};
-               $altid = $ibx->{altid};
-               $version = $ibx->{version} || 1;
-               if ($altid) {
-                       require PublicInbox::AltId;
-                       $altid = [ map {
-                               PublicInbox::AltId->new($ibx, $_);
-                       } @$altid ];
-               }
-               if ($ibx->{indexlevel}) {
-                       if ($ibx->{indexlevel} =~ $levels) {
-                               $indexlevel = $ibx->{indexlevel};
-                       } else {
-                               die("Invalid indexlevel $ibx->{indexlevel}\n");
-                       }
+       my $altid = $ibx->{altid};
+       if ($altid) {
+               require PublicInbox::AltId;
+               $altid = [ map { PublicInbox::AltId->new($ibx, $_); } @$altid ];
+       }
+       if ($ibx->{indexlevel}) {
+               if ($ibx->{indexlevel} =~ $levels) {
+                       $indexlevel = $ibx->{indexlevel};
+               } else {
+                       die("Invalid indexlevel $ibx->{indexlevel}\n");
                }
-       } else { # v1
-               $ibx = { mainrepo => $git_dir, version => 1 };
        }
        $ibx = PublicInbox::InboxWritable->new($ibx);
-       require Search::Xapian::WritableDatabase;
        my $self = bless {
-               mainrepo => $mainrepo,
+               inboxdir => $inboxdir,
                -inbox => $ibx,
                git => $ibx->git,
                -altid => $altid,
@@ -88,13 +58,13 @@ sub new {
        }, $class;
        $ibx->umask_prepare;
        if ($version == 1) {
-               $self->{lock_path} = "$mainrepo/ssoma.lock";
+               $self->{lock_path} = "$inboxdir/ssoma.lock";
                my $dir = $self->xdir;
                $self->{over} = PublicInbox::OverIdx->new("$dir/over.sqlite3");
        } elsif ($version == 2) {
-               defined $part or die "partition is required for v2\n";
-               # partition is a number
-               $self->{partition} = $part;
+               defined $shard or die "shard is required for v2\n";
+               # shard is a number
+               $self->{shard} = $shard;
                $self->{lock_path} = undef;
        } else {
                die "unsupported inbox version=$version\n";
@@ -103,26 +73,46 @@ sub new {
        $self;
 }
 
+sub need_xapian ($) { $_[0]->{indexlevel} =~ $xapianlevels }
+
 sub _xdb_release {
        my ($self) = @_;
-       my $xdb = delete $self->{xdb} or croak 'not acquired';
-       $xdb->close;
+       if (need_xapian($self)) {
+               my $xdb = delete $self->{xdb} or croak 'not acquired';
+               $xdb->close;
+       }
        $self->lock_release if $self->{creat};
        undef;
 }
 
 sub _xdb_acquire {
        my ($self) = @_;
-       croak 'already acquired' if $self->{xdb};
+       my $flag;
        my $dir = $self->xdir;
-       my $flag = Search::Xapian::DB_OPEN;
+       if (need_xapian($self)) {
+               croak 'already acquired' if $self->{xdb};
+               PublicInbox::Search::load_xapian();
+               require Search::Xapian::WritableDatabase;
+               $flag = $self->{creat} ?
+                       Search::Xapian::DB_CREATE_OR_OPEN() :
+                       Search::Xapian::DB_OPEN();
+       }
        if ($self->{creat}) {
                require File::Path;
                $self->lock_acquire;
-               File::Path::mkpath($dir);
-               $flag = Search::Xapian::DB_CREATE_OR_OPEN;
+
+               # don't create empty Xapian directories if we don't need Xapian
+               my $is_shard = defined($self->{shard});
+               if (!$is_shard || ($is_shard && need_xapian($self))) {
+                       File::Path::mkpath($dir);
+               }
+       }
+       return unless defined $flag;
+       my $xdb = eval { Search::Xapian::WritableDatabase->new($dir, $flag) };
+       if ($@) {
+               die "Failed opening $dir: ", $@;
        }
-       $self->{xdb} = Search::Xapian::WritableDatabase->new($dir, $flag);
+       $self->{xdb} = $xdb;
 }
 
 sub add_val ($$$) {
@@ -354,14 +344,14 @@ sub add_xapian ($$$$$) {
 sub add_message {
        # mime = Email::MIME object
        my ($self, $mime, $bytes, $num, $oid, $mid0) = @_;
-       my $mids = mids($mime->header_obj);
+       my $mids = mids_for_index($mime->header_obj);
        $mid0 = $mids->[0] unless defined $mid0; # v1 compatibility
        unless (defined $num) { # v1
                $self->_msgmap_init;
                $num = index_mm($self, $mime);
        }
        eval {
-               if ($self->{indexlevel} =~ $xapianlevels) {
+               if (need_xapian($self)) {
                        $self->add_xapian($mime, $num, $oid, $mids, $mid0)
                }
                if (my $over = $self->{over}) {
@@ -384,6 +374,7 @@ sub find_doc_ids {
        ($db->postlist_begin($termval), $db->postlist_end($termval));
 }
 
+# v1 only
 sub batch_do {
        my ($self, $termval, $cb) = @_;
        my $batch_size = 1000; # don't let @ids grow too large to avoid OOM
@@ -398,35 +389,45 @@ sub batch_do {
        }
 }
 
+# v1 only, where $mid is unique
 sub remove_message {
        my ($self, $mid) = @_;
-       my $db = $self->{xdb};
-       my $called;
        $mid = mid_clean($mid);
-       my $over = $self->{over};
 
+       if (my $over = $self->{over}) {
+               my $nr = eval { $over->remove_oid(undef, $mid) };
+               if ($@) {
+                       warn "failed to remove <$mid> from overview: $@\n";
+               } elsif ($nr == 0) {
+                       warn "<$mid> missing for removal from overview\n";
+               }
+       }
+       return unless need_xapian($self);
+       my $db = $self->{xdb};
+       my $nr = 0;
        eval {
                batch_do($self, 'Q' . $mid, sub {
                        my ($ids) = @_;
                        $db->delete_document($_) for @$ids;
-                       $over->delete_articles($ids) if $over;
-                       $called = 1;
+                       $nr = scalar @$ids;
                });
        };
        if ($@) {
-               warn "failed to remove message <$mid>: $@\n";
-       } elsif (!$called) {
-               warn "cannot remove non-existent <$mid>\n";
+               warn "failed to remove <$mid> from Xapian: $@\n";
+       } elsif ($nr == 0) {
+               warn "<$mid> missing for removal from Xapian\n";
        }
 }
 
 # MID is a hint in V2
 sub remove_by_oid {
        my ($self, $oid, $mid) = @_;
-       my $db = $self->{xdb};
 
        $self->{over}->remove_oid($oid, $mid) if $self->{over};
 
+       return unless need_xapian($self);
+       my $db = $self->{xdb};
+
        # XXX careful, we cannot use batch_do here since we conditionally
        # delete documents based on other factors, so we cannot call
        # find_doc_ids twice.
@@ -536,17 +537,19 @@ sub do_cat_mail {
        $@ ? undef : $mime;
 }
 
+# called by public-inbox-index
 sub index_sync {
        my ($self, $opts) = @_;
+       delete $self->{lock_path} if $opts->{-skip_lock};
        $self->{-inbox}->with_umask(sub { $self->_index_sync($opts) })
 }
 
-sub batch_adjust ($$$$) {
-       my ($max, $bytes, $batch_cb, $latest) = @_;
+sub batch_adjust ($$$$$) {
+       my ($max, $bytes, $batch_cb, $latest, $nr) = @_;
        $$max -= $bytes;
        if ($$max <= 0) {
                $$max = BATCH_BYTES;
-               $batch_cb->($latest);
+               $batch_cb->($nr, $latest);
        }
 }
 
@@ -565,6 +568,7 @@ sub read_log {
        my %D;
        my $line;
        my $newest;
+       my $nr = 0;
        while (defined($line = <$log>)) {
                if ($line =~ /$addmsg/o) {
                        my $blob = $1;
@@ -576,7 +580,7 @@ sub read_log {
                                next;
                        }
                        my $mime = do_cat_mail($git, $blob, \$bytes) or next;
-                       batch_adjust(\$max, $bytes, $batch_cb, $latest);
+                       batch_adjust(\$max, $bytes, $batch_cb, $latest, ++$nr);
                        $add_cb->($self, $mime, $bytes, $blob);
                } elsif ($line =~ /$delmsg/o) {
                        my $blob = $1;
@@ -591,7 +595,7 @@ sub read_log {
                my $mime = do_cat_mail($git, $blob, \$bytes) or next;
                $del_cb->($self, $mime);
        }
-       $batch_cb->($latest, $newest);
+       $batch_cb->($nr, $latest, $newest);
 }
 
 sub _msgmap_init {
@@ -599,12 +603,12 @@ sub _msgmap_init {
        die "BUG: _msgmap_init is only for v1\n" if $self->{version} != 1;
        $self->{mm} ||= eval {
                require PublicInbox::Msgmap;
-               PublicInbox::Msgmap->new($self->{mainrepo}, 1);
+               PublicInbox::Msgmap->new($self->{inboxdir}, 1);
        };
 }
 
 sub _git_log {
-       my ($self, $range) = @_;
+       my ($self, $opts, $range) = @_;
        my $git = $self->{git};
 
        if (index($range, '..') < 0) {
@@ -621,12 +625,17 @@ sub _git_log {
        # Count the new files so they can be added newest to oldest
        # and still have numbers increasing from oldest to newest
        my $fcount = 0;
+       my $pr = $opts->{-progress};
+       $pr->("counting changes\n\t$range ... ") if $pr;
        # can't use 'rev-list --count' if we use --diff-filter
        my $fh = $git->popen(qw(log --pretty=tformat:%h
                             --no-notes --no-color --no-renames
                             --diff-filter=AM), $range);
        ++$fcount while <$fh>;
+       close $fh;
        my $high = $self->{mm}->num_highwater;
+       $pr->("$fcount\n") if $pr; # continue previous line
+       $self->{ntodo} = $fcount;
 
        if (index($range, '..') < 0) {
                if ($high && $high == $fcount) {
@@ -667,50 +676,64 @@ sub need_update ($$$) {
        ($n eq '' || $n > 0);
 }
 
+# The last git commit we indexed with Xapian or SQLite (msgmap)
+# This needs to account for cases where Xapian or SQLite is
+# out-of-date with respect to the other.
+sub _last_x_commit {
+       my ($self, $mm) = @_;
+       my $lm = $mm->last_commit || '';
+       my $lx = '';
+       if (need_xapian($self)) {
+               $lx = $self->{xdb}->get_metadata('last_commit') || '';
+       } else {
+               $lx = $lm;
+       }
+       # Use last_commit from msgmap if it is older or unset
+       if (!$lm || ($lx && $lm && is_ancestor($self->{git}, $lm, $lx))) {
+               $lx = $lm;
+       }
+       $lx;
+}
+
+sub reindex_from ($$) {
+       my ($reindex, $last_commit) = @_;
+       return $last_commit unless $reindex;
+       ref($reindex) eq 'HASH' ? $reindex->{from} : '';
+}
+
 # indexes all unindexed messages (v1 only)
 sub _index_sync {
        my ($self, $opts) = @_;
        my $tip = $opts->{ref} || 'HEAD';
-       my $reindex = $opts->{reindex};
-       my ($mkey, $last_commit, $lx, $xlog);
+       my ($last_commit, $lx, $xlog);
        my $git = $self->{git};
        $git->batch_prepare;
+       my $pr = $opts->{-progress};
 
        my $xdb = $self->begin_txn_lazy;
        my $mm = _msgmap_init($self);
        do {
                $xlog = undef;
-               $mkey = 'last_commit';
-               $last_commit = $xdb->get_metadata('last_commit');
-               $lx = $last_commit;
-               if ($reindex) {
-                       $lx = '';
-                       $mkey = undef if $last_commit ne '';
-               }
-
-               # use last_commit from msgmap if it is older or unset
-               my $lm = $mm->last_commit || '';
-               if (!$lm || ($lm && $lx && is_ancestor($git, $lm, $lx))) {
-                       $lx = $lm;
-               }
+               $last_commit = _last_x_commit($self, $mm);
+               $lx = reindex_from($opts->{reindex}, $last_commit);
 
                $self->{over}->rollback_lazy;
                $self->{over}->disconnect;
                $git->cleanup;
                delete $self->{txn};
-               $xdb->cancel_transaction;
+               $xdb->cancel_transaction if $xdb;
                $xdb = _xdb_release($self);
 
                # ensure we leak no FDs to "git log" with Xapian <= 1.2
                my $range = $lx eq '' ? $tip : "$lx..$tip";
-               $xlog = _git_log($self, $range);
+               $xlog = _git_log($self, $opts, $range);
 
                $xdb = $self->begin_txn_lazy;
-       } while ($xdb->get_metadata('last_commit') ne $last_commit);
+       } while (_last_x_commit($self, $mm) ne $last_commit);
 
        my $dbh = $mm->{dbh} if $mm;
        my $cb = sub {
-               my ($commit, $newest) = @_;
+               my ($nr, $commit, $newest) = @_;
                if ($dbh) {
                        if ($newest) {
                                my $cur = $mm->last_commit || '';
@@ -720,16 +743,17 @@ sub _index_sync {
                        }
                        $dbh->commit;
                }
-               if ($mkey && $newest && $self->{indexlevel} =~ $xapianlevels) {
-                       my $cur = $xdb->get_metadata($mkey);
+               if ($newest && need_xapian($self)) {
+                       my $cur = $xdb->get_metadata('last_commit');
                        if (need_update($self, $cur, $newest)) {
-                               $xdb->set_metadata($mkey, $newest);
+                               $xdb->set_metadata('last_commit', $newest);
                        }
                }
                $self->commit_txn_lazy;
                $git->cleanup;
                $xdb = _xdb_release($self);
                # let another process do some work... <
+               $pr->("indexed $nr/$self->{ntodo}\n") if $pr && $nr;
                if (!$newest) {
                        $xdb = $self->begin_txn_lazy;
                        $dbh->begin_work if $dbh;
@@ -773,7 +797,7 @@ sub remote_close {
 sub remote_remove {
        my ($self, $oid, $mid) = @_;
        if (my $w = $self->{w}) {
-               # triggers remove_by_oid in a partition
+               # triggers remove_by_oid in a shard
                print $w "D $oid $mid\n" or die "failed to write remove $!";
        } else {
                $self->begin_txn_lazy;
@@ -788,7 +812,7 @@ sub begin_txn_lazy {
        $self->{-inbox}->with_umask(sub {
                my $xdb = $self->{xdb} || $self->_xdb_acquire;
                $self->{over}->begin_lazy if $self->{over};
-               $xdb->begin_transaction;
+               $xdb->begin_transaction if $xdb;
                $self->{txn} = 1;
                $xdb;
        });
@@ -798,14 +822,27 @@ sub commit_txn_lazy {
        my ($self) = @_;
        delete $self->{txn} or return;
        $self->{-inbox}->with_umask(sub {
-               $self->{xdb}->commit_transaction;
+               if (my $xdb = $self->{xdb}) {
+
+                       # store 'indexlevel=medium' in v2 shard=0 and
+                       # v1 (only one shard)
+                       # This metadata is read by Admin::detect_indexlevel:
+                       if (!$self->{shard} # undef or 0, not >0
+                           && $self->{indexlevel} eq 'medium') {
+                               $xdb->set_metadata('indexlevel', 'medium');
+                       }
+
+                       $xdb->commit_transaction;
+               }
                $self->{over}->commit_lazy if $self->{over};
        });
 }
 
 sub worker_done {
        my ($self) = @_;
-       die "$$ $0 xdb not released\n" if $self->{xdb};
+       if (need_xapian($self)) {
+               die "$$ $0 xdb not released\n" if $self->{xdb};
+       }
        die "$$ $0 still in transaction\n" if $self->{txn};
 }