FS.pm

filter: Statementstmt 100% 113 / 113 Branchbran 83.3% 30 / 36 Conditioncond 63.8% 30 / 47 MC/DCmcdc 37.0% 10 / 27 Subroutinesub 100% 22 / 22 Podpod 100% 8 / 8 totaltotal 84.1% 213 / 253 CC 37 SCAR 14.6
CC
37
Cov
88%
set_inflated_column
7.1
_deflate_fs_column
7.0
DESTROY
6.0
CRAP 4.3
↑ Summary FS/ResultSet.pm »
1 package DBIx::Class::InflateColumn::FS;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use DBIx::Class::UUIDColumns;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Path::Class ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.01007';
12  
13 =head1 NAME
14  
15 DBIx::Class::InflateColumn::FS - Inflate/deflate columns to Path::Class::File objects
16  
17 =head1 SYNOPSIS
18  
19 __PACKAGE__->load_components(qw/InflateColumn::FS Core/);
20 __PACKAGE__->add_columns(
21 id => {
22 data_type => 'INT',
23 is_auto_increment => 1,
24 },
25 file => {
26 data_type => 'TEXT',
27 is_fs_column => 1,
28 fs_column_path => '/var/lib/myapp/myfiles',
29 },
30 file_2 => {
31 data_type => 'TEXT',
32 is_fs_column => 1,
33 fs_column_path => '/var/lib/myapp/myfiles',
34 fs_new_on_update => 1
35 },
36 );
37 __PACKAGE__->set_primary_key('id');
38  
39 # in application code
40 $rs->create({ file => $file_handle });
41  
42 $row = $rs->find({ id => $id });
43 my $fh = $row->file->open('r');
44  
45 =head1 DESCRIPTION
46  
47 Provides inflation to a Path::Class::File object allowing file system storage
48 of BLOBS.
49  
50 The storage path is specified with C<fs_column_path>. Each file receives a
51 unique name, so the storage for all FS columns can share the same path.
52  
53 Within the path specified by C<fs_column_path>, files are stored in
54 sub-directories based on the first 2 characters of the unique file names. Up to
55 256 sub-directories will be created, as needed. Override C<_fs_column_dirs> in
56 a derived class to change this behavior.
57  
58 C<fs_new_on_update> will create a new file name if the file has been updated.
59  
60 =cut
61  
62 =head1 METHODS
63  
64 =cut
65  
66 =head2 inflate_result
67  
68 =cut
69  
70 16 ▶sub inflate_result {
Subroutine inflate_result: called CC=5 SCAR=16.1
Pod documented
71 16 my ($class, $source, $me, $prefetch) = @_;
72  
73 16 my $new = $class->next::method($source, $me, $prefetch);
74
75 16 ▶ while ( my($column, $data) = each %{$new->{_column_data}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$new{"_column_data"};}) 56 16 72
76 56 ▶ if ( $source->has_column($column) && $source->column_info($column)->{is_fs_column} && defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->has_column($column) and $source->column_info($column)->{"is_fs_column"} and defined $data) 10 46 56
Condition100%6/6
$source->has_column($column) and $source->column_info($column)->{"is_fs_column"} and defined $data
!ll&&!rl&&r
361010
$source->has_column($column) and $source->column_info($column)->{"is_fs_column"}
!ll&&!rl&&r
63020
MC/DC100%3/3
$source->has_column($column) and $source->column_info($column)->{"is_fs_column"} and defined $data
$source->has_column($column) $source->column_info($column)->{"is_fs_column"} defined $data
Truth table100%4/4
A and B and C
A= $source->has_column($column)B= $source->column_info($column)->{"is_fs_column"}C= defined $data
ABCresult
0XX0
10X0
1100
1111
77 10 $new->{_fs_column_filename}{$column} = $data;
78 }
79 }
80
81 16 return $new;
82 }
83  
84  
85 =head2 register_column
86  
87 =cut
88  
89 32 ▶sub register_column {
Subroutine register_column: called CC=2 SCAR=6.9
Pod documented
90 32 my ($self, $column, $info, @rest) = @_;
91 32 $self->next::method($column, $info, @rest);
92 32 ▶ return unless defined($info->{is_fs_column});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"is_fs_column"} 20 12 32
93  
94 $self->inflate_column($column => {
95 10 ▶ inflate => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 10 my ($value, $obj) = @_;
97 10 $obj->_inflate_fs_column($column, $value);
98 },
99 33 ▶ deflate => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 33 my ($value, $obj) = @_;
101 33 $obj->_deflate_fs_column($column, $value);
102 },
103 12 });
104 }
105  
106 =head2 fs_file_name
107  
108 Provides the file naming algorithm. Override this method to change it.
109  
110 This method is called with two parameters: The name of the column and the
111 C<< column_info >> object.
112  
113 =cut
114  
115 28 ▶sub fs_file_name {
Subroutine fs_file_name: called CC=1 SCAR=0.0
Pod documented
116 28 my ($self, $column, $column_info) = @_;
117 28 return DBIx::Class::UUIDColumns->get_uuid;
118 }
119  
120 65 ▶sub _fs_column_storage {
Subroutine _fs_column_storage: called CC=3 SCAR=11.1
121 65 my ( $self, $column ) = @_;
122  
123 65 my $column_info = $self->result_source->column_info($column);
124 $self->throw_exception("$column is not an fs_column")
125 65 ▶ unless $column_info->{is_fs_column};
Branch: 1 branch
BranchTrueFalseTotal
unless $column_info->{"is_fs_column"} 0 65 65
126  
127 65 ▶ $self->{_fs_column_filename}{$column} ||= do {
Condition66%2/3
$self->{"_fs_column_filename"}{$column} ||= do { my $filename = $self->fs_file_name($column, $column_info); "File::Spec"->catfile($self->_fs_column_dirs($filename), $filename) }
l!l&&r!l&&!r
37280
MC/DC0%0/2
$self->{"_fs_column_filename"}{$column} ||= do { my $filename = $self->fs_file_name($column, $column_info); "File::Spec"->catfile($self->_fs_column_dirs($filename), $filename) }
$self->{"_fs_column_filename"}{$column} do { my $filename = $self->fs_file_name($column, $column_info); "File::Spec"->catfile($self->_fs_column_dirs($filename), $filename) }
Truth table66%2/3
A ||= B
A= $self->{"_fs_column_filename"}{$column}B= do { my $filename = $self->fs_file_name($column, $column_info); "File::Spec"->catfile($self->_fs_column_dirs($filename), $filename) }
ABresult
1X1
011
000
128 28 my $filename = $self->fs_file_name($column, $column_info);
129 28 File::Spec->catfile($self->_fs_column_dirs($filename), $filename);
130 };
131  
132 65 return Path::Class::File->new($column_info->{fs_column_path}, $self->{_fs_column_filename}{$column});
133 }
134  
135 =head2 _fs_column_dirs
136  
137 Returns the sub-directory components for a given file name. Override it to
138 provide a deeper directory tree or change the algorithm.
139  
140 =cut
141  
142 28 ▶sub _fs_column_dirs {
Subroutine _fs_column_dirs: called CC=1 SCAR=0.0
143 28 shift;
144 28 my $filename = shift;
145  
146 28 return $filename =~ /(..)/;
147 }
148  
149 =head2 copy
150  
151 Copies a row object, duplicating the files backing fs columns.
152  
153 =cut
154  
155 4 ▶sub copy {
Subroutine copy: called CC=5 SCAR=16.1
Pod documented
156 4 my ($self, $changes) = @_;
157  
158 4 ▶ $changes ||= {};
Condition50%1/2
$changes ||= {}
l!l
04
MC/DC0%0/1
$changes ||= {}
$changes
Truth table50%1/2
A
A= $changes
Aresult
11
00
159 4 my $col_data = { %{$self->{_column_data}} };
160  
161 4 foreach my $col ( keys %$col_data ) {
162 20 my $column_info = $self->result_source->column_info($col);
163 20 ▶ if ( $column_info->{is_fs_column} && defined $col_data->{$col} ) { # nothing special required for NULLs
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"is_fs_column"} and defined $col_data->{$col}) 4 16 20
Condition100%3/3
$column_info->{"is_fs_column"} and defined $col_data->{$col}
!ll&&!rl&&r
1244
MC/DC100%2/2
$column_info->{"is_fs_column"} and defined $col_data->{$col}
$column_info->{"is_fs_column"} defined $col_data->{$col}
Truth table100%3/3
A and B
A= $column_info->{"is_fs_column"}B= defined $col_data->{$col}
ABresult
0X0
100
111
164 4 delete $col_data->{$col};
165
166 # pass the original file to produce a copy on deflate
167 4 $changes->{$col} = $self->get_inflated_column($col);
168 }
169 }
170  
171 4 my $temp = bless { _column_data => $col_data }, ref $self;
172 4 $temp->result_source($self->result_source);
173  
174 4 return $temp->next::method($changes);
175 }
176  
177 =head2 delete
178  
179 Deletes the associated file system storage when a row is deleted.
180  
181 =cut
182  
183 6 ▶sub delete {
Subroutine delete: called CC=5 SCAR=16.2
Pod documented
184 6 my ( $self, @rest ) = @_;
185  
186 6 for my $column ( $self->columns ) {
187 30 my $column_info = $self->result_source->column_info($column);
188 30 ▶ if ( $column_info->{is_fs_column} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"is_fs_column"}) 12 18 30
189 12 ▶ my $accessor = $column_info->{accessor} || $column;
Condition33%1/3
$column_info->{'accessor'} || $column
l!l&&r!l&&!r
0120
MC/DC0%0/2
$column_info->{'accessor'} || $column
$column_info->{'accessor'} $column
Truth table33%1/3
A || B
A= $column_info->{'accessor'}B= $column
ABresult
1X1
011
000
190 12 ▶ $self->$accessor && $self->$accessor->remove;
Branch: 1 branch
BranchTrueFalseTotal
$self->$accessor and $self->$accessor->remove 6 6 12
191 }
192 }
193  
194 6 return $self->next::method(@rest);
195 }
196  
197 =head2 set_column
198  
199 Deletes file storage when an fs_column is set to undef.
200  
201 =cut
202  
203 23 ▶sub set_column {
Subroutine set_column: called CC=4 SCAR=14.0
Pod documented
204 23 my ($self, $column, $new_value) = @_;
205  
206 23 ▶ if ( !defined $new_value && $self->result_source->column_info($column)->{is_fs_column}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $new_value and $self->result_source->column_info($column)->{"is_fs_column"} and $self->{"_fs_column_filename"}{$column}) 2 21 23
Condition66%4/6
not defined $new_value and $self->result_source->column_info($column)->{"is_fs_column"} and $self->{"_fs_column_filename"}{$column}
!ll&&!rl&&r
2102
not defined $new_value and $self->result_source->column_info($column)->{"is_fs_column"}
!ll&&!rl&&r
2102
MC/DC33%1/3
not defined $new_value and $self->result_source->column_info($column)->{"is_fs_column"} and $self->{"_fs_column_filename"}{$column}
not defined $new_value $self->result_source->column_info($column)->{"is_fs_column"} $self->{"_fs_column_filename"}{$column}
Truth table50%2/4
not(A) and B and C
A= not defined $new_valueB= $self->result_source->column_info($column)->{"is_fs_column"}C= $self->{"_fs_column_filename"}{$column}
ABCresult
0XX0
10X0
1100
1111
207 && $self->{_fs_column_filename}{$column} ) {
208 2 $self->_fs_column_storage($column)->remove;
209 2 delete $self->{_fs_column_filename}{$column};
210 }
211  
212 23 return $self->next::method($column, $new_value);
213 }
214  
215 =head2 set_inflated_column
216  
217 Re-inflates after setting an fs_column.
218  
219 =cut
220  
221 17 ▶sub set_inflated_column {
Subroutine set_inflated_column: called CC=5 SCAR=19.6
Pod documented
222 17 my ($self, $column, $inflated) = @_;
223  
224 17 $self->next::method($column, $inflated);
225  
226 # reinflate
227 17 ▶ if ( defined $inflated && ref $inflated && ref $inflated ne 'SCALAR'
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inflated and ref $inflated and ref $inflated ne "SCALAR" and $self->result_source->column_info($column)->{"is_fs_column"}) 17 0 17
Condition33%3/9
defined $inflated and ref $inflated and ref $inflated ne "SCALAR" and $self->result_source->column_info($column)->{"is_fs_column"}
!ll&&!rl&&r
0017
defined $inflated and ref $inflated and ref $inflated ne "SCALAR"
!ll&&!rl&&r
0017
defined $inflated and ref $inflated
!ll&&!rl&&r
0017
MC/DC0%0/4
defined $inflated and ref $inflated and ref $inflated ne "SCALAR" and $self->result_source->column_info($column)->{"is_fs_column"}
defined $inflated ref $inflated ref $inflated ne "SCALAR" $self->result_source->column_info($column)->{"is_fs_column"}
Truth table20%1/5
A and B and C and D
A= defined $inflatedB= ref $inflatedC= ref $inflated ne "SCALAR"D= $self->result_source->column_info($column)->{"is_fs_column"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
228 && $self->result_source->column_info($column)->{is_fs_column} ) {
229 17 $inflated = $self->{_inflated_column}{$column} = $self->_fs_column_storage($column);
230 }
231 17 return $inflated;
232 }
233  
234 =head2 _inflate_fs_column
235  
236 Inflates a file column to a Path::Class::File object.
237  
238 =cut
239  
240 10 ▶sub _inflate_fs_column {
Subroutine _inflate_fs_column: called CC=2 SCAR=7.0
241 10 my ( $self, $column, $value ) = @_;
242 10 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 10 10
243  
244 10 $self->{_fs_column_filename}{$column} = $value;
245 10 return $self->_fs_column_storage($column);
246 }
247  
248 =head2 _deflate_fs_column
249  
250 Deflates a file column to its storage path name, relative to C<fs_column_path>.
251 In the database, a file column is just a place holder for inflation/deflation.
252 The actual file lives in the file system.
253  
254 =cut
255  
256 33 ▶sub _deflate_fs_column {
Subroutine _deflate_fs_column: called CC=7 SCAR=19.5
257 33 my ( $self, $column, $value ) = @_;
258  
259 33 my $column_info = $self->result_source->column_info($column);
260  
261 # kill the old storage, rather than overwrite, if fs_new_on_update
262 33 ▶ if ( $column_info->{fs_new_on_update} && $self->{_fs_column_filename}{$column} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"fs_new_on_update"} and $self->{"_fs_column_filename"}{$column}) 3 30 33
Condition100%3/3
$column_info->{"fs_new_on_update"} and $self->{"_fs_column_filename"}{$column}
!ll&&!rl&&r
2733
MC/DC100%2/2
$column_info->{"fs_new_on_update"} and $self->{"_fs_column_filename"}{$column}
$column_info->{"fs_new_on_update"} $self->{"_fs_column_filename"}{$column}
Truth table100%3/3
A and B
A= $column_info->{"fs_new_on_update"}B= $self->{"_fs_column_filename"}{$column}
ABresult
0X0
100
111
263 3 my $oldfile = $self->_fs_column_storage($column);
264 3 ▶ if ( $oldfile ne $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldfile ne $value) 3 0 3
265 3 $oldfile->remove;
266 3 delete $self->{_fs_column_filename}{$column};
267 }
268 }
269
270 33 my $file = $self->_fs_column_storage($column);
271 33 ▶ if ( $value ne $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne $file) 30 3 33
272 30 File::Path::mkpath([$file->dir]);
273  
274 # get a filehandle if we were passed a Path::Class::File
275 30 ▶ my $fh1 = eval { $value->openr } || $value;
Condition66%2/3
eval { do { $value->openr } } || $value
l!l&&r!l&&!r
2550
MC/DC0%0/2
eval { do { $value->openr } } || $value
eval { do { $value->openr } } $value
Truth table66%2/3
A || B
A= eval { do { $value->openr } }B= $value
ABresult
1X1
011
000
276 30 ▶ my $fh2 = $file->openw or die;
Branch: 1 branch
BranchTrueFalseTotal
my $fh2 = $file->openw or die 30 0 30
277 30 File::Copy::copy($fh1, $fh2);
278  
279 30 $self->{_inflated_column}{$column} = $file;
280  
281 # ensure the column will be marked dirty
282 30 $self->{_column_data}{$column} = undef;
283 }
284 33 return $self->{_fs_column_filename}{$column};
285 }
286  
287 39 ▶sub DESTROY {
Subroutine DESTROY: called CC=6 SCAR=18.0
288 39 my $self = shift;
289  
290 39 ▶ return if $self->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_storage 28 11 39
291  
292 # If fs columns were deflated, but the row was never stored, we need to delete the
293 # backing files.
294 11 ▶ while ( my ( $col, $data ) = each %{ $self->{_column_data} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_column_data"};}) 44 11 55
295 44 my $column_info = $self->result_source->column_info($col);
296 44 ▶ if ( $column_info->{is_fs_column} && defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_info->{"is_fs_column"} and defined $data) 7 37 44
Condition100%3/3
$column_info->{"is_fs_column"} and defined $data
!ll&&!rl&&r
2987
MC/DC100%2/2
$column_info->{"is_fs_column"} and defined $data
$column_info->{"is_fs_column"} defined $data
Truth table100%3/3
A and B
A= $column_info->{"is_fs_column"}B= defined $data
ABresult
0X0
100
111
297 7 ▶ my $accessor = $column_info->{accessor} || $col;
Condition33%1/3
$column_info->{'accessor'} || $col
l!l&&r!l&&!r
070
MC/DC0%0/2
$column_info->{'accessor'} || $col
$column_info->{'accessor'} $col
Truth table33%1/3
A || B
A= $column_info->{'accessor'}B= $col
ABresult
1X1
011
000
298 7 $self->$accessor->remove;
299 }
300 }
301 }
302  
303 =head2 table
304  
305 Overridden to provide a hook for specifying the resultset_class. If
306 you provide your own resultset_class, inherit from
307 InflateColumn::FS::ResultSet.
308  
309 =cut
310  
311 8 ▶sub table {
Subroutine table: called CC=3 SCAR=11.8
Pod documented
312 8 my $self = shift;
313  
314 8 my $ret = $self->next::method(@_);
315 8 ▶ if ( @_ && $self->result_source_instance->resultset_class
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $self->result_source_instance->resultset_class eq "DBIx::Class::ResultSet") 8 0 8
Condition33%1/3
@_ and $self->result_source_instance->resultset_class eq "DBIx::Class::ResultSet"
!ll&&!rl&&r
008
MC/DC0%0/2
@_ and $self->result_source_instance->resultset_class eq "DBIx::Class::ResultSet"
@_ $self->result_source_instance->resultset_class eq "DBIx::Class::ResultSet"
Truth table33%1/3
A and B
A= @_B= $self->result_source_instance->resultset_class eq "DBIx::Class::ResultSet"
ABresult
0X0
100
111
316 eq 'DBIx::Class::ResultSet' ) {
317 8 $self->result_source_instance
318 ->resultset_class('DBIx::Class::InflateColumn::FS::ResultSet');
319 }
320 8 return $ret;
321 }
322  
323 =head1 SUPPORT
324  
325 Community support can be found via:
326  
327 Mailing list: http://lists.scsys.co.uk/mailman/listinfo/dbix-class/
328  
329 IRC: irc.perl.org#dbix-class
330  
331 The author is C<semifor> on IRC and a member of the mailing list.
332  
333 =head1 AUTHOR
334  
335 semifor: Marc Mims <marc@questright.com>
336  
337 =head1 CONTRIBUTORS
338  
339 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
340  
341 mo: Moritz Onken <onken@netcubed.de>
342  
343 norbi: Norbert Buchmuller <norbi@nix.hu>
344  
345 =head1 LICENSE
346  
347 You may distribute this code under the same terms as Perl itself.
348  
349 =cut
350  
351 5 1;
↑ Summary FS/ResultSet.pm »