]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
nntp: smsg_range_i: favor ->{$field} lookups when possible
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index 5f5ae895e43570ed5a26b806b7fe8501a1511e66..f10a9104e789fe82beae8e8a7b98d3ec9932f6c9 100644 (file)
@@ -22,11 +22,9 @@ 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 {
-       BATCH_BYTES => defined($ENV{XAPIAN_FLUSH_THRESHOLD}) ?
-                       0x7fffffff : 1_000_000,
-       DEBUG => !!$ENV{DEBUG},
-};
+our $BATCH_BYTES = defined($ENV{XAPIAN_FLUSH_THRESHOLD}) ?
+                       0x7fffffff : 1_000_000;
+use constant DEBUG => !!$ENV{DEBUG};
 
 my $xapianlevels = qr/\A(?:full|medium)\z/;
 
@@ -158,16 +156,14 @@ sub index_text ($$$$) {
        }
 }
 
-sub index_users ($$) {
+sub index_headers ($$) {
        my ($self, $smsg) = @_;
-
-       my $from = $smsg->from;
-       my $to = $smsg->to;
-       my $cc = $smsg->cc;
-
-       index_text($self, $from, 1, 'A'); # A - author
-       index_text($self, $to, 1, 'XTO') if $to ne '';
-       index_text($self, $cc, 1, 'XCC') if $cc ne '';
+       my @x = (from => 'A', # Author
+               subject => 'S', to => 'XTO', cc => 'XCC');
+       while (my ($field, $pfx) = splice(@x, 0, 2)) {
+               my $val = $smsg->{$field};
+               index_text($self, $val, 1, $pfx) if $val ne '';
+       }
 }
 
 sub index_diff_inc ($$$$) {
@@ -287,9 +283,9 @@ sub index_xapian { # msg_iter callback
        if ($part->{is_submsg}) {
                my $mids = mids_for_index($part);
                index_ids($self, $doc, $part, $mids);
-               my $smsg = PublicInbox::Smsg->new($part);
-               index_users($self, $smsg);
-               index_text($self, $smsg->subject, 1, 'S') if $smsg->subject;
+               my $smsg = bless {}, 'PublicInbox::Smsg';
+               $smsg->populate($part);
+               index_headers($self, $smsg);
        }
 
        my ($s, undef) = msg_part_text($part, $ct);
@@ -337,10 +333,8 @@ sub index_ids ($$$$) {
 
 sub add_xapian ($$$$) {
        my ($self, $mime, $smsg, $mids) = @_;
-       $smsg->{mime} = $mime; # XXX dangerous
        my $hdr = $mime->header_obj;
        my $doc = $X->{Document}->new;
-       my $subj = $smsg->subject;
        add_val($doc, PublicInbox::Search::TS(), $smsg->{ts});
        my @ds = gmtime($smsg->{ds});
        my $yyyymmdd = strftime('%Y%m%d', @ds);
@@ -350,8 +344,7 @@ sub add_xapian ($$$$) {
 
        my $tg = term_generator($self);
        $tg->set_document($doc);
-       index_text($self, $subj, 1, 'S') if $subj;
-       index_users($self, $smsg);
+       index_headers($self, $smsg);
 
        msg_iter($mime, \&index_xapian, [ $self, $doc ]);
        index_ids($self, $doc, $hdr, $mids);
@@ -394,8 +387,7 @@ sub add_message {
        };
 
        # v1 and tests only:
-       $smsg->{ds} //= msg_datestamp($hdr, $self->{autime});
-       $smsg->{ts} //= msg_timestamp($hdr, $self->{cotime});
+       $smsg->populate($hdr, $self);
 
        eval {
                # order matters, overview stores every possible piece of
@@ -492,7 +484,7 @@ sub remove_by_oid {
        for (; $head != $tail; $head++) {
                my $docid = $head->get_docid;
                my $doc = $db->get_document($docid);
-               my $smsg = PublicInbox::Smsg->wrap($mid);
+               my $smsg = bless { mid => $mid }, 'PublicInbox::Smsg';
                $smsg->load_expand($doc);
                if ($smsg->{blob} eq $oid) {
                        push(@delete, $docid);
@@ -585,7 +577,7 @@ sub batch_adjust ($$$$$) {
        my ($max, $bytes, $batch_cb, $latest, $nr) = @_;
        $$max -= $bytes;
        if ($$max <= 0) {
-               $$max = BATCH_BYTES;
+               $$max = $BATCH_BYTES;
                $batch_cb->($nr, $latest);
        }
 }
@@ -610,7 +602,7 @@ sub read_log {
        my $git = $self->{git};
        my $latest;
        my $bytes;
-       my $max = BATCH_BYTES;
+       my $max = $BATCH_BYTES;
        local $/ = "\n";
        my %D;
        my $line;
@@ -651,6 +643,7 @@ sub read_log {
                my $mime = do_cat_mail($git, $blob, \$bytes);
                $del_cb->($self, $mime);
        }
+       delete @$self{qw(autime cotime)};
        $batch_cb->($nr, $latest, $newest);
 }