X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchIdx.pm;h=66faed312c3243f1a56789c898b7d5bd4dd6e4a3;hb=3d41aa23f35501ca92aab8aa42980fa73f7fa74f;hp=316111bf0be4d8a8227be046a33867f6b8939fcf;hpb=7eeadcb62729b0efbcb53cd9b7b181897c92cf9a;p=public-inbox.git
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 316111bf..66faed31 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -1,5 +1,5 @@
-# Copyright (C) 2015 all contributors
-# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+# Copyright (C) 2015-2018 all contributors
+# License: AGPL-3.0+
# based on notmuch, but with no concept of folders, files or flags
#
# Indexes mail with Xapian and our (SQLite-based) ::Msgmap for use
@@ -20,15 +20,36 @@ use Carp qw(croak);
use POSIX qw(strftime);
require PublicInbox::Git;
-use constant MAX_MID_SIZE => 244; # max term size - 1 in Xapian
use constant {
+ MAX_MID_SIZE => 244, # max term size - 1 in Xapian
PERM_UMASK => 0,
OLD_PERM_GROUP => 1,
OLD_PERM_EVERYBODY => 2,
PERM_GROUP => 0660,
PERM_EVERYBODY => 0664,
+ BATCH_BYTES => 1_000_000,
+ DEBUG => !!$ENV{DEBUG},
};
+my %GIT_ESC = (
+ a => "\a",
+ b => "\b",
+ f => "\f",
+ n => "\n",
+ r => "\r",
+ t => "\t",
+ v => "\013",
+);
+
+sub git_unquote ($) {
+ my ($s) = @_;
+ return $s unless ($s =~ /\A"(.*)"\z/);
+ $s = $1;
+ $s =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g;
+ $s =~ s/\\([0-7]{1,3})/chr(oct($1))/ge;
+ $s;
+}
+
sub new {
my ($class, $inbox, $creat) = @_;
my $git_dir = $inbox;
@@ -71,7 +92,6 @@ sub _xdb_acquire {
require File::Path;
_lock_acquire($self);
File::Path::mkpath($dir);
- $self->{batch_size} = 100;
$flag = Search::Xapian::DB_CREATE_OR_OPEN;
}
$self->{xdb} = Search::Xapian::WritableDatabase->new($dir, $flag);
@@ -134,11 +154,108 @@ sub index_users ($$) {
$tg->increase_termpos;
}
+sub index_text_inc ($$$) {
+ my ($tg, $text, $pfx) = @_;
+ $tg->index_text($text, 1, $pfx);
+ $tg->increase_termpos;
+}
+
+sub index_old_diff_fn {
+ my ($tg, $seen, $fa, $fb) = @_;
+
+ # no renames or space support for traditional diffs,
+ # find the number of leading common paths to strip:
+ my @fa = split('/', $fa);
+ my @fb = split('/', $fb);
+ while (scalar(@fa) && scalar(@fb)) {
+ $fa = join('/', @fa);
+ $fb = join('/', @fb);
+ if ($fa eq $fb) {
+ index_text_inc($tg, $fa,'XDFN') unless $seen->{$fa}++;
+ return 1;
+ }
+ shift @fa;
+ shift @fb;
+ }
+ 0;
+}
+
+sub index_diff ($$$) {
+ my ($tg, $lines, $doc) = @_;
+ my %seen;
+ my $in_diff;
+ foreach (@$lines) {
+ if ($in_diff && s/^ //) { # diff context
+ index_text_inc($tg, $_, 'XDFCTX');
+ } 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];
+ index_text_inc($tg, $fn, 'XDFN') unless $seen{$fn}++;
+ $fn = (split('/', git_unquote($fb), 2))[1];
+ index_text_inc($tg, $fn, 'XDFN') unless $seen{$fn}++;
+ $in_diff = 1;
+ # traditional diff:
+ } elsif (m/^diff -(.+) (\S+) (\S+)$/) {
+ my ($opt, $fa, $fb) = ($1, $2, $3);
+ # only support unified:
+ next unless $opt =~ /[uU]/;
+ $in_diff = index_old_diff_fn($tg, \%seen, $fa, $fb);
+ } elsif (m!^--- ("?a/.+)!) {
+ my $fn = (split('/', git_unquote($1), 2))[1];
+ index_text_inc($tg, $fn, 'XDFN') unless $seen{$fn}++;
+ $in_diff = 1;
+ } elsif (m!^\+\+\+ ("?b/.+)!) {
+ my $fn = (split('/', git_unquote($1), 2))[1];
+ index_text_inc($tg, $fn, 'XDFN') unless $seen{$fn}++;
+ $in_diff = 1;
+ } elsif (/^--- (\S+)/) {
+ $in_diff = $1;
+ } elsif (defined $in_diff && /^\+\+\+ (\S+)/) {
+ $in_diff = index_old_diff_fn($tg, \%seen, $in_diff, $1);
+ } elsif ($in_diff && s/^\+//) { # diff added
+ index_text_inc($tg, $_, 'XDFB');
+ } elsif ($in_diff && s/^-//) { # diff removed
+ index_text_inc($tg, $_, 'XDFA');
+ } elsif (m!^index ([a-f0-9]+)\.\.([a-f0-9]+)!) {
+ my ($ba, $bb) = ($1, $2);
+ index_git_blob_id($doc, 'XDFPRE', $ba);
+ index_git_blob_id($doc, 'XDFPOST', $bb);
+ $in_diff = 1;
+ } elsif (/^@@ (?:\S+) (?:\S+) @@\s*$/) {
+ # traditional diff w/o -p
+ } elsif (/^@@ (?:\S+) (?:\S+) @@\s*(\S+.*)$/) {
+ # hunk header context
+ index_text_inc($tg, $1, 'XDFHH');
+ # ignore the following lines:
+ } elsif (/^(?:dis)similarity index/) {
+ } elsif (/^(?:old|new) mode/) {
+ } elsif (/^(?:deleted|new) file mode/) {
+ } elsif (/^(?:copy|rename) (?:from|to) /) {
+ } elsif (/^(?:dis)?similarity index /) {
+ } elsif (/^\\ No newline at end of file/) {
+ } elsif (/^Binary files .* differ/) {
+ } elsif ($_ eq '') {
+ $in_diff = undef;
+ } else {
+ warn "non-diff line: $_\n" if DEBUG && $_ ne '';
+ $in_diff = undef;
+ }
+ }
+}
+
sub index_body ($$$) {
- my ($tg, $lines, $inc) = @_;
- $tg->index_text(join("\n", @$lines), $inc, $inc ? 'XNQ' : 'XQUOT');
- @$lines = ();
+ my ($tg, $lines, $doc) = @_;
+ my $txt = join("\n", @$lines);
+ $tg->index_text($txt, !!$doc, $doc ? 'XNQ' : 'XQUOT');
$tg->increase_termpos;
+ # does it look like a diff?
+ if ($doc && $txt =~ /^(?:diff|---|\+\+\+) /ms) {
+ $txt = undef;
+ index_diff($tg, $lines, $doc);
+ }
+ @$lines = ();
}
sub add_message {
@@ -205,7 +322,7 @@ sub add_message {
my @lines = split(/\n/, $body);
while (defined(my $l = shift @lines)) {
if ($l =~ /^>/) {
- index_body($tg, \@orig, 1) if @orig;
+ index_body($tg, \@orig, $doc) if @orig;
push @quot, $l;
} else {
index_body($tg, \@quot, 0) if @quot;
@@ -213,7 +330,7 @@ sub add_message {
}
}
index_body($tg, \@quot, 0) if @quot;
- index_body($tg, \@orig, 1) if @orig;
+ index_body($tg, \@orig, $doc) if @orig;
});
link_message($self, $smsg, $old_tid);
@@ -249,8 +366,12 @@ sub remove_message {
$mid = mid_clean($mid);
eval {
- $doc_id = $self->find_unique_doc_id('mid', $mid);
- $db->delete_document($doc_id) if defined $doc_id;
+ $doc_id = $self->find_unique_doc_id('Q' . $mid);
+ if (defined $doc_id) {
+ $db->delete_document($doc_id);
+ } else {
+ warn "cannot remove non-existent <$mid>\n";
+ }
};
if ($@) {
@@ -293,9 +414,8 @@ sub link_message {
# last References should be IRT, but some mail clients do things
# out of order, so trust IRT over References iff IRT exists
- my @refs = ($hdr->header_raw('References'),
- $hdr->header_raw('In-Reply-To'));
- @refs = ((join(' ', @refs)) =~ /<([^>]+)>/g);
+ my @refs = (($hdr->header_raw('References') || '') =~ /<([^>]+)>/g);
+ push(@refs, (($hdr->header_raw('In-Reply-To') || '') =~ /<([^>]+)>/g));
my $tid;
if (@refs) {
@@ -339,6 +459,16 @@ sub index_blob {
$self->add_message($mime, $bytes, $num, $blob);
}
+sub index_git_blob_id {
+ my ($doc, $pfx, $objid) = @_;
+
+ my $len = length($objid);
+ for (my $len = length($objid); $len >= 7; ) {
+ $doc->add_term($pfx.$objid);
+ $objid = substr($objid, 0, --$len);
+ }
+}
+
sub unindex_blob {
my ($self, $mime) = @_;
my $mid = eval { mid_clean(mid_mime($mime)) };
@@ -347,7 +477,12 @@ sub unindex_blob {
sub index_mm {
my ($self, $mime) = @_;
- $self->{mm}->mid_insert(mid_clean(mid_mime($mime)));
+ my $mid = mid_clean(mid_mime($mime));
+ my $mm = $self->{mm};
+ my $num = $mm->mid_insert($mid);
+
+ # fallback to num_for since filters like RubyLang set the number
+ defined $num ? $num : $mm->num_for($mid);
}
sub unindex_mm {
@@ -395,6 +530,15 @@ sub index_sync {
with_umask($self, sub { $self->_index_sync($opts) });
}
+sub batch_adjust ($$$$) {
+ my ($max, $bytes, $batch_cb, $latest) = @_;
+ $$max -= $bytes;
+ if ($$max <= 0) {
+ $$max = BATCH_BYTES;
+ $batch_cb->($latest, 1);
+ }
+}
+
sub rlog {
my ($self, $log, $add_cb, $del_cb, $batch_cb) = @_;
my $hex = '[a-f0-9]';
@@ -404,23 +548,21 @@ sub rlog {
my $git = $self->{git};
my $latest;
my $bytes;
- my $max = $self->{batch_size}; # may be undef
+ my $max = BATCH_BYTES;
local $/ = "\n";
my $line;
while (defined($line = <$log>)) {
if ($line =~ /$addmsg/o) {
my $blob = $1;
my $mime = do_cat_mail($git, $blob, \$bytes) or next;
+ batch_adjust(\$max, $bytes, $batch_cb, $latest);
$add_cb->($self, $mime, $bytes, $blob);
} elsif ($line =~ /$delmsg/o) {
my $blob = $1;
- my $mime = do_cat_mail($git, $blob) or next;
+ my $mime = do_cat_mail($git, $blob, \$bytes) or next;
+ batch_adjust(\$max, $bytes, $batch_cb, $latest);
$del_cb->($self, $mime);
} elsif ($line =~ /^commit ($h40)/o) {
- if (defined $max && --$max <= 0) {
- $max = $self->{batch_size};
- $batch_cb->($latest, 1);
- }
$latest = $1;
}
}