]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Msgmap.pm
over+msgmap: respect WAL journal_mode if set
[public-inbox.git] / lib / PublicInbox / Msgmap.pm
index 5fe14383ac484a79e1b279b718e28db68b5449d4..d28e96c8588b002e62a09b64dc6eaaad51346309 100644 (file)
@@ -9,10 +9,10 @@
 # This is maintained by ::SearchIdx
 package PublicInbox::Msgmap;
 use strict;
-use warnings;
 use DBI;
 use DBD::SQLite;
-use File::Temp qw(tempfile);
+use PublicInbox::Over;
+use PublicInbox::Spawn;
 
 sub new {
        my ($class, $git_dir, $writable) = @_;
@@ -24,39 +24,18 @@ sub new {
        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;
        }
@@ -65,14 +44,15 @@ sub new_file {
 
 # 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->{tmp_name} = $fn; # SQLite won't work if unlinked, apparently
+       $tmp = ref($self)->new_file($fn, 2);
+       $tmp->{dbh}->do('PRAGMA journal_mode = MEMORY');
        $tmp->{pid} = $$;
-       close $fh or die "failed to close $fn: $!";
        $tmp;
 }
 
@@ -158,17 +138,20 @@ sub num_for {
        $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 {
@@ -189,18 +172,20 @@ sub num_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
@@ -242,29 +227,75 @@ sub mid_set {
 
 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;