]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAPTracker.pm
imaptracker: preserve WAL journal_mode if set by user
[public-inbox.git] / lib / PublicInbox / IMAPTracker.pm
index 0bbabe07faef6c76f1bc4bcd94c75eabbfd8d1ab..92f21584842d97d2d6d1d3747596e251ad8719d6 100644 (file)
@@ -2,6 +2,7 @@
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 package PublicInbox::IMAPTracker;
 use strict;
+use parent qw(PublicInbox::Lock);
 use DBI;
 use DBD::SQLite;
 use PublicInbox::Config;
@@ -28,7 +29,12 @@ sub dbh_new ($) {
                sqlite_use_immediate_transaction => 1,
        });
        $dbh->{sqlite_unicode} = 1;
-       $dbh->do('PRAGMA journal_mode = TRUNCATE');
+
+       # TRUNCATE reduces I/O compared to the default (DELETE).
+       # Allow and preserve user-overridden WAL, but don't force it.
+       my $jm = $dbh->selectrow_array('PRAGMA journal_mode');
+       $dbh->do('PRAGMA journal_mode = TRUNCATE') if $jm ne 'wal';
+
        create_tables($dbh);
        $dbh;
 }
@@ -48,7 +54,10 @@ sub update_last ($$$) {
 INSERT OR REPLACE INTO imap_last (url, uid_validity, uid)
 VALUES (?, ?, ?)
 
-       $sth->execute($self->{url}, $validity, $last);
+       $self->lock_acquire;
+       my $rv = $sth->execute($self->{url}, $validity, $last);
+       $self->lock_release;
+       $rv;
 }
 
 sub new {
@@ -68,8 +77,11 @@ sub new {
                require File::Basename;
                File::Path::mkpath(File::Basename::dirname($dbname));
        }
-
-       bless { url => $url, dbh => dbh_new($dbname) }, $class;
+       my $self = bless { lock_path => "$dbname.lock", url => $url }, $class;
+       $self->lock_acquire;
+       $self->{dbh} = dbh_new($dbname);
+       $self->lock_release;
+       $self;
 }
 
 1;