From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 4BE9D1FA17 for ; Thu, 20 Aug 2020 20:24:59 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 07/23] search: v2: ensure shards are numerically sorted Date: Thu, 20 Aug 2020 20:24:41 +0000 Message-Id: <20200820202457.21042-8-e@yhbt.net> In-Reply-To: <20200820202457.21042-1-e@yhbt.net> References: <20200820202457.21042-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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. --- lib/PublicInbox/Search.pm | 27 ++++++++++++++++----------- 1 file 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);