]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Search.pm
search: reduce columns stored in Xapian
[public-inbox.git] / lib / PublicInbox / Search.pm
index 7cad31a711d5fa300ec16a030ad22288cfdb79cf..ca389e320b2706edacbbd578b33dacc65812dece 100644 (file)
@@ -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
@@ -173,6 +171,7 @@ sub reopen {
        if (my $skel = $self->{skel}) {
                $skel->reopen;
        }
+       $self; # make chaining easier
 }
 
 # read-only
@@ -191,9 +190,8 @@ sub query {
 
 sub get_thread {
        my ($self, $mid, $opts) = @_;
-       my $smsg = retry_reopen($self, sub { lookup_skeleton($self, $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 '') {
@@ -214,18 +212,20 @@ sub get_thread {
 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 {
@@ -344,67 +344,37 @@ sub query_ts {
        _do_enquire($self, $query, $opts);
 }
 
-sub lookup_skeleton {
-       my ($self, $mid) = @_;
-       my $skel = $self->{skel} or return lookup_message($self, $mid);
-       $mid = mid_clean($mid);
-       my $term = 'Q' . $mid;
-       my $smsg;
-       my $beg = $skel->postlist_begin($term);
-       if ($beg != $skel->postlist_end($term)) {
-               my $doc_id = $beg->get_docid;
-               if (defined $doc_id) {
-                       # raises on error:
-                       my $doc = $skel->get_document($doc_id);
-                       $smsg = PublicInbox::SearchMsg->wrap($doc, $mid);
-                       $smsg->{doc_id} = $doc_id;
-               }
-       }
-       $smsg;
-}
-
-sub lookup_message {
+sub first_smsg_by_mid {
        my ($self, $mid) = @_;
-       $mid = mid_clean($mid);
-
-       my $doc_id = $self->find_first_doc_id('Q' . $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;
-       }
-       $smsg;
-}
-
-sub lookup_mail { # no ghosts!
-       my ($self, $mid) = @_;
        retry_reopen($self, sub {
-               my $smsg = lookup_skeleton($self, $mid) or return;
-               $smsg->load_expand;
+               each_smsg_by_mid($self, $mid, sub { $smsg = $_[0]; undef });
        });
+       $smsg;
 }
 
 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;
+       my $db = $self->{skel} || $self->{xdb};
+       retry_reopen($self, sub {
+               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 each_smsg_by_mid {
@@ -415,6 +385,12 @@ sub each_smsg_by_mid {
        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);
@@ -424,39 +400,6 @@ sub each_smsg_by_mid {
        }
 }
 
-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;
-}
-
 # normalize subjects so they are suitable as pathnames for URLs
 # XXX: consider for removal
 sub subject_path {