1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # "lei inspect" general purpose inspector for stuff in SQLite and
5 # Xapian. Will eventually be useful with plain public-inboxes,
6 # not just lei/store. This is totally half-baked at the moment
7 # but useful for testing.
8 package PublicInbox::LeiInspect;
11 use parent qw(PublicInbox::IPC);
12 use PublicInbox::Config;
13 use PublicInbox::MID qw(mids);
14 use PublicInbox::NetReader qw(imap_uri nntp_uri);
16 sub inspect_blob ($$) {
17 my ($lei, $oidhex) = @_;
19 if (my $lse = $lei->{lse}) {
20 my $oidbin = pack('H*', $oidhex);
21 my @docids = $lse ? $lse->over->oidbin_exists($oidbin) : ();
22 $ent->{'lei/store'} = \@docids if @docids;
24 if (my $loc = $lms ? $lms->locations_for($oidbin) : undef) {
25 $ent->{'mail-sync'} = $loc;
31 sub inspect_imap_uid ($$) {
32 my ($lei, $uid_uri) = @_;
34 my $lms = $lei->lms or return $ent;
35 my @oidhex = $lms->imap_oidhex($lei, $uid_uri);
36 $ent->{$$uid_uri} = @oidhex == 1 ? $oidhex[0] :
37 ((@oidhex == 0) ? undef : \@oidhex);
41 sub inspect_nntp_range {
43 my ($ng, $beg, $end) = $uri->group;
47 my $ret = { "$uri" => $ent };
48 my $lms = $lei->lms or return $ret;
49 my $err = $lms->arg2folder($lei, my $folders = [ $$uri ]);
51 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
54 for my $art ($beg..$end) {
55 my @oidhex = map { unpack('H*', $_) }
56 $lms->num_oidbin($folders->[0], $art);
57 $ent->{$art} = @oidhex == 1 ? $oidhex[0] :
58 ((@oidhex == 0) ? undef : \@oidhex);
63 sub inspect_sync_folder ($$) {
64 my ($lei, $folder) = @_;
66 my $lms = $lei->lms or return $ent;
67 my $folders = [ $folder ];
68 my $err = $lms->arg2folder($lei, $folders);
71 $lei->qerr("# no folders match $folder (non-fatal)");
74 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
76 for my $f (@$folders) {
77 $ent->{$f} = $lms->location_stats($f); # may be undef
82 sub inspect_docid ($$;$) {
83 my ($lei, $docid, $ent) = @_;
84 require PublicInbox::Search;
87 if ($xdb = delete $ent->{xdb}) { # from inspect_num
88 } elsif (defined(my $dir = $lei->{opt}->{dir})) {
90 $xdb = $PublicInbox::Search::X{Database}->new($dir);
92 $xdb = $lei->{lse}->xdb;
94 $xdb or return $lei->fail('no Xapian DB');
95 my $doc = $xdb->get_document($docid); # raises
96 my $data = $doc->get_data;
97 $ent->{docid} = $docid;
98 $ent->{data_length} = length($data);
99 $ent->{description} = $doc->get_description;
100 $ent->{$_} = $doc->$_ for (qw(termlist_count values_count));
101 my $cur = $doc->termlist_begin;
102 my $end = $doc->termlist_end;
103 for (; $cur != $end; $cur++) {
104 my $tn = $cur->get_termname;
105 $tn =~ s/\A([A-Z]+)// or warn "$tn no prefix! (???)";
106 my $term = ($1 // '');
107 push @{$ent->{terms}->{$term}}, $tn;
109 @$_ = sort(@$_) for values %{$ent->{terms} // {}};
110 $cur = $doc->values_begin;
111 $end = $doc->values_end;
112 for (; $cur != $end; $cur++) {
113 my $n = $cur->get_valueno;
114 my $v = $cur->get_value;
115 my $iv = PublicInbox::Search::sortable_unserialise($v);
116 $v = $iv + 0 if defined $iv;
117 # not using ->[$n] since we may have large gaps in $n
118 $ent->{'values'}->{$n} = $v;
124 my ($lei, $dir) = @_;
125 if (-f "$dir/ei.lock") {
126 require PublicInbox::ExtSearch;
127 PublicInbox::ExtSearch->new($dir);
128 } elsif (-f "$dir/inbox.lock" || -d "$dir/public-inbox") {
129 require PublicInbox::Inbox; # v2, v1
130 bless { inboxdir => $dir }, 'PublicInbox::Inbox';
132 $lei->fail("no (indexed) inbox or extindex at $dir");
136 sub inspect_num ($$) {
137 my ($lei, $num) = @_;
139 my $ent = { num => $num };
140 if (defined(my $dir = $lei->{opt}->{dir})) {
141 $ibx = dir2ibx($lei, $dir) or return;
142 if ($ent->{xdb} = $ibx->xdb) {
143 my $num2docid = $lei->{lse}->can('num2docid');
144 $docid = $num2docid->($ibx, $num);
148 $lei->{lse}->xdb; # set {nshard} for num2docid
149 $docid = $lei->{lse}->num2docid($num);
151 if ($ibx && $ibx->over) {
152 my $smsg = $ibx->over->get_art($num);
153 $ent->{smsg} = { %$smsg } if $smsg;
155 defined($docid) ? inspect_docid($lei, $docid, $ent) : $ent;
158 sub inspect_mid ($$) {
159 my ($lei, $mid) = @_;
161 my $ent = { mid => $mid };
162 if (defined(my $dir = $lei->{opt}->{dir})) {
163 my $num2docid = $lei->{lse}->can('num mid => [ $mid ] 2docid');
164 $ibx = dir2ibx($lei, $dir) or return;
165 # $ent->{xdb} = $ibx->xdb //
166 # return $lei->fail("no Xapian DB for $dir");
169 $lei->{lse}->xdb; # set {nshard} for num2docid
171 if ($ibx && $ibx->over) {
173 while (my $smsg = $ibx->over->next_by_mid($mid, \$id, \$prev)) {
174 push @{$ent->{smsg}}, { %$smsg }
181 my ($lei, $item, $more) = @_;
183 if ($item =~ /\Ablob:(.+)/) {
184 $ent = inspect_blob($lei, $1);
185 } elsif ($item =~ m!\A(?:maildir|mh):!i || -d $item) {
186 $ent = inspect_sync_folder($lei, $item);
187 } elsif ($item =~ m!\Adocid:([0-9]+)\z!) {
188 $ent = inspect_docid($lei, $1 + 0);
189 } elsif ($item =~ m!\Anum:([0-9]+)\z!) {
190 $ent = inspect_num($lei, $1 + 0);
191 } elsif ($item =~ m!\A(?:mid|m):(.+)\z!) {
192 $ent = inspect_mid($lei, $1);
193 } elsif (my $iuri = imap_uri($item)) {
194 if (defined($iuri->uid)) {
195 $ent = inspect_imap_uid($lei, $iuri);
197 $ent = inspect_sync_folder($lei, $item);
199 } elsif (my $nuri = nntp_uri($item)) {
200 if (defined(my $mid = $nuri->message)) {
201 $ent = inspect_mid($lei, $mid);
203 my ($group, $beg, $end) = $nuri->group;
205 $ent = inspect_nntp_range($lei, $nuri);
207 $ent = inspect_sync_folder($lei, $item);
210 } else { # TODO: more things
211 return $lei->fail("$item not understood");
213 $lei->out($lei->{json}->encode($ent));
214 $lei->out(',') if $more;
218 sub inspect_argv { # via wq_do
220 my ($lei, $argv) = delete @$self{qw(lei argv)};
221 my $multi = scalar(@$argv) > 1;
222 $lei->{1}->autoflush(0);
223 $lei->out('[') if $multi;
224 while (defined(my $x = shift @$argv)) {
225 inspect1($lei, $x, scalar(@$argv)) or return;
227 $lei->out(']') if $multi;
230 sub inspect_start ($$) {
231 my ($lei, $argv) = @_;
232 my $self = bless { lei => $lei, argv => $argv }, __PACKAGE__;
233 my ($op_c, $ops) = $lei->workers_start($self, 1);
235 $lei->wait_wq_events($op_c, $ops);
236 $self->wq_do('inspect_argv');
240 sub ins_add { # InputPipe->consume callback
241 my ($lei) = @_; # $_[1] = $rbuf
243 $_[1] eq '' and return eval {
244 my $str = delete $lei->{istr};
245 $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
246 my $eml = PublicInbox::Eml->new(\$str);
247 inspect_start($lei, [
248 'blob:'.$lei->git_oid($eml)->hexdigest,
249 map { "mid:$_" } @{mids($eml)} ]);
251 $lei->{istr} .= $_[1];
253 $lei->fail("error reading stdin: $!");
258 my ($lei, @argv) = @_;
259 $lei->{json} = ref(PublicInbox::Config::json())->new->utf8->canonical;
260 $lei->{lse} = ($lei->{opt}->{external} // 1) ? do {
261 my $sto = $lei->_lei_store;
262 $sto ? $sto->search : undef;
264 my $isatty = -t $lei->{1};
265 $lei->{json}->pretty(1)->indent(2) if $lei->{opt}->{pretty} || $isatty;
266 $lei->start_pager if $isatty;
267 if ($lei->{opt}->{stdin}) {
268 return $lei->fail(<<'') if @argv;
269 no args allowed on command-line with --stdin
271 require PublicInbox::InputPipe;
272 PublicInbox::InputPipe::consume($lei->{0}, \&ins_add, $lei);
274 inspect_start($lei, \@argv);
278 sub _complete_inspect {
279 my ($lei, @argv) = @_;
280 my $lms = $lei->lms or return;
281 my $match_cb = $lei->complete_url_prepare(\@argv);
282 map { $match_cb->($_) } $lms->folders;