-# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# bidirectional Message-ID <-> Article Number mapping for the NNTP
use DBI;
use DBD::SQLite;
use File::Temp qw(tempfile);
+use PublicInbox::Over;
sub new {
my ($class, $git_dir, $writable) = @_;
new_file($class, "$d/msgmap.sqlite3", $writable);
}
-sub dbh_new {
- my ($f, $writable) = @_;
- if ($writable && !-f $f) { # SQLite defaults mode to 0644, we want 0666
- open my $fh, '+>>', $f or die "failed to open $f: $!";
- }
- my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
- AutoCommit => 1,
- RaiseError => 1,
- PrintError => 0,
- ReadOnly => !$writable,
- sqlite_use_immediate_transaction => 1,
- });
- $dbh;
-}
-
sub new_file {
- my ($class, $f, $writable) = @_;
- return if !$writable && !-r $f;
+ my ($class, $f, $rw) = @_;
+ return if !$rw && !-r $f;
- my $dbh = dbh_new($f, $writable);
- my $self = bless { dbh => $dbh }, $class;
-
- if ($writable) {
+ my $self = bless { filename => $f }, $class;
+ my $dbh = $self->{dbh} = PublicInbox::Over::dbh_new($self, $rw);
+ if ($rw) {
create_tables($dbh);
+
+ # TRUNCATE reduces I/O compared to the default (DELETE)
+ $dbh->do('PRAGMA journal_mode = TRUNCATE');
+
$dbh->begin_work;
$self->created_at(time) unless $self->created_at;
- my (undef, $max) = $self->minmax();
- $max ||= 0;
+ my $max = $self->max // 0;
$self->num_highwater($max);
$dbh->commit;
}
$self->{dbh}->sqlite_backup_to_file($fn);
my $tmp = ref($self)->new_file($fn, 1);
$tmp->{dbh}->do('PRAGMA synchronous = OFF');
- $tmp->{tmp_name} = $fn; # SQLite won't work if unlinked, apparently
+ $tmp->{dbh}->do('PRAGMA journal_mode = MEMORY');
$tmp->{pid} = $$;
close $fh or die "failed to close $fn: $!";
$tmp;
my ($self, $mid) = @_;
my $dbh = $self->{dbh};
my $sth = $dbh->prepare_cached(<<'');
-INSERT OR IGNORE INTO msgmap (mid) VALUES (?)
+INSERT INTO msgmap (mid) VALUES (?)
- return if $sth->execute($mid) == 0;
+ return unless eval { $sth->execute($mid) };
my $num = $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
$self->num_highwater($num) if defined($num);
$num;
$sth->fetchrow_array;
}
+sub max {
+ my $sth = $_[0]->{dbh}->prepare_cached('SELECT MAX(num) FROM msgmap',
+ undef, 1);
+ $sth->execute;
+ $sth->fetchrow_array;
+}
+
sub minmax {
- my ($self) = @_;
- my $dbh = $self->{dbh};
# breaking MIN and MAX into separate queries speeds up from 250ms
# to around 700us with 2.7million messages.
- my $sth = $dbh->prepare_cached('SELECT MIN(num) FROM msgmap', undef, 1);
+ my $sth = $_[0]->{dbh}->prepare_cached('SELECT MIN(num) FROM msgmap',
+ undef, 1);
$sth->execute;
- my $min = $sth->fetchrow_array;
- $sth = $dbh->prepare_cached('SELECT MAX(num) FROM msgmap', undef, 1);
- $sth->execute;
- ($min, $sth->fetchrow_array);
+ ($sth->fetchrow_array, max($_[0]));
}
sub mid_delete {
}
sub msg_range {
- my ($self, $beg, $end) = @_;
+ my ($self, $beg, $end, $cols) = @_;
+ $cols //= 'num,mid';
my $dbh = $self->{dbh};
my $attr = { Columns => [] };
- my $mids = $dbh->selectall_arrayref(<<'', $attr, $$beg, $end);
-SELECT num,mid FROM msgmap WHERE num >= ? AND num <= ?
+ 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;
sub DESTROY {
my ($self) = @_;
- delete $self->{dbh};
- my $f = delete $self->{tmp_name};
- if (defined $f && $self->{pid} == $$) {
+ my $dbh = $self->{dbh} or return;
+ if (($self->{pid} // 0) == $$) {
+ my $f = $dbh->sqlite_db_filename;
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);
+ $self->{pid} or die 'BUG: not a temporary clone';
+ $self->{dbh} and die 'BUG: tmp_clone dbh not prepared for parent';
+ defined($self->{filename}) or die 'BUG: {filename} not defined';
+ my $dbh = $self->{dbh} = PublicInbox::Over::dbh_new($self, 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";
+ my $pid = $self->{pid} or die 'BUG: not a temporary clone';
+ $pid == $$ or die "BUG: atfork_prepare not called by $pid";
+ my $dbh = $self->{dbh} or die 'BUG: temporary clone not open';
+
# must clobber prepared statements
- %$self = (tmp_name => $f, pid => $$);
+ %$self = (filename => $dbh->sqlite_db_filename, pid => $pid);
+}
+
+sub skip_artnum {
+ my ($self, $skip_artnum) = @_;
+ return meta_accessor($self, 'skip_artnum') if !defined($skip_artnum);
+
+ my $cur = num_highwater($self) // 0;
+ if ($skip_artnum < $cur) {
+ die "E: current article number $cur ",
+ "exceeds --skip-artnum=$skip_artnum\n";
+ } else {
+ my $ok;
+ for (1..10) {
+ my $mid = 'skip'.rand.'@'.rand.'.example.com';
+ $ok = mid_set($self, $skip_artnum, $mid);
+ if ($ok) {
+ mid_delete($self, $mid);
+ last;
+ }
+ }
+ $ok or die '--skip-artnum failed';
+
+ # in the future, the indexer may use this value for
+ # new messages in old epochs
+ meta_accessor($self, 'skip_artnum', $skip_artnum);
+ }
+}
+
+sub check_inodes {
+ my ($self) = @_;
+ # no filename if in-:memory:
+ my $f = $self->{dbh}->sqlite_db_filename // return;
+ if (my @st = stat($f)) { # did st_dev, st_ino change?
+ my $st = pack('dd', $st[0], $st[1]);
+ if ($st ne ($self->{st} // $st)) {
+ my $tmp = eval { ref($self)->new_file($f) };
+ if ($@) {
+ warn "E: DBI->connect($f): $@\n";
+ } else {
+ %$self = %$tmp;
+ }
+ }
+ } else {
+ warn "W: stat $f: $!\n";
+ }
}
1;