From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS6315 166.70.0.0/16 X-Spam-Status: No, score=-3.6 required=3.0 tests=AWL,BAYES_00, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from out02.mta.xmission.com (out02.mta.xmission.com [166.70.13.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 245701F4BD; Wed, 9 Oct 2019 08:25:49 +0000 (UTC) Received: from in01.mta.xmission.com ([166.70.13.51]) by out02.mta.xmission.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.87) (envelope-from ) id 1iI7I4-0003bX-GD; Wed, 09 Oct 2019 02:25:48 -0600 Received: from ip68-227-160-95.om.om.cox.net ([68.227.160.95] helo=x220.xmission.com) by in01.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.87) (envelope-from ) id 1iI7I3-0002A2-Kw; Wed, 09 Oct 2019 02:25:48 -0600 From: ebiederm@xmission.com (Eric W. Biederman) To: Eric Wong Cc: meta@public-inbox.org References: <87imp05hlm.fsf@alyssa.is> <20191008001050.rwd7bh7cek7qrydi@dcvr> <87wodfctwd.fsf@x220.int.ebiederm.org> <20191008221108.3wsso25kviiwd7ek@dcvr> <87wodec1um.fsf@x220.int.ebiederm.org> <20191008224104.GA24142@dcvr> <87h84ibb9m.fsf@x220.int.ebiederm.org> <87wode9vxw.fsf_-_@x220.int.ebiederm.org> Date: Wed, 09 Oct 2019 03:25:03 -0500 In-Reply-To: <87wode9vxw.fsf_-_@x220.int.ebiederm.org> (Eric W. Biederman's message of "Wed, 09 Oct 2019 03:15:07 -0500") Message-ID: <874l0i9vhc.fsf_-_@x220.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1iI7I3-0002A2-Kw;;;mid=<874l0i9vhc.fsf_-_@x220.int.ebiederm.org>;;;hst=in01.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX180aC/H55/GCfFyQYKM6ka61Xo/FpZoF+k= X-SA-Exim-Connect-IP: 68.227.160.95 X-SA-Exim-Mail-From: ebiederm@xmission.com Subject: [PATCH 4/4] IMAPTracker: Add a helper to track our place in reading imap mailboxes X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) List-Id: Date: Fri, 27 Jul 2018 20:54:27 -0500 This removes the need to delete from an imap mailbox when downloading it's messages. Signed-off-by: "Eric W. Biederman" --- This is simple and potentially very useful. lib/PublicInbox/IMAPTracker.pm | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/PublicInbox/IMAPTracker.pm diff --git a/lib/PublicInbox/IMAPTracker.pm b/lib/PublicInbox/IMAPTracker.pm new file mode 100644 index 000000000000..65241eeebf51 --- /dev/null +++ b/lib/PublicInbox/IMAPTracker.pm @@ -0,0 +1,73 @@ +# Copyright (C) 2018 all contributors +# License: AGPL-3.0+ + +package PublicInbox::IMAPTracker; +use strict; +use warnings; +use DBI; +use DBD::SQLite; +use PublicInbox::Config; + +sub create_tables ($) +{ + my ($dbh) = @_; + + $dbh->do(<<''); +CREATE TABLE IF NOT EXISTS imap_last ( + url VARCHAR PRIMARY KEY NOT NULL, + uid_validity INTEGER NOT NULL, + uid INTEGER NOT NULL, + UNIQUE (url) +) + +} + + +sub dbh_new ($) +{ + my ($dbname) = @_; + my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", '', '', { + AutoCommit => 1, + RaiseError => 1, + PrintError => 0, + ReadOnly => 0, + sqlite_use_immediate_transaction => 1, + }); + $dbh->{sqlite_unicode} = 1; + $dbh->do('PRAGMA journal_mode = TRUNCATE'); + $dbh->do('PRAGMA cache_size = 80000'); + create_tables($dbh); + $dbh; +} + +sub get_last($$) +{ + my ($self, $url) = @_; + my $dbh = $self->{dbh}; + + my $sth = $dbh->prepare_cached(<<'', undef, 1); +SELECT uid_validity, uid FROM imap_last WHERE url = ? + + $sth->execute($url); + $sth->fetchrow_array; +} + +sub update_last($$$$) +{ + my ($self, $url, $validity, $last) = @_; + my $dbh = $self->{dbh}; + my $sth = $dbh->prepare_cached(<<''); +INSERT OR REPLACE INTO imap_last (url, uid_validity, uid) +VALUES (?, ?, ?) + + $sth->execute($url, $validity, $last); +} + +sub new { + my ($class) = @_; + my $dbname = PublicInbox::Config->config_dir() . "/imap.sqlite3"; + my $dbh = dbh_new($dbname); + bless { dbname => $dbname, dbh => $dbh }, $class; +} + +1; -- 2.23.0