]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Search.pm
search: reopen DB if each_smsg_by_mid fails
[public-inbox.git] / lib / PublicInbox / Search.pm
index fb7a126a781ccb199471891cd146517bfcd3423b..a4e2498eb596d1cd9346b6620ffa464aa5a1ce6d 100644 (file)
@@ -8,11 +8,12 @@ use strict;
 use warnings;
 
 # values for searching
-use constant TS => 0; # timestamp
+use constant DS => 0; # Date: header in Unix time
 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 => 4;  # Received: header in Unix time
+use constant YYYYMMDD => 5; # for searching in the WWW UI
 
 use Search::Xapian qw/:standard/;
 use PublicInbox::SearchMsg;
@@ -59,6 +60,7 @@ my %bool_pfx_external = (
        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 +71,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 +87,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
@@ -172,6 +174,7 @@ sub reopen {
        if (my $skel = $self->{skel}) {
                $skel->reopen;
        }
+       $self; # make chaining easier
 }
 
 # read-only
@@ -329,6 +332,20 @@ sub query_xover {
        _do_enquire($self, $query, $opts);
 }
 
+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);
+}
+
 sub lookup_skeleton {
        my ($self, $mid) = @_;
        my $skel = $self->{skel} or return lookup_message($self, $mid);
@@ -366,20 +383,49 @@ sub lookup_message {
 sub lookup_mail { # no ghosts!
        my ($self, $mid) = @_;
        retry_reopen($self, sub {
-               my $smsg = lookup_message($self, $mid) or return;
+               my $smsg = lookup_skeleton($self, $mid) or return;
                $smsg->load_expand;
        });
 }
 
+sub lookup_article {
+       my ($self, $num) = @_;
+       my $term = 'XNUM'.$num;
+       my $smsg;
+       eval {
+               retry_reopen($self, sub {
+                       my $db = $self->{skel} || $self->{xdb};
+                       my $head = $db->postlist_begin($term);
+                       return if $head == $db->postlist_end($term);
+                       my $doc_id = $head->get_docid;
+                       return unless defined $doc_id;
+                       # raises on error:
+                       my $doc = $db->get_document($doc_id);
+                       $smsg = PublicInbox::SearchMsg->wrap($doc);
+                       $smsg->load_expand;
+                       $smsg->{doc_id} = $doc_id;
+               });
+       };
+       $smsg;
+}
+
 sub each_smsg_by_mid {
        my ($self, $mid, $cb) = @_;
-       my $xdb = $self->{xdb};
        # XXX retry_reopen isn't necessary for V2Writable, but the PSGI
        # interface will need it...
-       my ($head, $tail) = $self->find_doc_ids('Q' . $mid);
+       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 = $xdb->get_document($doc_id);
+               my $doc = $db->get_document($doc_id);
                my $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
                $smsg->{doc_id} = $doc_id;
                $cb->($smsg) or return;