]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Search.pm
search: cleanup uniqueness checking
[public-inbox.git] / lib / PublicInbox / Search.pm
index f08b98702492561f55df14c9f0fdd12a6b7ed1ca..584a508e45f15d6c80cbdaa46ff8fa466cbc8de9 100644 (file)
@@ -396,9 +396,16 @@ sub lookup_article {
                retry_reopen($self, sub {
                        my $db = $self->{skel} || $self->{xdb};
                        my $head = $db->postlist_begin($term);
-                       return if $head == $db->postlist_end($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);
                        $smsg = PublicInbox::SearchMsg->wrap($doc);
@@ -417,6 +424,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);
@@ -426,21 +439,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) = @_;