+# used by NNTP.pm
+sub ids_after {
+ my ($self, $num) = @_;
+ my $ids = $self->{dbh}->selectcol_arrayref(<<'', undef, $$num);
+SELECT num FROM msgmap WHERE num > ?
+ORDER BY num ASC LIMIT 1000
+
+ $$num = $ids->[-1] if @$ids;
+ $ids;
+}
+
+sub msg_range {
+ my ($self, $beg, $end, $cols) = @_;
+ $cols //= 'num,mid';
+ my $dbh = $self->{dbh};
+ my $attr = { Columns => [] };
+ my $mids = $dbh->selectall_arrayref(<<"", $attr, $$beg, $end);
+SELECT $cols FROM msgmap WHERE num >= ? AND num <= ?
+ORDER BY num ASC LIMIT 1000
+
+ $$beg = $mids->[-1]->[0] + 1 if @$mids;
+ $mids
+}
+
+# only used for mapping external serial numbers (e.g. articles from gmane)
+# see scripts/xhdr-num2mid or PublicInbox::Filter::RubyLang for usage
+sub mid_set {
+ my ($self, $num, $mid) = @_;
+ my $sth = $self->{mid_set} ||= do {
+ $self->{dbh}->prepare(
+ 'INSERT OR IGNORE INTO msgmap (num,mid) VALUES (?,?)');
+ };
+ my $result = $sth->execute($num, $mid);
+ $self->num_highwater($num) if (defined($result) && $result == 1);
+ $result;
+}
+
+sub DESTROY {
+ my ($self) = @_;
+ delete $self->{dbh};
+ my $f = delete $self->{tmp_name};
+ if (defined $f && $self->{pid} == $$) {
+ unlink $f or warn "failed to unlink $f: $!\n";
+ }
+}
+
+sub atfork_parent {
+ my ($self) = @_;
+ my $f = $self->{tmp_name} or die "not a temporary clone\n";
+ delete $self->{dbh} and die "tmp_clone dbh not prepared for parent";
+ my $dbh = $self->{dbh} = dbh_new($f, 1);
+ $dbh->do('PRAGMA synchronous = OFF');
+}
+
+sub atfork_prepare {
+ my ($self) = @_;
+ my $f = $self->{tmp_name} or die "not a temporary clone\n";
+ $self->{pid} == $$ or
+ die "BUG: atfork_prepare not called from $self->{pid}\n";
+ $self->{dbh} or die "temporary clone not open\n";
+ # must clobber prepared statements
+ %$self = (tmp_name => $f, pid => $$);
+}
+