blib/lib/File/FStore.pm untested

filter: Statementstmt 0.0% 0 / 427 Branchbran 0.0% 0 / 188 Conditioncond 0.0% 0 / 79 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 43 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 765 CC 95 SCAR 54.8
CC
95
Cov
0%
query
930.0
new
182.0
in_transaction
90.0
CRAP 240.7
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 use v5.10;
11 use strict;
12 use warnings;
13  
14 use Carp;
15 use DBI;
16 use File::Spec;
17 use Data::Identifier;
18 use Data::Identifier::Generate;
19 use Scalar::Util qw(weaken);
20  
21 use File::FStore::File;
22  
23 use parent 'Data::Identifier::Interface::Known';
24  
25 our $VERSION = v0.06;
26  
27 use constant {
28 DEFAULT_LINK_STYLE => '1-level',
29 DEFAULT_STORE_STYLE => '1-level-contentise',
30 };
31  
32 my %_types = (
33 db => 'Data::TagDB',
34 extractor => 'Data::URIID',
35 fii => 'File::Information',
36 );
37  
38 our %_valid_digests = map {$_ => int((/^[^-]+-[^-]-([0-9]+)$/)[0]/4)} (
39 qw(md-5-128 sha-1-160),
40 (map {'sha-2-'.$_, 'sha-3-'.$_} 224, 256, 384, 512),
41 );
42  
43 our %_valid_link_styles = map {$_ => 1} qw(none 1-level 2-level);
44 our %_valid_store_styles = map {$_ => 1} qw(1-level-contentise 2-level-contentise);
45  
46 my %_flat_settings = map {$_ => $_} qw(link_style store_style);
47  
48  
49 sub create {
50 my ($pkg, %opts) = @_;
51 my $path = delete($opts{path});
52 my $digests = delete($opts{digests}) // [keys %_valid_digests];
53 my %extra;
54 my $dbh;
55  
56 foreach my $key (keys(%_types), qw(weak)) {
57 my $v = delete $opts{$key};
58 $extra{$key} = $v if defined $v;
59 }
60  
61 croak 'Stray options passed' if scalar keys %opts;
62  
63 $digests = [split(/\s*,\s*|\s+/, $digests)] unless ref $digests;
64 foreach my $digest (@{$digests}) {
65 croak 'Invalid digest: '.$digest unless defined $_valid_digests{$digest};
66 }
67  
68 mkdir($path) or croak $!;
69 mkdir(File::Spec->catdir($path, qw(v2))) or croak $!;
70 mkdir(File::Spec->catdir($path, qw(v2 store))) or croak $!;
71 mkdir(File::Spec->catdir($path, qw(v2 by))) or croak $!;
72 foreach my $digest (@{$digests}) {
73 mkdir(File::Spec->catdir($path, qw(v2 by), $digest)) or croak $!;
74 }
75  
76 $dbh = DBI->connect('dbi:SQLite:dbname='.File::Spec->catfile($path => v2 => store => 'db.sqlite'), undef, undef, { RaiseError => 1, PrintError => undef });
77 #@inject SQLITE
78 $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 $dbh->disconnect;
99  
100 return $pkg->new(path => $path, %extra);
101 }
102  
103  
104 #@returns __PACKAGE__
105 sub new {
106 my ($pkg, %opts) = @_;
107 my $path = delete $opts{path} // croak 'No path given';
108 my $weak = delete $opts{weak};
109 my @used_digests;
110 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 foreach my $key (keys %_types) {
119 my $v = delete $opts{$key};
120 next unless defined $v;
121 croak 'Invalid type for key: '.$key unless eval {$v->isa($_types{$key})};
122 $self->{$key} = $v;
123 weaken($self->{$key}) if $weak;
124 }
125  
126 if (defined(my $link_style = delete $opts{link_style})) {
127 $link_style = DEFAULT_LINK_STYLE if $link_style eq 'default';
128 croak 'Not a valid link style: '.$link_style unless defined $_valid_link_styles{$link_style};
129 $self->{link_style} = $link_style;
130 }
131  
132 if (defined(my $store_style = delete $opts{store_style})) {
133 $store_style = DEFAULT_STORE_STYLE if $store_style eq 'default';
134 croak 'Not a valid store style: '.$store_style unless defined $_valid_store_styles{$store_style};
135 $self->{store_style} = $store_style;
136 }
137  
138 croak 'Stray options passed' if scalar keys %opts;
139  
140 opendir(my $dir, $self->_directory(v2 => 'by')) or croak $!;
141 while (defined(my $ent = readdir($dir))) {
142 next if $ent =~ /^\./;
143 croak 'Invalid store, unsupported/invalid digest used: '.$ent unless $_valid_digests{$ent};
144 push(@used_digests, $ent);
145 }
146 closedir($dir);
147  
148 $self->{dbh} = DBI->connect('dbi:SQLite:dbname='.$self->_file(v2 => store => 'db.sqlite'), undef, undef, { RaiseError => 1, PrintError => undef });
149  
150 return $self;
151 }
152  
153  
154 sub close {
155 my ($self) = @_;
156 $self->DESTROY;
157 }
158  
159  
160 sub setting {
161 my ($self, $key) = @_;
162  
163 croak 'No key given' unless defined $key;
164  
165 if (defined(my $flatkey = $_flat_settings{$key})) {
166 return $self->{$flatkey};
167 }
168  
169 croak 'Unknown key: '.$key;
170 }
171  
172  
173 sub in_transaction {
174 my ($self, $type, $code) = @_;
175 my $error;
176  
177 croak 'Bad transaction type' unless $type eq 'ro' || $type eq 'rw';
178  
179 return undef if defined $self->{transaction_error};
180  
181 if ($self->{transaction_count}) {
182 if ($type ne 'ro' && $type ne $self->{transaction_type}) {
183 croak 'Invalid inner transaction type '.$type.' within outer '.$self->{transaction_type}.' transaction';
184 }
185 } else {
186 $self->{dbh}->begin_work;
187 $self->{transaction_type} = $type;
188 $self->{transaction_error} = undef;
189 }
190  
191 $self->{transaction_count}++;
192  
193 eval {
194 $code->();
195 };
196 $error = $@ if $@;
197  
198 $self->{transaction_error} //= $error;
199  
200 $self->{transaction_count}--;
201  
202 unless ($self->{transaction_count}) {
203 if (defined $self->{transaction_error}) {
204 $self->{dbh}->rollback;
205 } else {
206 $self->{dbh}->commit;
207 }
208 }
209  
210 croak $error if defined $error;
211 }
212  
213  
214 sub query {
215 my ($self, @query) = @_;
216 my %fields = (properties => {}, digests => {});
217 my $limit;
218 my $offset;
219 my $order;
220 my @q;
221 my @p;
222  
223 while (defined(my $cmd = shift(@query))) {
224 $cmd = 'digests' if $cmd eq 'hashes';
225  
226 if ($cmd eq 'properties' || $cmd eq 'digests') {
227 my $n = shift(@query) // croak 'Incomplete query';
228 my $t = $fields{$cmd};
229  
230 if (ref($n)) {
231 %{$t} = (%{$t}, %{$n});
232 } else {
233 my $v = shift(@query) // croak 'Incomplete query';
234 $t->{$n} = $v;
235 }
236 } elsif ($cmd eq 'all') {
237 push(@q, 'SELECT id FROM file');
238 } elsif ($cmd eq 'limit') {
239 $limit = shift(@query) // croak 'Incomplete query';
240 } elsif ($cmd eq 'offset') {
241 $offset = shift(@query) // croak 'Incomplete query';
242 } elsif ($cmd eq 'order') {
243 $order = shift(@query) // croak 'Incomplete query';
244 $order = uc($order);
245 croak 'Bad order' if $order ne 'ASC' && $order ne 'DESC';
246 } elsif ($cmd eq 'min_size') {
247 my $v = shift(@query) // croak 'Incomplete query';
248 push(@q, 'SELECT file AS id FROM file_properties WHERE key = \'size\' AND CAST(value AS BIG INTEGER) >= ?');
249 push(@p, $v);
250 } elsif ($cmd eq 'max_size') {
251 my $v = shift(@query) // croak 'Incomplete query';
252 push(@q, 'SELECT file AS id FROM file_properties WHERE key = \'size\' AND CAST(value AS BIG INTEGER) <= ?');
253 push(@p, $v);
254 } elsif ($cmd eq 'dbname') {
255 my $v = shift(@query) // croak 'Incomplete query';
256  
257 $v = [$v] unless ref $v;
258  
259 croak 'Empty list for dbname' unless scalar(@{$v});
260  
261 push(@q, 'SELECT id FROM file WHERE '.$self->_placeholders(filename => $v));
262 push(@p, @{$v});
263 } elsif ($cmd eq 'ise') {
264 my $v = shift(@query) // croak 'Incomplete query';
265  
266 if (ref $v) {
267 if (eval {$v->isa('Data::Identifier::Cloudlet')}) {
268 $v = [$v->entries];
269 } elsif (eval {$v->can('ise')}) {
270 $v = [$v];
271 }
272 } else {
273 $v = [$v];
274 }
275  
276 croak 'Empty list for ise' unless scalar(@{$v});
277  
278 foreach my $s (@{$v}) {
279 $s = $s->ise if ref $s;
280 }
281  
282 push(@q, 'SELECT file AS id FROM file_properties WHERE key IN (\'contentise\', \'inodeise\') AND '.$self->_placeholders(value => $v));
283 push(@p, @{$v});
284 } else {
285 croak 'Invalid query command: '.$cmd;
286 }
287 }
288  
289 foreach my $digest (keys %{$fields{digests}}) {
290 my $v = $fields{digests}{$digest};
291 $v = [$v] unless ref($v) eq 'ARRAY';
292 unshift(@q, 'SELECT file AS id FROM file_hash WHERE algo = ? AND '.$self->_placeholders(hash => $v));
293 unshift(@p, $digest, @{$v});
294 }
295  
296 foreach my $property (keys %{$fields{properties}}) {
297 my $v = $fields{properties}{$property};
298 $v = [$v] unless ref($v) eq 'ARRAY';
299 push(@q, 'SELECT file AS id FROM file_properties WHERE key = ? AND '.$self->_placeholders(value => $v));
300 push(@p, $property, @{$v});
301 }
302  
303 unless (scalar @q) {
304 croak 'No filter given';
305 }
306  
307 {
308 my $q = join(' INTERSECT ', @q);
309 my $sth;
310 my @result;
311  
312 if (defined $order) {
313 $q .= ' ORDER BY id '.$order;
314 }
315  
316 $limit = 2 unless wantarray;
317  
318 if (defined $limit) {
319 $q .= ' LIMIT ?';
320 push(@p, $limit);
321 }
322  
323 if (defined $offset) {
324 $q .= ' OFFSET ?';
325 push(@p, $offset);
326 }
327 $sth = $self->{dbh}->prepare($q);
328 $sth->execute(@p);
329  
330 while (my $row = $sth->fetchrow_arrayref) {
331 push(@result, File::FStore::File->_new(store => $self, dbid => $row->[0]));
332 }
333  
334 $sth->finish;
335  
336 if (wantarray) {
337 return @result;
338 } else {
339 croak 'Multiple results for query but called in scalar context' if scalar(@result) > 1;
340 croak 'No results for query but called in scalar context' if scalar(@result) < 1;
341 return $result[0];
342 }
343 }
344  
345 die 'BUG';
346 }
347  
348  
349 sub scrub {
350 my ($self, %opts) = @_;
351  
352 croak 'Stray options passed' if scalar keys %opts;
353  
354 $self->in_transaction(ro => sub {
355 opendir(my $algodir, $self->_directory('v2' => 'by')) or croak $!;
356 while (defined(my $algoent = readdir($algodir))) {
357 next if $algoent =~ /^\./;
358 $self->_scrub_dir($algoent => $self->_directory(v2 => by => $algoent));
359 }
360 closedir($algodir);
361 });
362 }
363  
364  
365 sub scan {
366 my ($self, %opts) = @_;
367 my %extra;
368  
369 foreach my $key (qw(update no_digests on_pre_set on_post_set)) {
370 $extra{$key} = delete $opts{$key};
371 }
372  
373 $extra{update} //= 'all';
374  
375 croak 'Stray options passed' if scalar keys %opts;
376  
377 $self->in_transaction(rw => sub {
378 my $insert = $self->{dbh}->prepare('INSERT OR IGNORE INTO file (filename) VALUES (?)');
379  
380 opendir(my $dir, $self->_directory('v2', 'store')) or croak $!;
381 while (defined(my $ent = readdir($dir))) {
382 my $dirname;
383  
384 next if $ent =~ /^\./;
385  
386 $dirname = $self->_directory('v2', 'store', $ent);
387  
388 if (-d $dirname) {
389 $self->_scan_subdir($dirname, dbprefix => $ent, insert => $insert, %extra);
390 }
391 }
392 closedir($dir);
393 });
394 }
395  
396  
397 sub fix {
398 my ($self, @fixes) = @_;
399 my %fixes = map {$_ => 1} @fixes;
400  
401 foreach my $fix (qw(upgrade scrub remove-inode remove-mediasubtype remove-inodeise scan)) {
402 next unless delete $fixes{$fix};
403  
404 if ($fix eq 'scrub') {
405 $self->scrub;
406 } elsif ($fix eq 'scan') {
407 $self->scan;
408 } elsif ($fix eq 'upgrade') {
409 $self->migration->upgrade;
410 } elsif ($fix =~ /^remove-(inode|mediasubtype|inodeise)$/) {
411 my $what = $1;
412 my $sth = $self->{dbh}->prepare('DELETE FROM file_properties WHERE key = ?');
413 $sth->execute($what);;
414 } else {
415 croak 'BUG';
416 }
417 }
418  
419 if (scalar keys %fixes) {
420 croak 'Invalid/unknown fixes passed: '.join(', ', keys %fixes);
421 }
422 }
423  
424  
425 #@returns File::FStore::Adder
426 sub new_adder {
427 my ($self) = @_;
428 require File::FStore::Adder;
429 return File::FStore::Adder->_new(store => $self);
430 }
431  
432  
433 #@returns File::FStore::Migration
434 sub migration {
435 my ($self) = @_;
436 require File::FStore::Migration;
437 return File::FStore::Migration->_new(store => $self);
438 }
439  
440  
441 sub export {
442 my ($self, @args) = @_;
443 return $self->migration->export_data(@args);
444 }
445  
446  
447 sub import_data {
448 my ($self, @args) = @_;
449 return $self->migration->import_data(@args);
450 }
451  
452  
453 sub attach {
454 my ($self, %opts) = @_;
455 my $weak = delete $opts{weak};
456  
457 foreach my $key (keys %_types) {
458 my $v = delete $opts{$key};
459 next unless defined $v;
460 croak 'Invalid type for key: '.$key unless eval {$v->isa($_types{$key})};
461 $self->{$key} //= $v;
462 croak 'Missmatch for key: '.$key unless $self->{$key} == $v;
463 weaken($self->{$key}) if $weak;
464 }
465  
466 croak 'Stray options passed' if scalar keys %opts;
467 }
468  
469  
470 #@returns Data::TagDB
471 sub db {
472 my ($self, %opts) = @_;
473 return $self->{db} if defined $self->{db};
474 return $opts{default} if exists $opts{default};
475 croak 'No database known';
476 }
477  
478  
479 #@returns Data::URIID
480 sub extractor {
481 my ($self, %opts) = @_;
482 return $self->{extractor} if defined $self->{extractor};
483 return $opts{default} if exists $opts{default};
484 croak 'No extractor known';
485 }
486  
487  
488 #@returns File::Information
489 sub fii {
490 my ($self) = @_;
491 return $self->{fii} if defined $self->{fii};
492  
493 require File::Information;
494 File::Information->VERSION(v0.06);
495 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 my ($pkg, @args) = @_;
504 return $pkg->File::FStore::File::_known_provider(@args);
505 }
506  
507  
508  
509 # ---- Private helpers ----
510  
511 sub _placeholders {
512 my ($self, $field, $list) = @_;
513 my $placeholders = '?,' x scalar(@{$list});
514  
515 $placeholders =~ s/,$//;
516  
517 return sprintf('%s IN (%s)', $field, $placeholders);
518 }
519  
520 sub DESTROY {
521 my ($self) = @_;
522 $self->{dbh}->disconnect if defined $self->{dbh};
523 %{$self} = ();
524 }
525  
526 sub _file {
527 my ($self, @comp) = @_;
528 return File::Spec->catfile($self->{path}, @comp);
529 }
530  
531 sub _directory {
532 my ($self, @comp) = @_;
533 return File::Spec->catdir($self->{path}, @comp);
534 }
535  
536 sub _used_digests {
537 my ($self) = @_;
538 return $self->{used_digests};
539 }
540  
541 sub _init_link_style {
542 my ($self) = @_;
543  
544 unless ($self->{_init_link_style}) {
545 my $link_style = $self->setting('link_style');
546  
547 $self->{_init_link_style} = 1;
548  
549 if ($link_style eq '2-level') {
550 foreach my $digest (@{$self->_used_digests}) {
551 for (my $i = 0; $i < 0x100; $i++) {
552 mkdir($self->_directory(v2 => by => $digest => sprintf('%02x', $i)));
553 }
554 }
555 }
556 }
557 }
558  
559 sub _init_store_style {
560 my ($self) = @_;
561  
562 unless ($self->{_init_store_style}) {
563 my $store_style = $self->setting('store_style');
564  
565 $self->{_init_store_style} = 1;
566  
567 if ($store_style eq '2-level-contentise' || $store_style eq '1-level-contentise') {
568 mkdir($self->_directory(v2 => store => 'by-contentise'));
569 }
570  
571 if ($store_style eq '2-level-contentise') {
572 for (my $i = 0; $i < 0x100; $i++) {
573 mkdir($self->_directory(v2 => store => 'by-contentise' => sprintf('%02x', $i)));
574 }
575 }
576 }
577 }
578  
579 sub _scan_subdir {
580 my ($self, $dirname, %opts) = @_;
581 my $dbprefix = $opts{dbprefix};
582  
583 opendir(my $dir, $dirname) // croak $!;
584 while (defined(my $ent = readdir($dir))) {
585 my $filename;
586 my $fh;
587  
588 next if $ent =~ /^\./;
589  
590 $filename = File::Spec->catfile($dirname, $ent);
591 open($fh, '<', $filename);
592 if ($fh && -f $fh) {
593 my $dbname = File::Spec->catfile($dbprefix, $ent);
594 $self->_scan_file($fh, $dbname, %opts);
595 #warn sprintf('ent=<%s>, filename=<%s>, dbprefix=<%s>, dbname=<%s>', $ent, $filename, $dbprefix, $dbname);
596 } else {
597 $self->_scan_subdir(File::Spec->catdir($dirname, $ent), %opts, dbprefix => File::Spec->catdir($dbprefix, $ent));
598 }
599 }
600 closedir($dir);
601 }
602  
603 sub _scan_file {
604 my ($self, $fh, $dbname, %opts) = @_;
605 my File::FStore::File $file;
606 my $update = $opts{update};
607 my $new;
608  
609 $opts{insert}->execute($dbname);
610 $new = $opts{insert}->rows;
611  
612 $file = $self->query(dbname => $dbname);
613 $file->_open(fh => $fh);
614  
615 if ($update eq 'all' || ($new && $update eq 'new')) {
616 $file->update(%opts{qw(no_digests on_pre_set on_post_set)});
617 } else {
618 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
619 $atime,$mtime,$ctime,$blksize,$blocks) = $file->stat;
620  
621 $opts{on_pre_set}->($file) if defined $opts{on_pre_set};
622 $file->set({
623 properties => {
624 size => $size,
625 inode => $ino,
626 },
627 });
628 $opts{on_post_set}->($file) if defined $opts{on_post_set};
629 }
630 }
631  
632 sub _scrub_dir {
633 my ($self, $algoent, $dirname) = @_;
634  
635 opendir(my $dir, $dirname) or croak $!;
636 while (defined(my $ent = readdir($dir))) {
637 my $fullname;
638 my $hash;
639 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
640 $atime,$mtime,$ctime,$blksize,$blocks);
641 my $bad = 1;
642  
643 next if $ent =~ /^\./;
644  
645 $fullname = File::Spec->catdir($dirname, $ent);
646 if (-d $fullname) {
647 $self->_scrub_dir($algoent => $fullname);
648 next;
649 }
650  
651 $fullname = File::Spec->catfile($dirname, $ent);
652 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
653 $atime,$mtime,$ctime,$blksize,$blocks) = stat($fullname);
654  
655 if (defined $size) {
656 my ($hash) = $ent =~ /^([0-9a-fA-F]+)(?:\..+)?$/;
657 if (defined $hash) {
658 my $file;
659  
660 $hash = lc($hash);
661 $file = eval {$self->query(digests => $algoent => $hash)};
662  
663 if (defined $file) {
664 my $want_size = eval { $file->get(properties => 'size') } // $size;
665 if ($size == $want_size) {
666 $bad = 0;
667 }
668 }
669 }
670 }
671  
672 #warn $algoent.' -> '.$ent.' -> '.$bad;
673 if ($bad) {
674 unlink($fullname) or croak $!;
675 }
676 }
677 closedir($dir);
678 }
679  
680 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