]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
*idx: pass $smsg in more places instead of many args
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index c33a48c3bd2d5708d2414a008d25aabd40c3aca2..5ca819c383525de90c72eae9bc5c3f31efea76ce 100644 (file)
@@ -19,6 +19,7 @@ use POSIX qw(strftime);
 use PublicInbox::OverIdx;
 use PublicInbox::Spawn qw(spawn);
 use PublicInbox::Git qw(git_unquote);
+use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
 my $X = \%PublicInbox::Search::X;
 my ($DB_CREATE_OR_OPEN, $DB_OPEN);
 use constant {
@@ -305,13 +306,16 @@ sub index_xapian { # msg_iter callback
        index_body($self, $_, /\A>/ ? 0 : $doc) for @sections;
 }
 
-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);
-       my @ds = gmtime($smsg->ds);
+       add_val($doc, PublicInbox::Search::TS(), $smsg->{ts});
+       my @ds = gmtime($smsg->{ds});
        my $yyyymmdd = strftime('%Y%m%d', @ds);
        add_val($doc, PublicInbox::Search::YYYYMMDD(), $yyyymmdd);
        my $dt = strftime('%Y%m%d%H%M%S', @ds);
@@ -334,9 +338,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) {
@@ -349,7 +353,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 ($) {
@@ -363,19 +367,25 @@ sub _msgmap_init ($) {
 
 sub add_message {
        # mime = Email::MIME object
-       my ($self, $mime, $bytes, $num, $oid, $mid0) = @_;
+       my ($self, $mime, $smsg) = @_;
        my $mids = mids_for_index($mime->header_obj);
-       $mid0 //= $mids->[0]; # v1 compatibility
-       $num //= do { # v1
+       $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);
        };
        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, $self);
                }
-               if (my $over = $self->{over}) {
-                       $over->add_overview($mime, $bytes, $num, $oid, $mid0);
+               if (need_xapian($self)) {
+                       add_xapian($self, $mime, $smsg, $mids);
                }
        };
 
@@ -383,7 +393,7 @@ sub add_message {
                warn "failed to index message <".join('> <',@$mids).">: $@\n";
                return undef;
        }
-       $num;
+       $smsg->{num};
 }
 
 # returns begin and end PostingIterator
@@ -460,7 +470,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);
@@ -523,9 +533,10 @@ sub unindex_mm {
 }
 
 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 {
@@ -588,14 +599,21 @@ 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;
                } elsif ($line =~ /^commit ($h40)/o) {
                        $latest = $1;
                        $newest ||= $latest;
+               } elsif ($line =~ /^author .*? ([0-9]+) [\-\+][0-9]+$/) {
+                       $self->{over}->{autime} = $self->{autime} = $1;
+               } elsif ($line =~ /^committer .*? ([0-9]+) [\-\+][0-9]+$/) {
+                       $self->{over}->{cotime} = $self->{cotime} = $1;
                }
        }
        close($log) or die "git log failed: \$?=$?";
@@ -651,7 +669,7 @@ sub _git_log {
                $self->{regen_down} = $high + $fcount;
        }
 
-       $git->popen(qw/log --no-notes --no-color --no-renames
+       $git->popen(qw/log --pretty=raw --no-notes --no-color --no-renames
                                --raw -r --no-abbrev/, $range);
 }