]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
triewyde: ficks soem speling errrors
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index 261deb84fd67fbdf5d2e8aa628d01caa6e90539c..05689941e0b80bdaed9c6caca89c18846f57c5ea 100644 (file)
@@ -58,6 +58,7 @@ sub new {
                ibx_ver => $version,
                indexlevel => $indexlevel,
        }, $class;
+       $self->{-set_indexlevel_once} = 1 if $indexlevel eq 'medium';
        $ibx->umask_prepare;
        if ($version == 1) {
                $self->{lock_path} = "$inboxdir/ssoma.lock";
@@ -274,22 +275,8 @@ sub index_diff ($$$) {
        index_text($self, join("\n", @xnq), 1, 'XNQ');
 }
 
-sub index_body ($$$) {
-       my ($self, $txt, $doc) = @_;
-       if ($doc) {
-               # does it look like a diff?
-               if ($txt =~ /^(?:diff|---|\+\+\+) /ms) {
-                       index_diff($self, $txt, $doc);
-               } else {
-                       index_text($self, $txt, 1, 'XNQ');
-               }
-       } else {
-               index_text($self, $txt, 0, 'XQUOT');
-       }
-}
-
 sub index_xapian { # msg_iter callback
-       my ($part, $depth, @idx) = @{$_[0]};
+       my $part = $_[0]->[0]; # ignore $depth and @idx
        my ($self, $doc) = @{$_[1]};
        my $ct = $part->content_type || 'text/plain';
        my $fn = $part->filename;
@@ -299,19 +286,30 @@ sub index_xapian { # msg_iter callback
 
        my ($s, undef) = msg_part_text($part, $ct);
        defined $s or return;
+       $_[0]->[0] = $part = undef; # free memory
 
        # split off quoted and unquoted blocks:
-       my @sections = split(/((?:^>[^\n]*\n)+)/sm, $s);
-       $part = $s = undef;
-       index_body($self, $_, /\A>/ ? 0 : $doc) for @sections;
+       my @sections = PublicInbox::MsgIter::split_quotes($s);
+       undef $s; # free memory
+       for my $txt (@sections) {
+               if ($txt =~ /\A>/) {
+                       index_text($self, $txt, 0, 'XQUOT');
+               } else {
+                       # does it look like a diff?
+                       if ($txt =~ /^(?:diff|---|\+\+\+) /ms) {
+                               index_diff($self, $txt, $doc);
+                       } else {
+                               index_text($self, $txt, 1, 'XNQ');
+                       }
+               }
+               undef $txt; # free memory
+       }
 }
 
-sub add_xapian ($$$$$$) {
-       my ($self, $mime, $num, $oid, $mids, $mid0) = @_;
-       my $smsg = PublicInbox::SearchMsg->new($mime);
+sub add_xapian ($$$$) {
+       my ($self, $mime, $smsg, $mids) = @_;
+       $smsg->{mime} = $mime; # XXX dangerous
        my $hdr = $mime->header_obj;
-       $smsg->{ds} = msg_datestamp($hdr, $self->{autime});
-       $smsg->{ts} = msg_timestamp($hdr, $self->{cotime});
        my $doc = $X->{Document}->new;
        my $subj = $smsg->subject;
        add_val($doc, PublicInbox::Search::TS(), $smsg->{ts});
@@ -338,9 +336,9 @@ sub add_xapian ($$$$$$) {
                        index_text($self, join(' ', @long), 1, 'XM');
                }
        }
-       $smsg->{to} = $smsg->{cc} = '';
-       PublicInbox::OverIdx::parse_references($smsg, $mid0, $mids);
-       my $data = $smsg->to_doc_data($oid, $mid0);
+       $smsg->{to} = $smsg->{cc} = ''; # WWW doesn't need these, only NNTP
+       PublicInbox::OverIdx::parse_references($smsg, $hdr, $mids);
+       my $data = $smsg->to_doc_data;
        $doc->set_data($data);
        if (my $altid = $self->{-altid}) {
                foreach my $alt (@$altid) {
@@ -353,7 +351,7 @@ sub add_xapian ($$$$$$) {
                }
        }
        $doc->add_boolean_term('Q' . $_) foreach @$mids;
-       $self->{xdb}->replace_document($num, $doc);
+       $self->{xdb}->replace_document($smsg->{num}, $doc);
 }
 
 sub _msgmap_init ($) {
@@ -367,20 +365,31 @@ sub _msgmap_init ($) {
 
 sub add_message {
        # mime = Email::MIME object
-       my ($self, $mime, $bytes, $num, $oid, $mid0) = @_;
-       my $mids = mids_for_index($mime->header_obj);
-       $mid0 //= $mids->[0]; # v1 compatibility
-       $num //= do { # v1
+       my ($self, $mime, $smsg) = @_;
+       my $hdr = $mime->header_obj;
+       my $mids = mids_for_index($hdr);
+       $smsg //= bless { blob => '' }, 'PublicInbox::Smsg'; # test-only compat
+       $smsg->{mid} //= $mids->[0]; # v1 compatibility
+       $smsg->{num} //= do { # v1
                _msgmap_init($self);
                index_mm($self, $mime);
        };
+
+       # v1 and tests only:
+       $smsg->{ds} //= msg_datestamp($hdr, $self->{autime});
+       $smsg->{ts} //= msg_timestamp($hdr, $self->{cotime});
+
        eval {
-               if (need_xapian($self)) {
-                       add_xapian($self, $mime, $num, $oid, $mids, $mid0);
+               # order matters, overview stores every possible piece of
+               # data in doc_data (deflated).  Xapian only stores a subset
+               # of the fields which exist in over.sqlite3.  We may stop
+               # storing doc_data in Xapian sometime after we get multi-inbox
+               # search working.
+               if (my $over = $self->{over}) { # v1 only
+                       $over->add_overview($mime, $smsg);
                }
-               if (my $over = $self->{over}) {
-                       $over->add_overview($mime, $bytes, $num, $oid, $mid0,
-                                               $self);
+               if (need_xapian($self)) {
+                       add_xapian($self, $mime, $smsg, $mids);
                }
        };
 
@@ -388,7 +397,7 @@ sub add_message {
                warn "failed to index message <".join('> <',@$mids).">: $@\n";
                return undef;
        }
-       $num;
+       $smsg->{num};
 }
 
 # returns begin and end PostingIterator
@@ -465,7 +474,7 @@ sub remove_by_oid {
        for (; $head != $tail; $head++) {
                my $docid = $head->get_docid;
                my $doc = $db->get_document($docid);
-               my $smsg = PublicInbox::SearchMsg->wrap($mid);
+               my $smsg = PublicInbox::Smsg->wrap($mid);
                $smsg->load_expand($doc);
                if ($smsg->{blob} eq $oid) {
                        push(@delete, $docid);
@@ -487,13 +496,13 @@ sub index_git_blob_id {
 
 sub unindex_blob {
        my ($self, $mime) = @_;
-       my $mid = eval { mid_clean(mid_mime($mime)) };
+       my $mid = eval { mid_mime($mime) };
        $self->remove_message($mid) if defined $mid;
 }
 
 sub index_mm {
        my ($self, $mime) = @_;
-       my $mid = mid_clean(mid_mime($mime));
+       my $mid = mid_mime($mime);
        my $mm = $self->{mm};
        my $num;
 
@@ -524,13 +533,14 @@ sub index_mm {
 
 sub unindex_mm {
        my ($self, $mime) = @_;
-       $self->{mm}->mid_delete(mid_clean(mid_mime($mime)));
+       $self->{mm}->mid_delete(mid_mime($mime));
 }
 
 sub index_both {
-       my ($self, $mime, $bytes, $blob) = @_;
+       my ($self, $mime, $smsg) = @_;
        my $num = index_mm($self, $mime);
-       add_message($self, $mime, $bytes, $num, $blob);
+       $smsg->{num} = $num;
+       add_message($self, $mime, $smsg);
 }
 
 sub unindex_both {
@@ -593,8 +603,11 @@ sub read_log {
                                next;
                        }
                        my $mime = do_cat_mail($git, $blob, \$bytes) or next;
+                       my $smsg = bless {}, 'PublicInbox::Smsg';
                        batch_adjust(\$max, $bytes, $batch_cb, $latest, ++$nr);
-                       $add_cb->($self, $mime, $bytes, $blob);
+                       $smsg->{blob} = $blob;
+                       $smsg->{bytes} = $bytes;
+                       $add_cb->($self, $mime, $smsg);
                } elsif ($line =~ /$delmsg/o) {
                        my $blob = $1;
                        $D{$blob} = 1;
@@ -602,9 +615,9 @@ sub read_log {
                        $latest = $1;
                        $newest ||= $latest;
                } elsif ($line =~ /^author .*? ([0-9]+) [\-\+][0-9]+$/) {
-                       $self->{over}->{autime} = $self->{autime} = $1;
+                       $self->{autime} = $1;
                } elsif ($line =~ /^committer .*? ([0-9]+) [\-\+][0-9]+$/) {
-                       $self->{over}->{cotime} = $self->{cotime} = $1;
+                       $self->{cotime} = $1;
                }
        }
        close($log) or die "git log failed: \$?=$?";
@@ -621,7 +634,7 @@ sub _git_log {
        my $git = $self->{git};
 
        if (index($range, '..') < 0) {
-               # don't show annoying git errrors to users who run -index
+               # don't show annoying git errors to users who run -index
                # on empty inboxes
                $git->qx(qw(rev-parse -q --verify), "$range^0");
                if ($?) {
@@ -829,20 +842,27 @@ sub begin_txn_lazy {
        });
 }
 
+# store 'indexlevel=medium' in v2 shard=0 and v1 (only one shard)
+# This metadata is read by Admin::detect_indexlevel:
+sub set_indexlevel {
+       my ($self) = @_;
+
+       if (!$self->{shard} && # undef or 0, not >0
+                       delete($self->{-set_indexlevel_once})) {
+               my $xdb = $self->{xdb};
+               my $level = $xdb->get_metadata('indexlevel');
+               if (!$level || $level ne 'medium') {
+                       $xdb->set_metadata('indexlevel', 'medium');
+               }
+       }
+}
+
 sub commit_txn_lazy {
        my ($self) = @_;
        delete $self->{txn} or return;
        $self->{-inbox}->with_umask(sub {
                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');
-                       }
-
+                       set_indexlevel($self);
                        $xdb->commit_transaction;
                }
                $self->{over}->commit_lazy if $self->{over};