PObject/Driver/file.pm

filter: Statementstmt 73.9% 119 / 161 Branchbran 61.7% 42 / 68 Conditioncond 57.1% 16 / 28 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 100% 14 / 14 Podpod 83.3% 5 / 6 totaltotal 67.5% 198 / 293 CC 45 SCAR 25.6
CC
45
Cov
69%
load_ids
23.6
save
14.7
generate_id
14.1
CRAP 12.9
« PObject/Driver/DBM.pm ↑ Summary PObject/Driver/csv.pm »
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
l!l&&r!l&&!r
1100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
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
BranchTrueFalseTotal
$columns->{"id"} ||= $self->generate_id($object_name, $props) or return 11 0 11
Condition66%2/3
$columns->{"id"} ||= $self->generate_id($object_name, $props)
l!l&&r!l&&!r
470
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)
ABresult
1X1
011
000
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
BranchTrueFalseTotal
my $filename = $self->_filename($object_name, $props, $columns->{"id"}) or return 11 0 11
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
BranchTrueFalseTotal
unless (sysopen FH, $filename, 577, 438) 0 11 11
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
BranchTrueFalseTotal
unless (flock FH, 2) 0 11 11
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
BranchTrueFalseTotal
unless (close FH) 0 11 11
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
l!l&&r!l&&!r
4300
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
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 ||= {}
l!l
2320
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
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
BranchTrueFalseTotal
my $object_dir = $self->_dir($object_name, $props) or return 43 0 43
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
BranchTrueFalseTotal
unless (tie %files, "IO::Dir", $object_dir) 0 43 43
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
BranchTrueFalseTotal
if (() = each %files) 222 43 265
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
BranchTrueFalseTotal
if (defined $args->{"limit"} and not $args->{"offset"} and not $args->{"sort"} and $n == $args->{"limit"}) 0 222 222
Condition66%6/9
defined $args->{"limit"} and not $args->{"offset"} and not $args->{"sort"} and $n == $args->{"limit"}
!ll&&!rl&&r
22200
defined $args->{"limit"} and not $args->{"offset"} and not $args->{"sort"}
!ll&&!rl&&r
184380
defined $args->{"limit"} and not $args->{"offset"}
!ll&&!rl&&r
1721238
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"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
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
BranchTrueFalseTotal
if (S_ISDIR($stat->mode)) 86 136 222
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
BranchTrueFalseTotal
unless ($filename =~ /^$filef_pattern$/) 43 93 136
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
BranchTrueFalseTotal
unless (sysopen FH, "File::Spec"->catfile($object_dir, $filename), 0) 0 93 93
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
BranchTrueFalseTotal
unless (flock FH, 1) 0 93 93
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
BranchTrueFalseTotal
unless (defined $datastr) 0 93 93
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
BranchTrueFalseTotal
if ($self->_matches_terms($data, $terms)) 69 24 93
127 69 ▶ push @data_set, keys %$args ? $data : $data->{id};
Branch: 1 branch
BranchTrueFalseTotal
keys %$args ? : 40 29 69
128 69 $n++
129 }
130 }
131 43 untie(%files);
132  
133 # returning post-processed data set
134 43 ▶ unless ( keys %$args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$args) 26 17 43
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
l!l&&r!l&&!r
5000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
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
BranchTrueFalseTotal
my $filename = $self->_filename($object_name, $props, $id) or return 50 0 50
165  
166 # open that file
167 50 ▶ unless ( sysopen(FH, $filename, O_RDONLY) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen FH, $filename, 0) 1 49 50
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
BranchTrueFalseTotal
unless (flock FH, 1) 0 49 49
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
BranchTrueFalseTotal
unless ($data_str) 0 49 49
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
BranchTrueFalseTotal
unless (unlink $filename) 0 6 6
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
BranchTrueFalseTotal
unless (File::Path::rmtree($dir)) 0 5 5
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
BranchTrueFalseTotal
my $dir = $self->_dir($object_name, $props) or return 7 0 7
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
BranchTrueFalseTotal
unless (sysopen FH, $filename, 66) 0 7 7
236 0 $self->errstr("couldn't open/create '$filename': $!");
237 return undef
238 0 }
239 7 ▶ unless (flock(FH, LOCK_EX) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock FH, 2) 0 7 7
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
l!l
25
MC/DC100%1/1
readline FH || 0
readline FH
Truth table100%2/2
A
A= readline FH
Aresult
11
00
244 7 ▶ unless (seek(FH, 0, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (seek FH, 0, 0) 0 7 7
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
BranchTrueFalseTotal
unless (truncate FH, 0) 0 7 7
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
BranchTrueFalseTotal
unless (close FH) 0 7 7
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
BranchTrueFalseTotal
unless ($object_name and defined $id) 0 67 67
Condition33%1/3
$object_name and defined $id
!ll&&!rl&&r
0067
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
ABresult
0X0
100
111
267 0 logcroak "Usage: _filename(\$id)";
268 }
269 67 ▶ my $dir = $self->_dir($object_name, $props) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $self->_dir($object_name, $props) or return 67 0 67
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
BranchTrueFalseTotal
unless (defined $dir) 0 122 122
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
BranchTrueFalseTotal
unless (-e $object_dir) 5 117 122
295 5 require File::Path;
296 5 ▶ unless (File::Path::mkpath($object_dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Path::mkpath($object_dir)) 0 5 5
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__;
« PObject/Driver/DBM.pm ↑ Summary PObject/Driver/csv.pm »