blib/lib/DBIx/QuickORM/LookupTable/Scoped.pm

filter: Statementstmt 100% 81 / 81 Branchbran 100% 16 / 16 Conditioncond 92.3% 12 / 13 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 27 / 27 Podpod 10.5% 2 / 19 totaltotal 89.0% 146 / 164 CC 15 SCAR 9.0
CC
15
Cov
99%
canon_status
7.0
_resolve
3.0
is_canon
2.0
CRAP 2.5
« blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm ↑ Summary blib/lib/DBIx/QuickORM/Manual.pm »
1 package DBIx::QuickORM::LookupTable::Scoped;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.000033';
6  
7 2 ▶use Hash::Util::FieldHash qw/fieldhash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use parent 'DBIx::QuickORM::LookupTable';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 +levels
13 +tags
14 2 };
15  
16 =pod
17  
18 =encoding UTF-8
19  
20 =head1 NAME
21  
22 DBIx::QuickORM::LookupTable::Scoped - Guard transaction boundaries, with no row cache.
23  
24 =head1 DESCRIPTION
25  
26 C<< lookup => 'Scoped' >>. Rows work much as in L<DBIx::Class>: every read
27 gives new row data, nothing is cached, and two copies of one row are both
28 canonical. The one protection kept is the transaction boundary: row data from
29 outside a transaction is not canonical inside it, and row data from a
30 transaction that rolled back is not canonical anywhere. Writing with either
31 croaks under C<enforce_canon>, which inside a transaction is the default.
32  
33 It implements L<DBIx::QuickORM::Role::LookupTable> and inherits
34 L<DBIx::QuickORM::LookupTable>.
35  
36 =over 4
37  
38 =item *
39  
40 Row data read or written inside a transaction is tagged with that
41 transaction or savepoint (a fieldhash keyed by the hash, never stored in it).
42 It is canonical while its level is open. A savepoint commit hands its rows to
43 the enclosing level, a root commit makes them outside rows, and a rollback
44 ends them for good.
45  
46 =item *
47  
48 Row data from outside any transaction is not tagged, so outside a
49 transaction a read costs nothing beyond returning the row. Inside a
50 transaction untagged row data is not canonical.
51  
52 =back
53  
54 =head2 Risks
55  
56 Only the boundary is checked:
57  
58 =over 4
59  
60 =item *
61  
62 A stale copy of a row is canonical, inside a transaction or out: two reads
63 give two copies, and a write from the older one is not caught. Neither is a
64 row a forked child inherited, nor raw row data from another connection given to this one's CRUD
65 operations. Row objects always use their own ORM, so only raw hashes reach
66 the wrong connection.
67  
68 =item *
69  
70 Row data read before a transaction is canonical again once it ends, after a
71 commit too, even when the transaction changed that row. Such a copy holds the
72 old values, and a write from it can put them back.
73  
74 =item *
75  
76 Rows read around the lookup table (C<no_cache>, C<bare>) are never tagged.
77 Outside a transaction they are canonical, unlike with the default lookup
78 table. Inside one they count as read before it began, even when read in it.
79  
80 =back
81  
82 Use L<DBIx::QuickORM::LookupTable::Identity> (the default) where these
83 matter. C<retain_cache> croaks with this lookup table, and C<cache> options
84 always miss.
85  
86 =head1 SYNOPSIS
87  
88 my $orm = DBIx::QuickORM->new(credentials => \%credentials, lookup => 'Scoped');
89  
90 my $ann = $orm->insert(users => {name => 'Ann'});
91 my $id = $ann->field('id');
92  
93 my $one = $orm->find(users => $id);
94 my $two = $orm->find(users => $id); # new row data: both are canonical
95  
96 =cut
97  
98 7 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
99 7 my $self = shift;
100 7 $self->SUPER::init();
101 7 fieldhash my %tags;
102 7 $self->{+TAGS} = \%tags;
103 7 $self->{+LEVELS} = [];
104 }
105  
106 =head1 PUBLIC METHODS
107  
108 The role's, as described in L<DBIx::QuickORM::Role::LookupTable>. Those that
109 drop rows (C<gone>, C<forget>, C<clear>, C<remove_table>) do nothing, since
110 nothing is held. The examples continue the SYNOPSIS.
111  
112 =cut
113  
114 4 ▶sub caches { 0 }
Subroutine caches: called CC=1 SCAR=0.0
Pod undocumented
115  
116 9 ▶sub lookup { undef }
Subroutine lookup: called CC=1 SCAR=0.0
Pod undocumented
117  
118 2 ▶sub gone { }
Subroutine gone: called CC=1 SCAR=0.0
Pod undocumented
119 5 ▶sub forget { }
Subroutine forget: called CC=1 SCAR=0.0
Pod undocumented
120 3 ▶sub clear { }
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
121 1 ▶sub remove_table { }
Subroutine remove_table: called CC=1 SCAR=0.0
Pod undocumented
122  
123 40 ▶sub is_canon { $_[0]->canon_status($_[1], $_[2]) ? 0 : 1 }
Subroutine is_canon: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->canon_status($_[1], $_[2]) ? : 24 16 40
124  
125 13 ▶sub ingest { $_[0]->_tag($_[2]) }
Subroutine ingest: called CC=1 SCAR=0.0
Pod undocumented
126 11 ▶sub store { $_[0]->_tag($_[2]) }
Subroutine store: called CC=1 SCAR=0.0
Pod undocumented
127 5 ▶sub update_entry { $_[0]->_tag($_[4]) }
Subroutine update_entry: called CC=1 SCAR=0.0
Pod undocumented
128  
129 1 ▶sub ingest_many {
Subroutine ingest_many: called CC=2 SCAR=6.9
Pod undocumented
130 1 my $self = shift;
131 1 my ($table, $rows) = @_;
132 1 $self->_tag($_) for @$rows;
133 1 return $rows;
134 }
135  
136 =over 4
137  
138 =item $status = $lookup->canon_status($table, \%row_data)
139  
140 Undef while canonical, otherwise a hashref with its C<cause>. The ORM's
141 C<canon_status> calls it with a row's row data:
142  
143 my $txn = $orm->txn;
144 my $before = $orm->canon_status(users => $ann); # {cause => 'txn'}
145  
146 my $read = $orm->find(users => $id);
147 my $in = $orm->canon_status(users => $read); # undef
148 $txn->rollback;
149  
150 my $after = $orm->canon_status(users => $ann); # undef: outside again
151 my $dead = $orm->canon_status(users => $read); # {cause => 'rolled_back'}
152  
153 The causes: C<deleted> or C<not_in_db> (from the row's
154 L<DBIx::QuickORM::RowState>), C<txn> for row data from outside the open
155 transaction, or C<rolled_back> for row data from a transaction or savepoint
156 that rolled back, or whose outcome is unknown (C<clear_txn_levels>).
157  
158 =back
159  
160 =cut
161  
162 98 ▶sub canon_status {
Subroutine canon_status: called CC=7 SCAR=19.5
Pod documented
163 98 my $self = shift;
164 98 my ($table, $data) = @_;
165  
166 98 my $stored = $self->_stored_status($data);
167 98 ▶ return $stored if $stored;
Branch: 1 branch
BranchTrueFalseTotal
if $stored 26 72 98
168  
169 72 my $token = $self->_resolve($self->{+TAGS}->{$data});
170 72 ▶ return {cause => 'rolled_back'} if $token && $token->{dead};
Branch: 1 branch
BranchTrueFalseTotal
if $token and $token->{"dead"} 19 53 72
Condition100%3/3
$token and $token->{"dead"}
!ll&&!rl&&r
341919
MC/DC100%2/2
$token and $token->{"dead"}
$token $token->{"dead"}
Truth table100%3/3
A and B
A= $tokenB= $token->{"dead"}
ABresult
0X0
100
111
171  
172 # Outside, and data from before the open transaction, are both "outside".
173 53 ▶ my $outside = !$token || $token->{outside};
Condition100%3/3
!$token || $token->{'outside'}
l!l&&r!l&&!r
34514
MC/DC100%2/2
!$token || $token->{'outside'}
!$token $token->{'outside'}
Truth table100%3/3
not(A) || B
A= !$tokenB= $token->{'outside'}
ABresult
1X1
011
000
174 53 ▶ return undef if $outside xor @{$self->{+LEVELS}};
Branch: 1 branch
BranchTrueFalseTotal
if $outside xor @{$self->{"levels"};} 37 16 53
Condition75%3/4
$outside xor @{$self->{"levels"};}
l&&rl&&!r!l&&r!l&&!r
1623140
MC/DC100%2/2
$outside xor @{$self->{"levels"};}
$outside @{$self->{"levels"};}
Truth table75%3/4
A xor B
A= $outsideB= @{$self->{"levels"};}
ABresult
110
101
011
000
175 16 return {cause => 'txn'};
176 }
177  
178 =over 4
179  
180 =item $text = $lookup->canon_reason(\%status)
181  
182 Adds C<rolled_back> to the role's.
183  
184 my $text = $orm->connection->lookup->canon_reason({cause => 'rolled_back'});
185 # 'it was read or written in a transaction that rolled back.'
186  
187 =back
188  
189 =cut
190  
191 28 ▶sub canon_reason {
Subroutine canon_reason: called CC=2 SCAR=6.9
Pod documented
192 28 my $self = shift;
193 28 my ($status) = @_;
194 28 ▶ return 'it was read or written in a transaction that rolled back.' if $status->{cause} eq 'rolled_back';
Branch: 1 branch
BranchTrueFalseTotal
if $status->{"cause"} eq "rolled_back" 8 20 28
195 20 return $self->DBIx::QuickORM::Role::LookupTable::canon_reason($status);
196 }
197  
198 # {{{ Transaction events
199  
200 8 ▶sub begin_root { push @{$_[0]->{+LEVELS}} => {}; return }
Subroutine begin_root: called CC=1 SCAR=0.0
Pod undocumented
201 7 ▶sub push_level { push @{$_[0]->{+LEVELS}} => {}; return }
Subroutine push_level: called CC=1 SCAR=0.0
Pod undocumented
202 9 ▶sub rollback_level { (pop @{$_[0]->{+LEVELS}})->{dead} = 1; return }
Subroutine rollback_level: called CC=1 SCAR=0.0
Pod undocumented
203  
204 6 ▶sub commit_level {
Subroutine commit_level: called CC=2 SCAR=6.9
Pod undocumented
205 6 my $self = shift;
206 6 my $levels = $self->{+LEVELS};
207 6 my $token = pop @$levels;
208 6 ▶ if (@$levels) { $token->{into} = $levels->[-1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@$levels) { } 3 3 6
209 3 else { $token->{outside} = 1 }
210 6 return;
211 }
212  
213 3 ▶sub clear_txn_levels {
Subroutine clear_txn_levels: called CC=2 SCAR=6.9
Pod undocumented
214 3 my $self = shift;
215 3 for my $level (@{$self->{+LEVELS}}) {
216 6 $level->{dead} = 1;
217 6 $level = {};
218 }
219 3 return;
220 }
221  
222 # }}} Transaction events
223  
224 =head1 PRIVATE METHODS
225  
226 =over 4
227  
228 =item $row_data = $lookup->_tag(\%row_data)
229  
230 Tags the row data with the open level, if any, and returns it.
231  
232 =item $token = $lookup->_resolve($token)
233  
234 Follows a token through the savepoint commits that handed it on, to the level
235 token that decides: open, dead (rolled back), or outside (root committed).
236  
237 =back
238  
239 =cut
240  
241 31 ▶sub _tag {
Subroutine _tag: called CC=2 SCAR=6.9
242 31 my $self = shift;
243 31 my ($data) = @_;
244 31 my $levels = $self->{+LEVELS};
245 31 ▶ $self->{+TAGS}->{$data} = $levels->[-1] if @$levels;
Branch: 1 branch
BranchTrueFalseTotal
if @$levels 14 17 31
246 31 return $data;
247 }
248  
249 72 ▶sub _resolve {
Subroutine _resolve: called CC=3 SCAR=11.0
250 72 my $self = shift;
251 72 my ($token) = @_;
252 72 ▶ $token = $token->{into} while $token && $token->{into};
Branch: 1 branch
BranchTrueFalseTotal
$token and $token->{"into"} and do { $token = $token->{"into"} } 4 72 76
Condition100%3/3
$token and $token->{"into"}
!ll&&!rl&&r
34384
MC/DC100%2/2
$token and $token->{"into"}
$token $token->{"into"}
Truth table100%3/3
A and B
A= $tokenB= $token->{"into"}
ABresult
0X0
100
111
253 72 return $token;
254 }
255  
256 2 with 'DBIx::QuickORM::Role::LookupTable';
257  
258 2 1;
259  
260 __END__
« blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm ↑ Summary blib/lib/DBIx/QuickORM/Manual.pm »