lib/File/FStore/Adder.pm

filter: Statementstmt 88.7% 118 / 133 Branchbran 46.0% 23 / 50 Conditioncond 42.1% 16 / 38 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 95.0% 19 / 20 Podpod 100% 6 / 6 totaltotal 66.9% 182 / 272 CC 43 SCAR 25.5
CC
43
Cov
71%
done
30.0
set
14.2
_target_filename
7.6
CRAP 12.8
« blib/lib/File/FStore/Adder.pm ↑ Summary blib/lib/File/FStore/Base.pm »
1 # Copyright (c) 2025 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: Module for interacting with file stores
6  
7  
8 package File::FStore::Adder;
9  
10 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::Identifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use File::FStore::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use parent 'File::FStore::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our $VERSION = v0.06;
23  
24  
25 sub link_in {
26 0 ▶ my ($self, $if) = @_;
Subroutine link_in: not called CC=2 SCAR=17.9
Pod documented
27 0 my $tmpname = $self->_temp_filename;
28  
29 0 ▶ link($if, $tmpname) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
link $if, $tmpname or croak($!) 0 0 0
30 }
31  
32  
33 sub move_in {
34 1 ▶ my ($self, $if) = @_;
Subroutine move_in: called CC=2 SCAR=7.1
Pod documented
35 1 my $tmpname = $self->_temp_filename;
36  
37 1 ▶ rename($if, $tmpname) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
rename $if, $tmpname or croak($!) 0 1 1
38 }
39  
40  
41 sub set {
42 1 ▶ my ($self, $domain, $key, $value) = @_;
Subroutine set: called CC=9 SCAR=26.5
Pod documented
43 1 my $data;
44  
45 # get things in standard format:
46 1 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 0 1 1
47 0 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
48 0 $data = {$domain => {$key => $value}};
49 } else {
50 0 $data = {$domain => $key};
51 }
52 } else {
53 1 $data = $domain;
54 }
55  
56 # now try to merge:
57 1 foreach my $cdomain (keys %{$data}) {
58 2 my $ddata = $data->{$cdomain};
59 2 ▶ my $dd = $self->{data}{$cdomain} // croak 'Invalid domain: '.$cdomain;
Condition33%1/3
$self->{"data"}{$cdomain} // croak('Invalid domain: ' . $cdomain)
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->{"data"}{$cdomain} // croak('Invalid domain: ' . $cdomain)
$self->{"data"}{$cdomain} croak('Invalid domain: ' . $cdomain)
Truth table33%1/3
A // B
A= $self->{"data"}{$cdomain}B= croak('Invalid domain: ' . $cdomain)
ABresult
1X1
011
000
60  
61 2 foreach my $key (keys %{$ddata}) {
62 12 ▶ my $v = $ddata->{$key} // next;
Condition50%1/2
$ddata->{$key} // (next)
l!l
120
MC/DC0%0/1
$ddata->{$key} // (next)
$ddata->{$key}
Truth table50%1/2
A
A= $ddata->{$key}
Aresult
11
00
63 12 ▶ $dd->{$key} //= $v;
Condition33%1/3
$dd->{$key} //= $v
l!l&&r!l&&!r
0120
MC/DC0%0/2
$dd->{$key} //= $v
$dd->{$key} $v
Truth table33%1/3
A //= B
A= $dd->{$key}B= $v
ABresult
1X1
011
000
64 12 ▶ croak 'Data missmatch domain '.$cdomain.' key '.$key if $dd->{$key} ne $v;
Branch: 1 branch
BranchTrueFalseTotal
if $dd->{$key} ne $v 0 12 12
65 }
66 }
67 }
68  
69  
70 sub done {
71 1 ▶ my ($self) = @_;
Subroutine done: called CC=18 SCAR=34.0
Pod documented
72 1 my $link = $self->fii->for_link($self->_temp_filename);
73 1 my $inode = $link->inode;
74 my %data = (
75 1 properties => {%{$self->{data}{properties}}},
76 1 digests => {%{$self->{data}{digests}}},
77 );
78  
79 1 ▶ return if defined $self->{done};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"done"} 0 1 1
80 1 $self->{done} = 1;
81  
82 1 foreach my $lifecycle (qw(final current)) {
83 2 ▶ if (defined(my $v = $link->get('size', default => undef))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $link->get("size", "default", undef))) 2 0 2
84 2 ▶ $data{properties}{size} //= $v;
Condition66%2/3
$data{"properties"}{"size"} //= $v
l!l&&r!l&&!r
110
MC/DC0%0/2
$data{"properties"}{"size"} //= $v
$data{"properties"}{"size"} $v
Truth table66%2/3
A //= B
A= $data{"properties"}{"size"}B= $v
ABresult
1X1
011
000
85 2 ▶ croak 'Invalid size' if $data{properties}{size} != $v;
Branch: 1 branch
BranchTrueFalseTotal
if $data{"properties"}{"size"} != $v 0 2 2
86 }
87  
88 2 ▶ if (defined(my $v = $link->get('mediatype', default => undef))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $link->get("mediatype", "default", undef))) 0 2 2
89 0 ▶ $data{properties}{mediasubtype} //= $v;
Condition0%0/3
$data{"properties"}{"mediasubtype"} //= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$data{"properties"}{"mediasubtype"} //= $v
$data{"properties"}{"mediasubtype"} $v
Truth table0%0/3
A //= B
A= $data{"properties"}{"mediasubtype"}B= $v
ABresult
1X1
011
000
90 0 ▶ croak 'Invalid mediasubtype' if $data{properties}{mediasubtype} ne $v;
Branch: 1 branch
BranchTrueFalseTotal
if $data{"properties"}{"mediasubtype"} ne $v 0 0 0
91 }
92  
93 2 ▶ if (defined(my $v = $link->get('contentise', default => undef))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $link->get("contentise", "default", undef))) 2 0 2
94 2 ▶ $data{properties}{contentise} //= $v;
Condition66%2/3
$data{"properties"}{"contentise"} //= $v
l!l&&r!l&&!r
110
MC/DC0%0/2
$data{"properties"}{"contentise"} //= $v
$data{"properties"}{"contentise"} $v
Truth table66%2/3
A //= B
A= $data{"properties"}{"contentise"}B= $v
ABresult
1X1
011
000
95 2 ▶ croak 'Invalid contentise' if $data{properties}{contentise} ne $v;
Branch: 1 branch
BranchTrueFalseTotal
if $data{"properties"}{"contentise"} ne $v 0 2 2
96 }
97  
98 2 foreach my $digest (@{$self->_used_digests}) {
99 20 ▶ if (defined(my $v = $inode->digest($digest, lifecycle => $lifecycle, default => undef))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $inode->digest($digest, "lifecycle", $lifecycle, "default", undef))) 10 10 20
100 10 ▶ $data{digests}{$digest} //= $v;
Condition33%1/3
$data{"digests"}{$digest} //= $v
l!l&&r!l&&!r
0100
MC/DC0%0/2
$data{"digests"}{$digest} //= $v
$data{"digests"}{$digest} $v
Truth table33%1/3
A //= B
A= $data{"digests"}{$digest}B= $v
ABresult
1X1
011
000
101 10 ▶ croak 'Digest missmatch for '.$digest if $data{digests}{$digest} ne $v;
Branch: 1 branch
BranchTrueFalseTotal
if $data{"digests"}{$digest} ne $v 0 10 10
102 }
103 }
104 }
105  
106 1 $self->set(\%data);
107  
108 1 ▶ if (defined $data{properties}{contentise}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data{"properties"}{"contentise"}) 1 0 1
109 1 my $contentise = $self->{contentise} = Data::Identifier->new(ise => $data{properties}{contentise});
110 1 my @res = $self->store->query(properties => contentise => $contentise->uuid);
111 1 ▶ if (scalar @res) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @res) 0 1 1
112 0 croak 'File already in store';
113 }
114 }
115 }
116  
117  
118 sub reset {
119 3 ▶ my ($self) = @_;
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
120  
121 3 ▶ if (defined(my $fn = delete($self->{temp_filename}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = delete $self->{"temp_filename"})) 1 2 3
122 1 unlink($fn);
123 };
124  
125 3 %{$self} = (
126 store => $self->store,
127 data => {
128 properties => {},
129 digests => {},
130 },
131 );
132 }
133  
134  
135 #@returns File::FStore::File
136 sub insert {
137 1 ▶ my ($self) = @_;
Subroutine insert: called CC=4 SCAR=14.1
Pod documented
138 1 my $tmpname = $self->_temp_filename;
139 1 my $store = $self->store;
140 1 my $dbname;
141 my $fullname;
142 1 my File::FStore::File $file;
143  
144 1 $self->done;
145 1 $dbname = $self->_target_filename;
146  
147 {
148 1 my @res = (
149 $store->query(properties => contentise => $self->contentise),
150 $store->query(dbname => $dbname),
151 );
152 1 ▶ if (scalar @res) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @res) 0 1 1
153 0 croak 'File already in store';
154 }
155 }
156  
157 1 $fullname = $store->_file(v2 => store => $dbname);
158 1 ▶ if (-e $fullname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fullname) 0 1 1
159 0 croak 'File with duplicates dbname already in store';
160 }
161  
162 1 mkdir($store->_directory(v2 => store => $self->_target_dirname)); # ignore errors.
163 1 ▶ rename($tmpname => $fullname) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpname, $fullname or croak($!) 0 1 1
164  
165 1 $store->scan(update => 'none', no_digests => 1);
166 1 $file = $store->query(dbname => $dbname);
167  
168 1 $file->set($self->{data});
169 1 $file->update(no_digests => 1);
170  
171 1 $self->reset;
172
173 1 return $file;
174 }
175  
176 # ---- Private helpers ----
177 sub DESTROY {
178 1 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=1 SCAR=0.0
179 1 $self->reset;
180 }
181  
182 sub _new {
183 1 ▶ my ($pkg, %opts) = @_;
Subroutine _new: called CC=2 SCAR=7.0
184 1 my $self = bless \%opts, $pkg;
185  
186 1 ▶ croak 'No store is given' unless defined $self->{store};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"store"} 0 1 1
187  
188 1 $self->store->_init_store_style;
189 1 $self->reset;
190  
191 1 return $self;
192 }
193  
194 sub _temp_filename {
195 3 ▶ my ($self) = @_;
Subroutine _temp_filename: called CC=3 SCAR=11.1
196 3 ▶ return $self->{temp_filename} //= do {
Condition66%2/3
$self->{"temp_filename"} //= do {
l!l&&r!l&&!r
210
MC/DC0%0/2
$self->{"temp_filename"} //= do { state $c = int rand 65535; my $base = sprintf("tmp.%u.%u.%u.%u.%u.%u.%u", $$, $^T, time, int rand 65535, int $self->store, int $self, $c++); my $filename = $self->store->_file("v2", "store", $base); croak('Tempfile already exists: ' . $filename) if -e $filename; $filename }
$self->{"temp_filename"} do { state $c = int rand 65535; my $base = sprintf("tmp.%u.%u.%u.%u.%u.%u.%u", $$, $^T, time, int rand 65535, int $self->store, int $self, $c++); my $filename = $self->store->_file("v2", "store", $base); croak('Tempfile already exists: ' . $filename) if -e $filename; $filename }
Truth table66%2/3
A //= B
A= $self->{"temp_filename"}B= do { state $c = int rand 65535; my $base = sprintf("tmp.%u.%u.%u.%u.%u.%u.%u", $$, $^T, time, int rand 65535, int $self->store, int $self, $c++); my $filename = $self->store->_file("v2", "store", $base); croak('Tempfile already exists: ' . $filename) if -e $filename; $filename }
ABresult
1X1
011
000
197 1 state $c = int(rand 65535);
198 1 my $base = sprintf('tmp.%u.%u.%u.%u.%u.%u.%u',
199 $$, $^T, time,
200 int(rand 65535), int($self->store), int($self),
201 $c++,
202 );
203 1 my $filename = $self->store->_file(v2 => store => $base);
204  
205 1 ▶ croak 'Tempfile already exists: '.$filename if -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -e $filename 0 1 1
206  
207 1 $filename;
208 };
209 }
210  
211 sub _target_dirname {
212 2 ▶ my ($self) = @_;
Subroutine _target_dirname: called CC=4 SCAR=19.4
213 2 ▶ my $store_style = $self->store->setting('store_style') // File::FStore->DEFAULT_STORE_STYLE;
Condition33%1/3
$self->store->setting("store_style") // "File::FStore"->DEFAULT_STORE_STYLE
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->store->setting("store_style") // "File::FStore"->DEFAULT_STORE_STYLE
$self->store->setting("store_style") "File::FStore"->DEFAULT_STORE_STYLE
Truth table33%1/3
A // B
A= $self->store->setting("store_style")B= "File::FStore"->DEFAULT_STORE_STYLE
ABresult
1X1
011
000
214  
215 2 ▶ if ($store_style eq '1-level-contentise') {
Branch: 2 branches
BranchTrueFalseTotal
if ($store_style eq '1-level-contentise') { } 2 0 2
elsif ($store_style eq '2-level-contentise') { } 0 0 0
216 2 return 'by-contentise';
217 } elsif ($store_style eq '2-level-contentise') {
218 0 my $contentise = $self->contentise;
219 0 return File::Spec->catdir('by-contentise', substr($contentise, 0, 2));
220 }
221  
222 0 croak 'BUG: Invalid store style';
223 }
224  
225 sub _target_filename {
226 1 ▶ my ($self) = @_;
Subroutine _target_filename: called CC=5 SCAR=20.3
227 1 ▶ my $store_style = $self->store->setting('store_style') // File::FStore->DEFAULT_STORE_STYLE;
Condition33%1/3
$self->store->setting("store_style") // "File::FStore"->DEFAULT_STORE_STYLE
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->store->setting("store_style") // "File::FStore"->DEFAULT_STORE_STYLE
$self->store->setting("store_style") "File::FStore"->DEFAULT_STORE_STYLE
Truth table33%1/3
A // B
A= $self->store->setting("store_style")B= "File::FStore"->DEFAULT_STORE_STYLE
ABresult
1X1
011
000
228  
229 1 ▶ if ($store_style eq '1-level-contentise' || $store_style eq '2-level-contentise') {
Branch: 1 branch
BranchTrueFalseTotal
if ($store_style eq "1-level-contentise" or $store_style eq "2-level-contentise") 1 0 1
Condition33%1/3
$store_style eq "1-level-contentise" or $store_style eq "2-level-contentise"
l!l&&r!l&&!r
100
MC/DC0%0/2
$store_style eq "1-level-contentise" or $store_style eq "2-level-contentise"
$store_style eq "1-level-contentise" $store_style eq "2-level-contentise"
Truth table33%1/3
A or B
A= $store_style eq "1-level-contentise"B= $store_style eq "2-level-contentise"
ABresult
1X1
011
000
230 1 my $contentise = $self->contentise;
231 1 ▶ return $self->{target_filename} //= File::Spec->catfile($self->_target_dirname, $contentise);
Condition33%1/3
$self->{"target_filename"} //= "File::Spec"->catfile($self->_target_dirname, $contentise)
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"target_filename"} //= "File::Spec"->catfile($self->_target_dirname, $contentise)
$self->{"target_filename"} "File::Spec"->catfile($self->_target_dirname, $contentise)
Truth table33%1/3
A //= B
A= $self->{"target_filename"}B= "File::Spec"->catfile($self->_target_dirname, $contentise)
ABresult
1X1
011
000
232 }
233  
234 0 croak 'BUG: Invalid store style';
235 }
236  
237 sub _used_digests {
238 2 ▶ my ($self) = @_;
Subroutine _used_digests: called CC=2 SCAR=7.1
239 2 ▶ return $self->{used_digests} //= $self->store->_used_digests;
Condition66%2/3
$self->{"used_digests"} //= $self->store->_used_digests
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"used_digests"} //= $self->store->_used_digests
$self->{"used_digests"} $self->store->_used_digests
Truth table66%2/3
A //= B
A= $self->{"used_digests"}B= $self->store->_used_digests
ABresult
1X1
011
000
240 }
241  
242 1 1;
243  
244 __END__
245  
246 =pod
247  
248 =encoding UTF-8
249  
250 =head1 NAME
251  
252 File::FStore::Adder - Module for interacting with file stores
253  
254 =head1 VERSION
255  
256 version v0.06
257  
258 =head1 SYNOPSIS
259  
260 use File::FStore;
261  
262 my File::FStore $store = File::FStore->new(path => '...');
263  
264 my File::FStore::Adder $adder = $store->new_adder;
265  
266 $adder->move_in($filename);
267 $adder->set(...);
268  
269 my File::FStore::File $file = $adder->insert;
270  
271 This package implements an Adder, a helper object that can be used to add files to the store.
272  
273 This package inherits from L<File::FStore::Base>.
274  
275 =head1 METHODS
276  
277 =head2 link_in
278  
279 $adder->link_in($filename);
280  
281 Moves the given file in by means of adding a new hardlink.
282 This will not destroy the original hardlink.
283  
284 This however requires filesystem support for multiple hardlinks,
285 as well as the source file being on the same filesystem as the store.
286  
287 Also after L</insert> has been called no changes to the file (using any of it's names)
288 are allowed anymore.
289  
290 This method C<die>s if there is any problem.
291  
292 See also:
293 L</move_in>.
294  
295 =head2 move_in
296  
297 $adder->move_in($filename);
298  
299 Moves the given file in by means of renaming the file.
300 This will destroy the original hardlink.
301  
302 This will require the source file being on the same filesystem as the store.
303  
304 Once this call was successful the file is no longer available under the old name.
305  
306 If the file is not added to the store the file is unlinked.
307  
308 This method C<die>s if there is any problem.
309  
310 See also:
311 L</link_in>.
312  
313 =head2 set
314  
315 $file->set($domain => $key => $value);
316 # or:
317 $file->set($domain => {$key => $value});
318 # or:
319 $file->set({$domain => {$key => $value}});
320  
321 This sets a value on the file to be added alike L<File::FStore::File/set>.
322 This method works the same way as said method including safety checks.
323  
324 =head2 done
325  
326 $adder->done;
327  
328 Marks the adder as done with all changes, but not yet inserted.
329 This method C<die>s on error or on state missmatch.
330  
331 B<Note:>
332 You normally don't need to call this method manually.
333  
334 B<Note:>
335 All calls to L</set> must be done before calling this method.
336 Also all changes to the file must be done before.
337 After calling this method the file is considered read-only.
338  
339 B<Note:>
340 Calling this early can cause errors to be hidden or placed out of context.
341  
342 =head2 reset
343  
344 $adder->reset;
345  
346 Resets the adder. Any data that has already been set is removed.
347 Any temporary files are removed.
348 Files moved in with L</move_in> are unlinked.
349  
350 =head2 insert
351  
352 my File::FStore::File $file = $adder->insert;
353  
354 Performs the actual insert of the file into the store.
355  
356 This method C<die>s on error or returns the L<File::FStore::File> object of
357 the freshly inserted file.
358  
359 B<Note:>
360 This adder is in undefined state after this call.
361 The only save method to call after this is L</reset>.
362  
363 =head1 AUTHOR
364  
365 Philipp Schafft <lion@cpan.org>
366  
367 =head1 COPYRIGHT AND LICENSE
368  
369 This software is Copyright (c) 2025 by Philipp Schafft <lion@cpan.org>.
370  
371 This is free software, licensed under:
372  
373 The Artistic License 2.0 (GPL Compatible)
374  
375 =cut
« blib/lib/File/FStore/Adder.pm ↑ Summary blib/lib/File/FStore/Base.pm »