- my $sth = $dbh->prepare('SELECT num FROM msgmap WHERE num > ? '.
- 'ORDER BY num ASC LIMIT 1000');
- $sth->execute($num);
- my $ary = $sth->fetchall_arrayref;
- @$ary = map { $_->[0] } @$ary;
- my $nr = scalar @$ary;
- $cb->($ary) if $nr;
- $nr;
+ my $attr = { Columns => [] };
+ my $mids = $dbh->selectall_arrayref(<<'', $attr, $$beg, $end);
+SELECT num,mid FROM msgmap WHERE num >= ? AND num <= ?
+ORDER BY num ASC
+
+ $$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 (?,?)');
+ };
+ $sth->execute($num, $mid);
+}
+
+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 => $$);