X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FIMAPTracker.pm;h=92f21584842d97d2d6d1d3747596e251ad8719d6;hb=1e3c53a422b8d23cff961e43f77ea0a835cdef78;hp=c7da422b725f0c9d8cab1c8bb3ccdb22aa7898c6;hpb=80b6cf9fb3aafe7a2d5e2fee1b420a93c6c9b0ae;p=public-inbox.git diff --git a/lib/PublicInbox/IMAPTracker.pm b/lib/PublicInbox/IMAPTracker.pm index c7da422b..92f21584 100644 --- a/lib/PublicInbox/IMAPTracker.pm +++ b/lib/PublicInbox/IMAPTracker.pm @@ -2,6 +2,7 @@ # License: AGPL-3.0+ package PublicInbox::IMAPTracker; use strict; +use parent qw(PublicInbox::Lock); use DBI; use DBD::SQLite; use PublicInbox::Config; @@ -28,34 +29,59 @@ 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; } -sub get_last ($$) { - my ($self, $url) = @_; +sub get_last ($) { + my ($self) = @_; my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1); SELECT uid_validity, uid FROM imap_last WHERE url = ? - $sth->execute($url); + $sth->execute($self->{url}); $sth->fetchrow_array; } -sub update_last ($$$$) { - my ($self, $url, $validity, $last) = @_; +sub update_last ($$$) { + my ($self, $validity, $last) = @_; my $sth = $self->{dbh}->prepare_cached(<<''); INSERT OR REPLACE INTO imap_last (url, uid_validity, uid) VALUES (?, ?, ?) - $sth->execute($url, $validity, $last); + $self->lock_acquire; + my $rv = $sth->execute($self->{url}, $validity, $last); + $self->lock_release; + $rv; } sub new { - my ($class) = @_; + my ($class, $url) = @_; + + # original name for compatibility with old setups: my $dbname = PublicInbox::Config->config_dir() . "/imap.sqlite3"; - my $dbh = dbh_new($dbname); - bless { dbname => $dbname, dbh => $dbh }, $class; + + # use the new XDG-compliant name for new setups: + if (!-f $dbname) { + $dbname = ($ENV{XDG_DATA_HOME} // + (($ENV{HOME} // '/nonexistent').'/.local/share')) . + '/public-inbox/imap.sqlite3'; + } + if (!-f $dbname) { + require File::Path; + require File::Basename; + File::Path::mkpath(File::Basename::dirname($dbname)); + } + my $self = bless { lock_path => "$dbname.lock", url => $url }, $class; + $self->lock_acquire; + $self->{dbh} = dbh_new($dbname); + $self->lock_release; + $self; } 1;