| 1 | package Data::RecordStore; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | ####################################################################### | ||||||||||||||||||||||||||||||||
| 4 | # The RecordStore is an index-value store that is composed of fixed # | ||||||||||||||||||||||||||||||||
| 5 | # sized data silos to store its data, transaction info and # | ||||||||||||||||||||||||||||||||
| 6 | # indexes. The silos are primarily binary data files so finding a # | ||||||||||||||||||||||||||||||||
| 7 | # record in them is as easy as multiplying the record size times # | ||||||||||||||||||||||||||||||||
| 8 | # the index and performing a seek on the silo's file handle. # | ||||||||||||||||||||||||||||||||
| 9 | # # | ||||||||||||||||||||||||||||||||
| 10 | # Its primary subs are stow and fetch. Their description should # | ||||||||||||||||||||||||||||||||
| 11 | # give a good feel for how the record store works in general. # | ||||||||||||||||||||||||||||||||
| 12 | # # | ||||||||||||||||||||||||||||||||
| 13 | # fetch : # | ||||||||||||||||||||||||||||||||
| 14 | # When fetch is called for a id, it uses its index silo to look # | ||||||||||||||||||||||||||||||||
| 15 | # up which fixed record silo the data for that id is stored in # | ||||||||||||||||||||||||||||||||
| 16 | # and what silo index it has. # | ||||||||||||||||||||||||||||||||
| 17 | # The store then accesses that silo and gets the data it needs. # | ||||||||||||||||||||||||||||||||
| 18 | # Silos use pack and unpack to store and retreive data. # | ||||||||||||||||||||||||||||||||
| 19 | # # | ||||||||||||||||||||||||||||||||
| 20 | # stow : # | ||||||||||||||||||||||||||||||||
| 21 | # When stow is called with data and an id, it picks a data # | ||||||||||||||||||||||||||||||||
| 22 | # silo based on the size of the data. The silos are numbered # | ||||||||||||||||||||||||||||||||
| 23 | # starting with 12. The size of the silo is 2^N where N is # | ||||||||||||||||||||||||||||||||
| 24 | # the number of the silo. The smallest silo by default is 4096 # | ||||||||||||||||||||||||||||||||
| 25 | # bytes. # | ||||||||||||||||||||||||||||||||
| 26 | ####################################################################### | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||||||
| 30 | # The original that adheres the record store interface. | ||||||||||||||||||||||||||||||||
| 31 | # namely | ||||||||||||||||||||||||||||||||
| 32 | # * open_store | ||||||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||||||
| 34 | # * stow | ||||||||||||||||||||||||||||||||
| 35 | # * fetch | ||||||||||||||||||||||||||||||||
| 36 | # * delete_record | ||||||||||||||||||||||||||||||||
| 37 | # * next_id | ||||||||||||||||||||||||||||||||
| 38 | # * last_updated | ||||||||||||||||||||||||||||||||
| 39 | # | ||||||||||||||||||||||||||||||||
| 40 | # * highest_entry_id | ||||||||||||||||||||||||||||||||
| 41 | # | ||||||||||||||||||||||||||||||||
| 42 | # * lock | ||||||||||||||||||||||||||||||||
| 43 | # * unlock | ||||||||||||||||||||||||||||||||
| 44 | # | ||||||||||||||||||||||||||||||||
| 45 | # * use_transaction | ||||||||||||||||||||||||||||||||
| 46 | # * commit_transaction | ||||||||||||||||||||||||||||||||
| 47 | # * rollback_transaction | ||||||||||||||||||||||||||||||||
| 48 | # * list_transactions | ||||||||||||||||||||||||||||||||
| 49 | # * empty | ||||||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 53 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 54 | 9 | ▶ | no warnings 'numeric'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 55 | 9 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 9 | ▶ | use Fcntl qw( :flock SEEK_SET ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 58 | 9 | ▶ | use File::Path qw(make_path); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 59 | 9 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 60 | 9 | ▶ | use YAML; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 9 | ▶ | use Data::RecordStore::Silo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 63 | 9 | ▶ | use Data::RecordStore::Transaction; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 9 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 9 | $VERSION = '6.06'; | |||||||||||||||||||||||||||||||
| 68 | 9 | my $SILO_VERSION = '6.00'; | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 9 | ▶ | use constant { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 71 | # record state | ||||||||||||||||||||||||||||||||
| 72 | 9 | RS_ACTIVE => 1, | |||||||||||||||||||||||||||||||
| 73 | RS_DEAD => 2, | ||||||||||||||||||||||||||||||||
| 74 | RS_IN_TRANSACTION => 3, | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | TR_ACTIVE => 1, | ||||||||||||||||||||||||||||||||
| 77 | TR_IN_COMMIT => 2, | ||||||||||||||||||||||||||||||||
| 78 | TR_IN_ROLLBACK => 3, | ||||||||||||||||||||||||||||||||
| 79 | TR_COMPLETE => 4, | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | DIRECTORY => 0, | ||||||||||||||||||||||||||||||||
| 82 | ACTIVE_TRANS_FILE => 1, | ||||||||||||||||||||||||||||||||
| 83 | MAX_FILE_SIZE => 2, | ||||||||||||||||||||||||||||||||
| 84 | MIN_SILO_ID => 3, | ||||||||||||||||||||||||||||||||
| 85 | INDEX_SILO => 4, | ||||||||||||||||||||||||||||||||
| 86 | SILOS => 5, | ||||||||||||||||||||||||||||||||
| 87 | TRANSACTION_INDEX_SILO => 6, | ||||||||||||||||||||||||||||||||
| 88 | TRANSACTION => 7, | ||||||||||||||||||||||||||||||||
| 89 | HEADER_SIZE => 8, | ||||||||||||||||||||||||||||||||
| 90 | LOCK_FH => 9, | ||||||||||||||||||||||||||||||||
| 91 | LOCKS => 10, | ||||||||||||||||||||||||||||||||
| 92 | MAX_SILO_ID => 11, | ||||||||||||||||||||||||||||||||
| 93 | 9 | }; | |||||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||||||
| 95 | # On disc block size : If the reads align with block sizes, the reads will go faster | ||||||||||||||||||||||||||||||||
| 96 | # however, there are some annoying things about finding out what that blocksize is. | ||||||||||||||||||||||||||||||||
| 97 | # (spoiler: it is currently either 4096 or 512 bytes and probably the former) | ||||||||||||||||||||||||||||||||
| 98 | # | ||||||||||||||||||||||||||||||||
| 99 | # ways to determine block size : | ||||||||||||||||||||||||||||||||
| 100 | # sudo blockdev -getbsz /dev/sda1 | ||||||||||||||||||||||||||||||||
| 101 | # lsblk -o NAME,MOUNTPOINT,FSTYPE,TYPE,SIZE,STATE,DISC-MAX,DISC-GRAN,PHY-SEC,MIN-IO,RQ-SIZE | ||||||||||||||||||||||||||||||||
| 102 | # and they disagree :( | ||||||||||||||||||||||||||||||||
| 103 | # using 4096 in any case. It won't be slower, though the space required could be 8 times are large. | ||||||||||||||||||||||||||||||||
| 104 | # | ||||||||||||||||||||||||||||||||
| 105 | # note: this is only for the records, not for any indexes. | ||||||||||||||||||||||||||||||||
| 106 | # | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 84 | ▶ | sub reopen_store { | ||||||||||||||||||||||||||||||
|
Subroutine
reopen_store: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 109 | 84 | my( $cls, $dir ) = @_; | |||||||||||||||||||||||||||||||
| 110 | 84 | my $cfgfile = "$dir/config.yaml"; | |||||||||||||||||||||||||||||||
| 111 | 84 | ▶ | if( -e $cfgfile ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 72 | my $lockfile = "$dir/LOCK"; | |||||||||||||||||||||||||||||||
| 113 | 72 | ▶ | die "no lock file found" unless -e $lockfile; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | 64 | ▶ | open my $lock_fh, '+<', $lockfile or die "$@ $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | 56 | flock( $lock_fh, LOCK_EX ); | |||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 56 | my $lock_dir = "$dir/user_locks"; | |||||||||||||||||||||||||||||||
| 118 | 56 | my $trans_dir = "$dir/transactions"; | |||||||||||||||||||||||||||||||
| 119 | 56 | ▶ | die "locks directory not found" unless -d $lock_dir; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 48 | ▶ | die "transaction directory not found" unless -d $trans_dir; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 40 | my $cfg = YAML::LoadFile( $cfgfile ); | |||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 40 | my $index_silo = Data::RecordStore::Silo->reopen_silo( "$dir/index_silo" ); | |||||||||||||||||||||||||||||||
| 125 | 40 | my $transaction_index_silo = Data::RecordStore::Silo->reopen_silo( "$dir/transaction_index_silo" ); | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 40 | my $max_file_size = $cfg->{MAX_FILE_SIZE}; | |||||||||||||||||||||||||||||||
| 128 | 40 | my $min_file_size = $cfg->{MIN_FILE_SIZE}; | |||||||||||||||||||||||||||||||
| 129 | 40 | my $max_silo_id = int( log( $max_file_size ) / log( 2 )); | |||||||||||||||||||||||||||||||
| 130 | 40 | ▶ | $max_silo_id++ if 2 ** $max_silo_id < $max_file_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 40 | my $min_silo_id = int( log( $min_file_size ) / log( 2 )); | |||||||||||||||||||||||||||||||
| 132 | 40 | ▶ | $min_silo_id++ if 2 ** $min_silo_id < $min_file_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 40 | my $silo_dir = "$dir/data_silos"; | |||||||||||||||||||||||||||||||
| 135 | 40 | my $silos = []; | |||||||||||||||||||||||||||||||
| 136 | 40 | for my $silo_id ($min_silo_id..$max_silo_id) { | |||||||||||||||||||||||||||||||
| 137 | 680 | $silos->[$silo_id] = Data::RecordStore::Silo->reopen_silo( "$silo_dir/$silo_id" ); | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 32 | my $header = pack( 'ILL', 1,2,3 ); | |||||||||||||||||||||||||||||||
| 141 | 9 | ▶ | my $header_size = do { use bytes; length( $header ) }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 32 | my $store = bless [ | |||||||||||||||||||||||||||||||
| 144 | $dir, | ||||||||||||||||||||||||||||||||
| 145 | "$dir/ACTIVE_TRANS", | ||||||||||||||||||||||||||||||||
| 146 | $max_file_size, | ||||||||||||||||||||||||||||||||
| 147 | $min_silo_id, | ||||||||||||||||||||||||||||||||
| 148 | $index_silo, | ||||||||||||||||||||||||||||||||
| 149 | $silos, | ||||||||||||||||||||||||||||||||
| 150 | $transaction_index_silo, | ||||||||||||||||||||||||||||||||
| 151 | undef, | ||||||||||||||||||||||||||||||||
| 152 | $header_size, # the ILL from ILLa* | ||||||||||||||||||||||||||||||||
| 153 | $lock_fh, | ||||||||||||||||||||||||||||||||
| 154 | [], | ||||||||||||||||||||||||||||||||
| 155 | $max_silo_id, | ||||||||||||||||||||||||||||||||
| 156 | ], $cls; | ||||||||||||||||||||||||||||||||
| 157 | 32 | $store->_fix_transactions; | |||||||||||||||||||||||||||||||
| 158 | 32 | flock( $lock_fh, LOCK_UN ); | |||||||||||||||||||||||||||||||
| 159 | 32 | return $store; | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | 12 | die "could not find record store in $dir"; | |||||||||||||||||||||||||||||||
| 162 | } #reopen_store | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 333 | ▶ | sub open_store { | ||||||||||||||||||||||||||||||
|
Subroutine
open_store: called CC=13 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 165 | 333 | my( $cls, @options ) = @_; | |||||||||||||||||||||||||||||||
| 166 | 333 | ▶ | if( @options == 1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | 87 | unshift @options, 'BASE_PATH'; | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | 333 | my( %options ) = @options; | |||||||||||||||||||||||||||||||
| 170 | 333 | my $dir = $options{BASE_PATH}; | |||||||||||||||||||||||||||||||
| 171 | 333 | ▶ | unless( -d $dir ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 172 | 22 | _make_path( $dir, 'base' ); | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | 325 | my $max_file_size = $options{MAX_FILE_SIZE}; | |||||||||||||||||||||||||||||||
| 175 | 325 | ▶ | $max_file_size = $Data::RecordStore::Silo::DEFAULT_MAX_FILE_SIZE unless $max_file_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 325 | my $min_file_size = $options{MIN_FILE_SIZE}; | |||||||||||||||||||||||||||||||
| 178 | 325 | ▶ | $min_file_size = $Data::RecordStore::Silo::DEFAULT_MIN_FILE_SIZE unless $min_file_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 325 | ▶ | if( $min_file_size > $max_file_size ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 180 | 16 | die "MIN_FILE_SIZE cannot be more than MAX_FILE_SIZE"; | |||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | 309 | my $max_silo_id = int( log( $max_file_size ) / log( 2 )); | |||||||||||||||||||||||||||||||
| 183 | 309 | ▶ | $max_silo_id++ if 2 ** $max_silo_id < $max_file_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 184 | 309 | my $min_silo_id = int( log( $min_file_size ) / log( 2 )); | |||||||||||||||||||||||||||||||
| 185 | 309 | ▶ | $min_silo_id++ if 2 ** $min_silo_id < $min_file_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 186 | 309 | my $lockfile = "$dir/LOCK"; | |||||||||||||||||||||||||||||||
| 187 | 309 | my $lock_fh; | |||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 309 | my $silo_dir = "$dir/data_silos"; | |||||||||||||||||||||||||||||||
| 190 | 309 | my $lock_dir = "$dir/user_locks"; | |||||||||||||||||||||||||||||||
| 191 | 309 | my $trans_dir = "$dir/transactions"; | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 309 | ▶ | if( -e $lockfile ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 194 | 63 | ▶ | open $lock_fh, '+<', $lockfile or die "$@ $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 195 | 55 | flock( $lock_fh, LOCK_EX ); | |||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||||||
| 198 | 246 | ▶ | open $lock_fh, '>', $lockfile or die "$@ $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | 238 | flock( $lock_fh, LOCK_EX ); | |||||||||||||||||||||||||||||||
| 200 | 238 | $lock_fh->autoflush(1); | |||||||||||||||||||||||||||||||
| 201 | 238 | print $lock_fh "LOCK\n"; | |||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | 238 | my $vers_file = "$dir/VERSION"; | |||||||||||||||||||||||||||||||
| 204 | 238 | ▶ | if( -e $vers_file ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 205 | 8 | die "Aborting open : lock file did not exist but version file did. This may mean a partial store was in here at some point in $dir."; | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 230 | _make_path( $silo_dir, 'silo' ); | |||||||||||||||||||||||||||||||
| 209 | 230 | _make_path( $lock_dir, 'lock' ); | |||||||||||||||||||||||||||||||
| 210 | 230 | _make_path( $trans_dir, 'transaction' ); | |||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 230 | open my $out, '>', "$dir/config.yaml"; | |||||||||||||||||||||||||||||||
| 213 | 230 | print $out <<"END"; | |||||||||||||||||||||||||||||||
| 214 | VERSION: $VERSION | ||||||||||||||||||||||||||||||||
| 215 | MAX_FILE_SIZE: $max_file_size | ||||||||||||||||||||||||||||||||
| 216 | MIN_FILE_SIZE: $min_file_size | ||||||||||||||||||||||||||||||||
| 217 | END | ||||||||||||||||||||||||||||||||
| 218 | 230 | close $out; | |||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 230 | open my $vers_fh, '>', $vers_file; | |||||||||||||||||||||||||||||||
| 221 | 230 | print $vers_fh "$VERSION\n"; | |||||||||||||||||||||||||||||||
| 222 | 230 | close $vers_fh; | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | 285 | my $index_silo = Data::RecordStore::Silo->open_silo( "$dir/index_silo", | |||||||||||||||||||||||||||||||
| 226 | "ILL", #silo id, id in silo, last updated time | ||||||||||||||||||||||||||||||||
| 227 | 0, | ||||||||||||||||||||||||||||||||
| 228 | $max_file_size ); | ||||||||||||||||||||||||||||||||
| 229 | 285 | my $transaction_index_silo = Data::RecordStore::Silo->open_silo( "$dir/transaction_index_silo", | |||||||||||||||||||||||||||||||
| 230 | "IL", #state, time | ||||||||||||||||||||||||||||||||
| 231 | 0, | ||||||||||||||||||||||||||||||||
| 232 | $max_file_size ); | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | 285 | my $silos = []; | |||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | 285 | for my $silo_id ($min_silo_id..$max_silo_id) { | |||||||||||||||||||||||||||||||
| 237 | 4836 | $silos->[$silo_id] = Data::RecordStore::Silo->open_silo( "$silo_dir/$silo_id", | |||||||||||||||||||||||||||||||
| 238 | 'ILLa*', # status, id, data-length, data | ||||||||||||||||||||||||||||||||
| 239 | 2 ** $silo_id, | ||||||||||||||||||||||||||||||||
| 240 | $max_file_size ); | ||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||
| 242 | 285 | my $header = pack( 'ILL', 1,2,3 ); | |||||||||||||||||||||||||||||||
| 243 | 9 | ▶ | my $header_size = do { use bytes; length( $header ) }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | 285 | my $store = bless [ | |||||||||||||||||||||||||||||||
| 246 | $dir, | ||||||||||||||||||||||||||||||||
| 247 | "$dir/ACTIVE_TRANS", | ||||||||||||||||||||||||||||||||
| 248 | $max_file_size, | ||||||||||||||||||||||||||||||||
| 249 | $min_silo_id, | ||||||||||||||||||||||||||||||||
| 250 | $index_silo, | ||||||||||||||||||||||||||||||||
| 251 | $silos, | ||||||||||||||||||||||||||||||||
| 252 | $transaction_index_silo, | ||||||||||||||||||||||||||||||||
| 253 | undef, | ||||||||||||||||||||||||||||||||
| 254 | $header_size, # the ILL from ILLa* | ||||||||||||||||||||||||||||||||
| 255 | $lock_fh, | ||||||||||||||||||||||||||||||||
| 256 | [], | ||||||||||||||||||||||||||||||||
| 257 | $max_silo_id, | ||||||||||||||||||||||||||||||||
| 258 | ], $cls; | ||||||||||||||||||||||||||||||||
| 259 | 285 | $store->_fix_transactions; | |||||||||||||||||||||||||||||||
| 260 | 285 | flock( $lock_fh, LOCK_UN ); | |||||||||||||||||||||||||||||||
| 261 | 285 | return $store; | |||||||||||||||||||||||||||||||
| 262 | } #open_store | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | 803 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 265 | 803 | my( $self, $id, $no_trans ) = @_; | |||||||||||||||||||||||||||||||
| 266 | 803 | my $trans = $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 267 | 803 | ▶ | if( $trans && ! $no_trans ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$trans and not $no_trans
MC/DC100%2/2
$trans and not $no_trans
$trans
not $no_trans
Truth table100%3/3
A and not(B)
A= $transB= not $no_trans
| |||||||||||||||||||||||||||||||||
| 268 | 169 | ▶ | if( $trans->{state} != TR_ACTIVE ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 269 | 16 | die "Transaction is in a bad state. Cannot fetch"; | |||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||
| 271 | 153 | return $trans->fetch( $id ); | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | 634 | $self->_read_lock; | |||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | 634 | ▶ | if( $id > $self->entry_count ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | 1 | return undef; | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | 633 | my( $silo_id, $id_in_silo ) = @{$self->[INDEX_SILO]->get_record($id)}; | |||||||||||||||||||||||||||||||
| 281 | 633 | ▶ | if( $silo_id ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 282 | 536 | my $ret = $self->[SILOS]->[$silo_id]->get_record( $id_in_silo ); | |||||||||||||||||||||||||||||||
| 283 | 536 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 284 | 536 | return substr( $ret->[3], 0, $ret->[2] ); | |||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | 97 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 288 | 97 | return undef; | |||||||||||||||||||||||||||||||
| 289 | } #fetch | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 824 | ▶ | sub stow { | ||||||||||||||||||||||||||||||
|
Subroutine
stow: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 292 | 824 | my $self = $_[0]; | |||||||||||||||||||||||||||||||
| 293 | 824 | my $id = $_[2]; | |||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 824 | my $trans = $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 296 | 824 | ▶ | if( $trans ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 297 | 263 | return $trans->stow( $_[1], $id ); | |||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | 561 | my $index = $self->[INDEX_SILO]; | |||||||||||||||||||||||||||||||
| 301 | 561 | $self->_write_lock; | |||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | 561 | $index->ensure_entry_count( $id ); | |||||||||||||||||||||||||||||||
| 304 | 561 | ▶ | if( defined $id && $id < 1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $id and $id < 1
MC/DC100%2/2
defined $id and $id < 1
defined $id
$id < 1
Truth table100%3/3
A and B
A= defined $idB= $id < 1
| |||||||||||||||||||||||||||||||||
| 305 | 2 | die "The id must be a supplied as a positive integer"; | |||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||
| 307 | 559 | my( $old_silo_id, $old_id_in_silo ); | |||||||||||||||||||||||||||||||
| 308 | 559 | ▶ | if( $id > 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 309 | 49 | ( $old_silo_id, $old_id_in_silo ) = @{$index->get_record($id)}; | |||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||
| 311 | else { | ||||||||||||||||||||||||||||||||
| 312 | 510 | $id = $index->next_id; | |||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | 9 | ▶ | my $data_write_size = do { use bytes; length $_[1] }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 316 | 558 | my $new_silo_id = $self->silo_id_for_size( $data_write_size ); | |||||||||||||||||||||||||||||||
| 317 | 558 | my $new_silo = $self->[SILOS][$new_silo_id]; | |||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | 558 | my $new_id_in_silo = $new_silo->push( [RS_ACTIVE, $id, $data_write_size, $_[1]] ); | |||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | 558 | $index->put_record( $id, [$new_silo_id,$new_id_in_silo,time] ); | |||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | 558 | ▶ | if( $old_silo_id ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 324 | 20 | $self->_vacate( $old_silo_id, $old_id_in_silo ); | |||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | 558 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 328 | 558 | return $id; | |||||||||||||||||||||||||||||||
| 329 | } #stow | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | 193 | ▶ | sub next_id { | ||||||||||||||||||||||||||||||
|
Subroutine
next_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 332 | 193 | return shift->[INDEX_SILO]->next_id; | |||||||||||||||||||||||||||||||
| 333 | } #next_id | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 138 | ▶ | sub delete_record { | ||||||||||||||||||||||||||||||
|
Subroutine
delete_record: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 336 | 138 | my( $self, $del_id ) = @_; | |||||||||||||||||||||||||||||||
| 337 | 138 | $self->_write_lock; | |||||||||||||||||||||||||||||||
| 338 | 138 | my $trans = $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 339 | 138 | ▶ | if( $trans ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 340 | 72 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 341 | 72 | return $trans->delete_record( $del_id ); | |||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 66 | ▶ | if( $del_id > $self->[INDEX_SILO]->entry_count ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 345 | 1 | warn "Tried to delete past end of records"; | |||||||||||||||||||||||||||||||
| 346 | 1 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 347 | 1 | return undef; | |||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||
| 349 | 65 | my( $old_silo_id, $old_id_in_silo ) = @{$self->[INDEX_SILO]->get_record($del_id)}; | |||||||||||||||||||||||||||||||
| 350 | 65 | $self->[INDEX_SILO]->put_record( $del_id, [0,0,time] ); | |||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | 65 | ▶ | if( $old_silo_id ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 353 | 57 | $self->_vacate( $old_silo_id, $old_id_in_silo ); | |||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||
| 355 | 65 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 356 | } #delete_record | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | # locks the given lock names | ||||||||||||||||||||||||||||||||
| 359 | # they are locked in order to prevent deadlocks. | ||||||||||||||||||||||||||||||||
| 360 | 82 | ▶ | sub lock { | ||||||||||||||||||||||||||||||
|
Subroutine
lock: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 361 | 82 | my( $self, @locknames ) = @_; | |||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | 82 | my( %previously_locked ) = ( map { $_ => 1 } @{$self->[LOCKS]} ); | |||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | 82 | ▶ | if( @{$self->[LOCKS]} && grep { ! $previously_locked{$_} } @locknames ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@{$self->[10];} and grep {not $previously_locked{$_};} @locknames
MC/DC50%1/2
@{$self->[10];} and grep {not $previously_locked{$_};} @locknames
@{$self->[10];}
grep {not $previously_locked{$_};} @locknames
Truth table66%2/3
A and B
A= @{$self->[10];}B= grep {not $previously_locked{$_};} @locknames
| |||||||||||||||||||||||||||||||||
| 366 | 9 | die "Data::RecordStore->lock cannot be called twice in a row without unlocking between"; | |||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||
| 368 | 73 | my $fhs = []; | |||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||
| 370 | 73 | my $failed; | |||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||
| 372 | 73 | for my $name (sort @locknames) { | |||||||||||||||||||||||||||||||
| 373 | 126 | ▶ | next if $previously_locked{$name}++; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 374 | 117 | my $lockfile = "$self->[DIRECTORY]/user_locks/$name"; | |||||||||||||||||||||||||||||||
| 375 | 117 | my $fh; | |||||||||||||||||||||||||||||||
| 376 | 117 | ▶ | if( -e $lockfile ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 377 | 51 | ▶ | unless( open ( $fh, '+<', $lockfile ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 378 | 24 | $failed = 1; | |||||||||||||||||||||||||||||||
| 379 | 24 | last; | |||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||
| 381 | 27 | flock( $fh, LOCK_EX ); #WRITE LOCK | |||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||
| 383 | else { | ||||||||||||||||||||||||||||||||
| 384 | 66 | ▶ | unless( open( $fh, '>', $lockfile ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 385 | 8 | $failed = 1; | |||||||||||||||||||||||||||||||
| 386 | 8 | last; | |||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||
| 388 | 58 | flock( $fh, LOCK_EX ); #WRITE LOCK | |||||||||||||||||||||||||||||||
| 389 | 58 | $fh->autoflush(1); | |||||||||||||||||||||||||||||||
| 390 | 58 | print $fh ''; | |||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||
| 392 | 85 | push @$fhs, $fh; | |||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | 73 | ▶ | if( $failed ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 396 | # it must be able to lock all the locks or it fails | ||||||||||||||||||||||||||||||||
| 397 | # if it failed, unlock any locks it managed to get | ||||||||||||||||||||||||||||||||
| 398 | 32 | for my $fh (@$fhs) { | |||||||||||||||||||||||||||||||
| 399 | 8 | flock( $fh, LOCK_UN ); | |||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||
| 401 | 32 | die "Data::RecordStore->lock : lock failed"; | |||||||||||||||||||||||||||||||
| 402 | } else { | ||||||||||||||||||||||||||||||||
| 403 | 41 | $self->[LOCKS] = $fhs; | |||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||
| 406 | } #lock | ||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | # unlocks all locks | ||||||||||||||||||||||||||||||||
| 409 | 43 | ▶ | sub unlock { | ||||||||||||||||||||||||||||||
|
Subroutine
unlock: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 410 | 43 | my $self = shift; | |||||||||||||||||||||||||||||||
| 411 | 43 | my $fhs = $self->[LOCKS]; | |||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | 43 | for my $fh (@$fhs) { | |||||||||||||||||||||||||||||||
| 414 | 76 | flock( $fh, LOCK_UN ); | |||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||
| 416 | 43 | @$fhs = (); | |||||||||||||||||||||||||||||||
| 417 | } #unlock | ||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | 110 | ▶ | sub use_transaction { | ||||||||||||||||||||||||||||||
|
Subroutine
use_transaction: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 420 | 110 | my $self = shift; | |||||||||||||||||||||||||||||||
| 421 | 110 | ▶ | if( $self->[TRANSACTION] ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 422 | 24 | warn __PACKAGE__."->use_transaction : already in transaction"; | |||||||||||||||||||||||||||||||
| 423 | 24 | return $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||
| 425 | 86 | $self->_write_lock; | |||||||||||||||||||||||||||||||
| 426 | 86 | my $tid = $self->[TRANSACTION_INDEX_SILO]->push( [TR_ACTIVE, time] ); | |||||||||||||||||||||||||||||||
| 427 | 86 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 428 | 86 | my $tdir = "$self->[DIRECTORY]/transactions/$tid"; | |||||||||||||||||||||||||||||||
| 429 | 86 | make_path( $tdir, { error => \my $err } ); | |||||||||||||||||||||||||||||||
| 430 | 86 | ▶ | if( @$err ) { die join( ", ", map { values %$_ } @$err ) } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||
| 432 | 78 | $self->[TRANSACTION] = Data::RecordStore::Transaction->create( $self, $tdir, $tid ); | |||||||||||||||||||||||||||||||
| 433 | 70 | return $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 434 | } #use_transaction | ||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||
| 436 | 54 | ▶ | sub commit_transaction { | ||||||||||||||||||||||||||||||
|
Subroutine
commit_transaction: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 437 | 54 | my $self = shift; | |||||||||||||||||||||||||||||||
| 438 | 54 | $self->_write_lock; | |||||||||||||||||||||||||||||||
| 439 | 54 | my $trans = $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 440 | 54 | ▶ | unless( $trans ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 441 | 8 | die __PACKAGE__."->commit_transaction : no transaction to commit"; | |||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||
| 443 | 46 | my $trans_file = $self->[ACTIVE_TRANS_FILE]; | |||||||||||||||||||||||||||||||
| 444 | 46 | open my $trans_fh, '>', $trans_file; | |||||||||||||||||||||||||||||||
| 445 | 46 | print $trans_fh " "; | |||||||||||||||||||||||||||||||
| 446 | 46 | close $trans_fh; | |||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||
| 448 | 46 | $trans->commit; | |||||||||||||||||||||||||||||||
| 449 | 30 | delete $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 450 | 30 | unlink $trans_file; | |||||||||||||||||||||||||||||||
| 451 | 30 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 452 | } #commit_transaction | ||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||
| 454 | 32 | ▶ | sub rollback_transaction { | ||||||||||||||||||||||||||||||
|
Subroutine
rollback_transaction: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 455 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||
| 456 | 32 | $self->_write_lock; | |||||||||||||||||||||||||||||||
| 457 | 32 | my $trans = $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 458 | 32 | ▶ | unless( $trans ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 459 | 8 | die __PACKAGE__."->rollback_transaction : no transaction to roll back"; | |||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||
| 461 | 24 | my $trans_file = $self->[ACTIVE_TRANS_FILE]; | |||||||||||||||||||||||||||||||
| 462 | 24 | open my $trans_fh, '>', $trans_file; | |||||||||||||||||||||||||||||||
| 463 | 24 | print $trans_fh " "; | |||||||||||||||||||||||||||||||
| 464 | 24 | close $trans_fh; | |||||||||||||||||||||||||||||||
| 465 | 24 | $trans->rollback; | |||||||||||||||||||||||||||||||
| 466 | 16 | delete $self->[TRANSACTION]; | |||||||||||||||||||||||||||||||
| 467 | 16 | unlink $trans_file; | |||||||||||||||||||||||||||||||
| 468 | 16 | $self->_unlock; | |||||||||||||||||||||||||||||||
| 469 | } #rollback_transaction | ||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||
| 471 | 828 | ▶ | sub entry_count { | ||||||||||||||||||||||||||||||
|
Subroutine
entry_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 472 | 828 | return shift->[INDEX_SILO]->entry_count; | |||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | 519 | ▶ | sub index_silo { | ||||||||||||||||||||||||||||||
|
Subroutine
index_silo: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 476 | 519 | return shift->[INDEX_SILO]; | |||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||
| 479 | 86 | ▶ | sub max_file_size { | ||||||||||||||||||||||||||||||
|
Subroutine
max_file_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 480 | 86 | return shift->[MAX_FILE_SIZE]; | |||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||
| 483 | 574 | ▶ | sub silos { | ||||||||||||||||||||||||||||||
|
Subroutine
silos: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 484 | 574 | return [@{shift->[SILOS]}]; | |||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||
| 487 | 2026 | ▶ | sub transaction_silo { | ||||||||||||||||||||||||||||||
|
Subroutine
transaction_silo: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 488 | 2026 | return shift->[TRANSACTION_INDEX_SILO]; | |||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||
| 491 | 112 | ▶ | sub silos_entry_count { | ||||||||||||||||||||||||||||||
|
Subroutine
silos_entry_count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 492 | 112 | my $self = shift; | |||||||||||||||||||||||||||||||
| 493 | 112 | my $silos = $self->silos; | |||||||||||||||||||||||||||||||
| 494 | 112 | my $count = 0; | |||||||||||||||||||||||||||||||
| 495 | 112 | for my $silo (grep {defined} @$silos) { | |||||||||||||||||||||||||||||||
| 496 | 2240 | $count += $silo->entry_count; | |||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||
| 498 | 112 | return $count; | |||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||
| 501 | 8 | ▶ | sub record_count { | ||||||||||||||||||||||||||||||
|
Subroutine
record_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 502 | 8 | goto &active_entry_count; | |||||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||
| 505 | 64 | ▶ | sub active_entry_count { | ||||||||||||||||||||||||||||||
|
Subroutine
active_entry_count: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 506 | 64 | my $self = shift; | |||||||||||||||||||||||||||||||
| 507 | 64 | my $index = $self->index_silo; | |||||||||||||||||||||||||||||||
| 508 | 64 | my $count = 0; | |||||||||||||||||||||||||||||||
| 509 | 64 | for(1..$self->entry_count) { | |||||||||||||||||||||||||||||||
| 510 | 312 | my( $silo_id ) = @{$index->get_record( $_ )}; | |||||||||||||||||||||||||||||||
| 511 | 312 | ▶ | ++$count if $silo_id; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||
| 513 | 64 | return $count; | |||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||
| 516 | 15 | ▶ | sub detect_version { | ||||||||||||||||||||||||||||||
|
Subroutine
detect_version: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 517 | 15 | my( $cls, $dir ) = @_; | |||||||||||||||||||||||||||||||
| 518 | 15 | my $ver_file = "$dir/VERSION"; | |||||||||||||||||||||||||||||||
| 519 | 15 | my $source_version; | |||||||||||||||||||||||||||||||
| 520 | 15 | ▶ | if ( -e $ver_file ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 521 | 13 | open( my $FH, "<", $ver_file ); | |||||||||||||||||||||||||||||||
| 522 | 13 | $source_version = <$FH>; | |||||||||||||||||||||||||||||||
| 523 | 13 | chomp $source_version; | |||||||||||||||||||||||||||||||
| 524 | 13 | close $FH; | |||||||||||||||||||||||||||||||
| 525 | } elsif( -e "$dir/STORE_INDEX" ) { | ||||||||||||||||||||||||||||||||
| 526 | 1 | return 1; | |||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||
| 528 | 14 | return $source_version; | |||||||||||||||||||||||||||||||
| 529 | } #detect_version | ||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||
| 533 | 205 | ▶ | sub _vacate { | ||||||||||||||||||||||||||||||
|
Subroutine
_vacate: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 534 | 205 | my( $self, $silo_id, $id_to_empty ) = @_; | |||||||||||||||||||||||||||||||
| 535 | 205 | my $silo = $self->[SILOS][$silo_id]; | |||||||||||||||||||||||||||||||
| 536 | 205 | my $rc = $silo->entry_count; | |||||||||||||||||||||||||||||||
| 537 | 205 | ▶ | if( $id_to_empty == $rc ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 538 | 118 | $silo->pop; | |||||||||||||||||||||||||||||||
| 539 | } else { | ||||||||||||||||||||||||||||||||
| 540 | 87 | ▶ | while( $rc > $id_to_empty ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 541 | 63 | my( $state, $id ) = (@{$silo->get_record( $rc, 'IL' )}); | |||||||||||||||||||||||||||||||
| 542 | 63 | ▶ | if( $state == RS_ACTIVE ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 543 | 47 | $silo->copy_record($rc,$id_to_empty); | |||||||||||||||||||||||||||||||
| 544 | 47 | $self->[INDEX_SILO]->put_record( $id, [$silo_id,$id_to_empty], "IL" ); | |||||||||||||||||||||||||||||||
| 545 | 47 | $silo->pop; | |||||||||||||||||||||||||||||||
| 546 | 47 | return; | |||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||
| 548 | elsif( $state == RS_DEAD ) { | ||||||||||||||||||||||||||||||||
| 549 | 8 | $silo->pop; | |||||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||||||
| 551 | else { | ||||||||||||||||||||||||||||||||
| 552 | 8 | return; | |||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||
| 554 | 8 | $rc--; | |||||||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||
| 557 | } #_vacate | ||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||
| 559 | 821 | ▶ | sub silo_id_for_size { | ||||||||||||||||||||||||||||||
|
Subroutine
silo_id_for_size: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 560 | 821 | my( $self, $data_write_size ) = @_; | |||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||
| 562 | 821 | my $write_size = $self->[HEADER_SIZE] + $data_write_size; | |||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||
| 564 | 821 | my $silo_id = int( log( $write_size ) / log( 2 ) ); | |||||||||||||||||||||||||||||||
| 565 | 821 | ▶ | $silo_id++ if 2 ** $silo_id < $write_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 566 | 821 | ▶ | $silo_id = $self->[MIN_SILO_ID] if $silo_id < $self->[MIN_SILO_ID]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 567 | 821 | return $silo_id; | |||||||||||||||||||||||||||||||
| 568 | } #silo_id_for_size | ||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||
| 570 | # ---------------------- private stuffs ------------------------- | ||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||
| 572 | 712 | ▶ | sub _make_path { | ||||||||||||||||||||||||||||||
|
Subroutine
_make_path: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 573 | 712 | my( $dir, $msg ) = @_; | |||||||||||||||||||||||||||||||
| 574 | 712 | make_path( $dir, { error => \my $err } ); | |||||||||||||||||||||||||||||||
| 575 | 712 | ▶ | if( @$err ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 576 | 8 | die "unable to make $msg directory.". join( ", ", map { $_->{$dir} } @$err ); | |||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||
| 580 | 634 | ▶ | sub _read_lock { | ||||||||||||||||||||||||||||||
|
Subroutine
_read_lock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 581 | 634 | my $self = shift; | |||||||||||||||||||||||||||||||
| 582 | 634 | flock( $self->[LOCK_FH], LOCK_SH ); | |||||||||||||||||||||||||||||||
| 583 | 634 | $self->_fix_transactions; | |||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||
| 586 | 1485 | ▶ | sub _unlock { | ||||||||||||||||||||||||||||||
|
Subroutine
_unlock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 587 | 1485 | my( $self ) = @_; | |||||||||||||||||||||||||||||||
| 588 | 1485 | flock( $self->[LOCK_FH], LOCK_UN ); | |||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||
| 591 | 871 | ▶ | sub _write_lock { | ||||||||||||||||||||||||||||||
|
Subroutine
_write_lock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 592 | 871 | my $self = shift; | |||||||||||||||||||||||||||||||
| 593 | 871 | flock( $self->[LOCK_FH], LOCK_EX ); | |||||||||||||||||||||||||||||||
| 594 | 871 | $self->_fix_transactions; | |||||||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||
| 597 | 1822 | ▶ | sub _fix_transactions { | ||||||||||||||||||||||||||||||
|
Subroutine
_fix_transactions: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 598 | 1822 | my $self = shift; | |||||||||||||||||||||||||||||||
| 599 | # check the transactions | ||||||||||||||||||||||||||||||||
| 600 | # if the transaction is in an incomplete state, fix it. Since the store is write locked | ||||||||||||||||||||||||||||||||
| 601 | # during transactions, the lock has expired if this point has been reached. | ||||||||||||||||||||||||||||||||
| 602 | # that means the process that made the lock has fallen. | ||||||||||||||||||||||||||||||||
| 603 | # | ||||||||||||||||||||||||||||||||
| 604 | # of course, do a little experiment to test this with two processes and flock when | ||||||||||||||||||||||||||||||||
| 605 | # one exits before unflocking. | ||||||||||||||||||||||||||||||||
| 606 | # | ||||||||||||||||||||||||||||||||
| 607 | 1822 | my $transaction_index_silo = $self->transaction_silo; | |||||||||||||||||||||||||||||||
| 608 | 1822 | my $last_trans = $transaction_index_silo->entry_count; | |||||||||||||||||||||||||||||||
| 609 | 1822 | ▶ | while( $last_trans ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 610 | 333 | my( $state ) = @{$transaction_index_silo->get_record( $last_trans )}; | |||||||||||||||||||||||||||||||
| 611 | 333 | my $tdir = "$self->[DIRECTORY]/transactions/$last_trans"; | |||||||||||||||||||||||||||||||
| 612 | 333 | ▶ | if( $state == TR_IN_ROLLBACK || | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$state == 3 or $state == 2
MC/DC100%2/2
$state == 3 or $state == 2
$state == 3
$state == 2
Truth table100%3/3
A or B
A= $state == 3B= $state == 2
| |||||||||||||||||||||||||||||||||
| 613 | $state == TR_IN_COMMIT ) { | ||||||||||||||||||||||||||||||||
| 614 | # do a full rollback | ||||||||||||||||||||||||||||||||
| 615 | # load the transaction | ||||||||||||||||||||||||||||||||
| 616 | 16 | my $trans = Data::RecordStore::Transaction->create( $self, $tdir, $last_trans ); | |||||||||||||||||||||||||||||||
| 617 | 16 | $trans->rollback; | |||||||||||||||||||||||||||||||
| 618 | 16 | $transaction_index_silo->pop; | |||||||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||||||
| 620 | elsif( $state == TR_COMPLETE ) { | ||||||||||||||||||||||||||||||||
| 621 | 43 | $transaction_index_silo->pop; | |||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||
| 623 | else { | ||||||||||||||||||||||||||||||||
| 624 | 274 | return; | |||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||
| 626 | 59 | $last_trans--; | |||||||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||
| 629 | } #_fix_transactions | ||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||
| 632 | 9 | "I became Iggy because I had a sadistic boss at a record store. I'd been in a band called the Iguanas. And when this boss wanted to embarrass and demean me, he'd say, 'Iggy, get me a coffee, light.' - Iggy Pop"; | |||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||
| 634 | __END__ | ||||||||||||||||||||||||||||||||