about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-10-02 08:16:22 +0000
committerEric Wong <e@80x24.org>2021-10-02 20:09:22 +0000
commit502297d2cda95376b38e7d510f1d34fe89a3ec0e (patch)
tree92282db59ecb327091d740000664ad7c333a6309
parent3826b39b6433ecec96a0230ae4602b7c819fde66 (diff)
downloadpublic-inbox-502297d2cda95376b38e7d510f1d34fe89a3ec0e.tar.gz
This fixes inspect for uninitialized instances, and adds Xapian
("xdoc") output if available.

Reported-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
Message-ID: <20211001204943.l4yl6xvc45c5eapz@meerkat.local>
-rw-r--r--MANIFEST1
-rw-r--r--lib/PublicInbox/LeiInspect.pm59
-rw-r--r--t/lei-inspect.t14
3 files changed, 48 insertions, 26 deletions
diff --git a/MANIFEST b/MANIFEST
index 929f5f86..74b28d2d 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -450,6 +450,7 @@ t/lei-import-maildir.t
 t/lei-import-nntp.t
 t/lei-import.t
 t/lei-index.t
+t/lei-inspect.t
 t/lei-lcat.t
 t/lei-mirror.psgi
 t/lei-mirror.t
diff --git a/lib/PublicInbox/LeiInspect.pm b/lib/PublicInbox/LeiInspect.pm
index f18e31c5..590dfdab 100644
--- a/lib/PublicInbox/LeiInspect.pm
+++ b/lib/PublicInbox/LeiInspect.pm
@@ -78,22 +78,9 @@ sub inspect_sync_folder ($$) {
         $ent
 }
 
-sub inspect_docid ($$;$) {
-        my ($lei, $docid, $ent) = @_;
-        require PublicInbox::Search;
-        $ent //= {};
-        my $xdb;
-        if ($xdb = delete $ent->{xdb}) { # from inspect_num
-        } elsif (defined(my $dir = $lei->{opt}->{dir})) {
-                no warnings 'once';
-                $xdb = $PublicInbox::Search::X{Database}->new($dir);
-        } else {
-                $xdb = $lei->{lse}->xdb;
-        }
-        $xdb or return $lei->fail('no Xapian DB');
-        my $doc = $xdb->get_document($docid); # raises
+sub _inspect_doc ($$) {
+        my ($ent, $doc) = @_;
         my $data = $doc->get_data;
-        $ent->{docid} = $docid;
         $ent->{data_length} = length($data);
         $ent->{description} = $doc->get_description;
         $ent->{$_} = $doc->$_ for (qw(termlist_count values_count));
@@ -119,6 +106,24 @@ sub inspect_docid ($$;$) {
         $ent;
 }
 
+sub inspect_docid ($$;$) {
+        my ($lei, $docid, $ent) = @_;
+        require PublicInbox::Search;
+        $ent //= {};
+        my $xdb;
+        if ($xdb = delete $ent->{xdb}) { # from inspect_num
+        } elsif (defined(my $dir = $lei->{opt}->{dir})) {
+                no warnings 'once';
+                $xdb = $PublicInbox::Search::X{Database}->new($dir);
+        } elsif ($lei->{lse}) {
+                $xdb = $lei->{lse}->xdb;
+        }
+        $xdb or return $lei->fail('no Xapian DB');
+        my $doc = $xdb->get_document($docid); # raises
+        $ent->{docid} = $docid;
+        _inspect_doc($ent, $doc);
+}
+
 sub dir2ibx ($$) {
         my ($lei, $dir) = @_;
         if (-f "$dir/ei.lock") {
@@ -138,11 +143,9 @@ sub inspect_num ($$) {
         my $ent = { num => $num };
         if (defined(my $dir = $lei->{opt}->{dir})) {
                 $ibx = dir2ibx($lei, $dir) or return;
-                if ($ent->{xdb} = $ibx->xdb) {
-                        my $num2docid = $lei->{lse}->can('num2docid');
-                        $docid = $num2docid->($ibx, $num);
-                }
-        } else {
+                $ent->{xdb} = $ibx->xdb and # for inspect_docid
+                        $docid = PublicInbox::LeiSearch::num2docid($ibx, $num);
+        } elsif ($lei->{lse}) {
                 $ibx = $lei->{lse};
                 $lei->{lse}->xdb; # set {nshard} for num2docid
                 $docid = $lei->{lse}->num2docid($num);
@@ -156,16 +159,12 @@ sub inspect_num ($$) {
 
 sub inspect_mid ($$) {
         my ($lei, $mid) = @_;
-        my ($ibx, $over);
+        my $ibx;
         my $ent = { mid => $mid };
         if (defined(my $dir = $lei->{opt}->{dir})) {
-                my $num2docid = $lei->{lse}->can('num mid => [ $mid ] 2docid');
-                $ibx = dir2ibx($lei, $dir) or return;
-                # $ent->{xdb} = $ibx->xdb //
-                        # return $lei->fail("no Xapian DB for $dir");
+                $ibx = dir2ibx($lei, $dir)
         } else {
                 $ibx = $lei->{lse};
-                $lei->{lse}->xdb; # set {nshard} for num2docid
         }
         if ($ibx && $ibx->over) {
                 my ($id, $prev);
@@ -173,6 +172,14 @@ sub inspect_mid ($$) {
                         push @{$ent->{smsg}}, _json_prep($smsg);
                 }
         }
+        if ($ibx && $ibx->search) {
+                my $mset = $ibx->search->mset(qq{mid:"$mid"});
+                for (sort { $a->get_docid <=> $b->get_docid } $mset->items) {
+                        my $tmp = { docid => $_->get_docid };
+                        _inspect_doc($tmp, $_->get_document);
+                        push @{$ent->{xdoc}}, $tmp;
+                }
+        }
         $ent;
 }
 
diff --git a/t/lei-inspect.t b/t/lei-inspect.t
new file mode 100644
index 00000000..077d0d13
--- /dev/null
+++ b/t/lei-inspect.t
@@ -0,0 +1,14 @@
+#!perl -w
+# Copyright all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict; use v5.10.1; use PublicInbox::TestCommon;
+
+test_lei(sub {
+        my ($ro_home, $cfg_path) = setup_public_inboxes;
+        lei_ok qw(inspect --dir), "$ro_home/t1", 'mid:testmessage@example.com';
+        my $ent = json_utf8->decode($lei_out);
+        is(ref($ent->{smsg}), 'ARRAY', 'smsg array');
+        is(ref($ent->{xdoc}), 'ARRAY', 'xdoc array');
+});
+
+done_testing;