1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 package PublicInbox::IMAPTracker;
5 use parent qw(PublicInbox::Lock);
8 use PublicInbox::Config;
10 sub create_tables ($) {
14 CREATE TABLE IF NOT EXISTS imap_last (
15 url VARCHAR PRIMARY KEY NOT NULL,
16 uid_validity INTEGER NOT NULL,
25 my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", '', '', {
29 sqlite_use_immediate_transaction => 1,
31 $dbh->{sqlite_unicode} = 1;
33 # TRUNCATE reduces I/O compared to the default (DELETE).
34 # Allow and preserve user-overridden WAL, but don't force it.
35 my $jm = $dbh->selectrow_array('PRAGMA journal_mode');
36 $dbh->do('PRAGMA journal_mode = TRUNCATE') if $jm ne 'wal';
44 my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
45 SELECT uid_validity, uid FROM imap_last WHERE url = ?
47 $sth->execute($self->{url});
51 sub update_last ($$$) {
52 my ($self, $validity, $last) = @_;
53 my $sth = $self->{dbh}->prepare_cached(<<'');
54 INSERT OR REPLACE INTO imap_last (url, uid_validity, uid)
58 my $rv = $sth->execute($self->{url}, $validity, $last);
64 my ($class, $url) = @_;
66 # original name for compatibility with old setups:
67 my $dbname = PublicInbox::Config->config_dir() . "/imap.sqlite3";
69 # use the new XDG-compliant name for new setups:
71 $dbname = ($ENV{XDG_DATA_HOME} //
72 (($ENV{HOME} // '/nonexistent').'/.local/share')) .
73 '/public-inbox/imap.sqlite3';
77 require File::Basename;
78 File::Path::mkpath(File::Basename::dirname($dbname));
80 my $self = bless { lock_path => "$dbname.lock", url => $url }, $class;
82 $self->{dbh} = dbh_new($dbname);