line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::RecordStore::Transaction; |
2
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
55
|
use strict; |
|
9
|
|
|
|
|
26
|
|
|
9
|
|
|
|
|
253
|
|
4
|
9
|
|
|
9
|
|
53
|
use warnings; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
234
|
|
5
|
9
|
|
|
9
|
|
45
|
no warnings 'numeric'; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
264
|
|
6
|
9
|
|
|
9
|
|
53
|
no warnings 'uninitialized'; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
409
|
|
7
|
|
|
|
|
|
|
|
8
|
9
|
|
|
9
|
|
118
|
use File::Path qw(make_path remove_tree); |
|
9
|
|
|
|
|
26
|
|
|
9
|
|
|
|
|
582
|
|
9
|
|
|
|
|
|
|
|
10
|
9
|
|
|
9
|
|
55
|
use vars qw($VERSION); |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
760
|
|
11
|
|
|
|
|
|
|
$VERSION = '0.01'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use constant { |
14
|
9
|
|
|
|
|
8800
|
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
|
|
|
9
|
|
71
|
}; |
|
9
|
|
|
|
|
17
|
|
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
|
|
|
|
|
|
|
sub create { |
50
|
86
|
|
|
86
|
1
|
484
|
my( $cls, $store, $dir, $id ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# stack of id used |
53
|
86
|
|
|
|
|
331
|
my $stack_silo_dir = "$dir/stack_silo"; |
54
|
86
|
|
|
|
|
8184
|
make_path( $stack_silo_dir, { error => \my $err } ); |
55
|
86
|
50
|
|
|
|
1076
|
if( @$err ) { die join( ", ", map { values %$_ } @$err ) } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
56
|
|
|
|
|
|
|
|
57
|
86
|
|
|
|
|
522
|
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
|
|
|
|
|
1261
|
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
|
|
|
|
|
|
|
sub commit { |
75
|
46
|
|
|
46
|
1
|
140
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
46
|
|
|
|
|
113
|
my $store = $self->{store}; |
78
|
|
|
|
|
|
|
|
79
|
46
|
|
|
|
|
163
|
$store->transaction_silo->put_record( $self->{id}, [TR_IN_COMMIT], 'I' ); |
80
|
46
|
|
|
|
|
458
|
$self->{state} = TR_IN_COMMIT; |
81
|
|
|
|
|
|
|
|
82
|
46
|
|
|
|
|
208
|
my $store_index = $store->index_silo; |
83
|
46
|
|
|
|
|
141
|
my $store_silos = $store->silos; |
84
|
|
|
|
|
|
|
|
85
|
46
|
|
|
|
|
92
|
my $stack_silo = $self->{stack_silo}; |
86
|
46
|
|
|
|
|
98
|
my $changes = $self->{changes}; |
87
|
46
|
|
|
|
|
474
|
for my $id (sort { $a <=> $b } keys %$changes) { |
|
567
|
|
|
|
|
986
|
|
88
|
238
|
|
|
|
|
1454
|
my( $action, $rec_id, $orig_silo_id, $orig_idx_in_silo, $trans_silo_id, $trans_id_in_silo ) = @{$changes->{$id}}; |
|
238
|
|
|
|
|
950
|
|
89
|
238
|
100
|
|
|
|
696
|
if( $action == RS_ACTIVE ) { |
90
|
198
|
|
|
|
|
985
|
$store_silos->[$trans_silo_id]->put_record( $trans_id_in_silo, [ RS_ACTIVE ], 'I' ); |
91
|
198
|
|
|
|
|
1969
|
$store_index->put_record( $rec_id, [$trans_silo_id,$trans_id_in_silo,time] ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
40
|
|
|
|
|
80
|
my( $s_id, $id_in_s ) = @{$store_index->get_record( $rec_id )}; |
|
40
|
|
|
|
|
152
|
|
95
|
40
|
100
|
|
|
|
200
|
if( $s_id ) { |
96
|
32
|
|
|
|
|
136
|
$store->silos->[$s_id]->put_record( $id_in_s, [RS_DEAD], 'I' ); |
97
|
|
|
|
|
|
|
} |
98
|
40
|
|
|
|
|
496
|
$store_index->put_record( $rec_id, [0,0,time] ); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
38
|
|
|
|
|
217
|
$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
|
|
|
|
|
288
|
for my $id (sort { $a <=> $b } keys %$changes) { |
|
519
|
|
|
|
|
782
|
|
105
|
198
|
|
|
|
|
323
|
my( $action, $rec_id, $orig_silo_id, $orig_idx_in_silo ) = @{$changes->{$id}}; |
|
198
|
|
|
|
|
457
|
|
106
|
198
|
100
|
100
|
|
|
698
|
if( $action == RS_DEAD && $orig_silo_id ) { |
107
|
24
|
|
|
|
|
128
|
$store->_vacate( $orig_silo_id, $orig_idx_in_silo ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} #commit |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub rollback { |
113
|
40
|
|
|
40
|
1
|
112
|
my $self = shift; |
114
|
|
|
|
|
|
|
|
115
|
40
|
|
|
|
|
120
|
my $store = $self->{store}; |
116
|
40
|
|
|
|
|
176
|
my $index = $store->index_silo; |
117
|
|
|
|
|
|
|
|
118
|
40
|
|
|
|
|
152
|
$store->transaction_silo->put_record( $self->{id}, [TR_IN_ROLLBACK], 'I' ); |
119
|
40
|
|
|
|
|
528
|
$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
|
|
|
|
|
120
|
my $stack_silo = $self->{stack_silo}; |
126
|
40
|
|
|
|
|
168
|
my $count = $stack_silo->entry_count; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# go backwards to remove items that may have been partially created. |
129
|
40
|
|
|
|
|
240
|
for my $stack_id (reverse(1..$count)) { |
130
|
168
|
|
|
|
|
312
|
my( $action, $rec_id, $orig_silo_id, $id_in_orig_silo, $trans_silo_id, $trans_id_in_silo ) = @{$stack_silo->get_record( $stack_id )}; |
|
168
|
|
|
|
|
560
|
|
131
|
168
|
|
|
|
|
3280
|
my( $reported_silo_id, $id_reported_silo ) = @{$index->get_record( $rec_id )}; |
|
168
|
|
|
|
|
568
|
|
132
|
160
|
100
|
100
|
|
|
2928
|
if( $reported_silo_id != $orig_silo_id || $id_reported_silo != $id_in_orig_silo ) { |
133
|
32
|
|
|
|
|
152
|
$index->put_record( $rec_id, [$orig_silo_id,$id_in_orig_silo,time] ); |
134
|
|
|
|
|
|
|
} |
135
|
160
|
100
|
|
|
|
1344
|
if( $trans_silo_id ) { |
136
|
112
|
|
|
|
|
464
|
$store->_vacate( $trans_silo_id, $trans_id_in_silo ); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
32
|
|
|
|
|
208
|
$store->transaction_silo->put_record( $self->{id}, [TR_COMPLETE], 'I' ); |
141
|
|
|
|
|
|
|
} #rollback |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub fetch { |
144
|
153
|
|
|
153
|
1
|
340
|
my( $self, $id ) = @_; |
145
|
|
|
|
|
|
|
|
146
|
153
|
|
|
|
|
306
|
my $store = $self->{store}; |
147
|
|
|
|
|
|
|
|
148
|
153
|
|
|
|
|
244
|
my $changes = $self->{changes}; |
149
|
153
|
100
|
|
|
|
444
|
if( my $rec = $changes->{$id} ) { |
150
|
137
|
|
|
|
|
380
|
my( $action, $rec_id, $a, $b, $trans_silo_id, $trans_id_in_silo ) = @$rec; |
151
|
137
|
100
|
|
|
|
387
|
if( $action == RS_ACTIVE ) { |
152
|
89
|
|
|
|
|
251
|
my $ret = $store->silos->[$trans_silo_id]->get_record( $trans_id_in_silo ); |
153
|
89
|
|
|
|
|
79290
|
return substr( $ret->[3], 0, $ret->[2] ); |
154
|
|
|
|
|
|
|
} |
155
|
48
|
|
|
|
|
304
|
return undef; |
156
|
|
|
|
|
|
|
} |
157
|
16
|
|
|
|
|
72
|
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
|
|
|
|
|
|
|
sub stow { |
175
|
263
|
|
|
263
|
1
|
600
|
my $self = $_[0]; |
176
|
263
|
|
|
|
|
456
|
my $id = $_[2]; |
177
|
|
|
|
|
|
|
|
178
|
263
|
|
|
|
|
582
|
my $store = $self->{store}; |
179
|
263
|
100
|
|
|
|
806
|
if( $id == 0 ) { |
180
|
48
|
|
|
|
|
176
|
$id = $store->next_id; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
9
|
|
|
9
|
|
79
|
my $data_write_size = do { use bytes; length( $_[1] ) }; |
|
9
|
|
|
|
|
10
|
|
|
9
|
|
|
|
|
51
|
|
|
263
|
|
|
|
|
399
|
|
|
263
|
|
|
|
|
571
|
|
184
|
263
|
|
|
|
|
840
|
my $trans_silo_id = $store->silo_id_for_size( $data_write_size ); |
185
|
|
|
|
|
|
|
|
186
|
263
|
|
|
|
|
854
|
my $trans_silo = $store->silos->[$trans_silo_id]; |
187
|
263
|
|
|
|
|
702
|
my( $orig_silo_id, $orig_id_in_silo ) = @{$store->index_silo->get_record($id)}; |
|
263
|
|
|
|
|
652
|
|
188
|
263
|
|
|
|
|
2008
|
my $trans_id_in_silo = $trans_silo->push( [RS_IN_TRANSACTION, $id, $data_write_size, $_[1]] ); |
189
|
|
|
|
|
|
|
|
190
|
263
|
|
|
|
|
830
|
my $stack_silo = $self->{stack_silo}; |
191
|
263
|
|
|
|
|
537
|
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
|
|
|
|
|
819
|
my $update = [RS_ACTIVE,$id,$orig_silo_id,$orig_id_in_silo,$trans_silo_id,$trans_id_in_silo]; |
200
|
263
|
|
|
|
|
1092
|
$changes->{$id} = $update; |
201
|
263
|
|
|
|
|
1475
|
$stack_silo->push( $update ); |
202
|
263
|
|
|
|
|
1061
|
return $id; |
203
|
|
|
|
|
|
|
} #stow |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub delete_record { |
206
|
72
|
|
|
72
|
1
|
192
|
my( $self, $id ) = @_; |
207
|
|
|
|
|
|
|
|
208
|
72
|
|
|
|
|
112
|
my( $orig_silo_id, $orig_id_in_silo ) = @{$self->{store}->index_silo->get_record($id)}; |
|
72
|
|
|
|
|
248
|
|
209
|
|
|
|
|
|
|
|
210
|
72
|
|
|
|
|
288
|
my $stack_silo = $self->{stack_silo}; |
211
|
72
|
|
|
|
|
120
|
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
|
|
|
|
|
232
|
my $update = [RS_DEAD,$id,$orig_silo_id,$orig_id_in_silo,0,0]; |
221
|
72
|
|
|
|
|
248
|
$changes->{$id} = $update; |
222
|
72
|
|
|
|
|
312
|
$stack_silo->push( $update ); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
} #delete_record |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
"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__ |