From b9534449ecce2c59bb4aebad6051f91c3116b187 Mon Sep 17 00:00:00 2001 From: "Eric Wong (Contractor, The Linux Foundation)" Date: Tue, 3 Apr 2018 11:09:09 +0000 Subject: view: avoid offset during pagination OFFSET in SQLite gets painful to deal with. Instead, rely on timestamps (from Received:) for pagination. This also sets us up for more precise Date searching in case we want it. --- lib/PublicInbox/View.pm | 90 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 25 deletions(-) (limited to 'lib/PublicInbox/View.pm') diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index cad90a79..cbed9163 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -15,6 +15,7 @@ use PublicInbox::Address; use PublicInbox::WwwStream; use PublicInbox::Reply; require POSIX; +use Time::Local qw(timegm); use constant INDENT => ' '; use constant TCHILD => '` '; @@ -1032,43 +1033,82 @@ sub dump_topics { 200; } +sub ts2str ($) { + my ($ts) = @_; + POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts)); +} + +sub str2ts ($) { + my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]); + timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy); +} + +sub pagination_footer ($$) { + my ($ctx, $latest) = @_; + delete $ctx->{qp} or return; + my $next = $ctx->{next_page} || ''; + my $prev = $ctx->{prev_page} || ''; + if ($prev) { + $next = $next ? "$next " : ' '; + $prev .= qq! latest!; + } + "
page: $next$prev
"; +} + sub index_nav { # callback for WwwStream my (undef, $ctx) = @_; - delete $ctx->{qp} or return; - my ($next, $prev); - $next = $prev = ' '; - my $latest = ''; + pagination_footer($ctx, '.') +} + +sub paginate_recent ($) { + my ($ctx) = @_; + my $t = $ctx->{qp}->{t} || ''; + my $lim = 200; # this is our window + my $opts = { limit => $lim }; + my ($after, $before); + + # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers + # if only $after exists "YYYYMMDD.." because "." could be skipped + # if interpreted as an end-of-sentence + $t =~ s/\A(\d{8,14})-// and $after = str2ts($1); + $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1); - my $next_o = $ctx->{-next_o}; - if ($next_o) { - $next = qq!next!; + my $ibx = $ctx->{-inbox}; + my $msgs = $ibx->recent($opts, $after, $before); + my $nr = scalar @$msgs; + if ($nr < $lim && defined($after)) { + $after = $before = undef; + $msgs = $ibx->recent($opts); + $nr = scalar @$msgs; } - if (my $cur_o = $ctx->{-cur_o}) { - $latest = qq! latest!; - - my $o = $cur_o - ($next_o - $cur_o); - if ($o > 0) { - $prev = qq!prev!; - } elsif ($o == 0) { - $prev = qq!prev!; + my $more = $nr == $lim; + my ($newest, $oldest); + if ($nr) { + $newest = $msgs->[0]->{ts}; + $oldest = $msgs->[-1]->{ts}; + # if we only had $after, our SQL query in ->recent ordered + if ($newest < $oldest) { + ($oldest, $newest) = ($newest, $oldest); + $more = 0 if defined($after) && $after < $oldest; } } - "
page: $next $prev$latest
"; + if (defined($oldest) && $more) { + my $s = ts2str($oldest); + $ctx->{next_page} = qq!next!; + } + if (defined($newest) && (defined($before) || defined($after))) { + my $s = ts2str($newest); + $ctx->{prev_page} = qq!prev!; + } + $msgs; } sub index_topics { my ($ctx) = @_; - my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/); - - $ctx->{order} = []; - my $srch = $ctx->{srch}; - my $msgs = $ctx->{-inbox}->recent({offset => $off, limit => 200 }); - my $nr = scalar @$msgs; - if ($nr) { + my $msgs = paginate_recent($ctx); + if (@$msgs) { walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic); } - $ctx->{-next_o} = $off + $nr; - $ctx->{-cur_o} = $off; PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav); } -- cgit v1.2.3-24-ge0c7