lib/File/FStore.pm

filter: Statementstmt 67.1% 257 / 383 Branchbran 41.5% 94 / 226 Conditioncond 24.2% 17 / 70 MC/DCmcdc 2.2% 1 / 45 Subroutinesub 78.5% 33 / 42 Podpod 100% 17 / 17 totaltotal 53.5% 419 / 783 CC 150 SCAR 44.6
CC
150
Cov
54%
query
255.7
_scrub_dir
156.0
fix
72.0
CRAP 86.5
« blib/lib/File/FStore.pm ↑ Summary blib/lib/File/FStore/Migration.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;
9  
10 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Data::Identifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Data::Identifier::Generate;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use File::FStore::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use parent 'Data::Identifier::Interface::Known';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 our $VERSION = v0.06;
26  
27 use constant {
28 3 DEFAULT_LINK_STYLE => '1-level',
29 DEFAULT_STORE_STYLE => '1-level-contentise',
30 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 my %_types = (
33 db => 'Data::TagDB',
34 extractor => 'Data::URIID',
35 fii => 'File::Information',
36 );
37  
38 30 our %_valid_digests = map {$_ => int((/^[^-]+-[^-]-([0-9]+)$/)[0]/4)} (
39 qw(md-5-128 sha-1-160),
40 3 (map {'sha-2-'.$_, 'sha-3-'.$_} 224, 256, 384, 512),
41 );
42  
43 3 our %_valid_link_styles = map {$_ => 1} qw(none 1-level 2-level);
44 3 our %_valid_store_styles = map {$_ => 1} qw(1-level-contentise 2-level-contentise);
45  
46 3 my %_flat_settings = map {$_ => $_} qw(link_style store_style);
47  
48  
49 sub create {
50 2 ▶ my ($pkg, %opts) = @_;
Subroutine create: called CC=15 SCAR=28.8
Pod documented
51 2 my $path = delete($opts{path});
52 2 ▶ my $digests = delete($opts{digests}) // [keys %_valid_digests];
Condition50%1/2
delete $opts{"digests"} // [keys %File::FStore::_valid_digests]
l!l
02
MC/DC0%0/1
delete $opts{"digests"} // [keys %File::FStore::_valid_digests]
delete $opts{"digests"}
Truth table50%1/2
A
A= delete $opts{"digests"}
Aresult
11
00
53 2 my %extra;
54 my $dbh;
55  
56 2 foreach my $key (keys(%_types), qw(weak)) {
57 8 my $v = delete $opts{$key};
58 8 ▶ $extra{$key} = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 8 8
59 }
60  
61 2 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 2 2
62  
63 2 ▶ $digests = [split(/\s*,\s*|\s+/, $digests)] unless ref $digests;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $digests 0 2 2
64 2 foreach my $digest (@{$digests}) {
65 20 ▶ croak 'Invalid digest: '.$digest unless defined $_valid_digests{$digest};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $File::FStore::_valid_digests{$digest} 0 20 20
66 }
67  
68 2 ▶ mkdir($path) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $path or croak($!) 0 2 2
69 2 ▶ mkdir(File::Spec->catdir($path, qw(v2))) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "File::Spec"->catdir($path, "v2") or croak($!) 0 2 2
70 2 ▶ mkdir(File::Spec->catdir($path, qw(v2 store))) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "File::Spec"->catdir($path, ("v2", "store")) or croak($!) 0 2 2
71 2 ▶ mkdir(File::Spec->catdir($path, qw(v2 by))) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "File::Spec"->catdir($path, ("v2", "by")) or croak($!) 0 2 2
72 2 foreach my $digest (@{$digests}) {
73 20 ▶ mkdir(File::Spec->catdir($path, qw(v2 by), $digest)) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir "File::Spec"->catdir($path, ("v2", "by"), $digest) or croak($!) 0 20 20
74 }
75  
76 2 $dbh = DBI->connect('dbi:SQLite:dbname='.File::Spec->catfile($path => v2 => store => 'db.sqlite'), undef, undef, { RaiseError => 1, PrintError => undef });
77 #@inject SQLITE
78 2 $dbh->do($_) for split /;/, << 'SQL';
79 CREATE TABLE file (
80 id INTEGER NOT NULL UNIQUE PRIMARY KEY AUTOINCREMENT,
81 filename VARCHAR(128) NOT NULL UNIQUE
82 );
83  
84 CREATE TABLE file_properties (
85 file INTEGER NOT NULL REFERENCES file (id),
86 key VARCHAR(64) NOT NULL,
87 value VARCHAR(128) NOT NULL,
88 UNIQUE (file, key)
89 );
90  
91 CREATE TABLE file_hash (
92 file INTEGER NOT NULL REFERENCES file (id),
93 algo VARCHAR(32) NOT NULL,
94 hash VARCHAR(255) NOT NULL UNIQUE,
95 UNIQUE (file, algo)
96 );
97 SQL
98 2 $dbh->disconnect;
99  
100 2 return $pkg->new(path => $path, %extra);
101 }
102  
103  
104 #@returns __PACKAGE__
105 sub new {
106 4 ▶ my ($pkg, %opts) = @_;
Subroutine new: called CC=17 SCAR=39.7
Pod documented
107 4 ▶ my $path = delete $opts{path} // croak 'No path given';
Condition33%1/3
delete $opts{"path"} // croak("No path given")
l!l&&r!l&&!r
400
MC/DC0%0/2
delete $opts{"path"} // croak("No path given")
delete $opts{"path"} croak("No path given")
Truth table33%1/3
A // B
A= delete $opts{"path"}B= croak("No path given")
ABresult
1X1
011
000
108 4 my $weak = delete $opts{weak};
109 4 my @used_digests;
110 4 my $self = bless {
111 path => $path,
112 used_digests => \@used_digests,
113 transaction_count => 0,
114 link_style => DEFAULT_LINK_STYLE,
115 store_style => DEFAULT_STORE_STYLE,
116 }, $pkg;
117  
118 4 foreach my $key (keys %_types) {
119 12 my $v = delete $opts{$key};
120 12 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 12 0 12
121 0 ▶ croak 'Invalid type for key: '.$key unless eval {$v->isa($_types{$key})};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $v->isa($_types{$key}) } } 0 0 0
122 0 $self->{$key} = $v;
123 0 ▶ weaken($self->{$key}) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
124 }
125  
126 4 ▶ if (defined(my $link_style = delete $opts{link_style})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $link_style = delete $opts{"link_style"})) 0 4 4
127 0 ▶ $link_style = DEFAULT_LINK_STYLE if $link_style eq 'default';
Branch: 1 branch
BranchTrueFalseTotal
if $link_style eq "default" 0 0 0
128 0 ▶ croak 'Not a valid link style: '.$link_style unless defined $_valid_link_styles{$link_style};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $File::FStore::_valid_link_styles{$link_style} 0 0 0
129 0 $self->{link_style} = $link_style;
130 }
131  
132 4 ▶ if (defined(my $store_style = delete $opts{store_style})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $store_style = delete $opts{"store_style"})) 0 4 4
133 0 ▶ $store_style = DEFAULT_STORE_STYLE if $store_style eq 'default';
Branch: 1 branch
BranchTrueFalseTotal
if $store_style eq "default" 0 0 0
134 0 ▶ croak 'Not a valid store style: '.$store_style unless defined $_valid_store_styles{$store_style};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $File::FStore::_valid_store_styles{$store_style} 0 0 0
135 0 $self->{store_style} = $store_style;
136 }
137  
138 4 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 4 4
139  
140 4 ▶ opendir(my $dir, $self->_directory(v2 => 'by')) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $self->_directory("v2", "by") or croak($!) 0 4 4
141 4 ▶ while (defined(my $ent = readdir($dir))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ent = readdir $dir)) 48 4 52
142 48 ▶ next if $ent =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $ent =~ /^\./ 8 40 48
143 40 ▶ croak 'Invalid store, unsupported/invalid digest used: '.$ent unless $_valid_digests{$ent};
Branch: 1 branch
BranchTrueFalseTotal
unless $File::FStore::_valid_digests{$ent} 0 40 40
144 40 push(@used_digests, $ent);
145 }
146 4 closedir($dir);
147  
148 4 $self->{dbh} = DBI->connect('dbi:SQLite:dbname='.$self->_file(v2 => store => 'db.sqlite'), undef, undef, { RaiseError => 1, PrintError => undef });
149  
150 4 return $self;
151 }
152  
153  
154 sub close {
155 4 ▶ my ($self) = @_;
Subroutine close: called CC=1 SCAR=0.0
Pod documented
156 4 $self->DESTROY;
157 }
158  
159  
160 sub setting {
161 8 ▶ my ($self, $key) = @_;
Subroutine setting: called CC=3 SCAR=12.0
Pod documented
162  
163 8 ▶ croak 'No key given' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 8 8
164  
165 8 ▶ if (defined(my $flatkey = $_flat_settings{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $flatkey = $_flat_settings{$key})) 8 0 8
166 8 return $self->{$flatkey};
167 }
168  
169 0 croak 'Unknown key: '.$key;
170 }
171  
172  
173 sub in_transaction {
174 7 ▶ my ($self, $type, $code) = @_;
Subroutine in_transaction: called CC=12 SCAR=27.8
Pod documented
175 7 my $error;
176  
177 7 ▶ croak 'Bad transaction type' unless $type eq 'ro' || $type eq 'rw';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "ro" or $type eq "rw" 0 7 7
Condition33%1/3
$type eq "ro" or $type eq "rw"
l!l&&r!l&&!r
070
MC/DC0%0/2
$type eq "ro" or $type eq "rw"
$type eq "ro" $type eq "rw"
Truth table33%1/3
A or B
A= $type eq "ro"B= $type eq "rw"
ABresult
1X1
011
000
178  
179 7 ▶ return undef if defined $self->{transaction_error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"transaction_error"} 0 7 7
180  
181 7 ▶ if ($self->{transaction_count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'transaction_count'}) { } 3 4 7
182 3 ▶ if ($type ne 'ro' && $type ne $self->{transaction_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "ro" and $type ne $self->{"transaction_type"}) 0 3 3
Condition33%1/3
$type ne "ro" and $type ne $self->{"transaction_type"}
!ll&&!rl&&r
030
MC/DC0%0/2
$type ne "ro" and $type ne $self->{"transaction_type"}
$type ne "ro" $type ne $self->{"transaction_type"}
Truth table33%1/3
A and B
A= $type ne "ro"B= $type ne $self->{"transaction_type"}
ABresult
0X0
100
111
183 0 croak 'Invalid inner transaction type '.$type.' within outer '.$self->{transaction_type}.' transaction';
184 }
185 } else {
186 4 $self->{dbh}->begin_work;
187 4 $self->{transaction_type} = $type;
188 4 $self->{transaction_error} = undef;
189 }
190  
191 7 $self->{transaction_count}++;
192  
193 7 eval {
194 7 $code->();
195 };
196 7 ▶ $error = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
197  
198 7 ▶ $self->{transaction_error} //= $error;
Condition33%1/3
$self->{"transaction_error"} //= $error
l!l&&r!l&&!r
007
MC/DC0%0/2
$self->{"transaction_error"} //= $error
$self->{"transaction_error"} $error
Truth table33%1/3
A //= B
A= $self->{"transaction_error"}B= $error
ABresult
1X1
011
000
199  
200 7 $self->{transaction_count}--;
201  
202 7 ▶ unless ($self->{transaction_count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"transaction_count"}) 4 3 7
203 4 ▶ if (defined $self->{transaction_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'transaction_error'}) { } 0 4 4
204 0 $self->{dbh}->rollback;
205 } else {
206 4 $self->{dbh}->commit;
207 }
208 }
209  
210 7 ▶ croak $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 7 7
211 }
212  
213  
214 sub query {
215 6 ▶ my ($self, @query) = @_;
Subroutine query: called CC=46 SCAR=55.4
Pod documented
216 6 my %fields = (properties => {}, digests => {});
217 6 my $limit;
218 my $offset;
219 6 my $order;
220 6 my @q;
221 6 my @p;
222  
223 6 ▶ while (defined(my $cmd = shift(@query))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cmd = shift @query)) 6 6 12
224 6 ▶ $cmd = 'digests' if $cmd eq 'hashes';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "hashes" 0 6 6
225  
226 6 ▶ if ($cmd eq 'properties' || $cmd eq 'digests') {
Branch: 9 branches
BranchTrueFalseTotal
if ($cmd eq 'properties' or $cmd eq 'digests') { } 2 4 6
elsif ($cmd eq 'all') { } 0 4 4
elsif ($cmd eq 'limit') { } 0 4 4
elsif ($cmd eq 'offset') { } 0 4 4
elsif ($cmd eq 'order') { } 0 4 4
elsif ($cmd eq 'min_size') { } 0 4 4
elsif ($cmd eq 'max_size') { } 0 4 4
elsif ($cmd eq 'dbname') { } 3 1 4
elsif ($cmd eq 'ise') { } 1 0 1
Condition66%2/3
$cmd eq "properties" or $cmd eq "digests"
l!l&&r!l&&!r
204
MC/DC50%1/2
$cmd eq "properties" or $cmd eq "digests"
$cmd eq "properties" $cmd eq "digests"
Truth table66%2/3
A or B
A= $cmd eq "properties"B= $cmd eq "digests"
ABresult
1X1
011
000
227 2 ▶ my $n = shift(@query) // croak 'Incomplete query';
Condition33%1/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
200
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table33%1/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
228 2 my $t = $fields{$cmd};
229  
230 2 ▶ if (ref($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $n) { } 0 2 2
231 0 %{$t} = (%{$t}, %{$n});
232 } else {
233 2 ▶ my $v = shift(@query) // croak 'Incomplete query';
Condition33%1/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
200
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table33%1/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
234 2 $t->{$n} = $v;
235 }
236 } elsif ($cmd eq 'all') {
237 0 push(@q, 'SELECT id FROM file');
238 } elsif ($cmd eq 'limit') {
239 0 ▶ $limit = shift(@query) // croak 'Incomplete query';
Condition0%0/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table0%0/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
240 } elsif ($cmd eq 'offset') {
241 0 ▶ $offset = shift(@query) // croak 'Incomplete query';
Condition0%0/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table0%0/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
242 } elsif ($cmd eq 'order') {
243 0 ▶ $order = shift(@query) // croak 'Incomplete query';
Condition0%0/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table0%0/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
244 0 $order = uc($order);
245 0 ▶ croak 'Bad order' if $order ne 'ASC' && $order ne 'DESC';
Branch: 1 branch
BranchTrueFalseTotal
if $order ne "ASC" and $order ne "DESC" 0 0 0
Condition0%0/3
$order ne "ASC" and $order ne "DESC"
!ll&&!rl&&r
000
MC/DC0%0/2
$order ne "ASC" and $order ne "DESC"
$order ne "ASC" $order ne "DESC"
Truth table0%0/3
A and B
A= $order ne "ASC"B= $order ne "DESC"
ABresult
0X0
100
111
246 } elsif ($cmd eq 'min_size') {
247 0 ▶ my $v = shift(@query) // croak 'Incomplete query';
Condition0%0/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table0%0/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
248 0 push(@q, 'SELECT file AS id FROM file_properties WHERE key = \'size\' AND CAST(value AS BIG INTEGER) >= ?');
249 0 push(@p, $v);
250 } elsif ($cmd eq 'max_size') {
251 0 ▶ my $v = shift(@query) // croak 'Incomplete query';
Condition0%0/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table0%0/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
252 0 push(@q, 'SELECT file AS id FROM file_properties WHERE key = \'size\' AND CAST(value AS BIG INTEGER) <= ?');
253 0 push(@p, $v);
254 } elsif ($cmd eq 'dbname') {
255 3 ▶ my $v = shift(@query) // croak 'Incomplete query';
Condition33%1/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
300
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table33%1/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
256  
257 3 ▶ $v = [$v] unless ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 3 0 3
258  
259 3 ▶ croak 'Empty list for dbname' unless scalar(@{$v});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$v;} 0 3 3
260  
261 3 push(@q, 'SELECT id FROM file WHERE '.$self->_placeholders(filename => $v));
262 3 push(@p, @{$v});
263 } elsif ($cmd eq 'ise') {
264 1 ▶ my $v = shift(@query) // croak 'Incomplete query';
Condition33%1/3
shift @query // croak("Incomplete query")
l!l&&r!l&&!r
100
MC/DC0%0/2
shift @query // croak("Incomplete query")
shift @query croak("Incomplete query")
Truth table33%1/3
A // B
A= shift @queryB= croak("Incomplete query")
ABresult
1X1
011
000
265  
266 1 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 0 1 1
267 0 ▶ if (eval {$v->isa('Data::Identifier::Cloudlet')}) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { $v->isa('Data::Identifier::Cloudlet') } }) { } 0 0 0
elsif (eval { do { $v->can('ise') } }) { } 0 0 0
268 0 $v = [$v->entries];
269 0 } elsif (eval {$v->can('ise')}) {
270 0 $v = [$v];
271 }
272 } else {
273 1 $v = [$v];
274 }
275  
276 1 ▶ croak 'Empty list for ise' unless scalar(@{$v});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$v;} 0 1 1
277  
278 1 foreach my $s (@{$v}) {
279 1 ▶ $s = $s->ise if ref $s;
Branch: 1 branch
BranchTrueFalseTotal
if ref $s 0 1 1
280 }
281  
282 1 push(@q, 'SELECT file AS id FROM file_properties WHERE key IN (\'contentise\', \'inodeise\') AND '.$self->_placeholders(value => $v));
283 1 push(@p, @{$v});
284 } else {
285 0 croak 'Invalid query command: '.$cmd;
286 }
287 }
288  
289 6 foreach my $digest (keys %{$fields{digests}}) {
290 0 my $v = $fields{digests}{$digest};
291 0 ▶ $v = [$v] unless ref($v) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 0 0 0
292 0 unshift(@q, 'SELECT file AS id FROM file_hash WHERE algo = ? AND '.$self->_placeholders(hash => $v));
293 0 unshift(@p, $digest, @{$v});
294 }
295  
296 6 foreach my $property (keys %{$fields{properties}}) {
297 2 my $v = $fields{properties}{$property};
298 2 ▶ $v = [$v] unless ref($v) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 2 0 2
299 2 push(@q, 'SELECT file AS id FROM file_properties WHERE key = ? AND '.$self->_placeholders(value => $v));
300 2 push(@p, $property, @{$v});
301 }
302  
303 6 ▶ unless (scalar @q) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @q) 0 6 6
304 0 croak 'No filter given';
305 }
306  
307 {
308 6 my $q = join(' INTERSECT ', @q);
309 6 my $sth;
310 my @result;
311  
312 6 ▶ if (defined $order) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $order) 0 6 6
313 0 $q .= ' ORDER BY id '.$order;
314 }
315  
316 6 ▶ $limit = 2 unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 3 3 6
317  
318 6 ▶ if (defined $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $limit) 3 3 6
319 3 $q .= ' LIMIT ?';
320 3 push(@p, $limit);
321 }
322  
323 6 ▶ if (defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) 0 6 6
324 0 $q .= ' OFFSET ?';
325 0 push(@p, $offset);
326 }
327 6 $sth = $self->{dbh}->prepare($q);
328 6 $sth->execute(@p);
329  
330 6 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 3 6 9
331 3 push(@result, File::FStore::File->_new(store => $self, dbid => $row->[0]));
332 }
333  
334 6 $sth->finish;
335  
336 6 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 3 3 6
337 3 return @result;
338 } else {
339 3 ▶ croak 'Multiple results for query but called in scalar context' if scalar(@result) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @result > 1 0 3 3
340 3 ▶ croak 'No results for query but called in scalar context' if scalar(@result) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @result < 1 0 3 3
341 3 return $result[0];
342 }
343 }
344  
345 0 die 'BUG';
346 }
347  
348  
349 sub scrub {
350 0 ▶ my ($self, %opts) = @_;
Subroutine scrub: not called CC=2 SCAR=17.9
Pod documented
351  
352 0 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 0 0
353  
354 $self->in_transaction(ro => sub {
355 0 ▶ opendir(my $algodir, $self->_directory('v2' => 'by')) or croak $!;
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
opendir my $algodir, $self->_directory("v2", "by") or croak($!) 0 0 0
356 0 ▶ while (defined(my $algoent = readdir($algodir))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $algoent = readdir $algodir)) 0 0 0
357 0 ▶ next if $algoent =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $algoent =~ /^\./ 0 0 0
358 0 $self->_scrub_dir($algoent => $self->_directory(v2 => by => $algoent));
359 }
360 0 closedir($algodir);
361 0 });
362 }
363  
364  
365 sub scan {
366 1 ▶ my ($self, %opts) = @_;
Subroutine scan: called CC=4 SCAR=13.9
Pod documented
367 1 my %extra;
368  
369 1 foreach my $key (qw(update no_digests on_pre_set on_post_set)) {
370 4 $extra{$key} = delete $opts{$key};
371 }
372  
373 1 ▶ $extra{update} //= 'all';
Condition50%1/2
$extra{"update"} //= "all"
l!l
10
MC/DC0%0/1
$extra{"update"} //= "all"
$extra{"update"}
Truth table50%1/2
A
A= $extra{"update"}
Aresult
11
00
374  
375 1 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 1 1
376  
377 $self->in_transaction(rw => sub {
378 1 ▶ my $insert = $self->{dbh}->prepare('INSERT OR IGNORE INTO file (filename) VALUES (?)');
Subroutine __ANON__: called CC=5 SCAR=16.1
379  
380 1 ▶ opendir(my $dir, $self->_directory('v2', 'store')) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $self->_directory("v2", "store") or croak($!) 0 1 1
381 1 ▶ while (defined(my $ent = readdir($dir))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ent = readdir $dir)) 4 1 5
382 4 my $dirname;
383  
384 4 ▶ next if $ent =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $ent =~ /^\./ 2 2 4
385  
386 2 $dirname = $self->_directory('v2', 'store', $ent);
387  
388 2 ▶ if (-d $dirname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dirname) 1 1 2
389 1 $self->_scan_subdir($dirname, dbprefix => $ent, insert => $insert, %extra);
390 }
391 }
392 1 closedir($dir);
393 1 });
394 }
395  
396  
397 sub fix {
398 0 ▶ my ($self, @fixes) = @_;
Subroutine fix: not called CC=8 SCAR=42.8
Pod documented
399 0 my %fixes = map {$_ => 1} @fixes;
400  
401 0 foreach my $fix (qw(upgrade scrub remove-inode remove-mediasubtype remove-inodeise scan)) {
402 0 ▶ next unless delete $fixes{$fix};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $fixes{$fix} 0 0 0
403  
404 0 ▶ if ($fix eq 'scrub') {
Branch: 4 branches
BranchTrueFalseTotal
if ($fix eq 'scrub') { } 0 0 0
elsif ($fix eq 'scan') { } 0 0 0
elsif ($fix eq 'upgrade') { } 0 0 0
elsif ($fix =~ /^remove-(inode|mediasubtype|inodeise)$/) { } 0 0 0
405 0 $self->scrub;
406 } elsif ($fix eq 'scan') {
407 0 $self->scan;
408 } elsif ($fix eq 'upgrade') {
409 0 $self->migration->upgrade;
410 } elsif ($fix =~ /^remove-(inode|mediasubtype|inodeise)$/) {
411 0 my $what = $1;
412 0 my $sth = $self->{dbh}->prepare('DELETE FROM file_properties WHERE key = ?');
413 0 $sth->execute($what);;
414 } else {
415 0 croak 'BUG';
416 }
417 }
418  
419 0 ▶ if (scalar keys %fixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %fixes) 0 0 0
420 0 croak 'Invalid/unknown fixes passed: '.join(', ', keys %fixes);
421 }
422 }
423  
424  
425 #@returns File::FStore::Adder
426 sub new_adder {
427 1 ▶ my ($self) = @_;
Subroutine new_adder: called CC=1 SCAR=0.0
Pod documented
428 1 require File::FStore::Adder;
429 1 return File::FStore::Adder->_new(store => $self);
430 }
431  
432  
433 #@returns File::FStore::Migration
434 sub migration {
435 0 ▶ my ($self) = @_;
Subroutine migration: not called CC=1 SCAR=6.9
Pod documented
436 0 require File::FStore::Migration;
437 0 return File::FStore::Migration->_new(store => $self);
438 }
439  
440  
441 sub export {
442 0 ▶ my ($self, @args) = @_;
Subroutine export: not called CC=1 SCAR=6.9
Pod documented
443 0 return $self->migration->export_data(@args);
444 }
445  
446  
447 sub import_data {
448 0 ▶ my ($self, @args) = @_;
Subroutine import_data: not called CC=1 SCAR=6.9
Pod documented
449 0 return $self->migration->import_data(@args);
450 }
451  
452  
453 sub attach {
454 0 ▶ my ($self, %opts) = @_;
Subroutine attach: not called CC=8 SCAR=42.8
Pod documented
455 0 my $weak = delete $opts{weak};
456  
457 0 foreach my $key (keys %_types) {
458 0 my $v = delete $opts{$key};
459 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
460 0 ▶ croak 'Invalid type for key: '.$key unless eval {$v->isa($_types{$key})};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $v->isa($_types{$key}) } } 0 0 0
461 0 ▶ $self->{$key} //= $v;
Condition0%0/3
$self->{$key} //= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$key} //= $v
$self->{$key} $v
Truth table0%0/3
A //= B
A= $self->{$key}B= $v
ABresult
1X1
011
000
462 0 ▶ croak 'Missmatch for key: '.$key unless $self->{$key} == $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$key} == $v 0 0 0
463 0 ▶ weaken($self->{$key}) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
464 }
465  
466 0 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 0 0
467 }
468  
469  
470 #@returns Data::TagDB
471 sub db {
472 5 ▶ my ($self, %opts) = @_;
Subroutine db: called CC=3 SCAR=12.5
Pod documented
473 5 ▶ return $self->{db} if defined $self->{db};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"db"} 0 5 5
474 5 ▶ return $opts{default} if exists $opts{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"default"} 5 0 5
475 0 croak 'No database known';
476 }
477  
478  
479 #@returns Data::URIID
480 sub extractor {
481 5 ▶ my ($self, %opts) = @_;
Subroutine extractor: called CC=3 SCAR=12.5
Pod documented
482 5 ▶ return $self->{extractor} if defined $self->{extractor};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"extractor"} 0 5 5
483 5 ▶ return $opts{default} if exists $opts{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"default"} 5 0 5
484 0 croak 'No extractor known';
485 }
486  
487  
488 #@returns File::Information
489 sub fii {
490 3 ▶ my ($self) = @_;
Subroutine fii: called CC=2 SCAR=6.9
Pod documented
491 3 ▶ return $self->{fii} if defined $self->{fii};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"fii"} 1 2 3
492  
493 2 require File::Information;
494 2 File::Information->VERSION(v0.06);
495 2 return $self->{fii} = File::Information->new(
496 db => $self->db(default => undef),
497 extractor => $self->extractor(default => undef),
498 );
499 }
500  
501 # --- Overrides for Data::Identifier::Interface::Known ---
502 sub _known_provider {
503 0 ▶ my ($pkg, @args) = @_;
Subroutine _known_provider: not called CC=1 SCAR=6.9
504 0 return $pkg->File::FStore::File::_known_provider(@args);
505 }
506  
507  
508  
509 # ---- Private helpers ----
510  
511 sub _placeholders {
512 6 ▶ my ($self, $field, $list) = @_;
Subroutine _placeholders: called CC=1 SCAR=0.0
513 6 my $placeholders = '?,' x scalar(@{$list});
514  
515 6 $placeholders =~ s/,$//;
516  
517 6 return sprintf('%s IN (%s)', $field, $placeholders);
518 }
519  
520 sub DESTROY {
521 8 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=2 SCAR=6.9
522 8 ▶ $self->{dbh}->disconnect if defined $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dbh"} 4 4 8
523 8 %{$self} = ();
524 }
525  
526 sub _file {
527 28 ▶ my ($self, @comp) = @_;
Subroutine _file: called CC=1 SCAR=0.0
528 28 return File::Spec->catfile($self->{path}, @comp);
529 }
530  
531 sub _directory {
532 9 ▶ my ($self, @comp) = @_;
Subroutine _directory: called CC=1 SCAR=0.0
533 9 return File::Spec->catdir($self->{path}, @comp);
534 }
535  
536 sub _used_digests {
537 3 ▶ my ($self) = @_;
Subroutine _used_digests: called CC=1 SCAR=0.0
538 3 return $self->{used_digests};
539 }
540  
541 sub _init_link_style {
542 2 ▶ my ($self) = @_;
Subroutine _init_link_style: called CC=5 SCAR=21.7
543  
544 2 ▶ unless ($self->{_init_link_style}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_init_link_style"}) 2 0 2
545 2 my $link_style = $self->setting('link_style');
546  
547 2 $self->{_init_link_style} = 1;
548  
549 2 ▶ if ($link_style eq '2-level') {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_style eq "2-level") 0 2 2
550 0 foreach my $digest (@{$self->_used_digests}) {
551 0 ▶ for (my $i = 0; $i < 0x100; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 256) 0 0 0
552 0 mkdir($self->_directory(v2 => by => $digest => sprintf('%02x', $i)));
553 }
554 }
555 }
556 }
557 }
558  
559 sub _init_store_style {
560 1 ▶ my ($self) = @_;
Subroutine _init_store_style: called CC=6 SCAR=22.3
561  
562 1 ▶ unless ($self->{_init_store_style}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_init_store_style"}) 1 0 1
563 1 my $store_style = $self->setting('store_style');
564  
565 1 $self->{_init_store_style} = 1;
566  
567 1 ▶ if ($store_style eq '2-level-contentise' || $store_style eq '1-level-contentise') {
Branch: 1 branch
BranchTrueFalseTotal
if ($store_style eq "2-level-contentise" or $store_style eq "1-level-contentise") 1 0 1
Condition33%1/3
$store_style eq "2-level-contentise" or $store_style eq "1-level-contentise"
l!l&&r!l&&!r
010
MC/DC0%0/2
$store_style eq "2-level-contentise" or $store_style eq "1-level-contentise"
$store_style eq "2-level-contentise" $store_style eq "1-level-contentise"
Truth table33%1/3
A or B
A= $store_style eq "2-level-contentise"B= $store_style eq "1-level-contentise"
ABresult
1X1
011
000
568 1 mkdir($self->_directory(v2 => store => 'by-contentise'));
569 }
570  
571 1 ▶ if ($store_style eq '2-level-contentise') {
Branch: 1 branch
BranchTrueFalseTotal
if ($store_style eq "2-level-contentise") 0 1 1
572 0 ▶ for (my $i = 0; $i < 0x100; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 256) 0 0 0
573 0 mkdir($self->_directory(v2 => store => 'by-contentise' => sprintf('%02x', $i)));
574 }
575 }
576 }
577 }
578  
579 sub _scan_subdir {
580 1 ▶ my ($self, $dirname, %opts) = @_;
Subroutine _scan_subdir: called CC=6 SCAR=18.7
581 1 my $dbprefix = $opts{dbprefix};
582  
583 1 ▶ opendir(my $dir, $dirname) // croak $!;
Condition33%1/3
opendir(my $dir, $dirname) // croak($!)
l!l&&r!l&&!r
100
MC/DC0%0/2
opendir(my $dir, $dirname) // croak($!)
opendir(my $dir, $dirname) croak($!)
Truth table33%1/3
A // B
A= opendir(my $dir, $dirname)B= croak($!)
ABresult
1X1
011
000
584 1 ▶ while (defined(my $ent = readdir($dir))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ent = readdir $dir)) 3 1 4
585 3 my $filename;
586 my $fh;
587  
588 3 ▶ next if $ent =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $ent =~ /^\./ 2 1 3
589  
590 1 $filename = File::Spec->catfile($dirname, $ent);
591 1 open($fh, '<', $filename);
592 1 ▶ if ($fh && -f $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh and -f $fh) { } 1 0 1
Condition33%1/3
$fh and -f $fh
!ll&&!rl&&r
001
MC/DC0%0/2
$fh and -f $fh
$fh -f $fh
Truth table33%1/3
A and B
A= $fhB= -f $fh
ABresult
0X0
100
111
593 1 my $dbname = File::Spec->catfile($dbprefix, $ent);
594 1 $self->_scan_file($fh, $dbname, %opts);
595 #warn sprintf('ent=<%s>, filename=<%s>, dbprefix=<%s>, dbname=<%s>', $ent, $filename, $dbprefix, $dbname);
596 } else {
597 0 $self->_scan_subdir(File::Spec->catdir($dirname, $ent), %opts, dbprefix => File::Spec->catdir($dbprefix, $ent));
598 }
599 }
600 1 closedir($dir);
601 }
602  
603 sub _scan_file {
604 1 ▶ my ($self, $fh, $dbname, %opts) = @_;
Subroutine _scan_file: called CC=6 SCAR=19.5
605 1 my File::FStore::File $file;
606 1 my $update = $opts{update};
607 1 my $new;
608  
609 1 $opts{insert}->execute($dbname);
610 1 $new = $opts{insert}->rows;
611  
612 1 $file = $self->query(dbname => $dbname);
613 1 $file->_open(fh => $fh);
614  
615 1 ▶ if ($update eq 'all' || ($new && $update eq 'new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update eq 'all' or $new and $update eq 'new') { } 0 1 1
Condition33%2/6
$update eq "all" or $new and $update eq "new"
l!l&&r!l&&!r
001
$new and $update eq "new"
!ll&&!rl&&r
010
MC/DC0%0/3
$update eq "all" or $new and $update eq "new"
$update eq "all" $new $update eq "new"
Truth table25%1/4
A or B and C
A= $update eq "all"B= $newC= $update eq "new"
ABCresult
1XX1
0111
00X0
0100
616 0 $file->update(%opts{qw(no_digests on_pre_set on_post_set)});
617 } else {
618 1 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
619 $atime,$mtime,$ctime,$blksize,$blocks) = $file->stat;
620  
621 1 ▶ $opts{on_pre_set}->($file) if defined $opts{on_pre_set};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"on_pre_set"} 0 1 1
622 1 $file->set({
623 properties => {
624 size => $size,
625 inode => $ino,
626 },
627 });
628 1 ▶ $opts{on_post_set}->($file) if defined $opts{on_post_set};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"on_post_set"} 0 1 1
629 }
630 }
631  
632 sub _scrub_dir {
633 0 ▶ my ($self, $algoent, $dirname) = @_;
Subroutine _scrub_dir: not called CC=12 SCAR=50.5
634  
635 0 ▶ opendir(my $dir, $dirname) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $dirname or croak($!) 0 0 0
636 0 ▶ while (defined(my $ent = readdir($dir))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ent = readdir $dir)) 0 0 0
637 0 my $fullname;
638 my $hash;
639 0 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
640 $atime,$mtime,$ctime,$blksize,$blocks);
641 0 my $bad = 1;
642  
643 0 ▶ next if $ent =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $ent =~ /^\./ 0 0 0
644  
645 0 $fullname = File::Spec->catdir($dirname, $ent);
646 0 ▶ if (-d $fullname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $fullname) 0 0 0
647 0 $self->_scrub_dir($algoent => $fullname);
648 0 next;
649 }
650  
651 0 $fullname = File::Spec->catfile($dirname, $ent);
652 0 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
653 $atime,$mtime,$ctime,$blksize,$blocks) = stat($fullname);
654  
655 0 ▶ if (defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) 0 0 0
656 0 my ($hash) = $ent =~ /^([0-9a-fA-F]+)(?:\..+)?$/;
657 0 ▶ if (defined $hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash) 0 0 0
658 0 my $file;
659  
660 0 $hash = lc($hash);
661 0 $file = eval {$self->query(digests => $algoent => $hash)};
662  
663 0 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 0 0
664 0 ▶ my $want_size = eval { $file->get(properties => 'size') } // $size;
Condition0%0/3
eval { // $size
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $file->get("properties", "size") } } // $size
eval { do { $file->get("properties", "size") } } $size
Truth table0%0/3
A // B
A= eval { do { $file->get("properties", "size") } }B= $size
ABresult
1X1
011
000
665 0 ▶ if ($size == $want_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size == $want_size) 0 0 0
666 0 $bad = 0;
667 }
668 }
669 }
670 }
671  
672 #warn $algoent.' -> '.$ent.' -> '.$bad;
673 0 ▶ if ($bad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad) 0 0 0
674 0 ▶ unlink($fullname) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $fullname or croak($!) 0 0 0
675 }
676 }
677 0 closedir($dir);
678 }
679  
680 3 1;
681  
682 __END__
683  
684 =pod
685  
686 =encoding UTF-8
687  
688 =head1 NAME
689  
690 File::FStore - Module for interacting with file stores
691  
692 =head1 VERSION
693  
694 version v0.06
695  
696 =head1 SYNOPSIS
697  
698 use File::FStore;
699  
700 my File::FStore $store = File::FStore->new(path => '...');
701  
702 my @files = $store->query(properties => mediasubtype => 'image/png', order => 'asc', offset => 10, limit => 5);
703  
704 my $fh = $store->query(digests => 'sha-3-224' => '6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7')->open;
705  
706 This package provides access to a hash based/final state file store in Fellig format.
707 Files in such a store are considered final, meaning their content will no longer be altered.
708  
709 In addition to digests (hashes) some file level metadata is kept in the store (see L<File::FStore::File/PROPERTIES>).
710 This metadata is mainly used to ensure the integrity of the store.
711 Other metadata is intentionally not supported.
712 For storage of other metadata see L<Data::TagDB> and L</db>.
713 For reading metadata and file analysis see L<File::Information> and L</fii>.
714  
715 This package inherits from L<Data::Identifier::Interface::Known>.
716  
717 =head1 METHODS
718  
719 =head2 create
720  
721 my File::FStore $store = File::FStore->create(path => ..., ...);
722  
723 Creates a new file store.
724 C<die>s if the store cannot be created or already exists or invalid options are passed.
725 Takes the same options as L</new> plus the following:
726  
727 =over
728  
729 =item C<digests>
730  
731 List of digests to be used in the store. Each digest is given in the universal tag format (or utag)
732 (e.g. C<sha-3-224>.
733 The list can be passed as a arrayref or as a comma seperated list.
734  
735 The list can contain digests that are not supported by the system this runs on.
736 They may for example still be used with import/export functions.
737  
738 The list can be adjusted at a later time.
739  
740 =back
741  
742 =head2 new
743  
744 my File::FStore $store = File::FStore->new(path => ..., ...);
745  
746 Creates a new instance of the store and opens it.
747  
748 The following options are supported:
749  
750 =over
751  
752 =item C<db>
753  
754 A L<Data::TagDB> object. See L</db>.
755  
756 =item C<extractor>
757  
758 A L<Data::URIID> object. See L</extractor>.
759  
760 =item C<fii>
761  
762 A L<File::Information> object. See L</fii>.
763  
764 =item C<link_style>
765  
766 (since v0.05)
767  
768 The style to use for links. One of: C<none>, C<1-level>, or C<2-level>.
769  
770 B<Note:>
771 This does not affect already existing links unless one of the relevant calls is made.
772  
773 =item C<path>
774  
775 The path to the store.
776  
777 =item C<store_style>
778  
779 (since v0.05)
780  
781 The style to use for storing files. One of: C<1-level-contentise>, or C<1-level-contentise>.
782  
783 B<Note:>
784 This does not affect already existing files.
785  
786 =item C<weak>
787  
788 Marks the value for C<db>, C<extractor>, C<fii> as weak.
789 If only a specific one needs needs to be weaken use L</attach>.
790  
791 =back
792  
793 =head2 close
794  
795 $store->close;
796  
797 Closes the store. Any interaction with this object or any related objects after this call is invalid.
798  
799 =head2 setting
800  
801 my $value = $store->setting($key);
802  
803 (since v0.05)
804  
805 Gets a setting of the store.
806  
807 The type of the returned value depends on the key.
808  
809 =head2 in_transaction
810  
811 $db->in_transaction(ro => sub { ....});
812 # or:
813 $db->in_transaction(rw => sub { ....});
814  
815 Runs a block of code (a subref) inside a transaction.
816  
817 The passed block is run in a transaction. The transaction is commited after the code finishes.
818  
819 The type of the transaction can be C<ro> (read only) or C<rw> (read-write).
820 The module may optimise based on this information.
821 If a write operation is performed in a transaction that is marked C<ro> the behaviour is unspecified.
822  
823 Calls to this method can be stacked freely.
824 For example the following is valid:
825  
826 $store->in_transaction(ro => sub {
827 # do some read...
828 $store->in_transaction(rw => sub {
829 # do some write...
830 });
831 # do more reading, writing is invalid here
832 });
833  
834 B<Note:>
835 If the code C<die>s the transaction is aborted and the error is raised again.
836 Note that this affects all currently open transactions (as per stacking).
837 If the (parent) transaction is already aborted when this method is called the code block might not be run at all.
838  
839 B<Note:>
840 Data written might only be visible to other handles of the same database once I<all>
841 transactions have been finished.
842  
843 B<Note:>
844 It is undefined what the state of C<@_> is within the callback.
845  
846 =head2 query
847  
848 my File::FStore::File $file = $store->query(...);
849 # or:
850 my @files = $store->query(...);
851  
852 # e.g.:
853 my File::FStore::File $file = $store->query(digests => 'sha-3-512' => $digest);
854 # or:
855 my File::FStore::File $file = $store->query(digests => {'sha-3-512' => $digest});
856 # or:
857 foreach my File::FStore::File $file ($store->query(properties => size => 64)) {
858 # ...
859 }
860  
861 Returns files matching the given query.
862 If called in scalar context the method will return the file if the query matches one file.
863 If it matches zero or more than one it will C<die>.
864  
865 The query contains of commands followed by arguments.
866 It is optimised internally for performance, so order of arguments does not matter.
867  
868 Currently the following commands are defined:
869  
870 =over
871  
872 =item C<properties>
873  
874 This takes a key and value or a single hashref with properties to match exactly.
875 The value can also be an arrayref of values (which are or-ed).
876  
877 =item C<digests>
878  
879 This works the same as C<properties> but matches the digests.
880  
881 =item C<all>
882  
883 Selects all files. Takes no arguments.
884 This should be used with care as a very long list might be returned.
885  
886 =item C<dbname>
887  
888 Selects files by their I<dbname> (see L<File::FStore::File/dbname>).
889 Takes a single filename or a list of filenames (as an arrayref).
890  
891 =item C<ise>
892  
893 Selects files by their I<ise> (such as their L<File::FStore::File/contentise>, or L<File::FStore::File/inodeise>).
894 Takes a single ISE,
895 L<Data::Identifier>,
896 L<Data::TagDB::Tag>,
897 L<Data::URIID::Base>,
898 or a list of them (as an arrayref) or a L<Data::Identifier::Cloudlet>.
899  
900 If a L<Data::Identifier::Cloudlet> all entries are used (see L<Data::Identifier::Cloudlet/entries>), not just root entries.
901  
902 =item C<min_size>
903  
904 Selects files by their minimum size. This uses the C<size> value of the C<properties> domain.
905  
906 =item C<max_size>
907  
908 Selects files by their maximum size. This uses the C<size> value of the C<properties> domain.
909  
910 =item C<limit>
911  
912 Limits the number of returned files.
913 It is undefined what happens if this is used in scalar context.
914  
915 =item C<offset>
916  
917 The offset into the list. Often used with C<limit> to implement pagination.
918 B<Note:>
919 It is important to also give C<order> when using this, as otherwise the order is not stable.
920  
921 =item C<order>
922  
923 The order in which to return the files. One of C<asc> or C<desc>.
924  
925 =back
926  
927 =head2 scrub
928  
929 $store->scrub;
930  
931 Cleans up the store, including removing dangling links.
932  
933 This is normally only needed if files where removed or files got corrupted, or store settings have been altered.
934 However it is a good idea to call this method once in a while to maintain the store over a long time.
935  
936 It is typical application to call L</scan> after this call to check and regenerate active symlinks.
937  
938 B<Note:>
939 This method may take some time to run. Generally speaking the call takes more time the larger the store is.
940  
941 =head2 scan
942  
943 $store->scan;
944 # or:
945 $store->scan(%opts);
946  
947 Scans the store for existing and new files.
948 As this method checks all files in the store and may do calculations this method might run several seconds.
949  
950 The following options are supported:
951  
952 =over
953  
954 =item C<update>
955  
956 One of C<all> (default), C<new>, or C<none>.
957 This determines whether L<File::FStore::File/update> is called on all files, only new ones, or none.
958  
959 =item C<no_digests>
960  
961 Passed to L<File::FStore::File/update>.
962  
963 =item C<on_pre_set>
964  
965 See L<File::FStore::File/update>. Also called on each file if no update is performed.
966  
967 =item C<on_post_set>
968  
969 See L<File::FStore::File/update>. Also called on each file if no update is performed.
970  
971 =back
972  
973 =head2 fix
974  
975 $store->fix(qw(fixes...));
976  
977 Runs maintenance/fixes to the store.
978  
979 Takes the names of the fixes to run as arguments.
980 The order does not matter, as the method reorders them internally.
981  
982 On any error this method C<die>s.
983  
984 Currently the following fixes are supported:
985  
986 =over
987  
988 =item C<remove-inode>
989  
990 Removes the inode properties from the store.
991 This is useful when transfering the store to another filesystem,
992 or if the store is managed externally (e.g. via an vcs).
993  
994 =item C<remove-inodeise>
995  
996 Removes the inodeise properties from the store.
997 This is useful when transfering the store to another filesystem,
998 or if the store is managed externally (e.g. via an vcs).
999 In contrast to C<remove-inode> this might not be needed, depending on
1000 which schema for inodeise was used.
1001  
1002 =item C<remove-mediasubtype>
1003  
1004 Removes the mediasubtype properties from the store.
1005 This is useful when you imported invalid mediasubtypes from a bad source.
1006 Adding C<scan> to the list of fixes will add correct values back.
1007  
1008 =item C<scan>
1009  
1010 Runs L</scan> with default options.
1011  
1012 =item C<scrub>
1013  
1014 Runs L</scrub>.
1015  
1016 =item C<upgrade>
1017  
1018 Runs L<File::FStore::Migration/upgrade>.
1019  
1020 =back
1021  
1022 =head2 new_adder
1023  
1024 my File::FStore::Adder $adder = $store->new_adder;
1025  
1026 Create a new L<File::FStore::Adder>.
1027  
1028 =head2 migration
1029  
1030 my File::FStore::Migration $migration = $self->migration;
1031  
1032 Returns a new L<File::FStore::Migration> object for this store.
1033  
1034 =head2 export
1035  
1036 $migration->export_data(...);
1037  
1038 (since v0.01, deprecated since v0.05).
1039  
1040 Deprecated proxy for L<File::FStore::Migration/export_data>.
1041  
1042 This proxy will be removed in version v0.10. Future versions of this proxy may C<warn> about it's usage.
1043  
1044 =head2 import_data
1045  
1046 $store->import_data(...);
1047  
1048 (since v0.01, deprecated since v0.05).
1049  
1050 Deprecated proxy for L<File::FStore::Migration/import_data>.
1051  
1052 This proxy will be removed in version v0.10. Future versions of this proxy may C<warn> about it's usage.
1053  
1054 =head2 attach
1055  
1056 $self->attach(key => $obj, ...);
1057 # or:
1058 $self->attach(key => $obj, ..., weak => 1);
1059  
1060 Attaches objects of the given type.
1061 Takes the same list of objects as L</new>.
1062  
1063 If an object is allready attached for the given key this method C<die>s unless the object is actually the same.
1064  
1065 If C<weak> is set to a true value the object reference becomes weak.
1066  
1067 =head2 db
1068  
1069 my Data::TagDB $db = $store->db;
1070 # or:
1071 my Data::TagDB $db = $store->db(default => $def);
1072  
1073 Returns the instance of L<Data::TagDB> if any was given via L</new>.
1074  
1075 If no value is known returns the value of the option C<default> (if passed) or C<die>s.
1076  
1077 =head2 extractor
1078  
1079 my Data::URIID $extractor = $store->extractor;
1080 # or:
1081 my Data::URIID $extractor = $store->extractor(default => $def);
1082  
1083 Returns the instance of L<Data::URIID> if any was given via L</new>.
1084  
1085 If no value is known returns the value of the option C<default> (if passed) or C<die>s.
1086  
1087 =head2 fii
1088  
1089 my File::Information $fii = $store->fii;
1090  
1091 Returns the instance of L<File::Information> passed via L</new> or a internally created one if none were given.
1092  
1093 B<Note:>
1094 If the option C<default> is passed, it is ignored with no error.
1095  
1096 =head2 known
1097  
1098 my @list = File::FStore->known($class [, %opts ] );
1099 # or:
1100 my @list = $store->known($class [, %opts ] );
1101  
1102 (since v0.05)
1103  
1104 Proxy for L<File::FStore::File/known>.
1105  
1106 =head1 AUTHOR
1107  
1108 Philipp Schafft <lion@cpan.org>
1109  
1110 =head1 COPYRIGHT AND LICENSE
1111  
1112 This software is Copyright (c) 2025 by Philipp Schafft <lion@cpan.org>.
1113  
1114 This is free software, licensed under:
1115  
1116 The Artistic License 2.0 (GPL Compatible)
1117  
1118 =cut
« blib/lib/File/FStore.pm ↑ Summary blib/lib/File/FStore/Migration.pm »