# numbers for NNTP and allows prefix lookups for partial Message-IDs
# in case URLs get truncated from copy-n-paste errors by users.
#
-# This is maintained by ::SearchIdx
+# This is maintained by ::SearchIdx (v1) and ::V2Writable (v2)
package PublicInbox::Msgmap;
use strict;
-use warnings;
+use v5.10.1;
use DBI;
use DBD::SQLite;
-use File::Temp qw(tempfile);
+use PublicInbox::Over;
+use PublicInbox::Spawn;
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 $dbh = dbh_new($f, $writable);
- my $self = bless { dbh => $dbh }, $class;
-
- if ($writable) {
- create_tables($dbh);
-
- # TRUNCATE reduces I/O compared to the default (DELETE)
- $dbh->do('PRAGMA journal_mode = TRUNCATE');
+ my ($class, $f, $rw) = @_;
+ return if !$rw && !-r $f;
+ my $self = bless { filename => $f }, $class;
+ my $dbh = $self->{dbh} = PublicInbox::Over::dbh_new($self, $rw);
+ if ($rw) {
$dbh->begin_work;
+ create_tables($dbh);
$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;
}
# used to keep track of used numeric mappings for v2 reindex
sub tmp_clone {
- my ($self) = @_;
- my ($fh, $fn) = tempfile('msgmap-XXXXXXXX', EXLOCK => 0, TMPDIR => 1);
+ my ($self, $dir) = @_;
+ require File::Temp;
+ my $tmp = "mm_tmp-$$-XXXXXX";
+ my ($fh, $fn) = File::Temp::tempfile($tmp, EXLOCK => 0, DIR => $dir);
+ PublicInbox::Spawn::nodatacow_fd(fileno($fh));
$self->{dbh}->sqlite_backup_to_file($fn);
- my $tmp = ref($self)->new_file($fn, 1);
- $tmp->{dbh}->do('PRAGMA synchronous = OFF');
+ $tmp = ref($self)->new_file($fn, 2);
$tmp->{dbh}->do('PRAGMA journal_mode = MEMORY');
- $tmp->{tmp_name} = $fn; # SQLite won't work if unlinked, apparently
$tmp->{pid} = $$;
- close $fh or die "failed to close $fn: $!";
$tmp;
}
$self->meta_accessor("last_xap$version-$i", $commit);
}
+# this is the UIDVALIDITY for IMAP (cf. RFC 3501 sec 2.3.1.1. item 3)
sub created_at {
my ($self, $second) = @_;
$self->meta_accessor('created_at', $second);
$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 create_tables {
my ($dbh) = @_;
- my $e;
-
- $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM msgmap;') };
- defined $e or $dbh->do('CREATE TABLE msgmap (' .
- 'num INTEGER PRIMARY KEY AUTOINCREMENT, '.
- 'mid VARCHAR(1000) NOT NULL, ' .
- 'UNIQUE (mid) )');
-
- $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM meta') };
- defined $e or $dbh->do('CREATE TABLE meta (' .
- 'key VARCHAR(32) PRIMARY KEY, '.
- 'val VARCHAR(255) NOT NULL)');
+
+ $dbh->do(<<'');
+CREATE TABLE IF NOT EXISTS msgmap (
+ num INTEGER PRIMARY KEY AUTOINCREMENT,
+ mid VARCHAR(1000) NOT NULL,
+ UNIQUE (mid)
+)
+
+ $dbh->do(<<'');
+CREATE TABLE IF NOT EXISTS meta (
+ key VARCHAR(32) PRIMARY KEY,
+ val VARCHAR(255) NOT NULL
+)
+
}
# used by NNTP.pm
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);
- $dbh->do('PRAGMA synchronous = OFF');
+ $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';
+ $self->{dbh} = PublicInbox::Over::dbh_new($self, 2);
+ $self->{dbh}->do('PRAGMA journal_mode = MEMORY');
}
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;