| 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::Migration; | ||
| 9 | |||
| 10 | use v5.10; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use File::Spec; | ||
| 16 | |||
| 17 | use parent 'File::FStore::Base'; | ||
| 18 | |||
| 19 | our $VERSION = v0.06; | ||
| 20 | |||
| 21 | |||
| 22 | sub upgrade { | ||
| 23 | my ($self, @args) = @_; | ||
| 24 | croak 'Stray options passed' if scalar @args; | ||
| 25 | # Currently a no-op. | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | #@returns File::FStore::Adder | ||
| 30 | sub new_adder { | ||
| 31 | my ($self, @args) = @_; | ||
| 32 | return $self->store->new_adder(@args); | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub import_data { | ||
| 37 | my ($self, $handle, %opts) = @_; | ||
| 38 | my $store = $self->store; | ||
| 39 | my $format = delete($opts{format}) // 'json'; | ||
| 40 | |||
| 41 | croak 'Stray options passed' if scalar keys %opts; | ||
| 42 | |||
| 43 | $store->in_transaction(rw => sub { | ||
| 44 | if ($format eq 'json') { | ||
| 45 | require JSON; | ||
| 46 | my $json = do { | ||
| 47 | local $/ = undef; | ||
| 48 | JSON::decode_json(<$handle>); | ||
| 49 | }->{files}; | ||
| 50 | foreach my $dbname (keys %{$json}) { | ||
| 51 | my $file = $store->query(dbname => $dbname); | ||
| 52 | my $d = $json->{$dbname}; | ||
| 53 | $d = { | ||
| 54 | properties => $d->{properties} // {}, | ||
| 55 | digests => $d->{hashes} // {}, | ||
| 56 | }; | ||
| 57 | $file->set($d); | ||
| 58 | } | ||
| 59 | } else { | ||
| 60 | croak 'Unsupported format given: '.$format; | ||
| 61 | } | ||
| 62 | }); | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub export_data { | ||
| 67 | my ($self, $handle, %opts) = @_; | ||
| 68 | my $format = delete($opts{format}) // 'json'; | ||
| 69 | my $list = delete($opts{list}); | ||
| 70 | my $query = delete($opts{query}); | ||
| 71 | |||
| 72 | croak 'Stray options passed' if scalar keys %opts; | ||
| 73 | |||
| 74 | $list //= do { | ||
| 75 | $query //= ['all']; | ||
| 76 | [$self->store->query(@{$query})]; | ||
| 77 | }; | ||
| 78 | |||
| 79 | if ($format eq 'json') { | ||
| 80 | require JSON; | ||
| 81 | $handle->say(JSON::encode_json({ | ||
| 82 | files => { | ||
| 83 | map { | ||
| 84 | my $res = $_->get; | ||
| 85 | $res->{hashes} = delete $res->{digests}; | ||
| 86 | $_->dbname => $res | ||
| 87 | } @{$list} | ||
| 88 | }, | ||
| 89 | })); | ||
| 90 | } elsif ($format eq 'valuefile') { | ||
| 91 | require File::ValueFile::Simple::Writer; | ||
| 92 | my $writer = File::ValueFile::Simple::Writer->new($handle, format => 'e5da6a39-46d5-48a9-b174-5c26008e208e'); | ||
| 93 | my %mediasubtype_cache; | ||
| 94 | |||
| 95 | foreach my $file (@{$list}) { | ||
| 96 | my Data::Identifier $ise = Data::Identifier->new(uuid => $file->contentise(as => 'uuid'), displayname => $file->dbname); | ||
| 97 | my $size = eval {$file->get(properties => 'size')}; | ||
| 98 | my $mediasubtype = eval {$file->get(properties => 'mediasubtype')}; | ||
| 99 | my $digests = $file->get('digests'); | ||
| 100 | |||
| 101 | if (defined $mediasubtype) { | ||
| 102 | $mediasubtype = $mediasubtype_cache{$mediasubtype} //= Data::Identifier::Generate->generic( | ||
| 103 | namespace => '50d7c533-2d9b-4208-b560-bcbbf75ce3f9', | ||
| 104 | input => $mediasubtype, | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | |||
| 108 | $writer->write; | ||
| 109 | $writer->write_tag_ise($ise); | ||
| 110 | |||
| 111 | if (defined $size) { | ||
| 112 | $writer->write_tag_metadata($ise, '1cd4a6c6-0d7c-48d1-81e7-4e8d41fdb45d', $size); | ||
| 113 | foreach my $digest (sort keys %{$digests}) { | ||
| 114 | $writer->write_tag_metadata($ise, '79385945-0963-44aa-880a-bca4a42e9002', sprintf('v0 %s bytes 0-%u/%u %s', $digest, $size - 1, $size, $digests->{$digest})); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | $writer->write_tag_relation($ise, '448c50a8-c847-4bc7-856e-0db5fea8f23b', $mediasubtype) if defined $mediasubtype; | ||
| 118 | $writer->write_tag_relation($ise, 'd2750351-aed7-4ade-aa80-c32436cc6030', '52a516d0-25d8-47c7-a6ba-80983e576c54'); # also-has-role: proto-file | ||
| 119 | } | ||
| 120 | } else { | ||
| 121 | croak 'Unsupported format given: '.$format; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub insert_directory { | ||
| 127 | my ($self, $directory, %opts) = @_; | ||
| 128 | my $adder = $self->new_adder; | ||
| 129 | my $basename_filter = $opts{basename_filter}; | ||
| 130 | my $on_pre_insert = $opts{on_pre_insert}; | ||
| 131 | my $on_post_insert = $opts{on_post_insert}; | ||
| 132 | my $on_error = $opts{on_error} // sub {croak $@}; | ||
| 133 | my $update = $opts{update} // 'none'; # FIXME: $adder->insert already calls update, so this does not work as expected. | ||
| 134 | my $in_mode = $opts{in_mode} // 'link_in'; | ||
| 135 | my $in_func = $in_mode eq 'move_in' ? $adder->can('move_in') : $adder->can('link_in'); | ||
| 136 | |||
| 137 | $update = 'new' if $update eq 'all'; | ||
| 138 | $on_error = undef if $on_error eq 'ignore'; | ||
| 139 | |||
| 140 | opendir(my $d, $directory) or croak $!; | ||
| 141 | while (defined(my $e = readdir($d))) { | ||
| 142 | my $path; | ||
| 143 | |||
| 144 | next if $e =~ /^\./; | ||
| 145 | next if defined($basename_filter) && $e !~ $basename_filter; | ||
| 146 | |||
| 147 | $path = File::Spec->catfile($directory, $e); | ||
| 148 | if (-f $path) { | ||
| 149 | my $file; | ||
| 150 | |||
| 151 | $adder->$in_func($path); | ||
| 152 | |||
| 153 | $on_pre_insert->($adder, path => $path, basename => $e) if defined $on_pre_insert; | ||
| 154 | $file = eval { $adder->insert }; | ||
| 155 | $on_error->(undef, path => $path, basename => $e) if $on_error && $@; | ||
| 156 | $adder->reset; | ||
| 157 | |||
| 158 | if (defined $file) { | ||
| 159 | if ($update eq 'new') { | ||
| 160 | $file->update(%opts{qw(on_pre_set on_post_set)}); | ||
| 161 | } | ||
| 162 | |||
| 163 | $on_post_insert->($file, path => $path, basename => $e) if defined $on_post_insert; | ||
| 164 | } | ||
| 165 | } else { | ||
| 166 | $path = File::Spec->catdir($directory, $e); | ||
| 167 | #warn 'D: '.$path; | ||
| 168 | $self->insert_directory($path, %opts); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | closedir($d); | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub insert_tagpool { | ||
| 176 | my ($self, $path, %opts) = @_; | ||
| 177 | my $on_pre_insert = $opts{on_pre_insert}; | ||
| 178 | |||
| 179 | $opts{on_pre_insert} = sub { | ||
| 180 | my ($adder, %opts) = @_; | ||
| 181 | my ($uuid) = $opts{basename} =~ /^file\.([0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12})(?:\..*)?$/; | ||
| 182 | |||
| 183 | $adder->set(properties => inodeise => $uuid) if defined $uuid; | ||
| 184 | |||
| 185 | $on_pre_insert->($adder, %opts) if defined $on_pre_insert; | ||
| 186 | }; | ||
| 187 | |||
| 188 | $opts{basename_filter} = qr/^file\./; | ||
| 189 | |||
| 190 | $self->insert_directory( | ||
| 191 | File::Spec->catdir($path, 'data'), | ||
| 192 | %opts, | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | |||
| 196 | # ---- Private helpers ---- | ||
| 197 | sub _new { | ||
| 198 | my ($pkg, %opts) = @_; | ||
| 199 | my $self = bless \%opts, $pkg; | ||
| 200 | |||
| 201 | croak 'No store is given' unless defined $self->{store}; | ||
| 202 | |||
| 203 | return $self; | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; | ||
| 207 | |||
| 208 | __END__ | ||
| 209 | |||
| 210 | =pod | ||
| 211 | |||
| 212 | =encoding UTF-8 | ||
| 213 | |||
| 214 | =head1 NAME | ||
| 215 | |||
| 216 | File::FStore::Migration - Module for interacting with file stores | ||
| 217 | |||
| 218 | =head1 VERSION | ||
| 219 | |||
| 220 | version v0.06 | ||
| 221 | |||
| 222 | =head1 SYNOPSIS | ||
| 223 | |||
| 224 | use File::FStore; | ||
| 225 | |||
| 226 | my File::FStore::Migration $migration = $store->migration; | ||
| 227 | |||
| 228 | This package provides simple migration utilities. | ||
| 229 | |||
| 230 | This package inherits from L<File::FStore::Base>. | ||
| 231 | However L<File::FStore::Base/contentise> is not supported by this package. | ||
| 232 | Calling that method will C<die>. | ||
| 233 | |||
| 234 | =head1 METHODS | ||
| 235 | |||
| 236 | =head2 upgrade | ||
| 237 | |||
| 238 | $migration->upgrade; | ||
| 239 | |||
| 240 | Upgrade database to current schema. | ||
| 241 | |||
| 242 | =head2 new_adder | ||
| 243 | |||
| 244 | my File::FStore::Adder $adder = $migration->new_adder; | ||
| 245 | |||
| 246 | Proxy for L<File::FStore/new_adder>. | ||
| 247 | |||
| 248 | =head2 import_data | ||
| 249 | |||
| 250 | $migration->import_data($handle); | ||
| 251 | # or: | ||
| 252 | $migration->import_data($handle, format => ...); | ||
| 253 | |||
| 254 | (since v0.04) | ||
| 255 | |||
| 256 | This method allows importing data into the database from an open handle. | ||
| 257 | |||
| 258 | The data is imported the same way as per L<File::FStore::File/set> including all safety checks. | ||
| 259 | |||
| 260 | The following (all optional) options are supported: | ||
| 261 | |||
| 262 | =over | ||
| 263 | |||
| 264 | =item C<format> | ||
| 265 | |||
| 266 | The format to use. Currently supported is C<json> for the classic JSON format. | ||
| 267 | |||
| 268 | =back | ||
| 269 | |||
| 270 | =head2 export_data | ||
| 271 | |||
| 272 | $migration->export_data($handle, %opts); | ||
| 273 | |||
| 274 | (since v0.04) | ||
| 275 | |||
| 276 | Exports the store metadata. | ||
| 277 | |||
| 278 | The following (all optional) options are supported: | ||
| 279 | |||
| 280 | =over | ||
| 281 | |||
| 282 | =item C<format> | ||
| 283 | |||
| 284 | The format to use. | ||
| 285 | Currently supported is C<json> for the classic JSON format. | ||
| 286 | And C<valuefile> for universal tag based ValueFile output. | ||
| 287 | |||
| 288 | =item C<list> | ||
| 289 | |||
| 290 | A arrayref to a list of files to include. | ||
| 291 | |||
| 292 | =item C<query> | ||
| 293 | |||
| 294 | A arrayref with a query in the same format as L</query> takes it. | ||
| 295 | |||
| 296 | =back | ||
| 297 | |||
| 298 | B<Note:> | ||
| 299 | If you want a stringified result you can use a memory handle as documented in L<perlfunc/open>. | ||
| 300 | E.g.: C<open(my $fh, 'E<gt>', \$result)>. | ||
| 301 | |||
| 302 | =head2 insert_directory | ||
| 303 | |||
| 304 | $migration->insert_directory($path, %opts); | ||
| 305 | |||
| 306 | Inserts the files in the given directory into the store. | ||
| 307 | |||
| 308 | C<$path> is the path of the directory (in OS specific format). | ||
| 309 | |||
| 310 | The following options (all optional) are supported: | ||
| 311 | |||
| 312 | =over | ||
| 313 | |||
| 314 | =item C<basename_filter> | ||
| 315 | |||
| 316 | A regex used to filter files before insert by basename. | ||
| 317 | See L<perlop/qr>. | ||
| 318 | |||
| 319 | B<Note:> | ||
| 320 | This filter applies to files and sub-directories alike. | ||
| 321 | It matches the OS specific basename format. | ||
| 322 | |||
| 323 | =item C<in_mode> | ||
| 324 | |||
| 325 | The mode to use. C<link_in> (the default) or C<move_in>. | ||
| 326 | See L<File::FStore::Adder/link_in> and L<File::FStore::Adder/move_in>. | ||
| 327 | |||
| 328 | B<Note:> | ||
| 329 | While C<move_in> can be more easy to use and slightly more portable, | ||
| 330 | it comes at a higher risk of loosing files if the insert fails. | ||
| 331 | |||
| 332 | =item C<on_error> | ||
| 333 | |||
| 334 | A function to call on insert errors. | ||
| 335 | The first argument is undefined. | ||
| 336 | The following arguments are a hash. | ||
| 337 | |||
| 338 | The key C<path> holds the path to the file to be inserted (in OS specific format). | ||
| 339 | The key C<basename> holds the basename of the file (in OS specific format). | ||
| 340 | |||
| 341 | The error can be found in C<$@>. | ||
| 342 | |||
| 343 | =item C<on_post_insert> | ||
| 344 | |||
| 345 | A function to be called after the insert. | ||
| 346 | The first argument is the newly created L<File::FStore::File>. | ||
| 347 | The rest is a hash as per C<on_error>. | ||
| 348 | |||
| 349 | =item C<on_pre_insert> | ||
| 350 | |||
| 351 | A function to be called before the insert. | ||
| 352 | The first argument is the used L<File::FStore::Adder>. | ||
| 353 | The rest is a hash as per C<on_error>. | ||
| 354 | |||
| 355 | =back | ||
| 356 | |||
| 357 | =head2 insert_tagpool | ||
| 358 | |||
| 359 | $migration->insert_tagpool($path, %opts); | ||
| 360 | |||
| 361 | Inserts the content of a tagpool into the store. | ||
| 362 | |||
| 363 | C<$path> is the same path to the pool (in OS specific format). | ||
| 364 | |||
| 365 | This method accepts all options of L</insert_directory> but C<basename_filter>. | ||
| 366 | |||
| 367 | B<Note:> | ||
| 368 | For best performace the object returned by L<File::FStore::Base/fii> should be aware of the pool. | ||
| 369 | |||
| 370 | =head1 AUTHOR | ||
| 371 | |||
| 372 | Philipp Schafft <lion@cpan.org> | ||
| 373 | |||
| 374 | =head1 COPYRIGHT AND LICENSE | ||
| 375 | |||
| 376 | This software is Copyright (c) 2025 by Philipp Schafft <lion@cpan.org>. | ||
| 377 | |||
| 378 | This is free software, licensed under: | ||
| 379 | |||
| 380 | The Artistic License 2.0 (GPL Compatible) | ||
| 381 | |||
| 382 | =cut |