]> 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 4dc274723d6ca8e34ca19821256308d47ea7058d..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
@@ -381,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;
        });
 }
@@ -409,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;