KiokuDB/Backend/Role/TXN/Memory.pm

filter: Statementstmt 94.5% 104 / 110 Branchbran 86.8% 33 / 38 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.7% 15 / 16 Podpod 12.5% 1 / 8 totaltotal 87.0% 154 / 177 CC 25 SCAR 16.6
CC
25
Cov
91%
get
10.0
exists
7.1
exists_in_storage
6.0
CRAP 5.3
« KiokuDB/TypeMap.pm ↑ Summary KiokuDB/LinkChecker.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Backend::Role::TXN::Memory;
4 21 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 ▶use KiokuDB::Util qw(deprecate);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 with qw(KiokuDB::Backend::Role::TXN);
11  
12 21 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 21 requires qw(commit_entries get_from_storage);
15  
16 # extremely slow/shitty fallback method, will be deprecated eventually
17 sub exists_in_storage {
18 0 ▶ my ( $self, @uuids ) = @_;
Subroutine exists_in_storage: not called CC=2 SCAR=17.9
Pod documented
19  
20 0 deprecate('0.37', 'exists_in_storage should be implemented in TXN::Memory using backends');
21  
22 0 ▶ map { $self->get_from_storage($_) ? 1 : '' } @uuids;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_from_storage($_) ? : 0 0 0
23 }
24  
25 has _txn_stack => (
26 isa => "ArrayRef",
27 is => "ro",
28 99 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 21 );
30  
31 sub _new_frame {
32 return {
33 2322 ▶ 'live' => {},
Subroutine _new_frame: called CC=1 SCAR=0.0
34 'log' => [],
35 'cleared' => !1,
36 };
37 }
38  
39 sub txn_begin {
40 2090 ▶ my $self = shift;
Subroutine txn_begin: called CC=1 SCAR=0.0
Pod undocumented
41  
42 2090 push @{ $self->_txn_stack }, $self->_new_frame;
43 }
44  
45 sub txn_rollback {
46 469 ▶ my $self = shift;
Subroutine txn_rollback: called CC=2 SCAR=6.9
Pod undocumented
47  
48 469 ▶ pop @{ $self->_txn_stack } || croak "no open transaction";
Branch: 1 branch
BranchTrueFalseTotal
pop @{$self->_txn_stack;} or croak("no open transaction") 25 444 469
49 }
50  
51 sub txn_commit {
52 1646 ▶ my $self = shift;
Subroutine txn_commit: called CC=4 SCAR=13.9
Pod undocumented
53  
54 1646 my $stack = $self->_txn_stack;
55  
56 1646 ▶ my $txn = pop @$stack || croak "no open transaction";
Condition33%1/3
pop @$stack || croak('no open transaction')
l!l&&r!l&&!r
164600
MC/DC0%0/2
pop @$stack || croak('no open transaction')
pop @$stack croak('no open transaction')
Truth table33%1/3
A || B
A= pop @$stackB= croak('no open transaction')
ABresult
1X1
011
000
57  
58 1646 ▶ if ( @{ $self->_txn_stack } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_txn_stack;}) { } 99 1547 1646
59 99 $stack->[-1] = $self->_collapse_txn_frames($txn, $stack->[-1]);
60 } else {
61 1547 ▶ $self->clear_storage if $txn->{cleared};
Branch: 1 branch
BranchTrueFalseTotal
if $txn->{"cleared"} 166 1381 1547
62 1547 $self->commit_entries(@{ $txn->{log} });
63 }
64 }
65  
66 sub _collapsed_txn_stack {
67 829 ▶ my $self = shift;
Subroutine _collapsed_txn_stack: called CC=1 SCAR=0.0
68  
69 829 $self->_collapse_txn_frames(reverse @{ $self->_txn_stack });
70 }
71  
72 sub _collapse_txn_frames {
73 961 ▶ my ( $self, $head, @tail ) = @_;
Subroutine _collapse_txn_frames: called CC=4 SCAR=13.9
74  
75 961 ▶ return $self->_new_frame unless $head;
Branch: 1 branch
BranchTrueFalseTotal
unless $head 0 961 961
76  
77 961 ▶ return $head unless @tail;
Branch: 1 branch
BranchTrueFalseTotal
unless @tail 829 132 961
78  
79 132 my $next = shift @tail;
80  
81 132 ▶ if ( $head->{cleared} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head->{'cleared'}) { } 99 33 132
82 99 return $head;
83 } else {
84 my $merged = {
85 cleared => $next->{cleared},
86 log => [
87 33 @{ $next->{log} },
88 33 @{ $head->{log} },
89 ],
90 live => {
91 33 %{ $next->{live} },
92 33 %{ $head->{live} },
93 },
94 };
95  
96 33 return $self->_collapse_txn_frames( $merged, @tail );
97 }
98 }
99  
100 # FIXME remove duplication between get/exists
101 sub get {
102 3039 ▶ my ( $self, @uuids ) = @_;
Subroutine get: called CC=10 SCAR=23.0
Pod undocumented
103  
104 3039 my %entries;
105 3039 my %remaining = map { $_ => undef } @uuids;
106  
107 3039 my $stack = $self->_txn_stack;
108  
109 3039 foreach my $frame ( @$stack ) {
110 # try to find a modified entry for every remaining key
111 1916 foreach my $id ( keys %remaining ) {
112 2409 ▶ if ( my $entry = $frame->{live}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $frame->{"live"}{$id}) 244 2165 2409
113 244 ▶ if ( $entry->deleted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->deleted) 26 218 244
114 26 return ();
115 }
116 218 $entries{$id} = $entry;
117 218 delete $remaining{$id};
118 }
119 }
120  
121 # if there are no more remaining keys, we can stop examining the
122 # transaction frames
123 1890 ▶ last unless keys %remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %remaining 242 1648 1890
124  
125 # if the current frame has cleared the DB and there are still remaining
126 # keys, they are supposed to fail the lookup
127 1648 ▶ return () if $frame->{cleared};
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"cleared"} 0 1648 1648
128 }
129  
130 3013 ▶ if ( keys %remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %remaining) 2769 244 3013
131 2769 my @remaining = $self->get_from_storage(keys %remaining);
132  
133 2769 ▶ if ( @remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remaining) { } 2734 35 2769
134 2734 @entries{keys %remaining} = @remaining;
135 2734 ▶ @{ $stack->[-1]{live} }{keys %remaining} = @remaining if @$stack;
Branch: 1 branch
BranchTrueFalseTotal
if @$stack 1615 1119 2734
136 } else {
137 35 return ();
138 }
139 }
140  
141 2978 return @entries{@uuids};
142 }
143  
144 # FIXME remove duplication between get/exists
145 sub exists {
146 532 ▶ my ( $self, @uuids ) = @_;
Subroutine exists: called CC=7 SCAR=19.6
Pod undocumented
147  
148 532 my %exists;
149 532 my %remaining = map { $_ => undef } @uuids;
150  
151 532 foreach my $frame ( @{ $self->_txn_stack } ) {
152 283 foreach my $id ( keys %remaining ) {
153 316 ▶ if ( my $entry = $frame->{live}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $frame->{"live"}{$id}) 101 215 316
154 101 $exists{$id} = not $entry->deleted;
155 101 delete $remaining{$id};
156 }
157 }
158  
159 283 ▶ last unless keys %remaining;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %remaining 101 182 283
160  
161 182 ▶ if ( $frame->{cleared} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{"cleared"}) 0 182 182
162 0 @exists{keys %remaining} = ('') x keys %remaining;
163 0 return @exists{@uuids};
164 }
165 }
166  
167 532 ▶ if ( keys %remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %remaining) 431 101 532
168 431 @exists{keys %remaining} = $self->exists_in_storage(keys %remaining);
169 }
170  
171 532 return @exists{@uuids};
172 }
173  
174 sub delete {
175 138 ▶ my ( $self, @ids_or_entries ) = @_;
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
176  
177 138 my @entries = grep { ref } @ids_or_entries;
178  
179 138 my @ids = grep { not ref } @ids_or_entries;
180  
181 138 my @new_entries = map { $_->deletion_entry } $self->get(@ids);
182  
183 138 $self->insert(@entries, @new_entries);
184  
185 138 return @new_entries;
186 }
187  
188 sub insert {
189 1484 ▶ my ( $self, @entries ) = @_;
Subroutine insert: called CC=2 SCAR=6.9
Pod undocumented
190  
191 1484 ▶ if ( @{ $self->_txn_stack } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_txn_stack;}) { } 1286 198 1484
192 1286 my $head = $self->_txn_stack->[-1];
193 1286 push @{ $head->{log} }, @entries;
194 1286 @{$head->{live}}{map { $_->id } @entries} = @entries;
195 } else {
196 198 $self->commit_entries(@entries);
197 }
198 }
199  
200 21 __PACKAGE__
201  
202 __END__
203  
204 =pod
205  
206 =head1 NAME
207  
208 KiokuDB::Backend::Role::TXN::Memory - In memory transactions.
209  
210 =head1 SYNOPSIS
211  
212 with qw(KiokuDB::Backend::Role::TXN::Memory);
213  
214 sub commit_entries {
215 my ( $self, @entries ) = @_;
216  
217 # atomically apply @entries
218  
219 # deleted entries have the deleted flag set
220 # if an entry has no 'prev' entry it's an insert
221 # otherwise it's an update
222 }
223  
224 =head1 DESCRIPTION
225  
226 This backend provides in memory transactions for backends which support atomic
227 modification of data, but not full commit/rollback support.
228  
229 This backend works by buffering all operations in memory. Entries are kept
230 alive allowing read operations go to the live entry even for objects that are
231 out of scope.
232  
233 This implementation provides repeatable read level isolation. Durability,
234 concurrency and atomicity are still the responsibility of the backend.
235  
236 =head1 REQUIRED METHODS
237  
238 =over 4
239  
240 =item commit_entries
241  
242 Insert, update or delete entries as specified.
243  
244 This operation should either fail or succeed atomically.
245  
246 Entries with C<deleted> should be removed from the database, entries with a
247 C<prev> entry should be inserted, and all other entries should be updated.
248  
249 Multiple entries may be given for a single object, for instance an object that
250 was first inserted and then modified will have an insert entry and an update
251 entry.
252  
253 =item get_from_storage
254  
255 Should be the same as L<KiokuDB::Backend/get>.
256  
257 When no memory buffered entries are available for the object one is fetched
258 from the backend.
259  
260 =item exists_in_storage
261  
262 Required as of L<KiokuDB> version 0.37.
263  
264 A fallback implementation is provided, but should not be used and will issue a
265 deprecation warning.
266  
267 =back
268  
269 =cut
270  
« KiokuDB/TypeMap.pm ↑ Summary KiokuDB/LinkChecker.pm »