X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchIdx.pm;h=63be68101b40c8ae7f4f8d8e39ee94492594a589;hb=3cda6050b7c8f73e7fd86f88efc5cd42d0c13f73;hp=87243268a8ad71aa6c85494a5c608fcbdfabd3a2;hpb=d51d7261fb75b9a2c378e3d46c6c370397e44c8f;p=public-inbox.git diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm index 87243268..63be6810 100644 --- a/lib/PublicInbox/SearchIdx.pm +++ b/lib/PublicInbox/SearchIdx.pm @@ -1,13 +1,20 @@ # Copyright (C) 2015 all contributors # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt) # based on notmuch, but with no concept of folders, files or flags +# +# Indexes mail with Xapian and our (SQLite-based) ::Msgmap for use +# with the web and NNTP interfaces. This index maintains thread +# relationships for use by Mail::Thread. This writes to the search +# index. package PublicInbox::SearchIdx; use strict; use warnings; use base qw(PublicInbox::Search); -use PublicInbox::MID qw/mid_clean mid_compress/; +use PublicInbox::MID qw/mid_clean id_compress mid_mime/; +require PublicInbox::Git; *xpfx = *PublicInbox::Search::xpfx; +use constant MAX_MID_SIZE => 244; # max term size - 1 in Xapian use constant { PERM_UMASK => 0, OLD_PERM_GROUP => 1, @@ -36,16 +43,23 @@ sub new { $self; } +sub add_val { + my ($doc, $col, $num) = @_; + $num = Search::Xapian::sortable_serialise($num); + $doc->add_value($col, $num); +} + sub add_message { - my ($self, $mime) = @_; # mime = Email::MIME object + my ($self, $mime, $bytes, $num) = @_; # mime = Email::MIME object my $db = $self->{xdb}; my $doc_id; - my $mid = mid_clean($mime->header('Message-ID')); + my $mid = mid_clean(mid_mime($mime)); my $was_ghost = 0; my $ct_msg = $mime->header('Content-Type') || 'text/plain'; eval { + die 'Message-ID too long' if length($mid) > MAX_MID_SIZE; my $smsg = $self->lookup_message($mid); my $doc; @@ -74,14 +88,20 @@ sub add_message { my $subj = $smsg->subject; if ($subj ne '') { - $doc->add_term(xpfx('subject') . $subj); - my $path = $self->subject_path($subj); - $doc->add_term(xpfx('path') . mid_compress($path)); + $doc->add_term(xpfx('path') . id_compress($path)); } - my $ts = Search::Xapian::sortable_serialise($smsg->ts); - $doc->add_value(PublicInbox::Search::TS, $ts); + add_val($doc, &PublicInbox::Search::TS, $smsg->ts); + + defined($num) and + add_val($doc, &PublicInbox::Search::NUM, $num); + + defined($bytes) and + add_val($doc, &PublicInbox::Search::BYTES, $bytes); + + add_val($doc, &PublicInbox::Search::LINES, + $mime->body_raw =~ tr!\n!\n!); my $tg = $self->term_generator; @@ -91,7 +111,7 @@ sub add_message { $tg->index_text($subj) if $subj; $tg->increase_termpos; - $tg->index_text($smsg->from->format); + $tg->index_text($smsg->from); $tg->increase_termpos; $mime->walk_parts(sub { @@ -202,9 +222,10 @@ sub link_message_to_parents { my $doc = $smsg->{doc}; my $mid = $smsg->mid; my $mime = $smsg->mime; - my $refs = $mime->header('References'); + my $hdr = $mime->header_obj; + my $refs = $hdr->header_raw('References'); my @refs = $refs ? ($refs =~ /<([^>]+)>/g) : (); - if (my $irt = $mime->header('In-Reply-To')) { + if (my $irt = $hdr->header_raw('In-Reply-To')) { # last References should be $irt # we will de-dupe later push @refs, mid_clean($irt); @@ -218,13 +239,16 @@ sub link_message_to_parents { # prevent circular references via References: here: foreach my $ref (@orig_refs) { + if (length($ref) > MAX_MID_SIZE) { + warn "References: <$ref> too long, ignoring\n"; + } next if $uniq{$ref}; $uniq{$ref} = 1; push @refs, $ref; } } if (@refs) { - $smsg->{references_sorted} = '<'.join('><', @refs).'>'; + $smsg->{references} = '<'.join('> <', @refs).'>'; # first ref *should* be the thread root, # but we can never trust clients to do the right thing @@ -245,30 +269,42 @@ sub link_message_to_parents { } sub index_blob { - my ($self, $git, $mime) = @_; - $self->add_message($mime); + my ($self, $git, $mime, $bytes, $num) = @_; + $self->add_message($mime, $bytes, $num); } sub unindex_blob { my ($self, $git, $mime) = @_; - my $mid = mid_clean($mime->header('Message-ID')); + my $mid = eval { mid_clean(mid_mime($mime)) }; $self->remove_message($mid) if defined $mid; } sub index_mm { my ($self, $git, $mime) = @_; - $self->{mm}->mid_insert(mid_clean($mime->header('Message-ID'))); + $self->{mm}->mid_insert(mid_clean(mid_mime($mime))); } sub unindex_mm { my ($self, $git, $mime) = @_; - $self->{mm}->mid_delete(mid_clean($mime->header('Message-ID'))); + $self->{mm}->mid_delete(mid_clean(mid_mime($mime))); } -sub index_both { +sub index_mm2 { + my ($self, $git, $mime, $bytes) = @_; + my $num = $self->{mm}->num_for(mid_clean(mid_mime($mime))); + index_blob($self, $git, $mime, $bytes, $num); +} + +sub unindex_mm2 { my ($self, $git, $mime) = @_; - index_blob($self, $git, $mime); - index_mm($self, $git, $mime); + $self->{mm}->mid_delete(mid_clean(mid_mime($mime))); + unindex_blob($self, $git, $mime); +} + +sub index_both { + my ($self, $git, $mime, $bytes) = @_; + my $num = index_mm($self, $git, $mime); + index_blob($self, $git, $mime, $bytes, $num); } sub unindex_both { @@ -278,9 +314,9 @@ sub unindex_both { } sub do_cat_mail { - my ($git, $blob) = @_; + my ($git, $blob, $sizeref) = @_; my $mime = eval { - my $str = $git->cat_file($blob); + my $str = $git->cat_file($blob, $sizeref); Email::MIME->new($str); }; $@ ? undef : $mime; @@ -297,19 +333,15 @@ sub rlog { my $h40 = $hex .'{40}'; my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!; my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!; - my $git_dir = $self->{git_dir}; - require PublicInbox::GitCatFile; - my $git = PublicInbox::GitCatFile->new($git_dir); - my @cmd = ('git', "--git-dir=$git_dir", "log", - qw/--reverse --no-notes --no-color --raw -r --no-abbrev/, - $range); + my $git = PublicInbox::Git->new($self->{git_dir}); + my $log = $git->popen(qw/log --reverse --no-notes --no-color + --raw -r --no-abbrev/, $range); my $latest; - my $pid = open(my $log, '-|', @cmd) or - die('open` '.join(' ', @cmd) . " pipe failed: $!\n"); - while (my $line = <$log>) { + my $bytes; + while (defined(my $line = <$log>)) { if ($line =~ /$addmsg/o) { - my $mime = do_cat_mail($git, $1) or next; - $add_cb->($self, $git, $mime); + my $mime = do_cat_mail($git, $1, \$bytes) or next; + $add_cb->($self, $git, $mime, $bytes); } elsif ($line =~ /$delmsg/o) { my $mime = do_cat_mail($git, $1) or next; $del_cb->($self, $git, $mime); @@ -317,7 +349,6 @@ sub rlog { $latest = $1; } } - close $log; $latest; } @@ -354,11 +385,11 @@ sub _index_sync { $mm->{dbh}->commit; $mm->last_commit($lm) if defined $lm; - goto xapian_only; + $lx = $self->rlog($range, *index_mm2, *unindex_mm2); + $db->set_metadata('last_commit', $lx) if defined $lx; } } else { # user didn't install DBD::SQLite and DBI -xapian_only: $lx = $self->rlog($range, *index_blob, *unindex_blob); $db->set_metadata('last_commit', $lx) if defined $lx; } @@ -412,12 +443,9 @@ sub merge_threads { sub _read_git_config_perm { my ($self) = @_; - my @cmd = ('git', "--git-dir=$self->{git_dir}", - qw(config core.sharedRepository)); - my $pid = open(my $fh, '-|', @cmd) or - die('open `'.join(' ', @cmd) . " pipe failed: $!\n"); + my @cmd = qw(config core.sharedRepository); + my $fh = PublicInbox::Git->new($self->{git_dir})->popen(@cmd); my $perm = <$fh>; - close $fh; chomp $perm if defined $perm; $perm; }