about summary refs log tree commit homepage
path: root/lib/PublicInbox/Search.pm
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-08-20 20:24:41 +0000
committerEric Wong <e@yhbt.net>2020-08-20 21:11:10 +0000
commitdd709cbfe8a33b89ba2b790a1d55b4dfeded30b0 (patch)
tree5556a318899ecbeab18b507e6b65bd031430c5cf /lib/PublicInbox/Search.pm
parentcda7ae7fb8da9eeb0e8aeb3df5a5ae8a9675102c (diff)
downloadpublic-inbox-dd709cbfe8a33b89ba2b790a1d55b4dfeded30b0.tar.gz
This seems required to correctly get the NNTP article number
from Xapian docid on combined Xapian DBs.  The default
(ASCII-betical) sorting was only acceptable for -imapd users
until somebody hit 11 (or more) shards, which is a rare case.
Diffstat (limited to 'lib/PublicInbox/Search.pm')
-rw-r--r--lib/PublicInbox/Search.pm27
1 files changed, 16 insertions, 11 deletions
diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index 4e08aed7..4d02a7c1 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -177,19 +177,24 @@ sub _xdb ($) {
         my ($xdb, $slow_phrase);
         my $qpf = \($self->{qp_flags} ||= $QP_FLAGS);
         if ($self->{ibx_ver} >= 2) {
-                my $n = 0;
-                foreach my $shard (<$dir/*>) {
-                        -d $shard && $shard =~ m!/[0-9]+\z! or next;
-                        my $sub = $X{Database}->new($shard);
-                        if ($xdb) {
-                                $xdb->add_database($sub);
-                        } else {
-                                $xdb = $sub;
+                my @xdb;
+                opendir(my $dh, $dir) or return; # not initialized yet
+
+                # We need numeric sorting so shard[0] is first for reading
+                # Xapian metadata, if needed
+                for (sort { $a <=> $b } grep(/\A[0-9]+\z/, readdir($dh))) {
+                        my $shard_dir = "$dir/$_";
+                        if (-d $shard_dir && -r _) {
+                                push @xdb, $X{Database}->new($shard_dir);
+                                $slow_phrase ||= -f "$shard_dir/iamchert";
+                        } else { # gaps from missing epochs throw off mdocid()
+                                warn "E: $shard_dir missing or unreadable\n";
+                                return;
                         }
-                        $slow_phrase ||= -f "$shard/iamchert";
-                        ++$n;
                 }
-                $self->{nshard} = $n;
+                $self->{nshard} = scalar(@xdb);
+                $xdb = shift @xdb;
+                $xdb->add_database($_) for @xdb;
         } else {
                 $slow_phrase = -f "$dir/iamchert";
                 $xdb = $X{Database}->new($dir);