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: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id CB4C21F4CE; Tue, 5 Apr 2022 08:18:24 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH] lei: always open mail_sync.sqlite3 R/W Date: Tue, 5 Apr 2022 08:18:24 +0000 Message-Id: <20220405081824.1670999-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This will make transparently upgrading from 1.7.0 -> 1.8.x easier. Only a single user has access to mail_sync.sqlite3, and R/W at the kernel-level is required for WAL, anyways. --- lib/PublicInbox/LEI.pm | 6 +++--- lib/PublicInbox/LeiImport.pm | 8 ++++---- lib/PublicInbox/LeiImportKw.pm | 6 +++--- lib/PublicInbox/LeiLcat.pm | 10 +++++----- lib/PublicInbox/LeiMailSync.pm | 34 +++++++++++++++++----------------- lib/PublicInbox/LeiSearch.pm | 4 ++-- lib/PublicInbox/NetReader.pm | 4 ++-- 7 files changed, 36 insertions(+), 36 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 4e0295fa..a7ddc21f 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # Backend for `lei' (local email interface). Unlike the C10K-oriented @@ -1502,11 +1502,11 @@ sub git_oid { } sub lms { - my ($lei, $rw) = @_; + my ($lei, $creat) = @_; my $sto = $lei->{sto} // _lei_store($lei) // return; require PublicInbox::LeiMailSync; my $f = "$sto->{priv_eidx}->{topdir}/mail_sync.sqlite3"; - (-f $f || $rw) ? PublicInbox::LeiMailSync->new($f) : undef; + (-f $f || $creat) ? PublicInbox::LeiMailSync->new($f) : undef; } sub sto_done_request { diff --git a/lib/PublicInbox/LeiImport.pm b/lib/PublicInbox/LeiImport.pm index bbc0634e..b9865829 100644 --- a/lib/PublicInbox/LeiImport.pm +++ b/lib/PublicInbox/LeiImport.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # front-end for the "lei import" sub-command @@ -36,7 +36,7 @@ sub pmdir_cb { # called via wq_io_do from LeiPmdir->each_mdir_fn my $kw = PublicInbox::MdirReader::flags2kw($fl); substr($folder, 0, 0) = 'maildir:'; # add prefix my $lse = $self->{lse} //= $self->{lei}->{sto}->search; - my $lms = $self->{-lms_ro} //= $self->{lei}->lms; # may be 0 or undef + my $lms = $self->{-lms_rw} //= $self->{lei}->lms; # may be 0 or undef my @oidbin = $lms ? $lms->name_oidbin($folder, $bn) : (); @oidbin > 1 and warn("W: $folder/*/$$bn not unique:\n", map { "\t".unpack('H*', $_)."\n" } @oidbin); @@ -87,8 +87,8 @@ sub do_import_index ($$@) { # $j = $net->net_concurrency($j); TODO if ($lei->{opt}->{incremental} // 1) { $net->{incremental} = 1; - $net->{-lms_ro} = $lei->lms // 0; - if ($self->{-import_kw} && $net->{-lms_ro} && + $net->{-lms_rw} = $lei->lms // 0; + if ($self->{-import_kw} && $net->{-lms_rw} && !$lei->{opt}->{'new-only'} && $net->{imap_order}) { require PublicInbox::LeiImportKw; diff --git a/lib/PublicInbox/LeiImportKw.pm b/lib/PublicInbox/LeiImportKw.pm index 54454511..52fd4043 100644 --- a/lib/PublicInbox/LeiImportKw.pm +++ b/lib/PublicInbox/LeiImportKw.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # WQ worker for dealing with LeiImport IMAP flags on already-imported messages @@ -28,13 +28,13 @@ sub ipc_atfork_child { $self->{verbose} = $lei->{opt}->{verbose}; $self->{lse} = $self->{sto}->search; $self->{over} = $self->{lse}->over; - $self->{-lms_ro} = $net->{-lms_ro} || die 'BUG: net->{-lms_ro} FALSE'; + $self->{-lms_rw} = $net->{-lms_rw} || die 'BUG: net->{-lms_rw} FALSE'; $self->SUPER::ipc_atfork_child; } sub ck_update_kw { # via wq_io_do my ($self, $url, $uid, $kw) = @_; - my @oidbin = $self->{-lms_ro}->num_oidbin($url, $uid); + my @oidbin = $self->{-lms_rw}->num_oidbin($url, $uid); my $uid_url = "$url/;UID=$uid"; @oidbin > 1 and warn("W: $uid_url not unique:\n", map { "\t".unpack('H*', $_)."\n" } @oidbin); diff --git a/lib/PublicInbox/LeiLcat.pm b/lib/PublicInbox/LeiLcat.pm index 191f6f24..8d89cb73 100644 --- a/lib/PublicInbox/LeiLcat.pm +++ b/lib/PublicInbox/LeiLcat.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # lcat: local cat, display a local message by Message-ID or blob, @@ -13,7 +13,7 @@ use PublicInbox::MID qw($MID_EXTRACT); sub lcat_folder ($$;$$) { my ($lei, $folder, $beg, $end) = @_; - my $lms = $lei->{-lms_ro} //= $lei->lms // return; + my $lms = $lei->{-lms_rw} //= $lei->lms // return; my $folders = [ $folder ]; eval { $lms->arg2folder($lei, $folders) }; return $lei->child_error(0, "# unknown folder: $folder") if $@; @@ -31,7 +31,7 @@ sub lcat_folder ($$;$$) { sub lcat_imap_uri ($$) { my ($lei, $uri) = @_; # cf. LeiXSearch->lcat_dump - my $lms = $lei->{-lms_ro} //= $lei->lms // return; + my $lms = $lei->{-lms_rw} //= $lei->lms // return; if (defined $uri->uid) { push @{$lei->{lcat_todo}}, $lms->imap_oidhex($lei, $uri); } elsif (defined(my $fid = $lms->fid_for($$uri))) { @@ -45,7 +45,7 @@ sub lcat_nntp_uri ($$) { my ($lei, $uri) = @_; my $mid = $uri->message; # already unescaped by URI::news return "mid:$mid" if defined($mid); - my $lms = $lei->{-lms_ro} //= $lei->lms // return; + my $lms = $lei->{-lms_rw} //= $lei->lms // return; my ($ng, $beg, $end) = $uri->group; $uri->group($ng); lcat_folder($lei, $$uri, $beg, $end); @@ -118,7 +118,7 @@ could not extract Message-ID from $x } } - delete $lei->{-lms_ro}; + delete $lei->{-lms_rw}; @q ? join(' OR ', @q) : $lei->fail("no Message-ID in: @argv"); } diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm index a9a65fd6..85480599 100644 --- a/lib/PublicInbox/LeiMailSync.pm +++ b/lib/PublicInbox/LeiMailSync.pm @@ -11,9 +11,9 @@ use PublicInbox::ContentHash qw(git_sha); use Carp (); sub dbh_new { - my ($self, $rw) = @_; + my ($self) = @_; my $f = $self->{filename}; - my $creat = $rw && !-s $f; + my $creat = !-s $f; if ($creat) { require PublicInbox::Syscall; open my $fh, '+>>', $f or Carp::croak "open($f): $!"; @@ -23,11 +23,10 @@ sub dbh_new { AutoCommit => 1, RaiseError => 1, PrintError => 0, - ReadOnly => !$rw, sqlite_use_immediate_transaction => 1, }); # no sqlite_unicode, here, all strings are binary - create_tables($self, $dbh) if $rw; + create_tables($self, $dbh); $dbh->do('PRAGMA journal_mode = WAL') if $creat; $dbh->do('PRAGMA case_sensitive_like = ON'); $dbh; @@ -42,7 +41,7 @@ sub new { }, $cls; } -sub lms_write_prepare { ($_[0]->{dbh} //= dbh_new($_[0], 1)); $_[0] } +sub lms_write_prepare { ($_[0]->{dbh} //= dbh_new($_[0])); $_[0] } sub lms_pause { my ($self) = @_; @@ -102,7 +101,7 @@ UPDATE folders SET loc = ? WHERE fid = ? } sub get_fid ($$$) { - my ($sth, $folder, $dbh) = @_; # $dbh is set iff RW + my ($sth, $folder, $dbh) = @_; $sth->bind_param(1, $folder, SQL_BLOB); $sth->execute; my ($fid) = $sth->fetchrow_array; @@ -118,36 +117,37 @@ sub get_fid ($$$) { } sub fid_for { - my ($self, $folder, $rw) = @_; - my $dbh = $self->{dbh} //= dbh_new($self, $rw); + my ($self, $folder, $creat) = @_; + my $dbh = $self->{dbh} //= dbh_new($self); my $sth = $dbh->prepare_cached(<<'', undef, 1); SELECT fid FROM folders WHERE loc = ? LIMIT 1 - my $rw_dbh = $dbh->{ReadOnly} ? undef : $dbh; - my $fid = get_fid($sth, $folder, $rw_dbh); + my $fid = get_fid($sth, $folder, $dbh); return $fid if defined($fid); # caller had trailing slash (LeiToMail) if ($folder =~ s!\A((?:maildir|mh):.*?)/+\z!$1!i) { - $fid = get_fid($sth, $folder, $rw_dbh); + $fid = get_fid($sth, $folder, $dbh); if (defined $fid) { - update_fid($dbh, $fid, $folder) if $rw; + update_fid($dbh, $fid, $folder); return $fid; } # sometimes we stored trailing slash.. } elsif ($folder =~ m!\A(?:maildir|mh):!i) { - $fid = get_fid($sth, $folder, $rw_dbh); + $fid = get_fid($sth, $folder, $dbh); if (defined $fid) { - update_fid($dbh, $fid, $folder) if $rw; + update_fid($dbh, $fid, $folder); return $fid; } - } elsif ($rw && $folder =~ m!\Aimaps?://!i) { + } elsif ($creat && $folder =~ m!\Aimaps?://!i) { require PublicInbox::URIimap; - PublicInbox::URIimap->new($folder)->uidvalidity // + my $uri = PublicInbox::URIimap->new($folder); + $uri->uidvalidity // Carp::croak("BUG: $folder has no UIDVALIDITY"); + defined($uri->uid) and Carp::confess("BUG: $folder has UID"); } - return unless $rw; + return unless $creat; ($fid) = $dbh->selectrow_array('SELECT MAX(fid) FROM folders'); $fid += 1; diff --git a/lib/PublicInbox/LeiSearch.pm b/lib/PublicInbox/LeiSearch.pm index d0ca13f0..936c2751 100644 --- a/lib/PublicInbox/LeiSearch.pm +++ b/lib/PublicInbox/LeiSearch.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # read-only counterpart for PublicInbox::LeiStore @@ -101,7 +101,7 @@ sub xoids_for { my $git = $self->git; my $xoids = {}; # no lms when used via {ale}: - my $lms = $self->{-lms_ro} //= lms($self) if defined($self->{topdir}); + my $lms = $self->{-lms_rw} //= lms($self) if defined($self->{topdir}); for my $mid (@$mids) { for my $o (@overs) { my ($id, $prev); diff --git a/lib/PublicInbox/NetReader.pm b/lib/PublicInbox/NetReader.pm index 032b4fda..c1af03a3 100644 --- a/lib/PublicInbox/NetReader.pm +++ b/lib/PublicInbox/NetReader.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # common reader code for IMAP and NNTP (and maybe JMAP) @@ -481,7 +481,7 @@ sub itrk_last ($$;$$) { my ($self, $uri, $r_uidval, $mic) = @_; return (undef, undef, $r_uidval) unless $self->{incremental}; my ($itrk, $l_uid, $l_uidval); - if (defined(my $lms = $self->{-lms_ro})) { # LeiMailSync or 0 + if (defined(my $lms = $self->{-lms_rw})) { # LeiMailSync or 0 $uri->uidvalidity($r_uidval) if defined $r_uidval; if ($mic) { my $auth = $mic->Authmechanism // '';