X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchMsg.pm;h=42384936b923364bb17fc515bd6b39c0d83bc35e;hb=95bdac7f09c69036efed537a4d03d5bdd2ae4eb6;hp=a1cd0c28cf8f11d2ac826d895c7420439e2ede6a;hpb=eb48e7d6675babdda9a36be1a490c29a2ccddbdc;p=public-inbox.git diff --git a/lib/PublicInbox/SearchMsg.pm b/lib/PublicInbox/SearchMsg.pm index a1cd0c28..42384936 100644 --- a/lib/PublicInbox/SearchMsg.pm +++ b/lib/PublicInbox/SearchMsg.pm @@ -1,82 +1,117 @@ -# Copyright (C) 2015-2018 all contributors +# Copyright (C) 2015-2020 all contributors # License: AGPL-3.0+ # based on notmuch, but with no concept of folders, files or flags # # Wraps a document inside our Xapian search index. +# There may be many of these objects loaded in memory at once +# for large threads in our WWW UI. package PublicInbox::SearchMsg; use strict; use warnings; -use Search::Xapian; +use base qw(Exporter); +our @EXPORT_OK = qw(subject_normalized); use PublicInbox::MID qw/mid_clean mid_mime/; use PublicInbox::Address; -use PublicInbox::MsgTime qw(msg_timestamp); +use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp); +use Time::Local qw(timegm); sub new { my ($class, $mime) = @_; - my $doc = Search::Xapian::Document->new; - $doc->add_boolean_term('T' . 'mail'); - - bless { type => 'mail', doc => $doc, mime => $mime }, $class; + bless { mime => $mime }, $class; } sub wrap { - my ($class, $doc, $mid) = @_; - bless { doc => $doc, mime => undef, mid => $mid }, $class; + my ($class, $mid) = @_; + bless { mid => $mid }, $class; } sub get_val ($$) { my ($doc, $col) = @_; - Search::Xapian::sortable_unserialise($doc->get_value($col)); + # sortable_unserialise is defined by PublicInbox::Search::load_xapian() + sortable_unserialise($doc->get_value($col)); +} + +sub to_doc_data { + my ($self, $oid, $mid0) = @_; + $oid = '' unless defined $oid; + join("\n", + $self->subject, + $self->from, + $self->references, + $self->to, + $self->cc, + $oid, + $mid0, + $self->{bytes} || '', + $self->{lines} || '' + ); } sub load_from_data ($$) { my ($self) = $_[0]; # data = $_[1] - my ($subj, $from, $refs, $to, $cc, $blob, $mid0) = split(/\n/, $_[1]); - $self->{subject} = $subj; - $self->{from} = $from; - $self->{references} = $refs; - $self->{to} = $to; - $self->{cc} = $cc; - $self->{blob} = $blob; - $self->{mid} = $mid0; + ( + $self->{subject}, + $self->{from}, + $self->{references}, + + # To: and Cc: are stored to optimize HDR/XHDR in NNTP since + # some NNTP clients will use that for message displays. + # NNTP only, and only stored in Over(view), not Xapian + $self->{to}, + $self->{cc}, + + $self->{blob}, + $self->{mid}, + + # NNTP only + $self->{bytes}, + $self->{lines} + ) = split(/\n/, $_[1]); } sub load_expand { - my ($self) = @_; - my $doc = $self->{doc}; + my ($self, $doc) = @_; my $data = $doc->get_data or return; - $self->{ts} = get_val($doc, &PublicInbox::Search::TS); + $self->{ts} = get_val($doc, PublicInbox::Search::TS()); + my $dt = get_val($doc, PublicInbox::Search::DT()); + my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt); + $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy); utf8::decode($data); load_from_data($self, $data); $self; } -sub load_doc { - my ($class, $doc) = @_; - my $data = $doc->get_data or return; - my $ts = get_val($doc, &PublicInbox::Search::TS); - utf8::decode($data); - my $self = bless { doc => $doc, ts => $ts }, $class; - load_from_data($self, $data); - $self +sub psgi_cull ($) { + my ($self) = @_; + from_name($self); # fill in {from_name} so we can delete {from} + + # drop NNTP-only fields which aren't relevant to PSGI results: + # saves ~80K on a 200 item search result: + delete @$self{qw(from ts to cc bytes lines)}; + $self; } -# :bytes and :lines metadata in RFC 3977 -sub bytes ($) { get_val($_[0]->{doc}, &PublicInbox::Search::BYTES) } -sub lines ($) { get_val($_[0]->{doc}, &PublicInbox::Search::LINES) } -sub num ($) { - $_[0]->{num} ||= get_val($_[0]->{doc}, PublicInbox::Search::NUM) +# Only called by PSGI interface, not NNTP +sub from_mitem { + my ($mitem, $srch) = @_; + return $srch->retry_reopen(\&from_mitem, $mitem) if $srch; + my $self = bless {}, __PACKAGE__; + psgi_cull(load_expand($self, $mitem->get_document)); } +# :bytes and :lines metadata in RFC 3977 +sub bytes ($) { $_[0]->{bytes} } +sub lines ($) { $_[0]->{lines} } + sub __hdr ($$) { my ($self, $field) = @_; my $val = $self->{$field}; return $val if defined $val; my $mime = $self->{mime} or return; - $val = $mime->header($field); - $val = '' unless defined $val; - $val =~ tr/\n/ /; + my @raw = $mime->header($field); + $val = join(', ', @raw); + $val =~ tr/\t\n/ /; $val =~ tr/\r//d; $self->{$field} = $val; } @@ -91,9 +126,9 @@ my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); sub date ($) { my ($self) = @_; - my $ts = $self->{ts}; - return unless defined $ts; - my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ts); + my $ds = $self->{ds}; + return unless defined $ds; + my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds); "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000", $mday, $year+1900, $hour, $min, $sec); @@ -119,17 +154,12 @@ sub from_name { sub ts { my ($self) = @_; - $self->{ts} ||= eval { - msg_timestamp($self->{mime}->header_obj); - } || 0; + $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0; } -sub to_doc_data { - my ($self, $oid, $mid0) = @_; - my @rows = ($self->subject, $self->from, $self->references, - $self->to, $self->cc); - $oid = '' unless defined $oid; - join("\n", @rows, $oid, $mid0); +sub ds { + my ($self) = @_; + $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0; } sub references { @@ -138,54 +168,31 @@ sub references { defined $x ? $x : ''; } -sub _get_term_val ($$$) { - my ($self, $pfx, $re) = @_; - my $doc = $self->{doc}; - my $end = $doc->termlist_end; - my $i = $doc->termlist_begin; - $i->skip_to($pfx); - if ($i != $end) { - my $val = $i->get_termname; - $val =~ s/$re// and return $val; - } - undef; -} - sub mid ($;$) { my ($self, $mid) = @_; if (defined $mid) { $self->{mid} = $mid; - } elsif (my $rv = $self->{mid}) { + } elsif (defined(my $rv = $self->{mid})) { $rv; } else { - $self->{mid} = _get_term_val($self, 'Q', qr/\AQ/) || - $self->_extract_mid; + die "NO {mime} for mid\n" unless $self->{mime}; + $self->_extract_mid; # v1 w/o Xapian } } sub _extract_mid { mid_clean(mid_mime($_[0]->{mime})) } -sub thread_id { - my ($self) = @_; - my $tid = $self->{thread}; - return $tid if defined $tid; - $self->{thread} = _get_term_val($self, 'G', qr/\AG/); # *G*roup -} - -# XXX: consider removing this, we can phrase match subject -sub path { - my ($self) = @_; - my $path = $self->{path}; - return $path if defined $path; - $self->{path} = _get_term_val($self, 'XPATH', qr/\AXPATH/); # path -} +our $REPLY_RE = qr/^re:\s+/i; -sub type { - my ($self) = @_; - my $type = $self->{type}; - return $type if defined $type; - $self->{type} = _get_term_val($self, 'T', qr/\AT/); +sub subject_normalized ($) { + my ($subj) = @_; + $subj =~ s/\A\s+//s; # no leading space + $subj =~ s/\s+\z//s; # no trailing space + $subj =~ s/\s+/ /gs; # no redundant spaces + $subj =~ s/\.+\z//; # no trailing '.' + $subj =~ s/$REPLY_RE//igo; # remove reply prefix + $subj; } 1;