about summary refs log tree commit homepage
path: root/lib/PublicInbox
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-07-14 02:14:32 +0000
committerEric Wong <e@yhbt.net>2020-07-14 23:23:03 +0000
commitf06e84220e5566e74c4af675a7afaf1636b024e4 (patch)
tree9ce2bb6cf147c24cb346ab3f36f53751863cec68 /lib/PublicInbox
parent322a79a6421b5993775f63ea25294a97c5203ac0 (diff)
downloadpublic-inbox-f06e84220e5566e74c4af675a7afaf1636b024e4.tar.gz
over+msgmap: do not store filename after DBI->connect
SQLite already knows the filename internally, so avoid having it
as a long-lived Perl SV to save some bytes when there's many
inboxes and open DBs.
Diffstat (limited to 'lib/PublicInbox')
-rw-r--r--lib/PublicInbox/Inbox.pm4
-rw-r--r--lib/PublicInbox/Msgmap.pm14
-rw-r--r--lib/PublicInbox/Over.pm30
-rw-r--r--lib/PublicInbox/OverIdx.pm2
4 files changed, 33 insertions, 17 deletions
diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm
index 3d9754dc..267be4e3 100644
--- a/lib/PublicInbox/Inbox.pm
+++ b/lib/PublicInbox/Inbox.pm
@@ -204,9 +204,9 @@ sub search ($;$) {
 sub over ($) {
         my ($self) = @_;
         my $srch = search($self, 1) or return;
-        $self->{over} ||= eval {
+        $self->{over} //= eval {
                 my $over = $srch->{over_ro};
-                $over->dbh_new; # may fail
+                $over->connect; # may fail
                 $over;
         }
 }
diff --git a/lib/PublicInbox/Msgmap.pm b/lib/PublicInbox/Msgmap.pm
index e86fb854..38ec7858 100644
--- a/lib/PublicInbox/Msgmap.pm
+++ b/lib/PublicInbox/Msgmap.pm
@@ -229,9 +229,9 @@ sub mid_set {
 
 sub DESTROY {
         my ($self) = @_;
-        delete $self->{dbh};
-        my $f = $self->{filename};
+        my $dbh = $self->{dbh} or return;
         if (($self->{pid} // 0) == $$) {
+                my $f = $dbh->sqlite_db_filename;
                 unlink $f or warn "failed to unlink $f: $!\n";
         }
 }
@@ -239,8 +239,9 @@ sub DESTROY {
 sub atfork_parent {
         my ($self) = @_;
         $self->{pid} or die "not a temporary clone\n";
-        delete $self->{dbh} and die "tmp_clone dbh not prepared for parent";
-        my $dbh = $self->{dbh} = PublicInbox::Over::dbh_new($self, 1);
+        my $dbh = $self->{dbh} and die "tmp_clone dbh not prepared for parent";
+        $self->{filename} = $dbh->sqlite_db_filename;
+        $dbh = $self->{dbh} = PublicInbox::Over::dbh_new($self, 1);
         $dbh->do('PRAGMA synchronous = OFF');
 }
 
@@ -249,9 +250,10 @@ sub atfork_prepare {
         $self->{pid} or die "not a temporary clone\n";
         $self->{pid} == $$ or
                 die "BUG: atfork_prepare not called from $self->{pid}\n";
-        $self->{dbh} or die "temporary clone not open\n";
+        my $dbh = $self->{dbh} or die "temporary clone not open\n";
+
         # must clobber prepared statements
-        %$self = (filename => $self->{filename}, pid => $$);
+        %$self = (filename => $dbh->sqlite_db_filename, pid => $$);
 }
 
 sub skip_artnum {
diff --git a/lib/PublicInbox/Over.pm b/lib/PublicInbox/Over.pm
index 5d285057..e3f26456 100644
--- a/lib/PublicInbox/Over.pm
+++ b/lib/PublicInbox/Over.pm
@@ -15,9 +15,13 @@ use constant DEFAULT_LIMIT => 1000;
 
 sub dbh_new {
         my ($self, $rw) = @_;
-        my $f = $self->{filename};
-        if ($rw && !-f $f) { # SQLite defaults mode to 0644, we want 0666
-                open my $fh, '+>>', $f or die "failed to open $f: $!";
+        my $f = delete $self->{filename};
+        if (!-f $f) { # SQLite defaults mode to 0644, we want 0666
+                if ($rw) {
+                        open my $fh, '+>>', $f or die "failed to open $f: $!";
+                } else {
+                        $self->{filename} = $f; # die on stat() below:
+                }
         }
         my (@st, $st, $dbh);
         my $tries = 0;
@@ -44,9 +48,14 @@ sub new {
         bless { filename => $f }, $class;
 }
 
-sub disconnect { $_[0]->{dbh} = undef }
+sub disconnect {
+        my ($self) = @_;
+        if (my $dbh = delete $self->{dbh}) {
+                $self->{filename} = $dbh->sqlite_db_filename;
+        }
+}
 
-sub connect { $_[0]->{dbh} ||= $_[0]->dbh_new }
+sub connect { $_[0]->{dbh} //= $_[0]->dbh_new }
 
 sub load_from_row ($;$) {
         my ($smsg, $cull) = @_;
@@ -258,13 +267,18 @@ SELECT COUNT(num) FROM over WHERE num > ? AND num <= ?
 
 sub check_inodes {
         my ($self) = @_;
-        if (my @st = stat($self->{filename})) { # did st_dev, st_ino change?
+        my $dbh = $self->{dbh} or return;
+        my $f = $dbh->sqlite_db_filename;
+        if (my @st = stat($f)) { # did st_dev, st_ino change?
                 my $st = pack('dd', $st[0], $st[1]);
 
                 # don't actually reopen, just let {dbh} be recreated later
-                delete($self->{dbh}) if ($st ne ($self->{st} // $st));
+                if ($st ne ($self->{st} // $st)) {
+                        delete($self->{dbh});
+                        $self->{filename} = $f;
+                }
         } else {
-                warn "W: stat $self->{filename}: $!\n";
+                warn "W: stat $f: $!\n";
         }
 }
 
diff --git a/lib/PublicInbox/OverIdx.pm b/lib/PublicInbox/OverIdx.pm
index 13aa2d74..ea8da723 100644
--- a/lib/PublicInbox/OverIdx.pm
+++ b/lib/PublicInbox/OverIdx.pm
@@ -431,7 +431,7 @@ sub rollback_lazy {
 sub disconnect {
         my ($self) = @_;
         die "in transaction" if $self->{txn};
-        $self->{dbh} = undef;
+        $self->SUPER::disconnect;
 }
 
 sub create {