]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
*search: favor wantarray form of xap_terms
authorEric Wong <e@80x24.org>
Fri, 8 Dec 2023 03:54:34 +0000 (03:54 +0000)
committerEric Wong <e@80x24.org>
Sat, 9 Dec 2023 02:03:48 +0000 (02:03 +0000)
Most xap_terms callers do not benefit from the hashref
return value, and we can delay hashmap use until
List::Util::uniqstr if needed.

lib/PublicInbox/CodeSearch.pm
lib/PublicInbox/LeiSearch.pm
lib/PublicInbox/LeiStore.pm

index 3092718da2c2edf4c268f7c5336432da188f6cb3..48697cdccd11c793ef66b57f9b91ea0f1a8969cb 100644 (file)
@@ -9,6 +9,7 @@ use v5.12;
 use parent qw(PublicInbox::Search);
 use PublicInbox::Config;
 use PublicInbox::Search qw(retry_reopen int_val xap_terms);
+use PublicInbox::Compat qw(uniqstr);
 use Compress::Zlib qw(uncompress);
 use constant {
        AT => 0, # author time YYYYMMDDHHMMSS, dt: for mail)
@@ -199,12 +200,11 @@ sub roots2paths { # for diagnostics
                do {
                        my $mset = $enq->get_mset($off += $size, $lim);
                        for my $x ($mset->items) {
-                               my $tmp = xap_terms('P', $x->get_document);
-                               push @$dirs, keys %$tmp;
+                               push @$dirs, xap_terms('P', $x->get_document);
                        }
                        $size = $mset->size;
                } while ($size);
-               @$dirs = sort @$dirs;
+               @$dirs = sort(uniqstr(@$dirs));
        }
        \%ret;
 }
@@ -223,12 +223,9 @@ sub root_oids ($$) {
        my @ids = docids_of_git_dir $self, $git_dir or warn <<"";
 BUG? (non-fatal) `$git_dir' not indexed in $self->{topdir}
 
-       my %ret;
-       for my $docid (@ids) {
-               my @oids = xap_terms('G', $self->xdb, $docid);
-               @ret{@oids} = @oids;
-       }
-       sort keys %ret;
+       my @ret = map { xap_terms('G', $self->xdb, $_) } @ids;
+       @ret = uniqstr(@ret) if @ids > 1;
+       @ret;
 }
 
 sub paths2roots {
index ba4c43097655ee3565d148507bf9209215b37680..29e3213f3688b954afdea4d9539317c365746f6f 100644 (file)
@@ -9,6 +9,7 @@ use parent qw(PublicInbox::ExtSearch); # PublicInbox::Search->reopen
 use PublicInbox::Search qw(xap_terms);
 use PublicInbox::ContentHash qw(content_digest content_hash git_sha);
 use PublicInbox::MID qw(mids mids_for_index);
+use PublicInbox::Compat qw(uniqstr);
 use Carp qw(croak);
 
 sub _msg_kw { # retry_reopen callback
@@ -44,20 +45,16 @@ sub oidbin_keywords {
 sub _xsmsg_vmd { # retry_reopen
        my ($self, $smsg, $want_label) = @_;
        my $xdb = $self->xdb; # set {nshard};
-       my (%kw, %L, $doc, $x);
-       $kw{flagged} = 1 if delete($smsg->{lei_q_tt_flagged});
+       my (@kw, @L, $doc, $x);
+       @kw = qw(flagged) if delete($smsg->{lei_q_tt_flagged});
        my @num = $self->over->blob_exists($smsg->{blob});
        for my $num (@num) { # there should only be one...
                $doc = $xdb->get_document($self->num2docid($num));
-               $x = xap_terms('K', $doc);
-               %kw = (%kw, %$x);
-               if ($want_label) { # JSON/JMAP only
-                       $x = xap_terms('L', $doc);
-                       %L = (%L, %$x);
-               }
+               push @kw, xap_terms('K', $doc);
+               push @L, xap_terms('L', $doc) if $want_label # JSON/JMAP only
        }
-       $smsg->{kw} = [ sort keys %kw ] if scalar(keys(%kw));
-       $smsg->{L} = [ sort keys %L ] if scalar(keys(%L));
+       @{$smsg->{kw}} = sort(uniqstr(@kw)) if @kw;
+       @{$smsg->{L}} = uniqstr(@L) if @L;
 }
 
 # lookup keywords+labels for external messages
index aebb85a9f75267e4e9ebcc0f65096bf383d13f60..a752174d3cd3476c2efbc7a3c56c5e4762d1c4a0 100644 (file)
@@ -27,6 +27,7 @@ use PublicInbox::MDA;
 use PublicInbox::Spawn qw(spawn);
 use PublicInbox::MdirReader;
 use PublicInbox::LeiToMail;
+use PublicInbox::Compat qw(uniqstr);
 use File::Temp qw(tmpnam);
 use POSIX ();
 use IO::Handle (); # ->autoflush
@@ -341,15 +342,15 @@ sub _add_vmd ($$$$) {
 sub _docids_and_maybe_kw ($$) {
        my ($self, $docids) = @_;
        return $docids unless wantarray;
-       my $kw = {};
+       my (@kw, $idx, @tmp);
        for my $num (@$docids) { # likely only 1, unless ContentHash changes
                # can't use ->search->msg_keywords on uncommitted docs
-               my $idx = $self->{priv_eidx}->idx_shard($num);
-               my $tmp = eval { $idx->ipc_do('get_terms', 'K', $num) };
-               if ($@) { warn "#$num get_terms: $@" }
-               else { @$kw{keys %$tmp} = values(%$tmp) };
+               $idx = $self->{priv_eidx}->idx_shard($num);
+               @tmp = eval { $idx->ipc_do('get_terms', 'K', $num) };
+               $@ ? warn("#$num get_terms: $@") : push(@kw, @tmp);
        }
-       ($docids, [ sort keys %$kw ]);
+       @kw = sort(uniqstr(@kw)) if @$docids > 1;
+       ($docids, \@kw);
 }
 
 sub _reindex_1 { # git->cat_async callback