From d0dcab33e0b02bf3299deea40f96ef5fff10fe73 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 10 Jun 2020 07:05:12 +0000 Subject: imap: STATUS/EXAMINE: rely on SQLite overview We can get exact values for EXISTS, UIDNEXT using SQLite rather than calculating off $ibx->mm->max ourselves. Furthermore, $ibx->mm is less useful than $ibx->over for IMAP (and for our read-only daemons in general) so do not depend on $ibx->mm outside of startup/reload to save FDs and reduce kernel page cache footprint. --- lib/PublicInbox/Over.pm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'lib/PublicInbox/Over.pm') diff --git a/lib/PublicInbox/Over.pm b/lib/PublicInbox/Over.pm index 1faeff41..e0f20ea6 100644 --- a/lib/PublicInbox/Over.pm +++ b/lib/PublicInbox/Over.pm @@ -229,4 +229,34 @@ sub uid_range { $dbh->selectcol_arrayref($q, undef, $beg, $end); } +sub max { + my ($self) = @_; + my $sth = $self->connect->prepare_cached(<<'', undef, 1); +SELECT MAX(num) FROM over WHERE num > 0 + + $sth->execute; + $sth->fetchrow_array // 0; +} + +sub imap_status { + my ($self, $uid_base, $uid_end) = @_; + my $dbh = $self->connect; + my $sth = $dbh->prepare_cached(<<'', undef, 1); +SELECT COUNT(num) FROM over WHERE num > ? AND num <= ? + + $sth->execute($uid_base, $uid_end); + my $exists = $sth->fetchrow_array; + + $sth = $dbh->prepare_cached(<<'', undef, 1); +SELECT MAX(num) + 1 FROM over WHERE num <= ? + + $sth->execute($uid_end); + my $uidnext = $sth->fetchrow_array; + + $sth = $dbh->prepare_cached(<<'', undef, 1); +SELECT MAX(num) FROM over WHERE num > 0 + + ($exists, $uidnext, $sth->fetchrow_array // 0); +} + 1; -- cgit v1.2.3-24-ge0c7