]> 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 802984b0ab8b7109bc0ca6e6b7ce6f721dc5c884..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;
@@ -173,6 +174,7 @@ sub reopen {
        if (my $skel = $self->{skel}) {
                $skel->reopen;
        }
+       $self; # make chaining easier
 }
 
 # read-only
@@ -330,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);
@@ -367,7 +383,7 @@ 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;
        });
 }
@@ -395,13 +411,21 @@ sub lookup_article {
 
 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;