X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearch.pm;h=ca389e320b2706edacbbd578b33dacc65812dece;hb=7503aeb540af5afd5cb1b554b3c29f35f5fc918d;hp=eac11bd44f905bd854af6e80cc4de104d26da93a;hpb=feabfb1809b911fc97538282234c8b1f087ddb6a;p=public-inbox.git diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm index eac11bd4..ca389e32 100644 --- a/lib/PublicInbox/Search.pm +++ b/lib/PublicInbox/Search.pm @@ -8,16 +8,14 @@ use strict; use warnings; # values for searching -use constant TS => 0; # timestamp -use constant NUM => 1; # NNTP article number -use constant BYTES => 2; # :bytes as defined in RFC 3977 -use constant LINES => 3; # :lines as defined in RFC 3977 -use constant YYYYMMDD => 4; # for searching in the WWW UI +use constant TS => 0; # Received: header in Unix time +use constant YYYYMMDD => 1; # for searching in the WWW UI +use constant NUM => 2; # NNTP article number use Search::Xapian qw/:standard/; use PublicInbox::SearchMsg; use PublicInbox::MIME; -use PublicInbox::MID qw/mid_clean id_compress/; +use PublicInbox::MID qw/id_compress/; # This is English-only, everything else is non-standard and may be confused as # a prefix common in patch emails @@ -56,9 +54,10 @@ my %bool_pfx_internal = ( ); my %bool_pfx_external = ( - mid => 'XMID', # Message-ID (full/exact) + mid => 'Q', # Message-ID (full/exact), this is mostly uniQue ); +my $non_quoted_body = 'XNQ XDFN XDFA XDFB XDFHH XDFCTX XDFPRE XDFPOST'; my %prob_prefix = ( # for mairix compatibility s => 'S', @@ -69,12 +68,12 @@ my %prob_prefix = ( c => 'XCC', tcf => 'XTO XCC A', a => 'XTO XCC A', - b => 'XNQ XQUOT', - bs => 'XNQ XQUOT S', + b => $non_quoted_body . ' XQUOT', + bs => $non_quoted_body . ' XQUOT S', n => 'XFN', q => 'XQUOT', - nq => 'XNQ', + nq => $non_quoted_body, dfn => 'XDFN', dfa => 'XDFA', dfb => 'XDFB', @@ -85,7 +84,7 @@ my %prob_prefix = ( dfblob => 'XDFPRE XDFPOST', # default: - '' => 'XM S A XNQ XQUOT XFN', + '' => 'XM S A XQUOT XFN ' . $non_quoted_body, ); # not documenting m: and mid: for now, the using the URLs works w/o Xapian @@ -124,7 +123,10 @@ sub xdir { if ($self->{version} == 1) { "$self->{mainrepo}/public-inbox/xapian" . SCHEMA_VERSION; } else { - "$self->{mainrepo}/xap" . SCHEMA_VERSION; + my $dir = "$self->{mainrepo}/xap" . SCHEMA_VERSION; + my $part = $self->{partition}; + defined $part or die "partition not given"; + $dir .= "/$part"; } } @@ -141,11 +143,36 @@ sub new { altid => $altid, version => $version, }, $class; - $self->{xdb} = Search::Xapian::Database->new($self->xdir); + if ($version >= 2) { + my $dir = "$self->{mainrepo}/xap" . SCHEMA_VERSION; + my $xdb; + my $parts = 0; + foreach my $part (<$dir/*>) { + -d $part && $part =~ m!/\d+\z! or next; + $parts++; + my $sub = Search::Xapian::Database->new($part); + if ($xdb) { + $xdb->add_database($sub); + } else { + $xdb = $sub; + } + } + $self->{xdb} = $xdb; + $self->{skel} = Search::Xapian::Database->new("$dir/skel"); + } else { + $self->{xdb} = Search::Xapian::Database->new($self->xdir); + } $self; } -sub reopen { $_[0]->{xdb}->reopen } +sub reopen { + my ($self) = @_; + $self->{xdb}->reopen; + if (my $skel = $self->{skel}) { + $skel->reopen; + } + $self; # make chaining easier +} # read-only sub query { @@ -163,9 +190,8 @@ sub query { sub get_thread { my ($self, $mid, $opts) = @_; - my $smsg = eval { $self->lookup_message($mid) }; - - return { total => 0, msgs => [] } unless $smsg; + my $smsg = first_smsg_by_mid($self, $mid) or + return { total => 0, msgs => [] }; my $qtid = Search::Xapian::Query->new('G' . $smsg->thread_id); my $path = $smsg->path; if (defined $path && $path ne '') { @@ -179,24 +205,27 @@ sub get_thread { # always sort threads by timestamp, this makes life easier # for the threading algorithm (in SearchThread.pm) $opts->{asc} = 1; - + $opts->{enquire} = enquire_skel($self); _do_enquire($self, $qtid, $opts); } sub retry_reopen { my ($self, $cb) = @_; my $ret; - for (1..10) { + for my $i (1..10) { eval { $ret = $cb->() }; return $ret unless $@; # Exception: The revision being read has been discarded - # you should call Xapian::Database::reopen() if (ref($@) eq 'Search::Xapian::DatabaseModifiedError') { + warn "reopen try #$i on $@\n"; reopen($self); } else { + warn "ref: ", ref($@), "\n"; die; } } + die "Too many Xapian database modifications in progress\n"; } sub _do_enquire { @@ -206,7 +235,7 @@ sub _do_enquire { sub _enquire_once { my ($self, $query, $opts) = @_; - my $enquire = $self->enquire; + my $enquire = $opts->{enquire} || enquire($self); if (defined $query) { $query = Search::Xapian::Query->new(OP_AND,$query,$mail_query); } else { @@ -288,67 +317,87 @@ sub num_range_processor { sub query_xover { my ($self, $beg, $end, $offset) = @_; my $qp = Search::Xapian::QueryParser->new; - $qp->set_database($self->{xdb}); + $qp->set_database($self->{skel} || $self->{xdb}); $qp->add_valuerangeprocessor($self->num_range_processor); my $query = $qp->parse_query("$beg..$end", QP_FLAGS); - _do_enquire($self, $query, {num => 1, limit => 200, offset => $offset}); + my $opts = { + enquire => enquire_skel($self), + num => 1, + limit => 200, + offset => $offset, + }; + _do_enquire($self, $query, $opts); } -sub lookup_message { - my ($self, $mid) = @_; - $mid = mid_clean($mid); +sub query_ts { + my ($self, $ts, $opts) = @_; + my $qp = $self->{qp_ts} ||= eval { + my $q = Search::Xapian::QueryParser->new; + $q->set_database($self->{skel} || $self->{xdb}); + $q->add_valuerangeprocessor( + Search::Xapian::NumberValueRangeProcessor->new(TS)); + $q + }; + my $query = $qp->parse_query($ts, QP_FLAGS); + $opts->{enquire} = enquire_skel($self); + _do_enquire($self, $query, $opts); +} - my $doc_id = $self->find_first_doc_id('XMID' . $mid); +sub first_smsg_by_mid { + my ($self, $mid) = @_; my $smsg; - if (defined $doc_id) { - # raises on error: - my $doc = $self->{xdb}->get_document($doc_id); - $smsg = PublicInbox::SearchMsg->wrap($doc, $mid); - $smsg->{doc_id} = $doc_id; - } + retry_reopen($self, sub { + each_smsg_by_mid($self, $mid, sub { $smsg = $_[0]; undef }); + }); $smsg; } -sub lookup_mail { # no ghosts! - my ($self, $mid) = @_; +sub lookup_article { + my ($self, $num) = @_; + my $term = 'XNUM'.$num; + my $db = $self->{skel} || $self->{xdb}; retry_reopen($self, sub { - my $smsg = lookup_message($self, $mid) or return; + my $head = $db->postlist_begin($term); + my $tail = $db->postlist_end($term); + return if $head->equal($tail); + my $doc_id = $head->get_docid; + return unless defined $doc_id; + $head->inc; + if ($head->nequal($tail)) { + my $loc= $self->{mainrepo} . + ($self->{skel} ? 'skel' : 'xdb'); + warn "article #$num is not unique in $loc\n"; + } + # raises on error: + my $doc = $db->get_document($doc_id); + my $smsg = PublicInbox::SearchMsg->wrap($doc); + $smsg->{doc_id} = $doc_id; $smsg->load_expand; }); } -sub find_unique_doc_id { - my ($self, $termval) = @_; - - my ($begin, $end) = $self->find_doc_ids($termval); - - return undef if $begin->equal($end); # not found - - my $rv = $begin->get_docid; - - # sanity check - $begin->inc; - $begin->equal($end) or die "Term '$termval' is not unique\n"; - $rv; -} - -# returns begin and end PostingIterator -sub find_doc_ids { - my ($self, $termval) = @_; - my $db = $self->{xdb}; - - ($db->postlist_begin($termval), $db->postlist_end($termval)); -} - -sub find_first_doc_id { - my ($self, $termval) = @_; - - my ($begin, $end) = $self->find_doc_ids($termval); - - return undef if $begin->equal($end); # not found - - $begin->get_docid; +sub each_smsg_by_mid { + my ($self, $mid, $cb) = @_; + # XXX retry_reopen isn't necessary for V2Writable, but the PSGI + # interface will need it... + my $db = $self->{skel} || $self->{xdb}; + my $term = 'Q' . $mid; + my $head = $db->postlist_begin($term); + my $tail = $db->postlist_end($term); + if ($head == $tail) { + $db->reopen; + $head = $db->postlist_begin($term); + $tail = $db->postlist_end($term); + } + return ($head, $tail, $db) if wantarray; + for (; $head->nequal($tail); $head->inc) { + my $doc_id = $head->get_docid; + my $doc = $db->get_document($doc_id); + my $smsg = PublicInbox::SearchMsg->wrap($doc, $mid); + $smsg->{doc_id} = $doc_id; + $cb->($smsg) or return; + } } # normalize subjects so they are suitable as pathnames for URLs @@ -375,6 +424,15 @@ sub enquire { $self->{enquire} ||= Search::Xapian::Enquire->new($self->{xdb}); } +sub enquire_skel { + my ($self) = @_; + if (my $skel = $self->{skel}) { + $self->{enquire_skel} ||= Search::Xapian::Enquire->new($skel); + } else { + enquire($self); + } +} + sub help { my ($self) = @_; $self->qp; # parse altids