RecordStore/Converter.pm

filter: Statementstmt 99.5% 233 / 234 Branchbran 100% 36 / 36 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 46 / 46 Podpod 100% 1 / 1 totaltotal 99.6% 321 / 322 CC 22 SCAR 12.9
CC
22
Cov
100%
convert
13.0
fetch
3.0
fetch
2.0
CRAP 3.6
« RecordStore/Silo.pm ↑ Summary RecordStore/Transaction.pm »
1 package Data::RecordStore::Converter;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Data::RecordStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use File::Copy::Recursive qw( dircopy dirmove );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 =head2 Data::RecordStore::Converter->convert( $source_dir, $dest_dir )
13  
14 Analyzes the source directory to find version
15 then creates a new version of the store in the
16 destination directory.
17  
18 =cut
19 12 ▶sub convert {
Subroutine convert: called CC=13 SCAR=25.6
Pod documented
20 12 my( $cls, $source_dir, $dest_dir, %args ) = @_;
21  
22 12 ▶ die "Data::RecordStore::Converter->convert must be given a destination directory" unless $dest_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dest_dir 1 11 12
23  
24 11 ▶ die "Data::RecordStore::Converter->convert must be given a source directory" unless $source_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_dir 1 10 11
25  
26 10 ▶ die "Data::RecordStore::Converter->convert : cannot find source directory '$source_dir'" unless -d $source_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $source_dir 1 9 10
27
28 9 ▶ if( -d $dest_dir && -e "$dest_dir/VERSION" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dest_dir and -e "$dest_dir/VERSION") 1 8 9
Condition100%3/3
-d $dest_dir and -e "$dest_dir/VERSION"
!ll&&!rl&&r
711
MC/DC100%2/2
-d $dest_dir and -e "$dest_dir/VERSION"
-d $dest_dir -e "$dest_dir/VERSION"
Truth table100%3/3
A and B
A= -d $dest_dirB= -e "$dest_dir/VERSION"
ABresult
0X0
100
111
29 1 die "Data::RecordStore::Converter->convert : Destination directory '$dest_dir' already exists";
30 }
31  
32 8 my $source_version = Data::RecordStore->detect_version( $source_dir );
33  
34 8 my $rs_pkg;
35 8 ▶ if( ! defined $source_version ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $source_version) 1 7 8
36 1 die "No store found in $source_dir";
37 }
38 7 ▶ if ( $source_version < 2 ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($source_version < 2) { } 1 6 7
elsif ($source_version < 3) { } 1 5 6
elsif ($source_version < 3.1) { } 1 4 5
elsif ($source_version < 4) { } 1 3 4
elsif ($source_version < 5) { } 1 2 3
39 1 $rs_pkg = 'RS_1';
40 }
41 elsif ( $source_version < 3 ) {
42 1 $rs_pkg = 'RS_2';
43 }
44 elsif ( $source_version < 3.1 ) {
45 1 $rs_pkg = 'RS_3';
46 }
47 elsif( $source_version < 4 ) {
48 1 $rs_pkg = 'RS_3_1';
49 }
50 elsif( $source_version < 5 ) {
51 1 $rs_pkg = 'RS_4';
52 }
53 else { #elsif( $source_version < 6 ) {
54 2 $rs_pkg = 'RS_5';
55 }
56  
57 7 my $old_rs = $rs_pkg->open( $source_dir );
58  
59 7 $args{BASE_PATH} = $dest_dir;
60  
61 7 my $new_rs = Data::RecordStore->open_store( %args );
62  
63 7 my $entries = $old_rs->entry_count;
64 7 for my $id (1..$entries) {
65 314 my $val = $old_rs->fetch( $id );
66 314 $new_rs->stow( $val );
67 }
68
69 } #convert
70  
71 package RS_1;
72  
73 1 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
74 1 my( $cls, $dir ) = @_;
75 1 my $store_idx = SiloPre3->open( "$dir/STORE_INDEX", "I" );
76 1 my $count = $store_idx->entry_count;
77 return bless {
78 count => $count,
79 index_silo => SiloPre3->open( "$dir/OBJ_INDEX", "IL" ),
80 1 entry_silos => [ map { SiloPre3->open( "$dir/${_}_OBJSTORE", "A*", $store_idx->get_record($_)->[0] ) } (1..$count) ]
81 }, $cls;
82 }
83 1 ▶sub entry_count {
Subroutine entry_count: called CC=1 SCAR=0.0
84 1 return shift->{index_silo}->entry_count;
85 }
86 24 ▶sub fetch {
Subroutine fetch: called CC=1 SCAR=0.0
87 24 my( $self, $idx ) = @_;
88 24 my $lookup = $self->{index_silo}->get_record( $idx );
89 24 my( $silo_id, $idx_in_silo ) = @$lookup;
90 24 my $silo = $self->{entry_silos}[$silo_id - 1];
91 24 my $result = $silo->get_record( $idx_in_silo );
92 24 return $result->[0];
93 }
94  
95 package RS_2;
96  
97 1 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
98 1 my( $cls, $dir ) = @_;
99  
100 1 opendir( my $dh, "$dir/stores/" );
101 1 my( @silo_ids ) = map { s/_OBJSTORE//; $_ } grep { /\d+_OBJSTORE/ } readdir( $dh );
102 1 closedir $dh;
103
104 return bless {
105 index_silo => SiloPre3->open( "$dir/OBJ_INDEX", "IL" ),
106 1 entry_silos => { map { $_ => SiloPre3->open( "$dir/stores/${_}_OBJSTORE", "LA*", int( exp( $_) ) ) } (@silo_ids)}
107 }, $cls;
108 }
109 1 ▶sub entry_count {
Subroutine entry_count: called CC=1 SCAR=0.0
110 1 return shift->{index_silo}->entry_count;
111 }
112 49 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=6.9
113 49 my( $self, $idx ) = @_;
114 49 my $lookup = $self->{index_silo}->get_record( $idx );
115 49 my( $silo_id, $id_in_silo ) = @$lookup;
116 49 ▶ return undef unless $silo_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $silo_id 12 37 49
117 37 my $silo = $self->{entry_silos}{$silo_id};
118 37 my $result = $silo->get_record( $id_in_silo );
119 37 return $result->[1];
120 }
121  
122 package RS_3;
123  
124 1 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
125 1 my( $cls, $dir ) = @_;
126  
127 1 opendir( my $dh, "$dir/silos/" );
128 1 my( @silo_ids ) = map { s/_OBJSTORE//; $_ } grep { /\d+_OBJSTORE/ } readdir( $dh );
129 1 closedir $dh;
130
131 return bless {
132 index_silo => Silo3_and_later->open_silo( "IL", "$dir/OBJ_INDEX" ),
133 1 entry_silos => { map { $_ => Silo3_and_later->open_silo( "LZ*", "$dir/silos/${_}_OBJSTORE", int( exp( $_) ) ) } (@silo_ids) }
134 }, $cls;
135 }
136 1 ▶sub entry_count {
Subroutine entry_count: called CC=1 SCAR=0.0
137 1 return shift->{index_silo}->entry_count;
138 }
139 49 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=6.9
140 49 my( $self, $idx ) = @_;
141 49 my $lookup = $self->{index_silo}->get_record( $idx );
142 49 my( $silo_id, $id_in_silo ) = @$lookup;
143 49 ▶ return undef unless $silo_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $silo_id 12 37 49
144 37 my $silo = $self->{entry_silos}{$silo_id};
145 37 my $result = $silo->get_record( $id_in_silo );
146 37 return $result->[1];
147 }
148  
149 package RS_3_1;
150  
151 1 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
152 1 my( $cls, $dir ) = @_;
153  
154 1 opendir( my $dh, "$dir/silos/" );
155 1 my( @silo_ids ) = map { s/_RECSTORE//; $_ } grep { /\d+_RECSTORE/ } readdir( $dh );
156 1 closedir $dh;
157
158 return bless {
159 index_silo => Silo3_and_later->open_silo( "IL", "$dir/RECORD_INDEX_SILO" ),
160 1 entry_silos => { map { $_ => Silo3_and_later->open_silo( "LIA*", "$dir/silos/${_}_RECSTORE", int( exp( $_) ) ) } (@silo_ids) }
161 }, $cls;
162 }
163 1 ▶sub entry_count {
Subroutine entry_count: called CC=1 SCAR=0.0
164 1 return shift->{index_silo}->entry_count;
165 }
166 45 ▶sub fetch {
Subroutine fetch: called CC=3 SCAR=11.0
167 45 my( $self, $idx ) = @_;
168 45 my $lookup = $self->{index_silo}->get_record( $idx );
169 45 my( $silo_id, $id_in_silo ) = @$lookup;
170 45 ▶ return undef unless $silo_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $silo_id 11 34 45
171 34 my $silo = $self->{entry_silos}{$silo_id};
172 34 my $result = $silo->get_record( $id_in_silo );
173 34 ▶ if( $result->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->[1]) 9 25 34
174 9 my $ret = unpack 'u', $result->[2];
175 9 chop $ret; #seems that included an extra byte :(
176 9 return $ret;
177 }
178 25 return $result->[2];
179 }
180  
181 package RS_4;
182  
183 1 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
184 1 my( $cls, $dir ) = @_;
185  
186 1 opendir( my $dh, "$dir/silos/" );
187 1 my( @silo_ids ) = map { s/_RECSTORE//; $_ } grep { /\d+_RECSTORE/ } readdir( $dh );
188 1 closedir $dh;
189  
190 return bless {
191 index_silo => Silo3_and_later->open_silo( "IL", "$dir/RECORD_INDEX_SILO" ),
192 1 entry_silos => { map { $_ => Silo3_and_later->open_silo( "LA*", "$dir/silos/${_}_RECSTORE", 2 ** $_ ) } (@silo_ids) }
193 }, $cls;
194 }
195 1 ▶sub entry_count {
Subroutine entry_count: called CC=1 SCAR=0.0
196 1 return shift->{index_silo}->entry_count;
197 }
198 49 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=6.9
199 49 my( $self, $idx ) = @_;
200 49 my $lookup = $self->{index_silo}->get_record( $idx );
201 49 my( $silo_id, $id_in_silo ) = @$lookup;
202 49 ▶ return undef unless $silo_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $silo_id 12 37 49
203 37 my $silo = $self->{entry_silos}{$silo_id};
204 37 my $result = $silo->get_record( $id_in_silo );
205 37 return $result->[1];
206 }
207  
208  
209 package RS_5;
210  
211 2 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
212 2 my( $cls, $dir ) = @_;
213
214 2 opendir( my $dh, "$dir/silos/" );
215 2 my( @silo_ids ) = map { s/_RECSTORE//; $_ } grep { /\d+_RECSTORE/ } readdir( $dh );
216 2 closedir $dh;
217
218 return bless {
219 index_silo => Silo3_and_later->open_silo( "ILL", "$dir/RECORD_INDEX_SILO" ),
220 2 entry_silos => { map { $_ => Silo3_and_later->open_silo( "LA*", "$dir/silos/${_}_RECSTORE", 2 ** $_ ) } (@silo_ids) },
221 }, $cls;
222 }
223 2 ▶sub entry_count {
Subroutine entry_count: called CC=1 SCAR=0.0
224 2 return shift->{index_silo}->entry_count;
225 }
226 98 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=6.9
227 98 my( $self, $idx ) = @_;
228 98 my $lookup = $self->{index_silo}->get_record( $idx );
229 98 my( $silo_id, $id_in_silo ) = @$lookup;
230 98 ▶ return undef unless $silo_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $silo_id 24 74 98
231 74 my $silo = $self->{entry_silos}{$silo_id};
232 74 my $result = $silo->get_record( $id_in_silo );
233 74 return $result->[1];
234 }
235  
236 package RS_6;
237  
238  
239  
240 package Silo3_and_later;
241  
242 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
245 1 ▶no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
246  
247 1 ▶use Fcntl qw( SEEK_SET );
Subroutine BEGIN: called CC=1 SCAR=0.0
248 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
249  
250 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
251 1 DIRECTORY => 0,
252 RECORD_SIZE => 1,
253 FILE_SIZE => 2,
254 FILE_MAX_RECORDS => 3,
255 TMPL => 4,
256 1 };
257  
258 # this really isn't much of a limit anymore, but...
259 # keeping it for now
260 1 $Silo3_and_later::MAX_SIZE = 2_000_000_000;
261  
262  
263 43 ▶sub open_silo {
Subroutine open_silo: called CC=2 SCAR=6.9
264 43 my( $class, $template, $directory, $size ) = @_;
265  
266 43 my $record_size = $size;
267 43 ▶ if( $record_size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_size == 0) 5 38 43
268 1 ▶ $record_size = do { use bytes; length( pack( $template ) ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
269 }
270 43 my $file_max_records = int( $Silo3_and_later::MAX_SIZE / $record_size );
271 43 my $file_max_size = $file_max_records * $record_size;
272  
273 43 my $silo = bless [
274 $directory,
275 $record_size,
276 $file_max_size,
277 $file_max_records,
278 $template,
279 ], $class;
280  
281 43 return $silo;
282 } #open_silo
283  
284 5 ▶sub entry_count {
Subroutine entry_count: called CC=2 SCAR=6.9
285 # return how many entries this silo has
286 5 my $self = shift;
287 5 my @files = $self->_files;
288 5 my $filesize;
289 5 for my $file (@files) {
290 5 $filesize += -s "$self->[DIRECTORY]/$file";
291 }
292 5 return int( $filesize / $self->[RECORD_SIZE] );
293 } #entry_count
294  
295 423 ▶sub get_record {
Subroutine get_record: called CC=1 SCAR=0.0
296 423 my( $self, $id ) = @_;
297  
298 423 my( $f_idx, $fh, $file, $file_id ) = $self->_fh( $id, 'readonly' );
299  
300 423 sysseek( $fh, $self->[RECORD_SIZE] * $f_idx, SEEK_SET );
301 423 my $srv = sysread $fh, my $data, $self->[RECORD_SIZE];
302 423 close $fh;
303  
304 423 return [unpack( $self->[TMPL], $data )];
305 } #get_record
306  
307 #
308 # Takes an insertion id and returns
309 # an insertion index for in the file
310 # filehandle.
311 # filepath/filename
312 # which number file this is (0 is the first)
313 #
314 423 ▶sub _fh {
Subroutine _fh: called CC=1 SCAR=0.0
315 423 my( $self, $id ) = @_;
316 423 my @files = $self->_files;
317  
318 423 my $f_idx = int( ($id-1) / $self->[FILE_MAX_RECORDS] );
319  
320 423 my $file = $files[$f_idx];
321 423 my $fh;
322 423 open( $fh, "<", "$self->[DIRECTORY]/$file" );
323 423 return (($id - ($f_idx*$self->[FILE_MAX_RECORDS])) - 1,$fh,"$self->[DIRECTORY]/$file",$f_idx);
324 } #_fh
325  
326 #
327 # Returns the list of filenames of the 'silos' of this store. They are numbers starting with 0
328 #
329 428 ▶sub _files {
Subroutine _files: called CC=1 SCAR=0.0
330 428 my $self = shift;
331 428 opendir( my $dh, $self->[DIRECTORY] );
332 my( @files ) = (
333 0 sort { $a <=> $b }
334 428 grep { /\d+/ }
335 readdir( $dh ) );
336 428 closedir $dh;
337 428 return @files;
338 } #_files
339  
340 # ----------- end Silo3_and_later
341  
342 package SiloPre3;
343  
344 #
345 # This package is a helper one that simulates
346 # older silo formats before version 3.1
347 #
348  
349 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
350 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
351 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
352  
353 1 ▶use Fcntl qw( SEEK_SET );
Subroutine BEGIN: called CC=1 SCAR=0.0
354  
355 18 ▶sub open {
Subroutine open: called CC=2 SCAR=6.9
356 18 my( $class, $filename, $template, $size ) = @_;
357 18 my $useSize = $size;
358 18 ▶ if( ! $useSize ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($useSize) 3 15 18
359 1 ▶ $useSize = do { use bytes; length( pack( $template ) ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
360 }
361 18 bless { TMPL => $template,
362 RECORD_SIZE => $useSize,
363 FILENAME => $filename,
364 }, $class;
365 } #open
366  
367 3 ▶sub entry_count {
Subroutine entry_count: called CC=1 SCAR=0.0
368 3 my $self = shift;
369 3 my $filesize = -s $self->{FILENAME};
370 3 return int( $filesize / $self->{RECORD_SIZE} );
371 }
372  
373 137 ▶sub get_record {
Subroutine get_record: called CC=1 SCAR=0.0
374 137 my( $self, $id ) = @_;
375  
376 137 my $fh = $self->_filehandle;
377 137 sysseek $fh, $self->{RECORD_SIZE} * ($id-1), SEEK_SET;
378 137 sysread $fh, my $data, $self->{RECORD_SIZE};
379 137 close $fh;
380 137 return [unpack( $self->{TMPL}, $data )];
381 } #get_record
382  
383 137 ▶sub _filehandle {
Subroutine _filehandle: called CC=1 SCAR=0.0
384 137 my $self = shift;
385 137 CORE::open( my $fh, "<", $self->{FILENAME} );
386 137 return $fh;
387 }
388  
389  
390  
391  
392 1 "You can't work in a steel mill and think small. Giant converters hundreds of feet high. Every night, the sky looked enormous. It was a torrent of flames - of fire. The place that Pittsburgh used to be had such scale - Jack Gilbert";
393  
394 __END__
« RecordStore/Silo.pm ↑ Summary RecordStore/Transaction.pm »