]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Msgmap.pm
msgmap: add tmp_clone to create an anonymous copy
[public-inbox.git] / lib / PublicInbox / Msgmap.pm
index 8e81fba08d12b81ba6b1ead7709881f137c38ff4..78922d3693c9647e0b11c3dc7fc300ce1a985147 100644 (file)
@@ -12,6 +12,7 @@ use strict;
 use warnings;
 use DBI;
 use DBD::SQLite;
+use File::Temp qw(tempfile);
 
 sub new {
        my ($class, $git_dir, $writable) = @_;
@@ -45,6 +46,18 @@ sub new_file {
        $self;
 }
 
+# used to keep track of used numeric mappings for v2 reindex
+sub tmp_clone {
+       my ($self) = @_;
+       my ($fh, $fn) = tempfile(EXLOCK => 0);
+       $self->{dbh}->sqlite_backup_to_file($fn);
+       my $tmp = ref($self)->new_file($fn, 1);
+       $tmp->{dbh}->do('PRAGMA synchronous = OFF');
+       $tmp->{tmp_name} = $fn; # SQLite won't work if unlinked, apparently
+       $fh = undef;
+       $tmp;
+}
+
 # n.b. invoked directly by scripts/xhdr-num2mid
 sub meta_accessor {
        my ($self, $key, $value) = @_;
@@ -189,4 +202,10 @@ sub mid_set {
        $sth->execute($num, $mid);
 }
 
+sub DESTROY {
+       my ($self) = @_;
+       delete $self->{dbh};
+       unlink $self->{tmp_name} if defined $self->{tmp_name};
+}
+
 1;