X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FIMAPTracker.pm;h=6d4fb22748e90e399f7370f603e1d3712e73f78c;hb=af0b0fb7a454470a32c452119d0392e0dedb3fe1;hp=bb4a39cc41a64968b526a09d3f4fe4b972ba8ec0;hpb=b6f82589757137f16ac9177676968cdde2b06400;p=public-inbox.git diff --git a/lib/PublicInbox/IMAPTracker.pm b/lib/PublicInbox/IMAPTracker.pm index bb4a39cc..6d4fb227 100644 --- a/lib/PublicInbox/IMAPTracker.pm +++ b/lib/PublicInbox/IMAPTracker.pm @@ -1,7 +1,8 @@ -# Copyright (C) 2018-2020 all contributors +# Copyright (C) 2018-2021 all contributors # 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,43 @@ 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_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($url, $validity, $last); + $self->lock_acquire; + my $rv = $sth->execute($self->{url}, $validity, $last_uid); + $self->lock_release; + $rv; } sub new { - my ($class, $dbname) = @_; + my ($class, $url) = @_; # original name for compatibility with old setups: - $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) { @@ -65,12 +75,14 @@ sub new { } if (!-f $dbname) { require File::Path; - require File::Basename;; + require File::Basename; File::Path::mkpath(File::Basename::dirname($dbname)); } - - my $dbh = dbh_new($dbname); - bless { dbname => $dbname, dbh => $dbh }, $class; + my $self = bless { lock_path => "$dbname.lock", url => $url }, $class; + $self->lock_acquire; + $self->{dbh} = dbh_new($dbname); + $self->lock_release; + $self; } 1;