blib/lib/File/FStore/Adder.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 43 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 264 CC 23 SCAR 39.1
CC
23
Cov
0%
done
156.0
set
20.0
insert
12.0
CRAP 49.9
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 use v5.10;
11 use strict;
12 use warnings;
13  
14 use Carp;
15 use File::Spec;
16 use Data::Identifier;
17  
18 use File::FStore::File;
19  
20 use parent 'File::FStore::Base';
21  
22 our $VERSION = v0.06;
23  
24  
25 sub link_in {
26 my ($self, $if) = @_;
27 my $tmpname = $self->_temp_filename;
28  
29 link($if, $tmpname) or croak $!;
30 }
31  
32  
33 sub move_in {
34 my ($self, $if) = @_;
35 my $tmpname = $self->_temp_filename;
36  
37 rename($if, $tmpname) or croak $!;
38 }
39  
40  
41 sub set {
42 my ($self, $domain, $key, $value) = @_;
43 my $data;
44  
45 # get things in standard format:
46 if (defined($key)) {
47 if (defined($value)) {
48 $data = {$domain => {$key => $value}};
49 } else {
50 $data = {$domain => $key};
51 }
52 } else {
53 $data = $domain;
54 }
55  
56 # now try to merge:
57 foreach my $cdomain (keys %{$data}) {
58 my $ddata = $data->{$cdomain};
59 my $dd = $self->{data}{$cdomain} // croak 'Invalid domain: '.$cdomain;
60  
61 foreach my $key (keys %{$ddata}) {
62 my $v = $ddata->{$key} // next;
63 $dd->{$key} //= $v;
64 croak 'Data missmatch domain '.$cdomain.' key '.$key if $dd->{$key} ne $v;
65 }
66 }
67 }
68  
69  
70 sub done {
71 my ($self) = @_;
72 my $link = $self->fii->for_link($self->_temp_filename);
73 my $inode = $link->inode;
74 my %data = (
75 properties => {%{$self->{data}{properties}}},
76 digests => {%{$self->{data}{digests}}},
77 );
78  
79 return if defined $self->{done};
80 $self->{done} = 1;
81  
82 foreach my $lifecycle (qw(final current)) {
83 if (defined(my $v = $link->get('size', default => undef))) {
84 $data{properties}{size} //= $v;
85 croak 'Invalid size' if $data{properties}{size} != $v;
86 }
87  
88 if (defined(my $v = $link->get('mediatype', default => undef))) {
89 $data{properties}{mediasubtype} //= $v;
90 croak 'Invalid mediasubtype' if $data{properties}{mediasubtype} ne $v;
91 }
92  
93 if (defined(my $v = $link->get('contentise', default => undef))) {
94 $data{properties}{contentise} //= $v;
95 croak 'Invalid contentise' if $data{properties}{contentise} ne $v;
96 }
97  
98 foreach my $digest (@{$self->_used_digests}) {
99 if (defined(my $v = $inode->digest($digest, lifecycle => $lifecycle, default => undef))) {
100 $data{digests}{$digest} //= $v;
101 croak 'Digest missmatch for '.$digest if $data{digests}{$digest} ne $v;
102 }
103 }
104 }
105  
106 $self->set(\%data);
107  
108 if (defined $data{properties}{contentise}) {
109 my $contentise = $self->{contentise} = Data::Identifier->new(ise => $data{properties}{contentise});
110 my @res = $self->store->query(properties => contentise => $contentise->uuid);
111 if (scalar @res) {
112 croak 'File already in store';
113 }
114 }
115 }
116  
117  
118 sub reset {
119 my ($self) = @_;
120  
121 if (defined(my $fn = delete($self->{temp_filename}))) {
122 unlink($fn);
123 };
124  
125 %{$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 my ($self) = @_;
138 my $tmpname = $self->_temp_filename;
139 my $store = $self->store;
140 my $dbname;
141 my $fullname;
142 my File::FStore::File $file;
143  
144 $self->done;
145 $dbname = $self->_target_filename;
146  
147 {
148 my @res = (
149 $store->query(properties => contentise => $self->contentise),
150 $store->query(dbname => $dbname),
151 );
152 if (scalar @res) {
153 croak 'File already in store';
154 }
155 }
156  
157 $fullname = $store->_file(v2 => store => $dbname);
158 if (-e $fullname) {
159 croak 'File with duplicates dbname already in store';
160 }
161  
162 mkdir($store->_directory(v2 => store => $self->_target_dirname)); # ignore errors.
163 rename($tmpname => $fullname) or croak $!;
164  
165 $store->scan(update => 'none', no_digests => 1);
166 $file = $store->query(dbname => $dbname);
167  
168 $file->set($self->{data});
169 $file->update(no_digests => 1);
170  
171 $self->reset;
172
173 return $file;
174 }
175  
176 # ---- Private helpers ----
177 sub DESTROY {
178 my ($self) = @_;
179 $self->reset;
180 }
181  
182 sub _new {
183 my ($pkg, %opts) = @_;
184 my $self = bless \%opts, $pkg;
185  
186 croak 'No store is given' unless defined $self->{store};
187  
188 $self->store->_init_store_style;
189 $self->reset;
190  
191 return $self;
192 }
193  
194 sub _temp_filename {
195 my ($self) = @_;
196 return $self->{temp_filename} //= do {
197 state $c = int(rand 65535);
198 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 my $filename = $self->store->_file(v2 => store => $base);
204  
205 croak 'Tempfile already exists: '.$filename if -e $filename;
206  
207 $filename;
208 };
209 }
210  
211 sub _target_dirname {
212 my ($self) = @_;
213 my $store_style = $self->store->setting('store_style') // File::FStore->DEFAULT_STORE_STYLE;
214  
215 if ($store_style eq '1-level-contentise') {
216 return 'by-contentise';
217 } elsif ($store_style eq '2-level-contentise') {
218 my $contentise = $self->contentise;
219 return File::Spec->catdir('by-contentise', substr($contentise, 0, 2));
220 }
221  
222 croak 'BUG: Invalid store style';
223 }
224  
225 sub _target_filename {
226 my ($self) = @_;
227 my $store_style = $self->store->setting('store_style') // File::FStore->DEFAULT_STORE_STYLE;
228  
229 if ($store_style eq '1-level-contentise' || $store_style eq '2-level-contentise') {
230 my $contentise = $self->contentise;
231 return $self->{target_filename} //= File::Spec->catfile($self->_target_dirname, $contentise);
232 }
233  
234 croak 'BUG: Invalid store style';
235 }
236  
237 sub _used_digests {
238 my ($self) = @_;
239 return $self->{used_digests} //= $self->store->_used_digests;
240 }
241  
242 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