]> Sergey Matveev's repositories - public-inbox.git/commitdiff
msgmap: fix use of transactions
authorEric Wong <e@80x24.org>
Sun, 31 Jul 2016 00:02:05 +0000 (00:02 +0000)
committerEric Wong <e@80x24.org>
Sun, 31 Jul 2016 01:15:58 +0000 (01:15 +0000)
We want transactions to be the responsibility of the
caller when possible; this fixes the potential for
the msgmap to internally become inconsistent when
using it from inside searchidx.

lib/PublicInbox/Msgmap.pm
lib/PublicInbox/SearchIdx.pm

index 8fe17a9505f0db151fe8c7f8f67ff9170b54e0b9..2583ff47422c4e1d7c3d9e36344c453ea891b9b5 100644 (file)
@@ -33,7 +33,9 @@ sub new {
 
        if ($writable) {
                create_tables($dbh);
+               $dbh->begin_work;
                $self->created_at(time) unless $self->created_at;
+               $dbh->commit;
        }
        $self;
 }
@@ -51,22 +53,14 @@ sub meta_accessor {
        defined $value or
                return $dbh->selectrow_array(meta_select, undef, $key);
 
-       $dbh->begin_work;
-       eval {
-               $prev = $dbh->selectrow_array(meta_select, undef, $key);
+       $prev = $dbh->selectrow_array(meta_select, undef, $key);
 
-               if (defined $prev) {
-                       $dbh->do(meta_update, undef, $value, $key);
-               } else {
-                       $dbh->do(meta_insert, undef, $key, $value);
-               }
-               $dbh->commit;
-       };
-       my $err = $@;
-       return $prev unless $err;
-
-       $dbh->rollback;
-       die $err;
+       if (defined $prev) {
+               $dbh->do(meta_update, undef, $value, $key);
+       } else {
+               $dbh->do(meta_insert, undef, $key, $value);
+       }
+       $prev;
 }
 
 sub last_commit {
index c2bf9a2ddc25d3232a58fb526566665407582ec6..3f2643c6ac1baf2f3125abe99c2da33241caf40e 100644 (file)
@@ -369,18 +369,18 @@ sub _index_sync {
                        # Common case is the indexes are synced,
                        # we only need to run git-log once:
                        $lx = $self->rlog($range, *index_both, *unindex_both);
-                       $mm->{dbh}->commit;
                        if (defined $lx) {
                                $db->set_metadata('last_commit', $lx);
                                $mm->last_commit($lx);
                        }
+                       $mm->{dbh}->commit;
                } else {
                        # dumb case, msgmap and xapian are out-of-sync
                        # do not care for performance:
                        my $r = $lm eq '' ? $head : "$lm..$head";
                        $lm = $self->rlog($r, *index_mm, *unindex_mm);
-                       $mm->{dbh}->commit;
                        $mm->last_commit($lm) if defined $lm;
+                       $mm->{dbh}->commit;
 
                        $lx = $self->rlog($range, *index_mm2, *unindex_mm2);
                        $db->set_metadata('last_commit', $lx) if defined $lx;
@@ -390,12 +390,7 @@ sub _index_sync {
                $lx = $self->rlog($range, *index_blob, *unindex_blob);
                $db->set_metadata('last_commit', $lx) if defined $lx;
        }
-       if ($@) {
-               $db->cancel_transaction;
-               $mm->{dbh}->rollback if $mm;
-       } else {
-               $db->commit_transaction;
-       }
+       $db->commit_transaction;
 }
 
 # this will create a ghost as necessary