dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 7/8] WIP thread bad mbox
Date: Wed, 24 Apr 2024 01:34:19 +0000	[thread overview]
Message-ID: <20240424013420.476353-7-e@80x24.org> (raw)
In-Reply-To: <20240424013420.476353-1-e@80x24.org>

---
 lib/PublicInbox/Isearch.pm |  2 +-
 lib/PublicInbox/Mbox.pm    | 54 ++++++++++++++++----------------------
 lib/PublicInbox/Search.pm  |  5 +++-
 lib/PublicInbox/XhcMset.pm |  5 ++--
 4 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/lib/PublicInbox/Isearch.pm b/lib/PublicInbox/Isearch.pm
index 20808d6d..ddc67f51 100644
--- a/lib/PublicInbox/Isearch.pm
+++ b/lib/PublicInbox/Isearch.pm
@@ -103,7 +103,7 @@ SELECT docid,xnum FROM xref3 WHERE ibx_id = ? AND docid IN ($qmarks)
 		warn "W: $self->{es}->{topdir} may need to be reindexed\n";
 		@xnums = grep { defined } @xnums;
 	}
-	\@xnums;
+	wantarray ? @xnums : \@xnums;
 }
 
 sub mset_to_smsg {
diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm
index e58fb4b6..19dcba05 100644
--- a/lib/PublicInbox/Mbox.pm
+++ b/lib/PublicInbox/Mbox.pm
@@ -175,64 +175,56 @@ sub mbox_all_ids {
 	PublicInbox::MboxGz::mbox_gz($ctx, \&all_ids_cb, 'all');
 }
 
-sub refill_result_ids ($) {
-	my ($ctx) = @_;
+sub refill_ids_cb { # async_mset_cb
+	my ($ctx, $mset, $err) = @_;
+	if ($err) {
+		warn "E: $err";
+		return;
+	}
 	# refill result set, deprioritize since there's many results
-	my $srch = $ctx->{ibx}->isrch or return $ctx->gone('search');
-	my $mset = $srch->mset($ctx->{query}, $ctx->{qopts});
 	my $size = $mset->size or return;
 	$ctx->{qopts}->{offset} += $size;
-	$ctx->{ids} = $srch->mset_to_artnums($mset, $ctx->{qopts});
+	$ctx->{ids} = $ctx->{srch}->mset_to_artnums($mset, $ctx->{qopts});
 	$ctx->{-low_prio} = 1; # true
+	results_cb($ctx);
 }
 
-sub results_cb {
+sub results_cb { # async_next or getline cb
 	my ($ctx) = @_;
 	my $over = $ctx->{ibx}->over or return $ctx->gone('over');
 	while (1) {
-		while (defined(my $num = shift(@{$ctx->{ids}}))) {
+		my $ids = $ctx->{xids} // $ctx->{ids};
+		while (defined(my $num = shift(@$ids))) {
 			my $smsg = $over->get_art($num) or next;
 			return $smsg;
 		}
-		refill_result_ids($ctx) or return; # refill ctx->{ids}
-	}
-}
-
-sub results_thread_cb {
-	my ($ctx) = @_;
-
-	my $over = $ctx->{ibx}->over or return $ctx->gone('over');
-	while (1) {
-		while (defined(my $num = shift(@{$ctx->{xids}}))) {
-			my $smsg = $over->get_art($num) or next;
-			return $smsg;
-		}
-		next if $over->expand_thread($ctx); # refills ctx->{xids}
-
-		refill_result_ids($ctx) or return; # refill ctx->{ids}
+		next if $ctx->{xids} && $over->expand_thread($ctx);
+		return if $ctx->{srch}->async_mset($ctx->{query}, $ctx->{qopts},
+						\&refill_ids_cb, $ctx);
 	}
 }
 
 sub mbox_qry_cb { # async_mset cb
-	my ($ctx, $opt, $q, $mset, $err) = @_;
+	my ($ctx, $q, $mset, $err) = @_;
 	my $wcb = delete $ctx->{wcb};
 	if ($err) {
 		warn "E: $err";
 		return $wcb->([500, [qw(Content-Type text/plain)],
 				[ "Internal server error\n" ]])
 	}
-	$opt->{offset} = $mset->size or
+	$ctx->{qopts}->{offset} = $mset->size or
 			return $wcb->([404, [qw(Content-Type text/plain)],
 					["No results found\n"]]);
-	$ctx->{ids} = $ctx->{srch}->mset_to_artnums($mset, $opt);
-	my @cb_fn;
+	$ctx->{ids} = $ctx->{srch}->mset_to_artnums($mset, $ctx->{qopts});
+	my $fn;
 	if ($q->{t} && $ctx->{srch}->has_threadid) {
-		@cb_fn = (\&results_thread_cb, "results-thread-$ctx->{query}");
+		$ctx->{xids} = []; # triggers over->expand_thread
+		$fn = "results-thread-$ctx->{query}";
 	} else {
-		@cb_fn = (\&results_cb, "results-$ctx->{query}");
+		$fn = "results-$ctx->{query}";
 	}
 	require PublicInbox::MboxGz;
-	my $res = PublicInbox::MboxGz::mbox_gz($ctx, @cb_fn);
+	my $res = PublicInbox::MboxGz::mbox_gz($ctx, \&results_cb, $fn);
 	ref($res) eq 'CODE' ? $res->($wcb) : $wcb->($res);
 }
 
@@ -259,7 +251,7 @@ sub mbox_all {
 	$ctx->{query} = $qstr;
 	sub {
 		$ctx->{wcb} = $_[0]; # PSGI server supplied write cb
-		$srch->async_mset($qstr, $opt, \&mbox_qry_cb, $ctx, $opt, $q);
+		$srch->async_mset($qstr, $opt, \&mbox_qry_cb, $ctx, $q);
 	};
 }
 
diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index 5c417be3..60d12dbf 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -461,6 +461,8 @@ sub xh_opt ($) {
 	@ret;
 }
 
+# returns a true value if actually handled asynchronously,
+# and a falsy value if handled synchronously
 sub async_mset {
 	my ($self, $qry_str, $opt, $cb, @args) = @_;
 	$XHC //= xhc_start_maybe;
@@ -469,9 +471,10 @@ sub async_mset {
 		my @margs = ($self->xh_args, xh_opt($opt));
 		my $rd = $XHC->mkreq(undef, 'mset', @margs, $qry_str);
 		PublicInbox::XhcMset->maybe_new($rd, $self, $cb, @args);
-	} else {
+	} else { # synchronous
 		my $mset = $self->mset($qry_str, $opt);
 		$cb->(@args, $mset);
+		undef;
 	}
 }
 
diff --git a/lib/PublicInbox/XhcMset.pm b/lib/PublicInbox/XhcMset.pm
index 2e6fa5f3..ac25eece 100644
--- a/lib/PublicInbox/XhcMset.pm
+++ b/lib/PublicInbox/XhcMset.pm
@@ -34,11 +34,12 @@ sub event_step {
 sub maybe_new {
 	my (undef, $rd, $srch, @cb_args) = @_;
 	my $self = bless { cb_args => \@cb_args, srch => $srch }, __PACKAGE__;
-	if ($PublicInbox::DS::in_loop) {
+	if ($PublicInbox::DS::in_loop) { # async
 		$self->SUPER::new($rd, EPOLLIN|EPOLLONESHOT);
-	} else {
+	} else { # synchronous
 		$self->{sock} = $rd;
 		event_step($self);
+		undef;
 	}
 }
 

  parent reply	other threads:[~2024-04-24  1:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-24  1:34 [PATCH 1/8] searchview: get rid of unused adump callback arg Eric Wong
2024-04-24  1:34 ` [PATCH 2/8] xap_helper.h: remove _SC_NPROCESSORS_ONLN default Eric Wong
2024-04-24  1:34 ` [PATCH 3/8] xap_helper: drop terms+data from `mset' command Eric Wong
2024-04-24  1:34 ` [PATCH 4/8] mbox: hoist out refill_result_ids Eric Wong
2024-04-24  1:34 ` [PATCH 5/8] www: start wiring up search to use async xap_helper Eric Wong
2024-04-24  1:34 ` [PATCH 6/8] mset: mbox Eric Wong
2024-04-24  1:34 ` Eric Wong [this message]
2024-04-24  1:34 ` [PATCH 8/8] mbox: fixup Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240424013420.476353-7-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).