about summary refs log tree commit homepage
path: root/lib/PublicInbox/Over.pm
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-06-10 07:05:12 +0000
committerEric Wong <e@yhbt.net>2020-06-13 07:55:45 +0000
commitd0dcab33e0b02bf3299deea40f96ef5fff10fe73 (patch)
tree2c0343463544400798fb73e7567978cb83af116b /lib/PublicInbox/Over.pm
parent56e4554469fb86c2cac63d9eabaffa64c531e835 (diff)
downloadpublic-inbox-d0dcab33e0b02bf3299deea40f96ef5fff10fe73.tar.gz
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.
Diffstat (limited to 'lib/PublicInbox/Over.pm')
-rw-r--r--lib/PublicInbox/Over.pm30
1 files changed, 30 insertions, 0 deletions
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;