| 1 | package Class::PObject::Driver::file; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # file.pm,v 1.18 2003/09/09 08:46:36 sherzodr Exp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | #use diagnostics; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Log::Agent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Class::PObject::Driver; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use vars ('$f', '$VERSION', '@ISA'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Fcntl (':DEFAULT', ':flock', ':mode'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | @ISA = ("Class::PObject::Driver"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | $VERSION = '2.00'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | $f = 'obj%05d.cpo'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # called when pobject's save() method is called. Note: this is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # the same as save() method as the one called by pobject. This is different! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 11 | ▶ | sub save { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=8 SCAR=26.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 11 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 11 | my ($object_name, $props, $columns) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 11 | logtrc 3, "%s->save()", $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # if 'id' does not already exist, we're being asked to save a newly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # created object. Before we do that, we create a new id for the object: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 11 | ▶ | $columns->{id} ||= $self->generate_id($object_name, $props) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$columns->{"id"} ||= $self->generate_id($object_name, $props)
MC/DC0%0/2
$columns->{"id"} ||= $self->generate_id($object_name, $props)
$columns->{"id"}
$self->generate_id($object_name, $props)
Truth table66%2/3
A ||= B
A= $columns->{"id"}B= $self->generate_id($object_name, $props)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # _filename() returns the name of the file this particular object should | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # be stored in. Look into _filename() for details | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 11 | ▶ | my $filename = $self->_filename($object_name, $props, $columns->{id}) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # if we can't open the file, we set error message, and return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | ▶ | unless ( sysopen(FH, $filename, O_WRONLY|O_CREAT|O_TRUNC, 0666) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | $self->errstr("couldn't open '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | logerr $self->errstr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # we do the same if we can't get exclusive lock on the file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 11 | ▶ | unless (flock(FH, LOCK_EX) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $self->errstr("couldn't lock '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | logerr $self->errstr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # and store frozen data into file: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 11 | print FH $self->freeze($object_name, $props, $columns); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # if we can't close the file handle, it means we couldn't store it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 11 | ▶ | unless( close(FH) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $self->errstr("couldn't save the object: $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | logerr $self->errstr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # if everything went swell, we should return object id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | return $columns->{id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 11 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 43 | ▶ | sub load_ids { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_ids: called CC=18 SCAR=31.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 43 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 43 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 43 | my ($object_name, $props, $terms, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 43 | logtrc 3, "%s->load_ids(@_)", $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # if we come this far, we're being asked to return either all the objects, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # or by some criteria | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 43 | my @data_set = ( ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 43 | ▶ | $args ||= { }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args ||= {}
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # to do it, we need to figure out which directory the objects of this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # type are most likely to be stored. For details look into '_dir()' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 43 | ▶ | my $object_dir = $self->_dir($object_name, $props) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # and iterate through each object file. For some reason I prefer using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # IO::Dir for retrieving objects, seems 'cleaner' this way | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 43 | require IO::Dir; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 43 | my %files = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 43 | ▶ | unless(tie %files, "IO::Dir", $object_dir) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $self->errstr("couldn't open '$object_dir': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | logerr $self->errstr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 43 | my $n = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 43 | ▶ | while ( my ($filename, $stat) = each %files ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # if 'limit' was given, and 'offset' is missing and sort is not given, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # then check we have already reached our 'limit'. Otherwise, we need to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # load all the objects to the memory before we can return the data set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 222 | ▶ | if ( defined($args->{limit}) && (!$args->{offset}) && (!$args->{'sort'}) && ($n == $args->{limit}) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
defined $args->{"limit"} and not $args->{"offset"} and not $args->{"sort"} and $n == $args->{"limit"}
defined $args->{"limit"} and not $args->{"offset"} and not $args->{"sort"}
defined $args->{"limit"} and not $args->{"offset"}
MC/DC0%0/4
defined $args->{"limit"} and not $args->{"offset"} and not $args->{"sort"} and $n == $args->{"limit"}
defined $args->{"limit"}
not $args->{"offset"}
not $args->{"sort"}
$n == $args->{"limit"}
Truth table60%3/5
A and not(B) and not(C) and D
A= defined $args->{"limit"}B= not $args->{"offset"}C= not $args->{"sort"}D= $n == $args->{"limit"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # if it is a directory, then skip to the next file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 222 | ▶ | if ( S_ISDIR($stat->mode) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 86 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # defining a regex pattern to check against the filename to determine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # if it can be the file object stored in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 136 | my $filef_pattern = $f; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 136 | $filef_pattern =~ s/\%\d*d/\\d\+/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 136 | $filef_pattern =~ s/\./\\./g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 136 | ▶ | unless ( $filename =~ m/^$filef_pattern$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 43 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # we open the file with read-only flag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 93 | ▶ | unless (sysopen(FH, File::Spec->catfile($object_dir, $filename), O_RDONLY)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $self->errstr("couldn't open '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | logerr $self->errstr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 93 | ▶ | unless(flock(FH, LOCK_SH)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->errstr("couldn't lock '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | logerr $self->errstr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 93 | local $/ = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 93 | my $datastr = <FH>; close(FH); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 93 | ▶ | unless( defined $datastr ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 93 | my $data = $self->thaw($object_name, $props, $datastr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 93 | ▶ | if ( $self->_matches_terms($data, $terms) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 69 | ▶ | push @data_set, keys %$args ? $data : $data->{id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 69 | $n++ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 43 | untie(%files); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # returning post-processed data set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 43 | ▶ | unless ( keys %$args ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | return \@data_set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 26 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 17 | my $data_set = $self->_filter_by_args(\@data_set, $args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 17 | return [ map { $_->{id} } @$data_set ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # load_by_id() is called only while object is to be retrieved by its id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 50 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=6 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 50 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 50 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 50 | my ($object_name, $props, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 50 | logtrc 3, "%s->load(%s)", $class, join ", ", @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # determine the name of the file for this object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 50 | ▶ | my $filename = $self->_filename($object_name, $props, $id) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # open that file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 50 | ▶ | unless ( sysopen(FH, $filename, O_RDONLY) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | $self->errstr("couldn't open '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # lock the file handle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 49 | ▶ | unless(flock(FH, LOCK_SH)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | $self->errstr("couldn't lock '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # undefined record separator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 49 | local $/ = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # slurp the whole file in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 49 | my $data_str = <FH>; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 49 | close(FH); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 49 | ▶ | unless ( $data_str ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | $self->errstr("object is empty"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 49 | return $self->thaw($object_name, $props, $data_str) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 6 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 6 | my ($self, $object_name, $props, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 6 | my $filename = $self->_filename($object_name, $props, $id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 6 | ▶ | unless ( unlink($filename) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | $self->errstr("couldn't unlink '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 6 | return 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 5 | ▶ | sub drop_datasource { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
drop_datasource: called CC=2 SCAR=7.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 5 | my ($self, $object_name, $props, $i) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 5 | my $dir = $self->_dir($object_name, $props); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 5 | logtrc 3, "removing '%s'", $dir; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 5 | require File::Path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 5 | ▶ | unless ( File::Path::rmtree($dir) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $self->errstr( "couldn't unlink '%s': %s", $dir, $!); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 5 | return 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 7 | ▶ | sub generate_id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_id: called CC=8 SCAR=26.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 7 | my ($self, $object_name, $props) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 7 | ▶ | my $dir = $self->_dir($object_name, $props) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 7 | my $filename = File::Spec->catfile($dir, 'counter.cpo'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 7 | ▶ | unless (sysopen(FH, $filename, O_RDWR|O_CREAT)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | $self->errstr("couldn't open/create '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 7 | ▶ | unless (flock(FH, LOCK_EX) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | $self->errstr("couldn't lock '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 7 | ▶ | my $num = <FH> || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
readline FH || 0
MC/DC100%1/1
readline FH || 0
readline FH
Truth table100%2/2
A
A= readline FH
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 7 | ▶ | unless (seek(FH, 0, 0)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | $self->errstr("couldn't seek to the start of '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 7 | ▶ | unless (truncate(FH, 0)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | $self->errstr("couldn't truncate '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 7 | print FH ++$num, "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 7 | ▶ | unless(close(FH)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $self->errstr("couldn't update '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 7 | return $num | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 67 | ▶ | sub _filename { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_filename: called CC=4 SCAR=16.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 67 | my ($self, $object_name, $props, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 67 | ▶ | unless ( $object_name && defined($id) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$object_name and defined $id
MC/DC0%0/2
$object_name and defined $id
$object_name
defined $id
Truth table33%1/3
A and B
A= $object_nameB= defined $id
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | logcroak "Usage: _filename(\$id)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 67 | ▶ | my $dir = $self->_dir($object_name, $props) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 67 | return File::Spec->catfile($dir, sprintf($f, $id)) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 122 | ▶ | sub _dir { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dir: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 122 | my ($self, $object_name, $props) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 122 | my ($object_dir, $object_name_as_str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 122 | my $dir = $props->{datasource}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # if 'datasource' was not specified, we should | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | # create a location for object of this type in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | # system's temp folder: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 122 | ▶ | unless ( defined $dir ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $dir = File::Spec->tmpdir() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | # creating a dirified version of the object name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 122 | $object_name_as_str = $object_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 122 | $object_name_as_str =~ s/\W+/_/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 122 | $object_dir = File::Spec->catfile($dir, $object_name_as_str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # if the directory that we just created doesn't exist, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | # we should create it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 122 | ▶ | unless ( -e $object_dir ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 5 | require File::Path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 5 | ▶ | unless (File::Path::mkpath($object_dir) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | $self->errstr("couldn't create datasource '$object_dir': $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # return the directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 122 | return $object_dir | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | __END__; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||