# called by PublicInbox::InboxIdle
sub on_inbox_unlock {
my ($self, $ibx) = @_;
- my $new = $ibx->mm->max;
+ my $new = $ibx->over->max;
my $uid_base = $self->{uid_base} // 0;
my $uid_end = $uid_base + UID_BLOCK;
defined(my $old = $self->{-idle_max}) or die 'BUG: -idle_max unset';
# IDLE seems allowed by dovecot w/o a mailbox selected *shrug*
my $ibx = $self->{ibx} or return "$tag BAD no mailbox selected\r\n";
$self->{-idle_tag} = $tag;
- my $max = $ibx->mm->max // 0;
+ my $max = $ibx->over->max;
my $uid_end = $self->{uid_base} + UID_BLOCK;
my $sock = $self->{sock} or return;
my $fd = fileno($sock);
my $mb_top = $1;
$uid_base = $2 * UID_BLOCK;
$ibx = $self->{imapd}->{mailboxes}->{lc $mailbox} or return;
- $exists = $ibx->mm->max // 0;
- ensure_ranges_exist($self->{imapd}, $ibx, $exists);
- my $uid_end = $uid_base + UID_BLOCK;
- $exists = $uid_end if $exists > $uid_end;
- $uidnext = $exists + 1;
- $exists -= $uid_base;
+ my $max;
+ ($exists, $uidnext, $max) = $ibx->over->imap_status($uid_base,
+ $uid_base + UID_BLOCK);
+ ensure_ranges_exist($self->{imapd}, $ibx, $max);
} else { # check for dummy inboxes
- $ibx = $self->{imapd}->{mailboxes}->{lc $mailbox} or return;
+ $mailbox = lc $mailbox;
+ $ibx = $self->{imapd}->{mailboxes}->{$mailbox} or return;
+
+ # if "INBOX.foo.bar" is selected and "INBOX.foo.bar.0",
+ # check for new UID ranges (e.g. "INBOX.foo.bar.1")
+ if (my $z = $self->{imapd}->{mailboxes}->{"$mailbox.0"}) {
+ ensure_ranges_exist($self->{imapd}, $z, $z->over->max);
+ }
+
$uid_base = $exists = 0;
$uidnext = 1;
}
($beg, $end) = ($1 + 0, $2 + 0);
} elsif ($range =~ /\A([0-9]+):\*\z/) {
$beg = $1 + 0;
- $end = $self->{ibx}->mm->max // 0;
+ $end = $self->{ibx}->over->max;
my $uid_end = $self->{uid_base} + UID_BLOCK;
$end = $uid_end if $end > $uid_end;
$beg = $end if $beg > $end;
if (!scalar(keys %$q)) {
$self->msg_more('* SEARCH');
my $beg = 1;
- my $end = $ibx->mm->max // 0;
+ my $end = $ibx->over->max;
uid_clamp($self, \$beg, \$end);
long_response($self, \&uid_search_uid_range,
$tag, \$beg, $end, $sql);
} elsif (my $uid = $q->{uid}) {
if ($uid =~ /\A([0-9]+):([0-9]+|\*)\z/s) {
my ($beg, $end) = ($1, $2);
- $end = $ibx->mm->max if $end eq '*';
+ $end = $ibx->over->max if $end eq '*';
uid_clamp($self, \$beg, \$end);
$self->msg_more('* SEARCH');
long_response($self, \&uid_search_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;
my ($tmpdir, $for_destroy) = tmpdir();
my $over = PublicInbox::OverIdx->new("$tmpdir/over.sqlite3");
$over->connect;
+is($over->max, 0, 'max is zero on new DB (scalar context)');
+is_deeply([$over->max], [0], 'max is zero on new DB (list context)');
my $x = $over->next_tid;
is(int($x), $x, 'integer tid');
my $y = $over->next_tid;
my $msgs = [ map { $_->{num} } @{$over->get_thread($mid)} ];
is_deeply([98, 99], $msgs, "linked messages by Message-ID: <$mid>");
}
+isnt($over->max, 0, 'max is non-zero');
$over->rollback_lazy;