RecordStore/Transaction.pm

filter: Statementstmt 100% 114 / 114 Branchbran 100% 18 / 18 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 100% 162 / 162 CC 15 SCAR 12.7
CC
15
Cov
100%
commit
7.0
rollback
5.0
fetch
3.0
CRAP 3.6
« RecordStore/Converter.pm ↑ Summary RecordStore/PopSilo.pm »
1 package Data::RecordStore::Transaction;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 $VERSION = '0.01';
12  
13 9 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 RS_ACTIVE => 1,
15 RS_DEAD => 2,
16 RS_IN_TRANSACTION => 3,
17  
18 TR_ACTIVE => 1,
19 TR_IN_COMMIT => 2,
20 TR_IN_ROLLBACK => 3,
21 TR_COMPLETE => 4,
22 9 };
23  
24 #######################################################################
25 # Transactions use a stack silo to record what happens #
26 # to entries affected by the transaction. #
27 # #
28 # When an entry is deleted in a transaction, the stack silo #
29 # marks that entry for deletion. #
30 # #
31 # When entry data is stowed in a transaction, the transaction #
32 # stores the data in the store. It marks this location as well #
33 # as the original location of the entry data (if any). #
34 # #
35 # The stack silo keeps only the last record for an entry, so #
36 # if an entry is stowed, then deleted, then stowed again, only #
37 # the most recent action is recorded. The stack silo contains the #
38 # id, the state, the original silo id (if there is one ), #
39 # the original idx in the silo (if there is one ), the silo id that #
40 # the transaction stored data in, the idx in the silo that the #
41 # the transaction stored data in #
42 # #
43 # When fetch is used from a transaction, the stack silo is checked #
44 # to see if there was any action on the fetched id. If so, it returns #
45 # the transactional value. #
46 #######################################################################
47  
48  
49 94 ▶sub create {
Subroutine create: called CC=2 SCAR=6.9
Pod documented
50 94 my( $cls, $store, $dir, $id ) = @_;
51  
52 # stack of id used
53 94 my $stack_silo_dir = "$dir/stack_silo";
54 94 make_path( $stack_silo_dir, { error => \my $err } );
55 94 ▶ if( @$err ) { die join( ", ", map { values %$_ } @$err ) }
Branch: 1 branch
BranchTrueFalseTotal
if (@$err) 8 86 94
56  
57 86 my $stack_silo = Data::RecordStore::Silo->open_silo(
58 $stack_silo_dir,
59 'ILILIL', # action, id, trans silo id, trans id in silo, orig silo id, orig id in silo
60 0,
61 $store->max_file_size );
62  
63 86 return bless {
64 directory => $dir,
65 id => $id,
66 stack_silo => $stack_silo,
67 changes => {},
68 store => $store,
69 state => TR_ACTIVE,
70 }, $cls;
71  
72 } #create
73  
74 46 ▶sub commit {
Subroutine commit: called CC=7 SCAR=19.5
Pod documented
75 46 my $self = shift;
76  
77 46 my $store = $self->{store};
78  
79 46 $store->transaction_silo->put_record( $self->{id}, [TR_IN_COMMIT], 'I' );
80 46 $self->{state} = TR_IN_COMMIT;
81  
82 46 my $store_index = $store->index_silo;
83 46 my $store_silos = $store->silos;
84  
85 46 my $stack_silo = $self->{stack_silo};
86 46 my $changes = $self->{changes};
87 46 for my $id (sort { $a <=> $b } keys %$changes) {
88 238 my( $action, $rec_id, $orig_silo_id, $orig_idx_in_silo, $trans_silo_id, $trans_id_in_silo ) = @{$changes->{$id}};
89 238 ▶ if( $action == RS_ACTIVE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action == 1) { } 198 40 238
90 198 $store_silos->[$trans_silo_id]->put_record( $trans_id_in_silo, [ RS_ACTIVE ], 'I' );
91 198 $store_index->put_record( $rec_id, [$trans_silo_id,$trans_id_in_silo,time] );
92 }
93 else {
94 40 my( $s_id, $id_in_s ) = @{$store_index->get_record( $rec_id )};
95 40 ▶ if( $s_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_id) 32 8 40
96 32 $store->silos->[$s_id]->put_record( $id_in_s, [RS_DEAD], 'I' );
97 }
98 40 $store_index->put_record( $rec_id, [0,0,time] );
99 }
100 }
101 38 $store->transaction_silo->put_record( $self->{id}, [TR_COMPLETE], 'I' );
102  
103 # this is sort of linting. The transaction is complete, but this cleans up any records marked deleted.
104 38 for my $id (sort { $a <=> $b } keys %$changes) {
105 198 my( $action, $rec_id, $orig_silo_id, $orig_idx_in_silo ) = @{$changes->{$id}};
106 198 ▶ if( $action == RS_DEAD && $orig_silo_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action == 2 and $orig_silo_id) 24 174 198
Condition100%3/3
$action == 2 and $orig_silo_id
!ll&&!rl&&r
166824
MC/DC100%2/2
$action == 2 and $orig_silo_id
$action == 2 $orig_silo_id
Truth table100%3/3
A and B
A= $action == 2B= $orig_silo_id
ABresult
0X0
100
111
107 24 $store->_vacate( $orig_silo_id, $orig_idx_in_silo );
108 }
109 }
110 } #commit
111  
112 40 ▶sub rollback {
Subroutine rollback: called CC=5 SCAR=16.1
Pod documented
113 40 my $self = shift;
114  
115 40 my $store = $self->{store};
116 40 my $index = $store->index_silo;
117  
118 40 $store->transaction_silo->put_record( $self->{id}, [TR_IN_ROLLBACK], 'I' );
119 40 $self->{state} = TR_IN_ROLLBACK;
120  
121 # [RS_ACTIVE, $id, $orig_silo_id, $orig_id_in_silo, $trans_silo_id, $trans_id_in_silo];
122 # [RS_DEAD , $id, $orig_silo_id, $orig_id_in_silo, 0, 0];
123
124 # go and mark dead any temporary stows
125 40 my $stack_silo = $self->{stack_silo};
126 40 my $count = $stack_silo->entry_count;
127  
128 # go backwards to remove items that may have been partially created.
129 40 for my $stack_id (reverse(1..$count)) {
130 168 my( $action, $rec_id, $orig_silo_id, $id_in_orig_silo, $trans_silo_id, $trans_id_in_silo ) = @{$stack_silo->get_record( $stack_id )};
131 168 my( $reported_silo_id, $id_reported_silo ) = @{$index->get_record( $rec_id )};
132 160 ▶ if( $reported_silo_id != $orig_silo_id || $id_reported_silo != $id_in_orig_silo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reported_silo_id != $orig_silo_id or $id_reported_silo != $id_in_orig_silo) 32 128 160
Condition100%3/3
$reported_silo_id != $orig_silo_id or $id_reported_silo != $id_in_orig_silo
l!l&&r!l&&!r
248128
MC/DC100%2/2
$reported_silo_id != $orig_silo_id or $id_reported_silo != $id_in_orig_silo
$reported_silo_id != $orig_silo_id $id_reported_silo != $id_in_orig_silo
Truth table100%3/3
A or B
A= $reported_silo_id != $orig_silo_idB= $id_reported_silo != $id_in_orig_silo
ABresult
1X1
011
000
133 32 $index->put_record( $rec_id, [$orig_silo_id,$id_in_orig_silo,time] );
134 }
135 160 ▶ if( $trans_silo_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trans_silo_id) 112 48 160
136 112 $store->_vacate( $trans_silo_id, $trans_id_in_silo );
137 }
138 }
139  
140 32 $store->transaction_silo->put_record( $self->{id}, [TR_COMPLETE], 'I' );
141 } #rollback
142  
143 153 ▶sub fetch {
Subroutine fetch: called CC=3 SCAR=11.0
Pod documented
144 153 my( $self, $id ) = @_;
145  
146 153 my $store = $self->{store};
147  
148 153 my $changes = $self->{changes};
149 153 ▶ if( my $rec = $changes->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = $changes->{$id}) 137 16 153
150 137 my( $action, $rec_id, $a, $b, $trans_silo_id, $trans_id_in_silo ) = @$rec;
151 137 ▶ if( $action == RS_ACTIVE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action == 1) 89 48 137
152 89 my $ret = $store->silos->[$trans_silo_id]->get_record( $trans_id_in_silo );
153 89 return substr( $ret->[3], 0, $ret->[2] );
154 }
155 48 return undef;
156 }
157 16 return $store->fetch( $id, 'no-trans' );
158 } #fetch
159  
160 #####################################################################################################################
161 # given data and id, #
162 # #
163 # uses the data size to find the appropriate silo id (new-silo-id) to store the data. #
164 # #
165 # looks up the original silo-id, index-in-silo from the stores index, #
166 # which may or not exist #
167 # #
168 # push the new data value into the store silo with the new-silo-id from above #
169 # #
170 # sees if the id already has an entry in the stack silo #
171 # - if yes, it updates it to include STOW,id,new_silo_id,new_idx_in_silo,$original-silo-id,original-idx-in-silo #
172 # - if no, pushes on to it to STOW,id,new_silo_id,new_idx_in_silo,$original-silo-id,original-idx-in-silo #
173 #####################################################################################################################
174 263 ▶sub stow {
Subroutine stow: called CC=2 SCAR=6.9
Pod documented
175 263 my $self = $_[0];
176 263 my $id = $_[2];
177  
178 263 my $store = $self->{store};
179 263 ▶ if( $id == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id == 0) 48 215 263
180 48 $id = $store->next_id;
181 }
182  
183 9 ▶ my $data_write_size = do { use bytes; length( $_[1] ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
184 263 my $trans_silo_id = $store->silo_id_for_size( $data_write_size );
185  
186 263 my $trans_silo = $store->silos->[$trans_silo_id];
187 263 my( $orig_silo_id, $orig_id_in_silo ) = @{$store->index_silo->get_record($id)};
188 263 my $trans_id_in_silo = $trans_silo->push( [RS_IN_TRANSACTION, $id, $data_write_size, $_[1]] );
189  
190 263 my $stack_silo = $self->{stack_silo};
191 263 my $changes = $self->{changes};
192 # if( my $rec = $changes->{$id} ) {
193 # my( $action, $rec_id, $a, $b, $old_trans_silo_id, $old_idx_in_trans_silo ) = @$rec;
194 # if( $old_trans_silo_id ) {
195 # my $old_trans_silo = $store->silos->[$old_trans_silo_id];
196 # $old_trans_silo->put_record( $old_idx_in_trans_silo, [RS_DEAD], 'I' );
197 # }
198 # }
199 263 my $update = [RS_ACTIVE,$id,$orig_silo_id,$orig_id_in_silo,$trans_silo_id,$trans_id_in_silo];
200 263 $changes->{$id} = $update;
201 263 $stack_silo->push( $update );
202 263 return $id;
203 } #stow
204  
205 72 ▶sub delete_record {
Subroutine delete_record: called CC=1 SCAR=0.0
Pod documented
206 72 my( $self, $id ) = @_;
207
208 72 my( $orig_silo_id, $orig_id_in_silo ) = @{$self->{store}->index_silo->get_record($id)};
209
210 72 my $stack_silo = $self->{stack_silo};
211 72 my $changes = $self->{changes};
212  
213 # if( my $rec = $changes->{$id} ) {
214 # my( $action, $rec_id, $a, $b, $old_trans_silo_id, $old_idx_in_trans_silo ) = @$rec;
215 # if( $old_trans_silo_id ) {
216 # my $old_trans_silo = $self->{store}->silos->[$old_trans_silo_id];
217 # $old_trans_silo->put_record( $old_idx_in_trans_silo, [RS_DEAD], 'I' );
218 # }
219 # }
220 72 my $update = [RS_DEAD,$id,$orig_silo_id,$orig_id_in_silo,0,0];
221 72 $changes->{$id} = $update;
222 72 $stack_silo->push( $update );
223  
224 } #delete_record
225  
226 9 "I think there comes a time when you start dropping expectations. Because the world doesn't owe you anything, and you don't owe the world anything in return. Things, feelings, are a very simple transaction. If you get it, be grateful. If you don't, be alright with it. - Fawad Khan";
227  
228 __END__
« RecordStore/Converter.pm ↑ Summary RecordStore/PopSilo.pm »