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

filter: Statementstmt 94.3% 83 / 88 Branchbran 64.2% 9 / 14 Conditioncond 54.5% 6 / 11 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 91.4% 32 / 35 Podpod 100% 18 / 18 totaltotal 87.7% 150 / 171 CC 13 SCAR 6.7
CC
13
Cov
87%
_track_statement
5.3
new
3.1
_assert_context_active
3.0
CRAP 2.0
« blib/lib/DBIO/Test/AsyncParity/Cases.pm ↑ Summary blib/lib/DBIO/ResultSource/View.pm »
1 package DBIO::Storage::Async::PipelineContext;
2 # ABSTRACT: Generic pinned-connection context for async pipeline batches
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 Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 109 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 109 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 109 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 77 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
16 77 my ($class, %args) = @_;
17 77 ▶ die('storage required') unless $args{storage};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"storage"} 0 77 77
18 77 ▶ die('pipeline_conn required') unless $args{pipeline_conn};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"pipeline_conn"} 0 77 77
19 return bless {
20 storage => $args{storage},
21 pipeline_conn => $args{pipeline_conn},
22 context_owner_token => $args{storage}->_context_owner_token,
23 77 }, $class;
24 }
25  
26  
27 41 ▶sub storage { $_[0]->{storage} }
Subroutine storage: called CC=1 SCAR=0.0
Pod documented
28  
29 # Snapshot captured at construction. Never derive it later from storage/pool:
30 # the same backend object can own a different pool generation after disconnect.
31 45 ▶sub _context_owner_token { $_[0]->{context_owner_token} }
Subroutine _context_owner_token: called CC=1 SCAR=0.0
32  
33  
34 2 ▶sub pipeline_conn { $_[0]->{pipeline_conn} }
Subroutine pipeline_conn: called CC=1 SCAR=0.0
Pod documented
35  
36  
37 0 ▶sub pool { $_[0]->{storage}->pool }
Subroutine pool: not called CC=1 SCAR=6.9
Pod documented
38  
39  
40 2 ▶sub future_class { $_[0]->{storage}->future_class }
Subroutine future_class: called CC=1 SCAR=0.0
Pod documented
41  
42  
43 1 ▶sub in_txn { 0 }
Subroutine in_txn: called CC=1 SCAR=0.0
Pod documented
44  
45  
46 47 ▶sub _context_active { ! $_[0]->{_invalidated} }
Subroutine _context_active: called CC=1 SCAR=0.0
47  
48 # Called by the pipeline scaffold once the synchronous batch run is over. From
49 # here on the bracketed connection is heading back to the pool, so the context
50 # must no longer accept work -- see _context_active.
51 72 ▶sub _invalidate { $_[0]->{_invalidated} = 1; return }
Subroutine _invalidate: called CC=1 SCAR=0.0
52  
53 41 ▶sub _assert_context_active {
Subroutine _assert_context_active: called CC=3 SCAR=11.1
54 41 my ($self) = @_;
55 41 ▶ DBIO::Exception->throw('pipeline context is no longer active')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_context_active 0 41 41
56 unless $self->_context_active;
57  
58 41 my $backend = $self->storage;
59 # The effective owner covers both the embedded connect path (a wired sync
60 # storage) and a primary/standalone backend that provisions its own sync
61 # sibling (ADR 0051, karr k204). Resolved through the shared helper so a bare
62 # driver mock backend -- one that fulfils only the older _owner_storage
63 # contract -- still works unchanged (ADR 0051 backward-compat addendum).
64 #
65 # Load-order: this context class is loaded standalone by dbio-async's
66 # DBIO::Async::PipelineContext (use base ...) WITHOUT pulling
67 # DBIO::Storage::Async, and a bare mock backend is not an Async subclass, so
68 # Async.pm may not be loaded yet when this runs -- the helper would then be an
69 # undefined subroutine (karr core k209). A compile-time
70 # `use DBIO::Storage::Async` cannot fix it: Async.pm use-s this class back
71 # (its line 14), so that closes a circular use. Break the cycle with a runtime
72 # require, which is idempotent and safe here because the helper is only ever
73 # needed at call time (dbio-perl-syntax: require to break a genuine circular
74 # dependency).
75 41 require DBIO::Storage::Async;
76 41 my $owner = DBIO::Storage::Async::_effective_owner_of($backend);
77 41 ▶ DBIO::Exception->throw('async context ownership cannot be proven')
Branch: 1 branch
BranchTrueFalseTotal
unless $owner 0 41 41
78 unless $owner;
79 41 $owner->_assert_async_context_owner($self);
80 38 return $self;
81 }
82  
83  
84 16 ▶sub _query_async {
Subroutine _query_async: called CC=1 SCAR=0.0
85 16 my ($self, $sql, $bind) = @_;
86 16 $self->_assert_context_active;
87 return $self->_track_statement(
88 16 $self->{storage}->_query_async_pinned($self->{pipeline_conn}, $sql, $bind)
89 );
90 }
91  
92  
93 37 ▶sub _track_statement {
Subroutine _track_statement: called CC=5 SCAR=16.7
94 37 my ($self, $future) = @_;
95 37 ▶ return $future unless Scalar::Util::blessed($future)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($future) and $future->can("then") and $future->can("is_ready") and not $future->is_ready 20 17 37
Condition44%4/9
builtin::blessed($future) and $future->can("then") and $future->can("is_ready") and not $future->is_ready
!ll&&!rl&&r
02017
builtin::blessed($future) and $future->can("then") and $future->can("is_ready")
!ll&&!rl&&r
0037
builtin::blessed($future) and $future->can("then")
!ll&&!rl&&r
0037
MC/DC25%1/4
builtin::blessed($future) and $future->can("then") and $future->can("is_ready") and not $future->is_ready
builtin::blessed($future) $future->can("then") $future->can("is_ready") not $future->is_ready
Truth table40%2/5
A and B and C and not(D)
A= builtin::blessed($future)B= $future->can("then")C= $future->can("is_ready")D= not $future->is_ready
ABCDresult
0XXX0
10XX0
110X0
11100
11111
96 && $future->can('then') && $future->can('is_ready')
97 && !$future->is_ready;
98 17 my $gate = ref($future)->new;
99 $self->{storage}->_hold_derived($future->then(
100 10 ▶ sub { $gate->done(@_) unless $gate->is_ready; return },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $gate->is_ready 8 2 10
101 5 ▶ sub { $gate->fail(@_) unless $gate->is_ready; return },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless $gate->is_ready 5 0 5
102 17 ));
103 17 push @{ $self->{_statements} }, $gate;
104 17 return $gate;
105 }
106  
107 # Every kept statement Future, answered or not, in the order handed out.
108 77 ▶sub _statements { return @{ $_[0]->{_statements} || [] } }
Subroutine _statements: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{'_statements'} || []
l!l
1562
MC/DC100%1/1
$_[0]{'_statements'} || []
$_[0]{'_statements'}
Truth table100%2/2
A
A= $_[0]{'_statements'}
Aresult
11
00
109  
110 # Every statement still open fails with the error that ended the batch.
111 35 ▶sub _fail_open_statements {
Subroutine _fail_open_statements: called CC=2 SCAR=6.9
112 35 my ($self, $error) = @_;
113 35 for my $gate (grep { !$_->is_ready } $self->_statements) {
114 4 $gate->fail($error);
115 }
116 35 return;
117 }
118  
119 # CRUD inside the batch MUST run on the connection the scaffold put into
120 # pipeline mode, not a fresh pooled one -- that is the whole point of the
121 # bracket. Each method routes through the storage's shared CRUD builder with
122 # the bracketed connection, so SQL generation (insert post-processing,
123 # select_single first-row) is not duplicated. Forwarding to
124 # $storage->select_async etc. would acquire a separate pool connection and run
125 # the query OUTSIDE the pipeline.
126  
127  
128 5 ▶sub select_async {
Subroutine select_async: called CC=1 SCAR=0.0
Pod documented
129 5 my $self = shift;
130 5 $self->_assert_context_active;
131 return $self->_track_statement(
132 4 $self->{storage}->_run_crud_pinned('select', $self->{pipeline_conn}, @_)
133 );
134 }
135  
136 2 ▶sub select_single_async {
Subroutine select_single_async: called CC=1 SCAR=0.0
Pod documented
137 2 my $self = shift;
138 2 $self->_assert_context_active;
139 return $self->_track_statement(
140 2 $self->{storage}->_run_crud_pinned('select_single', $self->{pipeline_conn}, @_)
141 );
142 }
143  
144 10 ▶sub insert_async {
Subroutine insert_async: called CC=1 SCAR=0.0
Pod documented
145 10 my $self = shift;
146 10 $self->_assert_context_active;
147 return $self->_track_statement(
148 10 $self->{storage}->_run_crud_pinned('insert', $self->{pipeline_conn}, @_)
149 );
150 }
151  
152 2 ▶sub update_async {
Subroutine update_async: called CC=1 SCAR=0.0
Pod documented
153 2 my $self = shift;
154 2 $self->_assert_context_active;
155 return $self->_track_statement(
156 2 $self->{storage}->_run_crud_pinned('update', $self->{pipeline_conn}, @_)
157 );
158 }
159  
160 3 ▶sub delete_async {
Subroutine delete_async: called CC=1 SCAR=0.0
Pod documented
161 3 my $self = shift;
162 3 $self->_assert_context_active;
163 return $self->_track_statement(
164 3 $self->{storage}->_run_crud_pinned('delete', $self->{pipeline_conn}, @_)
165 );
166 }
167  
168 # No blocking counterparts. The sync form of every other DBIO async surface is
169 # "call the *_async twin and ->get it" -- which cannot work here: a batched
170 # statement's result arrives at _pipeline_sync, and the sync only runs once the
171 # batch coderef has returned. ->get inside the batch would therefore wait for a
172 # result whose delivery is waiting on the caller it is blocking. Refusing with
173 # the reason beats hanging the process (and beats "Can't locate object method",
174 # which reads like the context is simply incomplete).
175  
176  
177 2 ▶sub select { $_[0]->_no_blocking('select') }
Subroutine select: called CC=1 SCAR=0.0
Pod documented
178 1 ▶sub select_single { $_[0]->_no_blocking('select_single') }
Subroutine select_single: called CC=1 SCAR=0.0
Pod documented
179 1 ▶sub insert { $_[0]->_no_blocking('insert') }
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
180 1 ▶sub update { $_[0]->_no_blocking('update') }
Subroutine update: called CC=1 SCAR=0.0
Pod documented
181 1 ▶sub delete { $_[0]->_no_blocking('delete') }
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
182  
183 6 ▶sub _no_blocking {
Subroutine _no_blocking: called CC=1 SCAR=0.0
184 6 my ($self, $op) = @_;
185 6 croak "Blocking ->$op is not available inside a pipeline batch: a batched "
186 . "statement resolves at the pipeline sync, which runs after the batch "
187 . "coderef returns -- blocking on it here would deadlock. Call "
188 . "->${op}_async and return an aggregate Future from the batch.";
189 }
190  
191 # Delegations
192  
193  
194 0 ▶sub sql_maker { my $self = shift; $self->{storage}->sql_maker(@_) }
Subroutine sql_maker: not called CC=1 SCAR=6.9
Pod documented
195 0 ▶sub debug { my $self = shift; $self->{storage}->debug(@_) }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
196  
197 109 1;
198  
199 __END__
« blib/lib/DBIO/Test/AsyncParity/Cases.pm ↑ Summary blib/lib/DBIO/ResultSource/View.pm »