X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchIdx.pm;h=114420e4b82a2b4f71fce48c8538d7ede36e9ea2;hb=e220b8b2ee5cfd458167dc2c6c92726352c4c80e;hp=8978914ab087b3d6e25c69b988e490f9ea71fd91;hpb=2b18ef9ca28e8c4e70a881dbad832d2f7cb838ee;p=public-inbox.git diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm index 8978914a..114420e4 100644 --- a/lib/PublicInbox/SearchIdx.pm +++ b/lib/PublicInbox/SearchIdx.pm @@ -18,35 +18,19 @@ use Carp qw(croak); use POSIX qw(strftime); use PublicInbox::OverIdx; use PublicInbox::Spawn qw(spawn); -require PublicInbox::Git; -use Compress::Zlib qw(compress); +use PublicInbox::Git qw(git_unquote); use constant { - BATCH_BYTES => 1_000_000, + BATCH_BYTES => defined($ENV{XAPIAN_FLUSH_THRESHOLD}) ? + 0x7fffffff : 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; -} +my $xapianlevels = qr/\A(?:full|medium)\z/; sub new { my ($class, $ibx, $creat, $part) = @_; + my $levels = qr/\A(?:full|medium|basic)\z/; my $mainrepo = $ibx; # for "public-inbox-index" w/o entry in config my $git_dir = $mainrepo; my ($altid, $git); @@ -62,11 +46,17 @@ sub new { PublicInbox::AltId->new($ibx, $_); } @$altid ]; } - } else { # v1 + if ($ibx->{indexlevel}) { + if ($ibx->{indexlevel} =~ $levels) { + $indexlevel = $ibx->{indexlevel}; + } else { + die("Invalid indexlevel $ibx->{indexlevel}\n"); + } + } + } else { # FIXME: old tests: old tests $ibx = { mainrepo => $git_dir, version => 1 }; } $ibx = PublicInbox::InboxWritable->new($ibx); - require Search::Xapian::WritableDatabase; my $self = bless { mainrepo => $mainrepo, -inbox => $ibx, @@ -92,25 +82,41 @@ sub new { $self; } +sub need_xapian ($) { $_[0]->{indexlevel} =~ $xapianlevels } + sub _xdb_release { my ($self) = @_; - my $xdb = delete $self->{xdb} or croak 'not acquired'; - $xdb->close; + if (need_xapian($self)) { + my $xdb = delete $self->{xdb} or croak 'not acquired'; + $xdb->close; + } $self->lock_release if $self->{creat}; undef; } sub _xdb_acquire { my ($self) = @_; - croak 'already acquired' if $self->{xdb}; + my $flag; my $dir = $self->xdir; - my $flag = Search::Xapian::DB_OPEN; + if (need_xapian($self)) { + croak 'already acquired' if $self->{xdb}; + PublicInbox::Search::load_xapian(); + require Search::Xapian::WritableDatabase; + $flag = $self->{creat} ? + Search::Xapian::DB_CREATE_OR_OPEN() : + Search::Xapian::DB_OPEN(); + } if ($self->{creat}) { require File::Path; $self->lock_acquire; - File::Path::mkpath($dir); - $flag = Search::Xapian::DB_CREATE_OR_OPEN; + + # don't create empty Xapian directories if we don't need Xapian + my $is_part = defined($self->{partition}); + if (!$is_part || ($is_part && need_xapian($self))) { + File::Path::mkpath($dir); + } } + return unless defined $flag; $self->{xdb} = Search::Xapian::WritableDatabase->new($dir, $flag); } @@ -271,7 +277,7 @@ sub index_body ($$$) { sub add_xapian ($$$$$) { my ($self, $mime, $num, $oid, $mids, $mid0) = @_; my $smsg = PublicInbox::SearchMsg->new($mime); - my $doc = $smsg->{doc}; + my $doc = Search::Xapian::Document->new; my $subj = $smsg->subject; add_val($doc, PublicInbox::Search::TS(), $smsg->ts); my @ds = gmtime($smsg->ds); @@ -294,24 +300,11 @@ sub add_xapian ($$$$$) { $self->index_text($fn, 1, 'XFN'); } - return if $ct =~ m!\btext/x?html\b!i; - - my $s = eval { $part->body_str }; - if ($@) { - if ($ct =~ m!\btext/plain\b!i) { - # Try to assume UTF-8 because Alpine - # seems to do wacky things and set - # charset=X-UNKNOWN - $part->charset_set('UTF-8'); - $s = eval { $part->body_str }; - $s = $part->body if $@; - } - } + my ($s, undef) = msg_part_text($part, $ct); defined $s or return; my (@orig, @quot); - my $body = $part->body; - my @lines = split(/\n/, $body); + my @lines = split(/\n/, $s); while (defined(my $l = shift @lines)) { if ($l =~ /^>/) { $self->index_body(\@orig, $doc) if @orig; @@ -356,7 +349,6 @@ sub add_xapian ($$$$$) { sub add_message { # mime = Email::MIME object my ($self, $mime, $bytes, $num, $oid, $mid0) = @_; - my $xapianlevels = qr/\A(?:full|medium)\z/; my $mids = mids($mime->header_obj); $mid0 = $mids->[0] unless defined $mid0; # v1 compatibility unless (defined $num) { # v1 @@ -364,7 +356,7 @@ sub add_message { $num = index_mm($self, $mime); } eval { - if ($self->{indexlevel} =~ $xapianlevels) { + if (need_xapian($self)) { $self->add_xapian($mime, $num, $oid, $mids, $mid0) } if (my $over = $self->{over}) { @@ -387,6 +379,7 @@ sub find_doc_ids { ($db->postlist_begin($termval), $db->postlist_end($termval)); } +# v1 only sub batch_do { my ($self, $termval, $cb) = @_; my $batch_size = 1000; # don't let @ids grow too large to avoid OOM @@ -401,35 +394,45 @@ sub batch_do { } } +# v1 only, where $mid is unique sub remove_message { my ($self, $mid) = @_; - my $db = $self->{xdb}; - my $called; $mid = mid_clean($mid); - my $over = $self->{over}; + if (my $over = $self->{over}) { + my $nr = eval { $over->remove_oid(undef, $mid) }; + if ($@) { + warn "failed to remove <$mid> from overview: $@\n"; + } elsif ($nr == 0) { + warn "<$mid> missing for removal from overview\n"; + } + } + return unless need_xapian($self); + my $db = $self->{xdb}; + my $nr = 0; eval { batch_do($self, 'Q' . $mid, sub { my ($ids) = @_; $db->delete_document($_) for @$ids; - $over->delete_articles($ids) if $over; - $called = 1; + $nr = scalar @$ids; }); }; if ($@) { - warn "failed to remove message <$mid>: $@\n"; - } elsif (!$called) { - warn "cannot remove non-existent <$mid>\n"; + warn "failed to remove <$mid> from Xapian: $@\n"; + } elsif ($nr == 0) { + warn "<$mid> missing for removal from Xapian\n"; } } # MID is a hint in V2 sub remove_by_oid { my ($self, $oid, $mid) = @_; - my $db = $self->{xdb}; $self->{over}->remove_oid($oid, $mid) if $self->{over}; + return unless need_xapian($self); + my $db = $self->{xdb}; + # XXX careful, we cannot use batch_do here since we conditionally # delete documents based on other factors, so we cannot call # find_doc_ids twice. @@ -442,8 +445,8 @@ sub remove_by_oid { for (; $head != $tail; $head->inc) { my $docid = $head->get_docid; my $doc = $db->get_document($docid); - my $smsg = PublicInbox::SearchMsg->wrap($doc, $mid); - $smsg->load_expand; + my $smsg = PublicInbox::SearchMsg->wrap($mid); + $smsg->load_expand($doc); if ($smsg->{blob} eq $oid) { push(@delete, $docid); } @@ -573,7 +576,8 @@ sub read_log { my $blob = $1; if (delete $D{$blob}) { if (defined $self->{regen_down}) { - $self->{regen_down}--; + my $num = $self->{regen_down}--; + $self->{mm}->num_highwater($num); } next; } @@ -610,28 +614,45 @@ sub _git_log { my $git = $self->{git}; if (index($range, '..') < 0) { - my $regen_max = 0; - # can't use 'rev-list --count' if we use --diff-filter - my $fh = $git->popen(qw(log --pretty=tformat:%h - --no-notes --no-color --no-renames - --diff-filter=AM), $range); - ++$regen_max while <$fh>; - my (undef, $max) = $self->{mm}->minmax; - - if ($max && $max == $regen_max) { + # don't show annoying git errrors to users who run -index + # on empty inboxes + $git->qx(qw(rev-parse -q --verify), "$range^0"); + if ($?) { + open my $fh, '<', '/dev/null' or + die "failed to open /dev/null: $!\n"; + return $fh; + } + } + + # Count the new files so they can be added newest to oldest + # and still have numbers increasing from oldest to newest + my $fcount = 0; + # can't use 'rev-list --count' if we use --diff-filter + my $fh = $git->popen(qw(log --pretty=tformat:%h + --no-notes --no-color --no-renames + --diff-filter=AM), $range); + ++$fcount while <$fh>; + my $high = $self->{mm}->num_highwater; + + if (index($range, '..') < 0) { + if ($high && $high == $fcount) { # fix up old bugs in full indexes which caused messages to # not appear in Msgmap - $self->{regen_up} = $max; + $self->{regen_up} = $high; } else { # normal regen is for for fresh data - $self->{regen_down} = $regen_max; + $self->{regen_down} = $fcount; } + } else { + # Give oldest messages the smallest numbers + $self->{regen_down} = $high + $fcount; } $git->popen(qw/log --no-notes --no-color --no-renames --raw -r --no-abbrev/, $range); } +# --is-ancestor requires git 1.8.0+ sub is_ancestor ($$$) { my ($git, $cur, $tip) = @_; return 0 unless $git->check($cur); @@ -652,12 +673,30 @@ sub need_update ($$$) { ($n eq '' || $n > 0); } +# The last git commit we indexed with Xapian or SQLite (msgmap) +# This needs to account for cases where Xapian or SQLite is +# out-of-date with respect to the other. +sub _last_x_commit { + my ($self, $mm) = @_; + my $lm = $mm->last_commit || ''; + my $lx = ''; + if (need_xapian($self)) { + $lx = $self->{xdb}->get_metadata('last_commit') || ''; + } else { + $lx = $lm; + } + # Use last_commit from msgmap if it is older or unset + if (!$lm || ($lx && $lx && is_ancestor($self->{git}, $lm, $lx))) { + $lx = $lm; + } + $lx; +} + # indexes all unindexed messages (v1 only) sub _index_sync { my ($self, $opts) = @_; my $tip = $opts->{ref} || 'HEAD'; - my $reindex = $opts->{reindex}; - my ($mkey, $last_commit, $lx, $xlog); + my ($last_commit, $lx, $xlog); my $git = $self->{git}; $git->batch_prepare; @@ -665,25 +704,14 @@ sub _index_sync { my $mm = _msgmap_init($self); do { $xlog = undef; - $mkey = 'last_commit'; - $last_commit = $xdb->get_metadata('last_commit'); - $lx = $last_commit; - if ($reindex) { - $lx = ''; - $mkey = undef if $last_commit ne ''; - } - - # use last_commit from msgmap if it is older or unset - my $lm = $mm->last_commit || ''; - if (!$lm || ($lm && $lx && is_ancestor($git, $lm, $lx))) { - $lx = $lm; - } + $last_commit = _last_x_commit($self, $mm); + $lx = $opts->{reindex} ? '' : $last_commit; $self->{over}->rollback_lazy; $self->{over}->disconnect; $git->cleanup; delete $self->{txn}; - $xdb->cancel_transaction; + $xdb->cancel_transaction if $xdb; $xdb = _xdb_release($self); # ensure we leak no FDs to "git log" with Xapian <= 1.2 @@ -691,7 +719,7 @@ sub _index_sync { $xlog = _git_log($self, $range); $xdb = $self->begin_txn_lazy; - } while ($xdb->get_metadata('last_commit') ne $last_commit); + } while (_last_x_commit($self, $mm) ne $last_commit); my $dbh = $mm->{dbh} if $mm; my $cb = sub { @@ -705,10 +733,10 @@ sub _index_sync { } $dbh->commit; } - if ($mkey && $newest) { - my $cur = $xdb->get_metadata($mkey); + if ($newest && need_xapian($self)) { + my $cur = $xdb->get_metadata('last_commit'); if (need_update($self, $cur, $newest)) { - $xdb->set_metadata($mkey, $newest); + $xdb->set_metadata('last_commit', $newest); } } $self->commit_txn_lazy; @@ -773,7 +801,7 @@ sub begin_txn_lazy { $self->{-inbox}->with_umask(sub { my $xdb = $self->{xdb} || $self->_xdb_acquire; $self->{over}->begin_lazy if $self->{over}; - $xdb->begin_transaction; + $xdb->begin_transaction if $xdb; $self->{txn} = 1; $xdb; }); @@ -783,14 +811,18 @@ sub commit_txn_lazy { my ($self) = @_; delete $self->{txn} or return; $self->{-inbox}->with_umask(sub { - $self->{xdb}->commit_transaction; + if (my $xdb = $self->{xdb}) { + $xdb->commit_transaction; + } $self->{over}->commit_lazy if $self->{over}; }); } sub worker_done { my ($self) = @_; - die "$$ $0 xdb not released\n" if $self->{xdb}; + if (need_xapian($self)) { + die "$$ $0 xdb not released\n" if $self->{xdb}; + } die "$$ $0 still in transaction\n" if $self->{txn}; }