X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchIdx.pm;h=7d089e7aee4271645ae562ccf1b13ab24f6d68f7;hb=8e1ec8836dabc58dfc0115b36ed440b4371b70d7;hp=62e836e0d2d936ed849eec108cca7852b554cb14;hpb=da1ae9ccd829966195bfe59f17e416f218746def;p=public-inbox.git
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 62e836e0..7d089e7a 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2019 all contributors
+# Copyright (C) 2015-2020 all contributors
# License: AGPL-3.0+
# based on notmuch, but with no concept of folders, files or flags
#
@@ -12,13 +12,14 @@ use warnings;
use base qw(PublicInbox::Search PublicInbox::Lock);
use PublicInbox::MIME;
use PublicInbox::InboxWritable;
-use PublicInbox::MID qw/mid_clean id_compress mid_mime mids_for_index/;
+use PublicInbox::MID qw/mid_clean mid_mime mids_for_index/;
use PublicInbox::MsgIter;
use Carp qw(croak);
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 {
@@ -34,7 +35,7 @@ sub new {
ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
my $levels = qr/\A(?:full|medium|basic)\z/;
my $inboxdir = $ibx->{inboxdir};
- my $version = $ibx->{version} || 1;
+ my $version = $ibx->version;
my $indexlevel = 'full';
my $altid = $ibx->{altid};
if ($altid) {
@@ -54,9 +55,10 @@ sub new {
-inbox => $ibx,
git => $ibx->git,
-altid => $altid,
- version => $version,
+ 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";
@@ -144,14 +146,14 @@ sub term_generator ($) { # write-only
}
sub index_text ($$$$) {
- my ($self, $field, $n, $text) = @_;
- my $tg = term_generator($self);
+ my ($self, $text, $wdf_inc, $prefix) = @_;
+ my $tg = term_generator($self); # man Search::Xapian::TermGenerator
if ($self->{indexlevel} eq 'full') {
- $tg->index_text($field, $n, $text);
+ $tg->index_text($text, $wdf_inc, $prefix);
$tg->increase_termpos;
} else {
- $tg->index_text_without_positions($field, $n, $text);
+ $tg->index_text_without_positions($text, $wdf_inc, $prefix);
}
}
@@ -199,22 +201,18 @@ sub index_old_diff_fn {
}
sub index_diff ($$$) {
- my ($self, $lines, $doc) = @_;
+ my ($self, $txt, $doc) = @_;
my %seen;
my $in_diff;
my @xnq;
my $xnq = \@xnq;
- foreach (@$lines) {
+ foreach (split(/\n/, $txt)) {
if ($in_diff && s/^ //) { # diff context
index_diff_inc($self, $_, 'XDFCTX', $xnq);
} elsif (/^-- $/) { # email signature begins
$in_diff = undef;
- } elsif (m!^diff --git ("?a/.+) ("?b/.+)\z!) {
- my ($fa, $fb) = ($1, $2);
- my $fn = (split('/', git_unquote($fa), 2))[1];
- $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
- $fn = (split('/', git_unquote($fb), 2))[1];
- $seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
+ } elsif (m!^diff --git "?[^/]+/.+ "?[^/]+/.+\z!) {
+ # wait until "---" and "+++" to capture filenames
$in_diff = 1;
# traditional diff:
} elsif (m/^diff -(.+) (\S+) (\S+)$/) {
@@ -224,12 +222,12 @@ sub index_diff ($$$) {
next unless $opt =~ /[uU]/;
$in_diff = index_old_diff_fn($self, \%seen, $fa, $fb,
$xnq);
- } elsif (m!^--- ("?a/.+)!) {
+ } elsif (m!^--- ("?[^/]+/.+)!) {
my $fn = $1;
$fn = (split('/', git_unquote($fn), 2))[1];
$seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
$in_diff = 1;
- } elsif (m!^\+\+\+ ("?b/.+)!) {
+ } elsif (m!^\+\+\+ ("?[^/]+/.+)!) {
my $fn = $1;
$fn = (split('/', git_unquote($fn), 2))[1];
$seen{$fn}++ or index_diff_inc($self, $fn, 'XDFN', $xnq);
@@ -278,20 +276,17 @@ sub index_diff ($$$) {
}
sub index_body ($$$) {
- my ($self, $lines, $doc) = @_;
- my $txt = join("\n", @$lines);
+ my ($self, $txt, $doc) = @_;
if ($doc) {
# does it look like a diff?
if ($txt =~ /^(?:diff|---|\+\+\+) /ms) {
- $txt = undef;
- index_diff($self, $lines, $doc);
+ index_diff($self, $txt, $doc);
} else {
index_text($self, $txt, 1, 'XNQ');
}
} else {
index_text($self, $txt, 0, 'XQUOT');
}
- @$lines = ();
}
sub index_xapian { # msg_iter callback
@@ -306,28 +301,20 @@ sub index_xapian { # msg_iter callback
my ($s, undef) = msg_part_text($part, $ct);
defined $s or return;
- my (@orig, @quot);
- my @lines = split(/\n/, $s);
- while (defined(my $l = shift @lines)) {
- if ($l =~ /^>/) {
- index_body($self, \@orig, $doc) if @orig;
- push @quot, $l;
- } else {
- index_body($self, \@quot, 0) if @quot;
- push @orig, $l;
- }
- }
- index_body($self, \@quot, 0) if @quot;
- index_body($self, \@orig, $doc) if @orig;
+ # split off quoted and unquoted blocks:
+ my @sections = split(/((?:^>[^\n]*\n)+)/sm, $s);
+ $part = $s = undef;
+ 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;
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);
@@ -350,9 +337,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) {
@@ -365,12 +352,12 @@ 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 ($) {
my ($self) = @_;
- die "BUG: _msgmap_init is only for v1\n" if $self->{version} != 1;
+ die "BUG: _msgmap_init is only for v1\n" if $self->{ibx_ver} != 1;
$self->{mm} //= eval {
require PublicInbox::Msgmap;
PublicInbox::Msgmap->new($self->{inboxdir}, 1);
@@ -379,19 +366,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);
+ if (need_xapian($self)) {
+ add_xapian($self, $mime, $smsg, $mids);
}
};
@@ -399,7 +398,7 @@ sub add_message {
warn "failed to index message <".join('> <',@$mids).">: $@\n";
return undef;
}
- $num;
+ $smsg->{num};
}
# returns begin and end PostingIterator
@@ -445,7 +444,7 @@ sub remove_message {
batch_do($self, 'Q' . $mid, sub {
my ($ids) = @_;
$db->delete_document($_) for @$ids;
- $nr = scalar @$ids;
+ $nr += scalar @$ids;
});
};
if ($@) {
@@ -476,7 +475,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);
@@ -539,9 +538,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 {
@@ -604,14 +604,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->{autime} = $1;
+ } elsif ($line =~ /^committer .*? ([0-9]+) [\-\+][0-9]+$/) {
+ $self->{cotime} = $1;
}
}
close($log) or die "git log failed: \$?=$?";
@@ -667,7 +674,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);
}
@@ -678,7 +685,6 @@ sub is_ancestor ($$$) {
my $cmd = [ 'git', "--git-dir=$git->{git_dir}",
qw(merge-base --is-ancestor), $cur, $tip ];
my $pid = spawn($cmd);
- defined $pid or die "spawning ".join(' ', @$cmd)." failed: $!";
waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish';
$? == 0;
}
@@ -837,20 +843,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};