]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
xap_helper: drop qp_extra_done flag and conditions
authorEric Wong <e@80x24.org>
Thu, 6 Mar 2025 20:34:41 +0000 (20:34 +0000)
committerEric Wong <e@80x24.org>
Fri, 7 Mar 2025 19:23:16 +0000 (19:23 +0000)
As with -d (directories), the -Q (extra query prefixes) flag is
already part of the cache key so there's no need to initialize
it lazily.

While we're at it, drop a `map' op from the cache key generation
for the Perl implementation.

lib/PublicInbox/XapHelper.pm
lib/PublicInbox/xap_helper.h

index e98553c876f738972be173789fc573e7966534e4..8f3e7e848a2dcd5b86ebe181fcc3330a3039dd68 100644 (file)
@@ -186,16 +186,15 @@ sub cmd_mset { # to be used by WWW + IMAP
        }
 }
 
-sub srch_init_extra ($) {
-       my ($req) = @_;
-       my $qp = $req->{srch}->{qp};
+sub srch_init_extra ($$) {
+       my ($srch, $req) = @_;
+       my $qp = $srch->{qp};
        for (@{$req->{Q}}) {
                my ($upfx, $m, $xpfx) = split /([:=])/;
                $xpfx // die "E: bad -Q $_";
                $m = $m eq '=' ? 'add_boolean_prefix' : 'add_prefix';
                $qp->$m($upfx, $xpfx);
        }
-       $req->{srch}->{qp_extra_done} = 1;
 }
 
 sub dispatch {
@@ -205,7 +204,7 @@ sub dispatch {
                or return;
        my $dirs = delete $req->{d} or die 'no -d args';
        my $key = "-d\0".join("\0-d\0", @$dirs);
-       $key .= "\0".join("\0", map { ('-Q', $_) } @{$req->{Q}}) if $req->{Q};
+       $key .= "\0-Q\0".join("\0-Q\0", @{$req->{Q}}) if $req->{Q};
        my $new;
        $req->{srch} = $SRCH{$key} // do {
                $new = { qp_flags => $QP_FLAGS };
@@ -240,11 +239,10 @@ sub dispatch {
                bless $new, $req->{c} ? 'PublicInbox::CodeSearch' :
                                        'PublicInbox::Search';
                $new->qparse_new;
+               srch_init_extra $new, $req;
                $SRCH{$key} = $new;
        };
        $req->{srch}->{xdb}->reopen unless $new;
-       $req->{Q} && !$req->{srch}->{qp_extra_done} and
-               srch_init_extra $req;
        my $timeo = $req->{K};
        alarm($timeo) if $timeo;
        $fn->($req, @argv);
index f6da52fb7daa405f22db2cfbc6cf81e5eb975374..fd52a70d6932bfeea11db30daa5124384dde5e1a 100644 (file)
@@ -120,7 +120,6 @@ static void *xreallocarray(void *ptr, size_t nmemb, size_t size)
 struct srch {
        int ckey_len; // int for comparisons
        unsigned qp_flags;
-       bool qp_extra_done;
        Xapian::Database *db;
        Xapian::QueryParser *qp;
        unsigned char ckey[]; // $shard_path0\0$shard_path1\0...
@@ -594,6 +593,30 @@ static void srch_cache_renew(struct srch *keep)
 #include "xh_date.h" // GitDateRangeProcessor + GitDateFieldProcessor
 #include "xh_thread_fp.h" // ThreadFieldProcessor
 
+// setup query parser for altid and arbitrary headers
+static void srch_init_extra(struct srch *srch, struct req *req)
+{
+       const char *XPFX;
+       for (int i = 0; i < req->qpfxc; i++) {
+               size_t len = strlen(req->qpfxv[i]);
+               char *c = (char *)memchr(req->qpfxv[i], '=', len);
+
+               if (c) { // it's boolean "gmane=XGMANE"
+                       XPFX = c + 1;
+                       *c = 0;
+                       srch->qp->add_boolean_prefix(req->qpfxv[i], XPFX);
+                       continue;
+               }
+               // maybe it's a non-boolean prefix "blob:XBLOBID"
+               c = (char *)memchr(req->qpfxv[i], ':', len);
+               if (!c)
+                       errx(EXIT_FAILURE, "bad -Q %s", req->qpfxv[i]);
+               XPFX = c + 1;
+               *c = 0;
+               srch->qp->add_prefix(req->qpfxv[i], XPFX);
+       }
+}
+
 static void srch_init(struct req *req)
 {
        int i;
@@ -659,31 +682,7 @@ static void srch_init(struct req *req)
                        srch->qp->add_boolean_prefix("L", "L");
                }
        }
-}
-
-// setup query parser for altid and arbitrary headers
-static void srch_init_extra(struct req *req)
-{
-       const char *XPFX;
-       for (int i = 0; i < req->qpfxc; i++) {
-               size_t len = strlen(req->qpfxv[i]);
-               char *c = (char *)memchr(req->qpfxv[i], '=', len);
-
-               if (c) { // it's boolean "gmane=XGMANE"
-                       XPFX = c + 1;
-                       *c = 0;
-                       req->srch->qp->add_boolean_prefix(req->qpfxv[i], XPFX);
-                       continue;
-               }
-               // maybe it's a non-boolean prefix "blob:XBLOBID"
-               c = (char *)memchr(req->qpfxv[i], ':', len);
-               if (!c)
-                       errx(EXIT_FAILURE, "bad -Q %s", req->qpfxv[i]);
-               XPFX = c + 1;
-               *c = 0;
-               req->srch->qp->add_prefix(req->qpfxv[i], XPFX);
-       }
-       req->srch->qp_extra_done = true;
+       srch_init_extra(req->srch, req);
 }
 
 #define OPT_U(ch, var, fn, max) do { \
@@ -764,7 +763,6 @@ static void dispatch(struct req *req)
        ERR_CLOSE(kfp, EXIT_FAILURE); // may ENOMEM, sets kbuf.srch
        kbuf.srch->db = NULL;
        kbuf.srch->qp = NULL;
-       kbuf.srch->qp_extra_done = false;
        kbuf.srch->ckey_len = size - offsetof(struct srch, ckey);
        if (kbuf.srch->ckey_len <= 0 || !req->dirc)
                ABORT("no -d args (or too many)");
@@ -780,8 +778,6 @@ static void dispatch(struct req *req)
                srch_free(kbuf.srch);
                req->srch->db->reopen();
        }
-       if (req->qpfxc && !req->srch->qp_extra_done)
-               srch_init_extra(req);
        if (req->timeout_sec)
                alarm(req->timeout_sec > UINT_MAX ?
                        UINT_MAX : (unsigned)req->timeout_sec);