blib/lib/DBIO/Storage/Async/TransactionContext.pm

filter: Statementstmt 95.2% 100 / 105 Branchbran 80.7% 21 / 26 Conditioncond 62.5% 10 / 16 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 92.1% 35 / 38 Podpod 100% 19 / 19 totaltotal 88.3% 189 / 214 CC 21 SCAR 7.6
CC
21
Cov
89%
pipeline
4.1
_invalidate
4.0
_track_statement
3.3
CRAP 2.1
« blib/lib/DBIO/Storage/DBI/Capabilities.pm ↑ Summary blib/lib/DBIO/ChangeLog/Entry.pm »
1 package DBIO::Storage::Async::TransactionContext;
2 # ABSTRACT: Generic pinned-connection context for async transactions
3  
4 109 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 109 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 109 our $VERSION = '0.900007';
8  
9 109 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 109 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 109 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 651 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
15 651 my ($class, %args) = @_;
16 651 ▶ die('storage required') unless $args{storage};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"storage"} 0 651 651
17 651 ▶ die('txn_conn required') unless $args{txn_conn};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"txn_conn"} 1 650 651
18 return bless {
19 storage => $args{storage},
20 txn_conn => $args{txn_conn},
21 context_owner_token => $args{storage}->_context_owner_token,
22 650 }, $class;
23 }
24  
25  
26 214 ▶sub storage { $_[0]->{storage} }
Subroutine storage: called CC=1 SCAR=0.0
Pod documented
27  
28 # Snapshot captured at construction. Never derive it later from storage/pool:
29 # the same backend object can own a different pool generation after disconnect.
30 548 ▶sub _context_owner_token { $_[0]->{context_owner_token} }
Subroutine _context_owner_token: called CC=1 SCAR=0.0
31  
32  
33 299 ▶sub txn_conn { $_[0]->{txn_conn} }
Subroutine txn_conn: called CC=1 SCAR=0.0
Pod documented
34  
35  
36 0 ▶sub pool { $_[0]->{storage}->pool }
Subroutine pool: not called CC=1 SCAR=6.9
Pod documented
37  
38  
39 37 ▶sub in_txn { return $_[0]->_context_active ? 1 : 0 }
Subroutine in_txn: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_context_active ? : 22 15 37
40  
41  
42 486 ▶sub _context_active { return $_[0]->{_invalidated} ? 0 : 1 }
Subroutine _context_active: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_invalidated'} ? : 103 383 486
43  
44 # The transaction has ended: nothing waits on its statements any more, and
45 # the context lists none. A statement still in flight keeps its Future all the
46 # same, until it is answered (karr k292). The context ends as ROLLBACK goes
47 # out, or in the finalizer on a connection nothing is waited for on -- before
48 # such a statement has its answer -- and the context may be that Future's only
49 # holder: a ->then sequence is held weakly by the Future it was made off, and
50 # Future.pm warns "lost a sequence Future" when the answer finds it gone. For
51 # a pipeline run on the transaction, that sequence is the one that leaves
52 # pipeline mode.
53 #
54 # So each Future is handed to itself: Future.pm's retain keeps a Future until
55 # it is ready -- done, failed or cancelled -- and lets go of it then, at once
56 # for one that is ready already. Asked through can(), as txn_do_async asks for
57 # its own Future: a class without retain (DBIO::Test::Future) holds a chain
58 # from its head and needs none.
59 1249 ▶sub _invalidate {
Subroutine _invalidate: called CC=4 SCAR=13.9
60 1249 my ($self) = @_;
61 1249 $self->{_invalidated} = 1;
62 1249 ▶ my $statements = delete $self->{_txn_statements} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $statements = delete $self->{"_txn_statements"} or return 172 1077 1249
63 172 for my $future (@$statements) {
64 198 ▶ $future->retain if $future->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $future->can("retain") 0 198 198
65 }
66 172 return;
67 }
68  
69 218 ▶sub _assert_context_active {
Subroutine _assert_context_active: called CC=3 SCAR=11.0
70 218 my ($self) = @_;
71 218 ▶ DBIO::Exception->throw('transaction context is no longer active')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_context_active 6 212 218
72 unless $self->_context_active;
73  
74 212 my $backend = $self->storage;
75 # The effective owner covers both the embedded connect path (a wired sync
76 # storage) and a primary/standalone backend that provisions its own sync
77 # sibling (ADR 0051, karr k204). Resolved through the shared helper so a bare
78 # driver mock backend -- one that fulfils only the older _owner_storage
79 # contract -- still works unchanged (ADR 0051 backward-compat addendum).
80 #
81 # Load-order: this context class is loaded standalone by dbio-async's
82 # DBIO::Async::TransactionContext (use base ...) WITHOUT pulling
83 # DBIO::Storage::Async, and a bare mock backend (above) is not an Async
84 # subclass, so Async.pm may not be loaded yet when this runs -- the helper
85 # would then be an undefined subroutine (karr core k209). A compile-time
86 # `use DBIO::Storage::Async` cannot fix it: Async.pm use-s this class back
87 # (its line 13), so that closes a circular use. Break the cycle with a
88 # runtime require, which is idempotent and safe here because the helper is
89 # only ever needed at call time (dbio-perl-syntax: require to break a genuine
90 # circular dependency).
91 212 require DBIO::Storage::Async;
92 212 my $owner = DBIO::Storage::Async::_effective_owner_of($backend);
93 212 ▶ DBIO::Exception->throw('async context ownership cannot be proven')
Branch: 1 branch
BranchTrueFalseTotal
unless $owner 0 212 212
94 unless $owner;
95 212 $owner->_assert_async_context_owner($self);
96 205 return $self;
97 }
98  
99  
100 19 ▶sub _query_async {
Subroutine _query_async: called CC=1 SCAR=0.0
101 19 my ($self, $sql, $bind) = @_;
102 19 $self->_assert_context_active;
103 return $self->_track_statement(
104 17 $self->{storage}->_query_async_pinned($self->{txn_conn}, $sql, $bind)
105 );
106 }
107  
108 # CRUD inside a transaction MUST run on the pinned connection that
109 # BEGIN/COMMIT ran on, not a fresh pooled one. Each method routes
110 # through the storage's shared CRUD builder with the pinned connection,
111 # so SQL generation (insert post-processing, select_single first-row)
112 # is not duplicated. Forwarding to $storage->select_async etc. would
113 # acquire a separate pool connection and run the query OUTSIDE the
114 # transaction.
115  
116  
117 61 ▶sub select_async {
Subroutine select_async: called CC=1 SCAR=0.0
Pod documented
118 61 my $self = shift;
119 61 $self->_assert_context_active;
120 return $self->_track_statement(
121 56 $self->{storage}->_run_crud_pinned('select', $self->{txn_conn}, @_)
122 );
123 }
124  
125 16 ▶sub select_single_async {
Subroutine select_single_async: called CC=1 SCAR=0.0
Pod documented
126 16 my $self = shift;
127 16 $self->_assert_context_active;
128 return $self->_track_statement(
129 16 $self->{storage}->_run_crud_pinned('select_single', $self->{txn_conn}, @_)
130 );
131 }
132  
133 66 ▶sub insert_async {
Subroutine insert_async: called CC=1 SCAR=0.0
Pod documented
134 66 my $self = shift;
135 66 $self->_assert_context_active;
136 return $self->_track_statement(
137 64 $self->{storage}->_run_crud_pinned('insert', $self->{txn_conn}, @_)
138 );
139 }
140  
141 16 ▶sub update_async {
Subroutine update_async: called CC=1 SCAR=0.0
Pod documented
142 16 my $self = shift;
143 16 $self->_assert_context_active;
144 return $self->_track_statement(
145 16 $self->{storage}->_run_crud_pinned('update', $self->{txn_conn}, @_)
146 );
147 }
148  
149 7 ▶sub delete_async {
Subroutine delete_async: called CC=1 SCAR=0.0
Pod documented
150 7 my $self = shift;
151 7 $self->_assert_context_active;
152 return $self->_track_statement(
153 7 $self->{storage}->_run_crud_pinned('delete', $self->{txn_conn}, @_)
154 );
155 }
156  
157 # Sync fallbacks -- block the event loop via ->get
158  
159  
160 1 ▶sub select { my $self = shift; $self->select_async(@_)->get }
Subroutine select: called CC=1 SCAR=0.0
Pod documented
161 1 ▶sub select_single { my $self = shift; $self->select_single_async(@_)->get }
Subroutine select_single: called CC=1 SCAR=0.0
Pod documented
162 3 ▶sub insert { my $self = shift; $self->insert_async(@_)->get }
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
163 1 ▶sub update { my $self = shift; $self->update_async(@_)->get }
Subroutine update: called CC=1 SCAR=0.0
Pod documented
164 1 ▶sub delete { my $self = shift; $self->delete_async(@_)->get }
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
165  
166 # Delegations
167  
168  
169 0 ▶sub sql_maker { my $self = shift; $self->{storage}->sql_maker(@_) }
Subroutine sql_maker: not called CC=1 SCAR=6.9
Pod documented
170 0 ▶sub debug { my $self = shift; $self->{storage}->debug(@_) }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
171  
172 27 ▶sub pipeline {
Subroutine pipeline: called CC=4 SCAR=14.0
Pod documented
173 27 my ($self, $coderef) = @_;
174 27 $self->_assert_context_active;
175 26 ▶ if (my $refusal = $self->{storage}->_pipeline_refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refusal = $self->{"storage"}->_pipeline_refusal) 1 25 26
176 1 return $refusal;
177 }
178 # No $release: the transaction boundary hands the connection back. A $retire
179 # that marks it instead, for that boundary to act on (karr k232).
180 my $batch = $self->{storage}->_pipeline_on_conn(
181 $self->{txn_conn}, $coderef, undef,
182 17 ▶ sub { $self->_mark_txn_conn_unusable(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
183 25 );
184 # Kept apart from the statements: the context lets go of those as it ends,
185 # and the transaction's ROLLBACK waits for these (karr k331). Weakly -- what
186 # holds the Future until it is ready is _track_statement and its retain
187 # (karr k292); a context still in hand must not hold it any longer.
188 25 ▶ if (Scalar::Util::blessed($batch) && $batch->can('catch')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($batch) and $batch->can("catch")) 25 0 25
Condition33%1/3
builtin::blessed($batch) and $batch->can("catch")
!ll&&!rl&&r
0025
MC/DC0%0/2
builtin::blessed($batch) and $batch->can("catch")
builtin::blessed($batch) $batch->can("catch")
Truth table33%1/3
A and B
A= builtin::blessed($batch)B= $batch->can("catch")
ABresult
0X0
100
111
189 25 push @{ $self->{_txn_pipelines} }, $batch;
190 25 Scalar::Util::weaken($self->{_txn_pipelines}[-1]);
191 }
192 25 return $self->_track_statement($batch);
193 }
194  
195  
196 17 ▶sub _mark_txn_conn_unusable {
Subroutine _mark_txn_conn_unusable: called CC=3 SCAR=11.0
197 17 my ($self, $error) = @_;
198 17 $self->{_txn_conn_unusable} = 1;
199 $self->{_txn_conn_unusable_error} = $error
200 17 ▶ if defined $error && !defined $self->{_txn_conn_unusable_error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $error and not defined $self->{"_txn_conn_unusable_error"} 7 10 17
Condition66%2/3
defined $error and not defined $self->{"_txn_conn_unusable_error"}
!ll&&!rl&&r
1007
MC/DC50%1/2
defined $error and not defined $self->{"_txn_conn_unusable_error"}
defined $error not defined $self->{"_txn_conn_unusable_error"}
Truth table66%2/3
A and not(B)
A= defined $errorB= not defined $self->{"_txn_conn_unusable_error"}
ABresult
0X0
100
111
201 17 return;
202 }
203  
204  
205 6 ▶sub _txn_conn_unusable_error { return $_[0]->{_txn_conn_unusable_error} }
Subroutine _txn_conn_unusable_error: called CC=1 SCAR=0.0
206  
207 1866 ▶sub _txn_conn_unusable { return $_[0]->{_txn_conn_unusable} ? 1 : 0 }
Subroutine _txn_conn_unusable: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_txn_conn_unusable'} ? : 35 1831 1866
208  
209  
210 636 ▶sub _set_txn_guard { $_[0]->{_txn_guard} = $_[1]; return }
Subroutine _set_txn_guard: called CC=1 SCAR=0.0
211  
212 133 ▶sub _txn_guard { return $_[0]->{_txn_guard} }
Subroutine _txn_guard: called CC=1 SCAR=0.0
213  
214  
215 201 ▶sub _track_statement {
Subroutine _track_statement: called CC=3 SCAR=12.0
216 201 my ($self, $future) = @_;
217 201 ▶ push @{ $self->{_txn_statements} }, $future
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($future) and $future->can("catch") 201 0 201
Condition33%1/3
builtin::blessed($future) and $future->can("catch")
!ll&&!rl&&r
00201
MC/DC0%0/2
builtin::blessed($future) and $future->can("catch")
builtin::blessed($future) $future->can("catch")
Truth table33%1/3
A and B
A= builtin::blessed($future)B= $future->can("catch")
ABresult
0X0
100
111
218 if Scalar::Util::blessed($future) && $future->can('catch');
219 201 return $future;
220 }
221  
222 648 ▶sub _txn_statements { return @{ $_[0]->{_txn_statements} || [] } }
Subroutine _txn_statements: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{'_txn_statements'} || []
l!l
247401
MC/DC100%1/1
$_[0]{'_txn_statements'} || []
$_[0]{'_txn_statements'}
Truth table100%2/2
A
A= $_[0]{'_txn_statements'}
Aresult
11
00
223  
224  
225 105 ▶sub _txn_pending_pipelines {
Subroutine _txn_pending_pipelines: called CC=3 SCAR=11.0
226 105 ▶ return grep { defined $_ && !$_->is_ready } @{ $_[0]->{_txn_pipelines} || [] };
Condition80%4/5
defined $_ && !$_->is_ready
!ll&&!rl&&r
104
$_[0]{'_txn_pipelines'} || []
l!l
5100
MC/DC50%1/2
defined $_ && !$_->is_ready
defined $_ !$_->is_ready
MC/DC100%1/1
$_[0]{'_txn_pipelines'} || []
$_[0]{'_txn_pipelines'}
Truth table66%2/3
A && not(B)
A= defined $_B= !$_->is_ready
ABresult
0X0
100
111
Truth table100%2/2
A
A= $_[0]{'_txn_pipelines'}
Aresult
11
00
227 }
228  
229 2 ▶sub txn_do_async {
Subroutine txn_do_async: called CC=1 SCAR=0.0
Pod documented
230 2 my $self = shift;
231 2 $self->_assert_context_active;
232 1 DBIO::Exception->throw(
233 'nested txn_do_async on a transaction context is not supported: there is '
234 . 'no async savepoint, so it could only open an independent transaction '
235 . 'on a second pool connection. Call $ctx->storage->txn_do_async if an '
236 . 'independent transaction is really intended'
237 );
238 }
239  
240 109 1;
241  
242 __END__
« blib/lib/DBIO/Storage/DBI/Capabilities.pm ↑ Summary blib/lib/DBIO/ChangeLog/Entry.pm »