| 1 | package Data::RecordStore::Silo; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||
| 4 | # I am a silo. I live in a directory. | ||||||||||||||||||||||||||||||||||||||
| 5 | # I keep my data in silo files in this directory. | ||||||||||||||||||||||||||||||||||||||
| 6 | # Each silo file is allowed to be only so large, | ||||||||||||||||||||||||||||||||||||||
| 7 | # so that is why there may be more than one of them. | ||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||
| 9 | # I may be changed by async processes, so a lot | ||||||||||||||||||||||||||||||||||||||
| 10 | # of my coordination and state is on the file system. | ||||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||||
| 12 | # You can init me by giving me a directory, | ||||||||||||||||||||||||||||||||||||||
| 13 | # a template and an optional size and a max size. | ||||||||||||||||||||||||||||||||||||||
| 14 | # I will figure out the record size based on what you give me. | ||||||||||||||||||||||||||||||||||||||
| 15 | # I will default to 2GB for a max size if no max size is given. | ||||||||||||||||||||||||||||||||||||||
| 16 | # I will have limitless size if you give me 0 for a max size | ||||||||||||||||||||||||||||||||||||||
| 17 | # I will save my version, the size, max size and template to the directory | ||||||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||||||||||||
| 20 | # You can open me by giving a directory, then | ||||||||||||||||||||||||||||||||||||||
| 21 | # push data to me and I return its id | ||||||||||||||||||||||||||||||||||||||
| 22 | # get data from me after giving me its id | ||||||||||||||||||||||||||||||||||||||
| 23 | # pop data from me | ||||||||||||||||||||||||||||||||||||||
| 24 | # ask how many records I have | ||||||||||||||||||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 28 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 29 | 13 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 30 | 13 | ▶ | no warnings 'numeric'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 31 | 13 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | 13 | ▶ | use Fcntl qw( SEEK_SET ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 34 | 13 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 35 | #use FileCache; | ||||||||||||||||||||||||||||||||||||||
| 36 | 13 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 37 | 13 | ▶ | use YAML; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | 13 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 40 | 13 | $VERSION = '6.00'; | |||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | 13 | $Data::RecordStore::Silo::DEFAULT_MAX_FILE_SIZE = 2_000_000_000; | |||||||||||||||||||||||||||||||||||||
| 43 | 13 | $Data::RecordStore::Silo::DEFAULT_MIN_FILE_SIZE = 4_096; | |||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | 13 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 46 | 13 | DIRECTORY => 0, | |||||||||||||||||||||||||||||||||||||
| 47 | VERSION => 1, | ||||||||||||||||||||||||||||||||||||||
| 48 | TEMPLATE => 2, | ||||||||||||||||||||||||||||||||||||||
| 49 | RECORD_SIZE => 3, | ||||||||||||||||||||||||||||||||||||||
| 50 | MAX_FILE_SIZE => 4, | ||||||||||||||||||||||||||||||||||||||
| 51 | RECORDS_PER_SUBSILO => 5, | ||||||||||||||||||||||||||||||||||||||
| 52 | DIR_HANDLE => 6, | ||||||||||||||||||||||||||||||||||||||
| 53 | 13 | }; | |||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | 6315 | ▶ | sub open_silo { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
open_silo: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 57 | 6315 | my( $class, $dir, $template, $size, $max_file_size ) = @_; | |||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||
| 59 | 6315 | ▶ | if( ! $dir ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 60 | 4 | die "must supply directory to open silo"; | |||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||
| 62 | 6311 | ▶ | if( ! $template ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 63 | 4 | die "must supply template to open silo"; | |||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||
| 65 | 13 | ▶ | my $record_size = $template =~ /\*/ ? $size : do { use bytes; length( pack( $template ) ) }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 66 | 6307 | ▶ | if( $record_size < 1 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 67 | 4 | die "no record size given to open silo"; | |||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||
| 69 | 6303 | ▶ | if( $size && $size != $record_size ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$size and $size != $record_size
MC/DC100%2/2
$size and $size != $record_size
$size
$size != $record_size
Truth table100%3/3
A and B
A= $sizeB= $size != $record_size
| |||||||||||||||||||||||||||||||||||||||
| 70 | 4 | die "Silo given size and template size do not match"; | |||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||
| 72 | 6299 | make_path( $dir, { error => \my $err } ); | |||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | 6299 | ▶ | if( @$err ) { die join( ", ", map { $_->{$dir} } @$err ) } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | 6295 | ▶ | if( $max_file_size < 1 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 77 | 35 | $max_file_size = $Data::RecordStore::Silo::DEFAULT_MAX_FILE_SIZE; | |||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | 6295 | ▶ | unless( -e "$dir/0" ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 81 | 4284 | open my $out, '>', "$dir/config.yaml"; | |||||||||||||||||||||||||||||||||||||
| 82 | 4284 | print $out <<"END"; | |||||||||||||||||||||||||||||||||||||
| 83 | VERSION: $VERSION | ||||||||||||||||||||||||||||||||||||||
| 84 | TEMPLATE: $template | ||||||||||||||||||||||||||||||||||||||
| 85 | RECORD_SIZE: $record_size | ||||||||||||||||||||||||||||||||||||||
| 86 | MAX_FILE_SIZE: $max_file_size | ||||||||||||||||||||||||||||||||||||||
| 87 | END | ||||||||||||||||||||||||||||||||||||||
| 88 | 4284 | close $out; | |||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | # must have at least an empty silo file | ||||||||||||||||||||||||||||||||||||||
| 91 | 4284 | open $out, '>', "$dir/0"; | |||||||||||||||||||||||||||||||||||||
| 92 | 4284 | print $out ''; | |||||||||||||||||||||||||||||||||||||
| 93 | 4284 | close $out; | |||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | 6295 | return bless [ | |||||||||||||||||||||||||||||||||||||
| 97 | $dir, | ||||||||||||||||||||||||||||||||||||||
| 98 | $VERSION, | ||||||||||||||||||||||||||||||||||||||
| 99 | $template, | ||||||||||||||||||||||||||||||||||||||
| 100 | $record_size, | ||||||||||||||||||||||||||||||||||||||
| 101 | $max_file_size, | ||||||||||||||||||||||||||||||||||||||
| 102 | int($max_file_size / $record_size), | ||||||||||||||||||||||||||||||||||||||
| 103 | ], $class; | ||||||||||||||||||||||||||||||||||||||
| 104 | } #open_silo | ||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | 764 | ▶ | sub reopen_silo { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
reopen_silo: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 107 | 764 | my( $cls, $dir ) = @_; | |||||||||||||||||||||||||||||||||||||
| 108 | 764 | my $cfgfile = "$dir/config.yaml"; | |||||||||||||||||||||||||||||||||||||
| 109 | 764 | ▶ | if( -e $cfgfile ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 110 | 756 | my $cfg = YAML::LoadFile( $cfgfile ); | |||||||||||||||||||||||||||||||||||||
| 111 | 756 | return $cls->open_silo( $dir, @$cfg{qw(TEMPLATE RECORD_SIZE MAX_FILE_SIZE)} ); | |||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||
| 113 | 8 | die "could not find silo in $dir"; | |||||||||||||||||||||||||||||||||||||
| 114 | } #reopen_silo | ||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||
| 116 | 1983 | ▶ | sub next_id { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
next_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 117 | 1983 | my( $self ) = @_; | |||||||||||||||||||||||||||||||||||||
| 118 | 1983 | my $next_id = 1 + $self->entry_count; | |||||||||||||||||||||||||||||||||||||
| 119 | 1983 | $self->ensure_entry_count( $next_id ); | |||||||||||||||||||||||||||||||||||||
| 120 | 1983 | return $next_id; | |||||||||||||||||||||||||||||||||||||
| 121 | } #next_id | ||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | 18692 | ▶ | sub entry_count { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
entry_count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 124 | # return how many entries this silo has | ||||||||||||||||||||||||||||||||||||||
| 125 | 18692 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 126 | 18692 | my @files = $self->subsilos; | |||||||||||||||||||||||||||||||||||||
| 127 | 18692 | my $filesize; | |||||||||||||||||||||||||||||||||||||
| 128 | 18692 | for my $file (@files) { | |||||||||||||||||||||||||||||||||||||
| 129 | 19256 | $filesize += -s "$self->[DIRECTORY]/$file"; | |||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||
| 131 | 18692 | return int( $filesize / $self->[RECORD_SIZE] ); | |||||||||||||||||||||||||||||||||||||
| 132 | } #entry_count | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | 2971 | ▶ | sub get_record { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_record: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||
| 135 | 2971 | my( $self, $id, $template, $offset ) = @_; | |||||||||||||||||||||||||||||||||||||
| 136 | 2971 | my $rec_size; | |||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | 2971 | ▶ | if( $template > 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 139 | 4 | $rec_size = $template; | |||||||||||||||||||||||||||||||||||||
| 140 | 4 | $template = $self->[TEMPLATE]; | |||||||||||||||||||||||||||||||||||||
| 141 | } elsif( $template ) { | ||||||||||||||||||||||||||||||||||||||
| 142 | 13 | ▶ | my $template_size = $template =~ /\*/ ? 0 : do { use bytes; length( pack( $template ) ) }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 143 | 87 | $rec_size = $template_size; | |||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||||||||||||
| 146 | 2880 | $rec_size = $self->[RECORD_SIZE]; | |||||||||||||||||||||||||||||||||||||
| 147 | 2880 | $template = $self->[TEMPLATE]; | |||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||
| 149 | 2971 | ▶ | if( $id > $self->entry_count || $id < 1 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$id > $self->entry_count or $id < 1
MC/DC100%2/2
$id > $self->entry_count or $id < 1
$id > $self->entry_count
$id < 1
Truth table100%3/3
A or B
A= $id > $self->entry_countB= $id < 1
| |||||||||||||||||||||||||||||||||||||||
| 150 | 17 | die "Data::RecordStore::Silo->get_record : ($$) index $id out of bounds for silo $self->[DIRECTORY]. Silo has entry count of ".$self->entry_count; | |||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||
| 152 | 2954 | my( $idx_in_f, $fh, $subsilo_idx ) = $self->_fh( $id ); | |||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||
| 154 | 2950 | ▶ | $offset //= 0; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$offset //= 0
MC/DC100%1/1
$offset //= 0
$offset
Truth table100%2/2
A
A= $offset
| |||||||||||||||||||||||||||||||||||||||
| 155 | 2950 | my $seek_pos = ( $self->[RECORD_SIZE] * $idx_in_f ) + $offset; | |||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||
| 157 | 2950 | sysseek( $fh, $seek_pos, SEEK_SET ); | |||||||||||||||||||||||||||||||||||||
| 158 | 2950 | my $srv = sysread $fh, (my $data), $rec_size; | |||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | 2950 | return [unpack( $template, $data )]; | |||||||||||||||||||||||||||||||||||||
| 161 | } #get_record | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | 2580 | ▶ | sub put_record { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
put_record: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||
| 164 | 2580 | my( $self, $id, $data, $template, $offset ) = @_; | |||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||
| 166 | 2580 | ▶ | if( $id > $self->entry_count || $id < 1 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$id > $self->entry_count or $id < 1
MC/DC100%2/2
$id > $self->entry_count or $id < 1
$id > $self->entry_count
$id < 1
Truth table100%3/3
A or B
A= $id > $self->entry_countB= $id < 1
| |||||||||||||||||||||||||||||||||||||||
| 167 | 20 | die "Data::RecordStore::Silo->put_record : index $id out of bounds for silo $self->[DIRECTORY]. Store has entry count of ".$self->entry_count; | |||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||
| 169 | 2560 | ▶ | if( ! $template ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 170 | 2175 | $template = $self->[TEMPLATE]; | |||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | 2560 | my $rec_size = $self->[RECORD_SIZE]; | |||||||||||||||||||||||||||||||||||||
| 174 | 2560 | ▶ | my $to_write = pack( $template, ref $data ? @$data : ($data) ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||
| 176 | # allows the put_record to grow the data store by no more than one entry | ||||||||||||||||||||||||||||||||||||||
| 177 | 13 | ▶ | my $write_size = do { use bytes; length( $to_write ) }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||
| 179 | 2560 | ▶ | if( $write_size > $rec_size) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 180 | 4 | die "Data::RecordStore::Silo->put_record : record size $write_size too large. Max is $rec_size"; | |||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | 2556 | my( $idx_in_f, $fh, $subsilo_idx ) = $self->_fh( $id ); | |||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||
| 185 | 2556 | ▶ | $offset //= 0; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$offset //= 0
MC/DC100%1/1
$offset //= 0
$offset
Truth table100%2/2
A
A= $offset
| |||||||||||||||||||||||||||||||||||||||
| 186 | 2556 | my $seek_pos = $rec_size * $idx_in_f + $offset; | |||||||||||||||||||||||||||||||||||||
| 187 | 2556 | sysseek( $fh, $seek_pos, SEEK_SET ); | |||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||
| 189 | 2556 | syswrite( $fh, $to_write ); | |||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||
| 191 | 2556 | return 1; | |||||||||||||||||||||||||||||||||||||
| 192 | } #put_record | ||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||
| 194 | 252 | ▶ | sub pop { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
pop: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 195 | 252 | my( $self ) = @_; | |||||||||||||||||||||||||||||||||||||
| 196 | 252 | my $entries = $self->entry_count; | |||||||||||||||||||||||||||||||||||||
| 197 | 252 | ▶ | unless( $entries ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 198 | 4 | return undef; | |||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||
| 200 | 248 | my $ret = $self->get_record( $entries ); | |||||||||||||||||||||||||||||||||||||
| 201 | 248 | my( $idx_in_f, $fh, $subsilo_idx ) = $self->_fh( $entries ); | |||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||
| 203 | 248 | my $new_subsilo_size = (($entries-1) - ($subsilo_idx * $self->[RECORDS_PER_SUBSILO] ))*$self->[RECORD_SIZE]; | |||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||
| 205 | 248 | ▶ | if( $new_subsilo_size || $subsilo_idx == 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$new_subsilo_size or $subsilo_idx == 0
MC/DC100%2/2
$new_subsilo_size or $subsilo_idx == 0
$new_subsilo_size
$subsilo_idx == 0
Truth table100%3/3
A or B
A= $new_subsilo_sizeB= $subsilo_idx == 0
| |||||||||||||||||||||||||||||||||||||||
| 206 | 240 | truncate $fh, $new_subsilo_size; | |||||||||||||||||||||||||||||||||||||
| 207 | } else { | ||||||||||||||||||||||||||||||||||||||
| 208 | 8 | unlink "$self->[DIRECTORY]/$subsilo_idx"; | |||||||||||||||||||||||||||||||||||||
| 209 | # FileCache::cacheout_close $fh; | ||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||
| 212 | 248 | return $ret; | |||||||||||||||||||||||||||||||||||||
| 213 | } #pop | ||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||
| 215 | 24 | ▶ | sub peek { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
peek: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 216 | 24 | my( $self ) = @_; | |||||||||||||||||||||||||||||||||||||
| 217 | 24 | my $entries = $self->entry_count; | |||||||||||||||||||||||||||||||||||||
| 218 | 24 | ▶ | unless( $entries ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 219 | 8 | return undef; | |||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||
| 221 | 16 | my $r = $self->get_record( $entries ); | |||||||||||||||||||||||||||||||||||||
| 222 | 12 | return $r; | |||||||||||||||||||||||||||||||||||||
| 223 | } #peek | ||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||
| 225 | 1263 | ▶ | sub push { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
push: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 226 | 1263 | my( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||
| 227 | 1263 | my $next_id = $self->next_id; | |||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||
| 229 | 1263 | $self->put_record( $next_id, $data ); | |||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||
| 231 | 1263 | return $next_id; | |||||||||||||||||||||||||||||||||||||
| 232 | } #push | ||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||
| 236 | 4 | ▶ | sub record_size { return shift->[RECORD_SIZE] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
record_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 237 | 8 | ▶ | sub template { return shift->[TEMPLATE] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||
| 239 | 4 | ▶ | sub max_file_size { return shift->[MAX_FILE_SIZE] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
max_file_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 240 | 4 | ▶ | sub records_per_subsilo { return shift->[RECORDS_PER_SUBSILO] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
records_per_subsilo: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||
| 242 | 52 | ▶ | sub size { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 243 | # return how many bytes of data this silo has | ||||||||||||||||||||||||||||||||||||||
| 244 | 52 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 245 | 52 | my @files = $self->subsilos; | |||||||||||||||||||||||||||||||||||||
| 246 | 52 | my $filesize = 0; | |||||||||||||||||||||||||||||||||||||
| 247 | 52 | for my $file (@files) { | |||||||||||||||||||||||||||||||||||||
| 248 | 124 | $filesize += -s "$self->[DIRECTORY]/$file"; | |||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||
| 250 | 52 | return $filesize; | |||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||
| 254 | 71 | ▶ | sub copy_record { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
copy_record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 255 | 71 | my( $self, $from_id, $to_id ) = @_; | |||||||||||||||||||||||||||||||||||||
| 256 | 71 | my $rec = $self->get_record($from_id); | |||||||||||||||||||||||||||||||||||||
| 257 | 63 | $self->put_record( $to_id, $rec ); | |||||||||||||||||||||||||||||||||||||
| 258 | 55 | return $rec; | |||||||||||||||||||||||||||||||||||||
| 259 | } #copy_record | ||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||
| 262 | # | ||||||||||||||||||||||||||||||||||||||
| 263 | # Destroys all the data in the silo | ||||||||||||||||||||||||||||||||||||||
| 264 | # | ||||||||||||||||||||||||||||||||||||||
| 265 | 8 | ▶ | sub empty_silo { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
empty_silo: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 266 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 267 | 8 | my $dir = $self->[DIRECTORY]; | |||||||||||||||||||||||||||||||||||||
| 268 | 8 | for my $file ($self->subsilos) { | |||||||||||||||||||||||||||||||||||||
| 269 | 24 | ▶ | if( $file eq '0' ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 270 | 8 | open my $fh, '+<', "$dir/0"; | |||||||||||||||||||||||||||||||||||||
| 271 | 8 | truncate $fh, 0; | |||||||||||||||||||||||||||||||||||||
| 272 | } else { | ||||||||||||||||||||||||||||||||||||||
| 273 | 16 | unlink "$dir/$file"; | |||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||
| 276 | } #empty_silo | ||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||
| 278 | # destroys the silo. The silo will not be | ||||||||||||||||||||||||||||||||||||||
| 279 | # functional after this call. | ||||||||||||||||||||||||||||||||||||||
| 280 | 4 | ▶ | sub unlink_silo { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
unlink_silo: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 281 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 282 | 4 | my $dir = $self->[DIRECTORY]; | |||||||||||||||||||||||||||||||||||||
| 283 | 4 | for my $file ($self->subsilos) { | |||||||||||||||||||||||||||||||||||||
| 284 | 4 | unlink "$dir/$file"; | |||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||
| 286 | 4 | unlink "$dir/SINFO"; | |||||||||||||||||||||||||||||||||||||
| 287 | 4 | @$self = (); | |||||||||||||||||||||||||||||||||||||
| 288 | } #unlink_silo | ||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||
| 291 | #Makes sure this silo has at least as many entries | ||||||||||||||||||||||||||||||||||||||
| 292 | #as the count given. This creates empty records if needed | ||||||||||||||||||||||||||||||||||||||
| 293 | #to rearch the target record count. | ||||||||||||||||||||||||||||||||||||||
| 294 | 2578 | ▶ | sub ensure_entry_count { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
ensure_entry_count: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 295 | 2578 | my( $self, $count ) = @_; | |||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||
| 297 | 2578 | my $ec = $self->entry_count; | |||||||||||||||||||||||||||||||||||||
| 298 | 2578 | my $needed = $count - $ec; | |||||||||||||||||||||||||||||||||||||
| 299 | 2578 | my $dir = $self->[DIRECTORY]; | |||||||||||||||||||||||||||||||||||||
| 300 | 2578 | my $rec_size = $self->[RECORD_SIZE]; | |||||||||||||||||||||||||||||||||||||
| 301 | 2578 | my $rec_per_subsilo = $self->[RECORDS_PER_SUBSILO]; | |||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||
| 303 | 2578 | ▶ | if( $needed > 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 304 | 2020 | my( @files ) = $self->subsilos; | |||||||||||||||||||||||||||||||||||||
| 305 | 2020 | my $write_file = $files[$#files]; | |||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||
| 307 | 2020 | my $existing_file_records = int( (-s "$dir/$write_file" ) / $rec_size ); | |||||||||||||||||||||||||||||||||||||
| 308 | 2020 | my $records_needed_to_fill = $rec_per_subsilo - $existing_file_records; | |||||||||||||||||||||||||||||||||||||
| 309 | 2020 | ▶ | $records_needed_to_fill = $needed if $records_needed_to_fill > $needed; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 310 | 2020 | my $nulls; | |||||||||||||||||||||||||||||||||||||
| 311 | 2020 | ▶ | if( $records_needed_to_fill > 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 312 | # fill the last file up with \0 | ||||||||||||||||||||||||||||||||||||||
| 313 | # my $fh = cacheout "+<", | ||||||||||||||||||||||||||||||||||||||
| 314 | 2008 | ▶ | open my $fh, '+<', "$dir/$write_file" or die "$dir/$write_file : $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 315 | # $fh->autoflush(1); | ||||||||||||||||||||||||||||||||||||||
| 316 | 2004 | $nulls = "\0" x ( $records_needed_to_fill * $rec_size ); | |||||||||||||||||||||||||||||||||||||
| 317 | 2004 | my $seek_pos = $rec_size * $existing_file_records; | |||||||||||||||||||||||||||||||||||||
| 318 | 2004 | sysseek( $fh, $seek_pos, SEEK_SET ); | |||||||||||||||||||||||||||||||||||||
| 319 | 2004 | syswrite( $fh, $nulls ); | |||||||||||||||||||||||||||||||||||||
| 320 | 2004 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 321 | 2004 | undef $nulls; | |||||||||||||||||||||||||||||||||||||
| 322 | 2004 | $needed -= $records_needed_to_fill; | |||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||
| 324 | 2016 | ▶ | while( $needed > $rec_per_subsilo ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 325 | # still needed, so create a new file | ||||||||||||||||||||||||||||||||||||||
| 326 | 20 | $write_file++; | |||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||
| 328 | 20 | ▶ | if( -e "$dir/$write_file" ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 329 | 4 | die "Data::RecordStore::Silo->ensure_entry_count : file $dir/$write_file already exists"; | |||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||
| 331 | 16 | open( my $fh, ">", "$dir/$write_file" ); | |||||||||||||||||||||||||||||||||||||
| 332 | # $fh->autoflush(1); | ||||||||||||||||||||||||||||||||||||||
| 333 | 16 | print $fh ''; | |||||||||||||||||||||||||||||||||||||
| 334 | 16 | ▶ | unless( $nulls ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 335 | 8 | $nulls = "\0" x ( $rec_per_subsilo * $rec_size ); | |||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||
| 337 | 16 | sysseek( $fh, 0, SEEK_SET ); | |||||||||||||||||||||||||||||||||||||
| 338 | 16 | syswrite( $fh, $nulls ); | |||||||||||||||||||||||||||||||||||||
| 339 | 16 | $needed -= $rec_per_subsilo; | |||||||||||||||||||||||||||||||||||||
| 340 | 16 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||
| 342 | 2012 | ▶ | if( $needed > 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 343 | # still needed, so create a new file | ||||||||||||||||||||||||||||||||||||||
| 344 | 16 | $write_file++; | |||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||
| 346 | 16 | ▶ | if( -e "$dir/$write_file" ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 347 | 4 | die "Data::RecordStore::Silo->ensure_entry_count : file $dir/$write_file already exists"; | |||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||
| 349 | 12 | open( my $fh, ">", "$dir/$write_file" ); | |||||||||||||||||||||||||||||||||||||
| 350 | # $fh->autoflush(1); | ||||||||||||||||||||||||||||||||||||||
| 351 | 12 | print $fh ''; | |||||||||||||||||||||||||||||||||||||
| 352 | 12 | my $nulls = "\0" x ( $needed * $rec_size ); | |||||||||||||||||||||||||||||||||||||
| 353 | 12 | sysseek( $fh, 0, SEEK_SET ); | |||||||||||||||||||||||||||||||||||||
| 354 | 12 | syswrite( $fh, $nulls ); | |||||||||||||||||||||||||||||||||||||
| 355 | 12 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||
| 358 | 2566 | $ec = $self->entry_count; | |||||||||||||||||||||||||||||||||||||
| 359 | 2566 | return; | |||||||||||||||||||||||||||||||||||||
| 360 | } #ensure_entry_count | ||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||
| 362 | # | ||||||||||||||||||||||||||||||||||||||
| 363 | # Returns the list of filenames of the 'silos' of this store. They are numbers starting with 0 | ||||||||||||||||||||||||||||||||||||||
| 364 | # | ||||||||||||||||||||||||||||||||||||||
| 365 | 20836 | ▶ | sub subsilos { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
subsilos: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 366 | 20836 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 367 | 20836 | my $dir = $self->[DIRECTORY]; | |||||||||||||||||||||||||||||||||||||
| 368 | 20836 | my $dh = $self->[DIR_HANDLE]; | |||||||||||||||||||||||||||||||||||||
| 369 | 20836 | ▶ | if( $dh ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 370 | 19206 | rewinddir $dh; | |||||||||||||||||||||||||||||||||||||
| 371 | } else { | ||||||||||||||||||||||||||||||||||||||
| 372 | 1630 | ▶ | opendir( $dh, $self->[DIRECTORY] ) or die "Data::RecordStore::Silo->subsilos : can't open $dir\n"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 373 | 1622 | $self->[DIR_HANDLE] = $dh; | |||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||
| 375 | 20828 | ▶ | my( @files ) = (sort { $a <=> $b } grep { $_ eq '0' || (-s "$dir/$_") > 0 } grep { $_ > 0 || $_ eq '0' } readdir( $dh ) ); | ||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$_ eq '0' || -s "$dir/$_" > 0
$_ > 0 || $_ eq '0'
MC/DC100%2/2
$_ eq '0' || -s "$dir/$_" > 0
$_ eq '0'
-s "$dir/$_" > 0
MC/DC100%2/2
$_ > 0 || $_ eq '0'
$_ > 0
$_ eq '0'
Truth table100%3/3
A || B
A= $_ eq '0'B= -s "$dir/$_" > 0
Truth table100%3/3
A || B
A= $_ > 0B= $_ eq '0'
| |||||||||||||||||||||||||||||||||||||||
| 376 | 20828 | return @files; | |||||||||||||||||||||||||||||||||||||
| 377 | } #subsilos | ||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||
| 380 | # | ||||||||||||||||||||||||||||||||||||||
| 381 | # Takes an insertion id and returns | ||||||||||||||||||||||||||||||||||||||
| 382 | # an insertion index for in the file | ||||||||||||||||||||||||||||||||||||||
| 383 | # filehandle. | ||||||||||||||||||||||||||||||||||||||
| 384 | # filepath/filename | ||||||||||||||||||||||||||||||||||||||
| 385 | # which number file this is (0 is the first) | ||||||||||||||||||||||||||||||||||||||
| 386 | # | ||||||||||||||||||||||||||||||||||||||
| 387 | 6150 | ▶ | sub _fh { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_fh: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 388 | 6150 | my( $self, $id ) = @_; | |||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||
| 390 | 6150 | my $dir = $self->[DIRECTORY]; | |||||||||||||||||||||||||||||||||||||
| 391 | 6150 | my $rec_per_subsilo = $self->[RECORDS_PER_SUBSILO]; | |||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||
| 393 | 6150 | my $subsilo_idx = int( ($id-1) / $rec_per_subsilo ); | |||||||||||||||||||||||||||||||||||||
| 394 | 6150 | my $idx_in_f = ($id - ($subsilo_idx*$rec_per_subsilo)) - 1; | |||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||
| 396 | 6150 | ▶ | open my $fh, "+<", "$dir/$subsilo_idx" or die "$dir/$subsilo_idx : $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 397 | 6146 | return $idx_in_f, $fh, $subsilo_idx; | |||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||
| 399 | } #_fh | ||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||
| 403 | 13 | "Silos are the great hidden constant of the industrialised world. | |||||||||||||||||||||||||||||||||||||
| 404 | - John Darnielle, Universal Harvester"; | ||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||
| 406 | __END__ | ||||||||||||||||||||||||||||||||||||||