-# Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
# 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;
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;
}
}
sub update_last ($$$) {
- my ($self, $validity, $last) = @_;
+ my ($self, $validity, $last_uid) = @_;
+ return unless defined $last_uid;
my $sth = $self->{dbh}->prepare_cached(<<'');
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_uid);
+ $self->lock_release;
+ $rv;
}
sub new {
my ($class, $url) = @_;
# original name for compatibility with old setups:
- my $dbname = PublicInbox::Config->config_dir() . "/imap.sqlite3";
+ my $dbname = PublicInbox::Config->config_dir() . '/imap.sqlite3';
# use the new XDG-compliant name for new setups:
if (!-f $dbname) {
}
if (!-f $dbname) {
require File::Path;
- require File::Basename;
- File::Path::mkpath(File::Basename::dirname($dbname));
+ require PublicInbox::Syscall;
+ my ($dir) = ($dbname =~ m!(.*?/)[^/]+\z!);
+ File::Path::mkpath($dir);
+ PublicInbox::Syscall::nodatacow_dir($dir);
+ open my $fh, '+>>', $dbname or die "failed to open $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;