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_uid) = @_;
53 return unless defined $last_uid;
54 my $sth = $self->{dbh}->prepare_cached(<<'');
55 INSERT OR REPLACE INTO imap_last (url, uid_validity, uid)
59 my $rv = $sth->execute($self->{url}, $validity, $last_uid);
65 my ($class, $url) = @_;
67 # original name for compatibility with old setups:
68 my $dbname = PublicInbox::Config->config_dir() . "/imap.sqlite3";
70 # use the new XDG-compliant name for new setups:
72 $dbname = ($ENV{XDG_DATA_HOME} //
73 (($ENV{HOME} // '/nonexistent').'/.local/share')) .
74 '/public-inbox/imap.sqlite3';
78 require File::Basename;
79 File::Path::mkpath(File::Basename::dirname($dbname));
81 my $self = bless { lock_path => "$dbname.lock", url => $url }, $class;
83 $self->{dbh} = dbh_new($dbname);