blib/lib/AmberDB/Tools/Maintain.pm untested

filter: Statementstmt 0.0% 0 / 556 Branchbran 0.0% 0 / 218 Conditioncond 0.0% 0 / 208 MC/DCmcdc 0.0% 0 / 126 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 1147 CC 110 SCAR 63.7
CC
110
Cov
0%
restore
1406.0
dump
702.0
all_tables
272.0
CRAP 583.4
1 package AmberDB::Tools::Maintain;
2  
3 use 5.016;
4 use warnings;
5 use strict;
6 use Carp qw(croak cluck);
7 use File::Spec;
8  
9 our $VERSION = '5.26.6';
10 my $CREATED = '2018-10-08';
11  
12 sub new {
13  
14 my $class = shift;
15 my $self = {};
16  
17 require AmberDB;
18  
19 my ( $adb, %inputs );
20  
21 if ( ref( $_[0] ) ) {
22 $adb = shift;
23 %inputs = @_;
24 }
25 else {
26 %inputs = @_;
27 $adb = AmberDB->new(%inputs);
28 }
29  
30 $self->{_adb} = $adb;
31  
32 foreach my $in ( keys %inputs ) {
33 $self->{ uc($in) } = $inputs{$in};
34 }
35 $self->{say} = "";
36  
37 bless $self, $class;
38 return $self;
39 }
40  
41 # my $ok = $tools->tie2csv("dbase_table");
42 # print $self->{say} if($ok);
43 # ---------------------------------------------------------------------
44 sub tie2csv {
45  
46 my ( $self, $tableid ) = @_;
47 my $adb = $self->{_adb} or return;
48  
49 $tableid or return;
50 my $table_path = $adb->table_path($tableid);
51  
52 my $i = 1;
53 if ( -e "$table_path.csv" ) {
54 my $day_id = $adb->day_id;
55 rename( "$table_path.csv", "$table_path-$day_id.csv" );
56 }
57  
58 # cevirme islemlerini yap
59 my $tie_path = "${table_path}.$adb->{db_ext}";
60 return 1 unless -e $tie_path;
61 $adb->table_read($tie_path) or return 1;
62  
63 my @records;
64 $adb->recs_scan(
65 $tie_path,
66 sub {
67 my ( $k, $v ) = @_;
68 my @fields = $adb->db_decode( $v );
69 push @records, [ $k, $adb->tsv_encode( @fields ) ];
70 $self->{say} .= "$i. $k ID record converted.\n\n";
71 $i++;
72 }
73 );
74  
75 @records = sort { $a->[0] <=> $b->[0] } @records;
76 open my $fh, ">", "$table_path.csv" or do {
77 cluck "[DB_TOOL] Could not open $table_path.csv: $!\n";
78 return;
79 };
80 foreach my $record ( @records ) {
81 print $fh "$record->[0]\t$record->[1]\n";
82 }
83 close $fh;
84 $adb->table_close($tie_path);
85  
86 return 1;
87 }
88  
89 # $tools->{ISO2UTF} = 1;
90 # my $ok = $tools->csv2tie("file");
91 # ---------------------------------------------------------------------
92 sub csv2tie {
93  
94 my ( $self, $tableid ) = @_;
95 my $adb = $self->{_adb} or return;
96  
97 $tableid or return;
98  
99 # file paths
100 my $table_path = $adb->table_path($tableid);
101 my $file_path = "${table_path}.$adb->{db_ext}";
102 my $csv_path = "$table_path.csv";
103 return unless -e $csv_path;
104  
105 # backup with timestamp if exists
106 if ( -e "${table_path}.$adb->{db_ext}" ) {
107 my $sec_id = $adb->second_id;
108 rename( "${table_path}.$adb->{db_ext}",
109 "${table_path}-$sec_id.$adb->{db_ext}" );
110 unlink("${table_path}.$adb->{db_ext}");
111 }
112  
113 # perform conversion operations
114 my $i = 1;
115 my @records;
116  
117 open my $FH, "<", $csv_path or do {
118 cluck "[DB_TOOL] Could not open $csv_path: $!\n";
119 return;
120 };
121 $adb->table_write($file_path) or do {
122 close $FH;
123 cluck "[DB_TOOL] Could not open $file_path for writing.\n";
124 return;
125 };
126 my @chunk;
127 while ( my $record = <$FH> ) {
128 chomp($record);
129 $record =~ s/\r$//;
130 my (@fields) = $adb->db_decode($record);
131 push @chunk, [@fields];
132 if ( @chunk >= 2000 ) {
133 $adb->recs_put( $file_path, @chunk );
134 @chunk = ();
135 }
136  
137 # collect into list for indexing
138 push @records, [@fields];
139 $self->{say} .= "$i. Record ID $fields[0] converted.\n\n" if $i % 1000 == 0;
140 $i++;
141 }
142 if ( @chunk ) {
143 $adb->recs_put( $file_path, @chunk );
144 }
145 close $FH;
146 $adb->table_close($file_path);
147  
148 $self->set_index( $tableid, @records );
149  
150 return 1;
151 }
152  
153 # my @tables = $tools->dir_tables();
154 # ------------------------------------------------
155 sub dir_tables {
156  
157 my ( $self, $dir ) = @_;
158 my $adb = $self->{_adb} or return;
159  
160 $dir or return;
161 my $target_dir = File::Spec->catdir( $adb->path('dbase_dir') || ".", $dir );
162 my $ext = $adb->{db_ext} || "db";
163 my @names = $adb->dir_files( $target_dir, "*.$ext", full_path => 0 );
164  
165 my %all_tables = map { /^(.+)\.\Q$ext\E$/i ? ( $1 => 1 ) : () } @names;
166  
167 return sort { $a cmp $b } keys %all_tables;
168 }
169  
170 # my @tables = $tools->vacuum($tableid, 1);
171 # ------------------------------------------------
172 sub vacuum {
173  
174 my ( $self, $tableid, $reindex ) = @_;
175 my $adb = $self->{_adb} or return;
176  
177 my $table_path = $adb->table_path($tableid);
178 my $tie_path = "$table_path.$adb->{db_ext}";
179 return unless -e $tie_path;
180  
181 my $sec_id = $adb->second_id;
182 my $pid_name = "$sec_id-$$";
183 my $tie_back = "$table_path-$pid_name.$adb->{db_ext}";
184 my $csv_path = "$table_path.csv";
185 my $csv_back = "$table_path-$pid_name.csv";
186  
187 # Backup and reset index structure
188 my $table_info = $adb->table_info($tableid);
189 $adb->table_attr( $tableid, {} );
190  
191 # Fetch records
192 my $count = {};
193 my @records = $adb->read_all($tableid);
194 $count->{tie1} = scalar @records;
195 if ( !$count->{tie1} ) {
196 cluck "[DB_TOOL] Cannot vacuum table $tableid as no records were found.\n";
197 return;
198 }
199  
200 -e $csv_path and rename( $csv_path, $csv_back );
201  
202 open my $FH, ">", $csv_path or do {
203 cluck "[DB_TOOL] Could not create $csv_path. Check file permissions: $!\n";
204 return;
205 };
206 foreach my $record (@records) {
207 my $new_record = $adb->tsv_encode(@$record);
208 print $FH "$new_record\n";
209 $count->{csv1}++;
210 }
211 close $FH;
212  
213 # Reverse operation
214 rename( $tie_path, $tie_back );
215  
216 open $FH, "<", $csv_path or do {
217 cluck "[DB_TOOL] Could not open $csv_path for reading: $!\n";
218 return;
219 };
220 $adb->table_write($tie_path);
221 my @chunk;
222 while ( my $record = <$FH> ) {
223 chomp($record);
224 $record =~ s/\r$//;
225 my @fields = $adb->db_decode($record);
226 push @chunk, [@fields];
227 if ( @chunk >= 2000 ) {
228 my $ok = $adb->recs_put( $tie_path, @chunk );
229 $count->{tie2} += scalar @chunk if $ok;
230 @chunk = ();
231 }
232 }
233 if ( @chunk ) {
234 my $ok = $adb->recs_put( $tie_path, @chunk );
235 $count->{tie2} += scalar @chunk if $ok;
236 }
237 close $FH;
238 $adb->table_close($tie_path);
239  
240 $adb->table_attr( $tableid, $table_info );
241 $self->set_index( $tableid, @records ) if $reindex;
242 $self->{say} .= "Vacuum completed. Record counts: Tie old: $count->{tie1}, CSV: $count->{csv1}, Tie new: $count->{tie2}\n";
243  
244 return 1;
245 }
246  
247 # my $tables_hash = $tools->all_tables(); # scalar context -> grouped hashref
248 # my @table_list = $tools->all_tables(); # list context -> flat array of table IDs
249 # ------------------------------------------------
250 sub all_tables {
251  
252 my ($self) = @_;
253 my $adb = $self->{_adb} or return;
254  
255 my @all_tables;
256 my %all_tables;
257  
258 my $dbase_dir = $adb->path('dbase_dir') || ".";
259 my $year_dir = $adb->path('year_dir') || "";
260 my $schema_dir = $adb->path('schema_dir') || "";
261  
262 # 1. Simple Mode: Single flat directory scan for files matching configured db_ext
263 if ( $adb->config('use_simple') ) {
264 my $ext = $adb->{db_ext} || "db";
265 my @files = $adb->dir_files( $dbase_dir, "*.$ext", full_path => 0 );
266 @all_tables = map { /^([a-z0-9_]+)\.\Q$ext\E$/i ? $1 : () } @files;
267 }
268 # 2. Standard Structured Mode: Multi-directory scan (table/ and year directories) for .db files
269 else {
270 my $tbl_dir = File::Spec->catdir( $dbase_dir, 'table' );
271 push @all_tables, $adb->dir_files( $tbl_dir, "*.db", full_path => 0 ) if -d $tbl_dir;
272  
273 my %seen_dirs = ( "table" => 1, "schema" => 1, "backup" => 1, "lock" => 1, "ramdisk" => 1, "journal" => 1, "session" => 1, "config" => 1 );
274 if ($year_dir) {
275 my $yd_path = File::Spec->catdir( $dbase_dir, $year_dir );
276 push @all_tables, $adb->dir_files( $yd_path, "*.db", full_path => 0 );
277 $seen_dirs{$year_dir} = 1;
278 }
279  
280 # Auto-discover any 4-digit year directories under $dbase_dir (e.g. 2024, 2025, 2026)
281 if ( -d $dbase_dir ) {
282 my @year_dirs = $adb->dir_files( $dbase_dir, qr/^\d{4}$/, full_path => 0, files_only => 0 );
283 foreach my $yd (@year_dirs) {
284 next if $seen_dirs{$yd} || !-d File::Spec->catdir( $dbase_dir, $yd );
285 my $yd_path = File::Spec->catdir( $dbase_dir, $yd );
286 push @all_tables, $adb->dir_files( $yd_path, "*.db", full_path => 0 );
287 }
288 }
289  
290 @all_tables = map { /^([a-z0-9_]+)\.db$/i ? $1 : () } @all_tables;
291 }
292  
293 foreach my $record (@all_tables) {
294 next unless $record;
295 next if $record =~ /^_/; # skip internal / temp files
296  
297 if ( my ( $dbs, $tbl ) = ( $record =~ /^([a-z0-9]+)_(.+)$/i ) ) {
298 $all_tables{$dbs}->{$record} = 1;
299 if ( $schema_dir && !-e "$schema_dir/$dbs.dbase" ) {
300 $all_tables{__NO_DBASE__}->{$dbs} = 1;
301 }
302 if ( $schema_dir && !-e "$schema_dir/$record.table" ) {
303 $all_tables{__NO_TABLE__}->{$record} = 1;
304 }
305 }
306 else {
307 # Single-token table name without underscore
308 $all_tables{_main}->{$record} = 1;
309 if ( $schema_dir && !-e "$schema_dir/$record.table" ) {
310 $all_tables{__NO_TABLE__}->{$record} = 1;
311 }
312 }
313 }
314  
315 if (wantarray) {
316 my @flat_list;
317 foreach my $dbs ( sort keys %all_tables ) {
318 next if $dbs =~ /^__/;
319 push @flat_list, sort keys %{ $all_tables{$dbs} };
320 }
321 return @flat_list;
322 }
323  
324 return \%all_tables;
325 }
326  
327 # =====================================================================
328 # TABLE MIGRATION & HISTORICAL FORMAT CONVERSION ENGINE
329 # =====================================================================
330  
331  
332 # Helper to binary copy a file safely without external modules
333 sub _copy_file {
334 my ( $self, $src, $dst ) = @_;
335 return 0 unless -e $src;
336 open my $in, '<:raw', $src or return 0;
337 open my $out, '>:raw', $dst or do { close $in; return 0; };
338 my $buf;
339 while ( read( $in, $buf, 65536 ) ) {
340 print $out $buf;
341 }
342 close $in;
343 close $out;
344 return 1;
345 }
346  
347 # my $table_path = $tools->del_table($tableid);
348 # ------------------------------------------------
349 sub del_table {
350  
351 my ( $self, $tableid ) = @_;
352 my $adb = $self->{_adb} or return;
353  
354 $tableid or return;
355  
356 $adb->config('no_write') and return;
357  
358 my $table_path = $adb->table_path($tableid);
359 return unless $table_path && -e "$table_path.$adb->{db_ext}";
360  
361 my ($parent_dir, $base_name) = $table_path =~ m{^(.+)[/\\]([^/\\]+)$};
362 $parent_dir //= ".";
363 $base_name //= $table_path;
364  
365 my @files1 = $adb->dir_files( $parent_dir, qr/^\Q$base_name\E(?:\.[a-z0-9]+|_[0-9]+\.[a-z0-9]+)$/i );
366  
367 foreach my $file (@files1) {
368 $adb->table_close($file);
369 unlink($file);
370 $self->{say} .= " * $file deleted.\n";
371 }
372  
373 $self->{say} .= " - Table $tableid deleted.\n";
374  
375 return 1;
376 }
377  
378 # Helper: Returns key validation closure according to table ID type and mode
379 # 1. use_simple mode: validates only according to use_simple rules (no control chars, 1-255 bytes, non-empty, no fallback)
380 # 2. ascii id_type: validates printable ASCII characters only (0x20 - 0x7E, 1-255 bytes)
381 # 3. num id_type (default): validates positive numeric integers (> 0, or >= 0 for .aut)
382 sub _get_key_validator {
383 my ( $self, $tid ) = @_;
384 my $adb = $self->{_adb};
385  
386 my $table_info = $adb->table_info($tid);
387 my $is_simple = $adb->config('use_simple') || ( $table_info && $table_info->{use_simple} );
388 my $id_type = $table_info ? ( $table_info->{id_type} || 'num' ) : 'num';
389  
390 if ($is_simple) {
391 # use_simple modu:
392 # Anahtar kontrolü SADECE use_simple şartlarına göre yapılır.
393 # Hatalı kayıt atlanır, fallback yapılmaz.
394 return sub {
395 my ( $k, $sfx ) = @_;
396 return 0 unless defined $k && length($k) > 0;
397 return 0 if length($k) > 255;
398 return 0 if $k =~ /[\x00-\x1f\x7f]/;
399 return 0 unless $k =~ /\S/;
400 return 1;
401 };
402 }
403 elsif ( $id_type eq 'ascii' ) {
404 # ascii id_type:
405 # ASCII'ye uygun olan kayıtları al, uygun olmayanları atla.
406 return sub {
407 my ( $k, $sfx ) = @_;
408 return 0 unless defined $k && length($k) > 0;
409 return 0 if length($k) > 255;
410 return 0 unless $k =~ /^[\x20-\x7e]+$/;
411 return 0 unless $k =~ /\S/;
412 return 1;
413 };
414 }
415 else {
416 # num id_type (veya varsayılan ilişkisel sayısal mod):
417 # ID no 'num' olmayan kayıtları atla.
418 # Kayıt ID'si kesinlikle pozitif tam sayı (> 0) olmalıdır.
419 # .aut dosyasında sayaç tabanı 0 olabileceğinden >= 0 kabul edilir.
420 return sub {
421 my ( $k, $sfx ) = @_;
422 return 0 unless defined $k && length($k) > 0;
423 return 0 unless $k =~ /^[0-9]+$/;
424 return 0 unless ( ( defined $sfx && $sfx eq 'aut' ) ? $k >= 0 : $k > 0 );
425 return 1;
426 };
427 }
428 }
429  
430 sub dump {
431 my ( $self, @args ) = @_;
432 my $adb = $self->{_adb} or return;
433  
434 require Archive::Tar;
435 require Digest::SHA;
436 require JSON::PP;
437 require MIME::Base64;
438 require File::Path;
439  
440 my %opts;
441 if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) {
442 %opts = %{ $args[0] };
443 }
444 elsif ( @args % 2 == 1 && !ref( $args[0] ) ) {
445 my $first = shift @args;
446 %opts = @args;
447 if ( defined $first && length $first ) {
448 if ( $first =~ /\.(?:amberdb|tar\.gz|tgz)$/i || $first =~ m{[/\\]} ) {
449 $opts{file} //= $first;
450 }
451 else {
452 $opts{table} //= $first;
453 }
454 }
455 }
456 else {
457 %opts = @args;
458 }
459  
460 # 1. Determine target tables (all tables if not specified)
461 my @tables;
462 if ( $opts{tables} && ref( $opts{tables} ) eq 'ARRAY' ) {
463 @tables = @{ $opts{tables} };
464 }
465 elsif ( $opts{table} ) {
466 @tables = ( $opts{table} );
467 }
468 else {
469 @tables = $self->all_tables();
470 }
471 return unless @tables;
472  
473 # 2. Flush and close open table handles for read consistency
474 $adb->close_all();
475  
476 # 3. Determine output file path
477 my $year = $adb->year;
478 my $month = $adb->month;
479 my $day = $adb->day;
480 my $date_iso = "$year-$month-$day";
481 my $time_id = $adb->second_id;
482  
483 my $backup_base = $adb->path('backup_dir')
484 || ( $adb->path('dbase_dir') ? $adb->path('dbase_dir') . "/backup" : "backup" );
485 my $year_dir = "$backup_base/$year";
486 $adb->make_path($year_dir);
487  
488 my $outfile = $opts{file} || "$year_dir/amberdb_${date_iso}_${time_id}.amberdb";
489  
490 # Ensure parent directory for $outfile exists
491 if ( my ($outdir) = $outfile =~ m{^(.*)[/\\]} ) {
492 $adb->make_path($outdir);
493 }
494  
495 my $tar = Archive::Tar->new();
496  
497 my $manifest = {
498 format => "AmberDB Archive",
499 format_version => 2,
500 amberdb_version => $AmberDB::VERSION || $VERSION,
501 created_at => "$date_iso " . sprintf( "%02d:%02d:%02d", (localtime)[2], (localtime)[1], (localtime)[0] ),
502 dbase_dir => $adb->path('dbase_dir'),
503 tables => {},
504 };
505  
506 my $schema_dir = $adb->path('schema_dir')
507 || ( $adb->path('dbase_dir') ? $adb->path('dbase_dir') . "/schema" : "schema" );
508  
509 # Collect .dbase database group schemas
510 if ( -d $schema_dir ) {
511 opendir( my $sdh, $schema_dir );
512 my @dbase_files = grep { /\.dbase$/i } readdir($sdh);
513 closedir $sdh;
514  
515 foreach my $df (@dbase_files) {
516 my ($dbs) = $df =~ /^([^.]+)\.dbase$/i;
517 next unless $dbs;
518  
519 # If dumping specific tables, only include matching dbase prefix
520 if ( $opts{tables} || $opts{table} ) {
521 my $matched = 0;
522 foreach my $tid (@tables) {
523 if ( $tid =~ /^\Q$dbs\E_/ or $tid eq $dbs ) {
524 $matched = 1;
525 last;
526 }
527 }
528 next unless $matched;
529 }
530  
531 my $dpath = "$schema_dir/$df";
532 if ( -e $dpath ) {
533 open my $dfh, "<:raw", $dpath or next;
534 local $/ = undef;
535 my $dcontent = <$dfh>;
536 close $dfh;
537 $tar->add_data( "schema/$df", $dcontent );
538 $manifest->{dbases}->{$dbs} = "schema/$df";
539 }
540 }
541 }
542  
543 my $base_dir = $adb->path('dbase_dir') || ".";
544 $base_dir =~ s{\\}{/}g;
545 $base_dir =~ s{/$}{};
546  
547 my $db_ext = $adb->{db_ext} || "db";
548 my %sfx_map = (
549 $db_ext => "dumpdb",
550 "del" => "dumpdel",
551 "aut" => "dumpaut",
552 "cnt" => "dumpcnt",
553 );
554  
555 foreach my $tid (@tables) {
556 my $tpath = $adb->table_path($tid);
557 my $db_file = "$tpath.$db_ext";
558 next unless -e $db_file;
559  
560 my $table_manifest = {
561 records => 0,
562 files => [],
563 sha256 => {},
564 };
565  
566 # A. Collect Schema file if exists
567 my $schema_file = "$schema_dir/$tid.table";
568 if ( -e $schema_file ) {
569 open my $sfh, "<:raw", $schema_file or next;
570 local $/ = undef;
571 my $schema_content = <$sfh>;
572 close $sfh;
573 $tar->add_data( "schema/$tid.table", $schema_content );
574 $table_manifest->{schema} = "schema/$tid.table";
575 }
576  
577 # Tablo ID tipine göre anahtar kontrolü (use_simple, ascii, num)
578 my $key_validator = $self->_get_key_validator($tid);
579  
580 # B. Export Data Files (.db, .del, .aut, .cnt) as Base64 text dump
581 foreach my $sfx ( keys %sfx_map ) {
582 my $fpath = "$tpath.$sfx";
583 if ( -e $fpath ) {
584 next unless $adb->table_read($fpath);
585  
586 my $dcontent = "";
587 my $valid_keys = 0;
588 $adb->recs_scan(
589 $fpath,
590 sub {
591 my ( $k, $v ) = @_;
592 return unless $key_validator->( $k, $sfx );
593 my $b64 = MIME::Base64::encode_base64( $v, "" );
594 $dcontent .= "$k\t$b64\n";
595 $valid_keys++;
596 }
597 );
598 $adb->table_close($fpath);
599  
600 my $dump_sfx = $sfx_map{$sfx};
601 my $norm_fpath = $fpath;
602 $norm_fpath =~ s{\\}{/}g;
603 my $arch_path;
604 if ( $norm_fpath =~ m{^\Q$base_dir\E/(.+)\.\Q$sfx\E$} ) {
605 $arch_path = "$1.$dump_sfx";
606 }
607 else {
608 $arch_path = "table/$tid.$dump_sfx";
609 }
610  
611 $tar->add_data( $arch_path, $dcontent );
612 push @{ $table_manifest->{files} }, $arch_path;
613  
614 my $sha256 = Digest::SHA::sha256_hex($dcontent);
615 $table_manifest->{sha256}->{$arch_path} = $sha256;
616  
617 if ( $sfx eq $db_ext ) {
618 $table_manifest->{records} = $valid_keys;
619 }
620 }
621 }
622  
623 $manifest->{tables}->{$tid} = $table_manifest;
624 }
625  
626 # Add manifest.json to archive
627 my $json = JSON::PP->new->utf8->pretty->encode($manifest);
628 $tar->add_data( "manifest.json", $json );
629  
630 # Write tar.gz archive
631 unless ( $tar->write( $outfile, Archive::Tar::COMPRESS_GZIP() ) ) {
632 cluck "[DB_BACKUP] Failed to write archive $outfile: " . $tar->error() . "\n";
633 return;
634 }
635  
636 $self->{say} .= "Archive successfully written to $outfile (" . ( -s $outfile ) . " bytes)\n";
637 return wantarray ? ( $outfile, $manifest ) : $outfile;
638 }
639  
640 # Restores a .amberdb archive into target database, validates checksums,
641 # writes data files via native DB_File, and deterministically rebuilds all indexes.
642 # my $res = $tools->restore( file => 'backup.amberdb', [force => 1], [reindex => 1] );
643 # ---------------------------------------------------------------------
644 sub restore {
645 my ( $self, @args ) = @_;
646 my $adb = $self->{_adb} or return;
647  
648 my %opts;
649 if ( @args == 1 && ref( $args[0] ) eq 'HASH' ) {
650 %opts = %{ $args[0] };
651 }
652 elsif ( @args % 2 == 1 && !ref( $args[0] ) ) {
653 my $first = shift @args;
654 %opts = @args;
655 $opts{file} //= $first if defined $first && length $first;
656 }
657 else {
658 %opts = @args;
659 }
660  
661 my $file = $opts{file} or do {
662 cluck "[DB_RESTORE] Missing required parameter 'file'.\n";
663 return;
664 };
665 return unless -e $file;
666  
667 require Archive::Tar;
668 require Digest::SHA;
669 require JSON::PP;
670 require MIME::Base64;
671 require File::Path;
672  
673 my $tar = Archive::Tar->new();
674 unless ( $tar->read($file) ) {
675 cluck "[DB_RESTORE] Cannot read archive $file: " . $tar->error() . "\n";
676 return;
677 }
678  
679 # 1. Read and parse manifest.json
680 my $manifest_content = $tar->get_content("manifest.json");
681 unless ($manifest_content) {
682 cluck "[DB_RESTORE] Archive $file is missing manifest.json.\n";
683 return;
684 }
685  
686 my $manifest = eval { JSON::PP->new->utf8->decode($manifest_content) };
687 if ( $@ || ref($manifest) ne 'HASH' ) {
688 cluck "[DB_RESTORE] Corrupted manifest.json in $file: $@\n";
689 return;
690 }
691  
692 # 2. Check if target DB is empty or force is set
693 my $schema_dir = $adb->path('schema_dir')
694 || ( $adb->path('dbase_dir') ? $adb->path('dbase_dir') . "/schema" : "schema" );
695 my $table_dir = $adb->path('table_dir')
696 || ( $adb->path('dbase_dir') ? $adb->path('dbase_dir') . "/table" : "table" );
697  
698 my $force = $opts{force} || $opts{overwrite};
699 unless ($force) {
700 my $has_existing = 0;
701 my $db_ext = $adb->{db_ext} || "db";
702 foreach my $tid ( keys %{ $manifest->{tables} || {} } ) {
703 if ( -e "$table_dir/$tid.$db_ext" || -e "$schema_dir/$tid.table" ) {
704 $has_existing = 1;
705 last;
706 }
707 }
708 if ($has_existing) {
709 cluck "[DB_RESTORE] Target database is not empty. Use 'force => 1' to overwrite existing tables.\n";
710 return;
711 }
712 }
713  
714 # Ensure target directories exist
715 $adb->make_path($schema_dir);
716 $adb->make_path($table_dir);
717  
718 # Flush all active handles before restoring
719 $adb->close_all();
720  
721 my @restored_tables;
722 my %table_filter = $opts{tables} ? map { $_ => 1 } @{ $opts{tables} } : ();
723  
724 # 3. Extract .dbase database group schemas
725 if ( ref( $manifest->{dbases} ) eq 'HASH' ) {
726 foreach my $dbs ( sort keys %{ $manifest->{dbases} } ) {
727 my $arch_path = $manifest->{dbases}->{$dbs};
728 my $scontent = $tar->get_content($arch_path);
729 if ( defined $scontent ) {
730 my $target_dbase = "$schema_dir/$dbs.dbase";
731 open my $dfh, ">:raw", $target_dbase or do {
732 cluck "[DB_RESTORE] Cannot write dbase schema $target_dbase: $!\n";
733 return;
734 };
735 print $dfh $scontent;
736 close $dfh;
737 }
738 }
739 }
740  
741 # 4. Extract table schemas and data files
742 my $base_dir = $adb->path('dbase_dir') || ".";
743 $base_dir =~ s{\\}{/}g;
744 $base_dir =~ s{/$}{};
745  
746 my %restore_sfx_map = (
747 dumpdb => ( $adb->{db_ext} || "db" ),
748 dumpdel => "del",
749 dumpaut => "aut",
750 dumpcnt => "cnt",
751 );
752  
753 foreach my $tid ( sort keys %{ $manifest->{tables} || {} } ) {
754 next if ( %table_filter && !$table_filter{$tid} );
755  
756 my $tinfo = $manifest->{tables}->{$tid};
757  
758 # A. Restore Schema
759 if ( $tinfo->{schema} ) {
760 my $scontent = $tar->get_content( $tinfo->{schema} );
761 if ( defined $scontent ) {
762 my $target_schema = "$schema_dir/$tid.table";
763 open my $sfh, ">:raw", $target_schema or do {
764 cluck "[DB_RESTORE] Cannot write schema $target_schema: $!\n";
765 return;
766 };
767 print $sfh $scontent;
768 close $sfh;
769 }
770 }
771  
772 # Clear cached schema so table_info reloads newly restored schema
773 delete $adb->{_table}->{$tid};
774 my $key_validator = $self->_get_key_validator($tid);
775  
776 # B. Restore Data Files (.dumpdb, .dumpdel, .dumpaut, .dumpcnt or legacy .db)
777 my $tpath = $adb->table_path($tid);
778  
779 foreach my $arch_path ( @{ $tinfo->{files} || [] } ) {
780 my $dcontent = $tar->get_content($arch_path);
781 next unless defined $dcontent;
782  
783 # Verify checksum if present
784 if ( my $expected_sha = $tinfo->{sha256}->{$arch_path} ) {
785 my $actual_sha = Digest::SHA::sha256_hex($dcontent);
786 if ( $expected_sha ne $actual_sha ) {
787 cluck "[DB_RESTORE] Checksum mismatch for $arch_path! Expected $expected_sha, got $actual_sha.\n";
788 return;
789 }
790 }
791  
792 if ( $arch_path =~ m{\.(dump(?:db|del|aut|cnt))$} ) {
793 my $dsfx = $1;
794 my $target_sfx = $restore_sfx_map{$dsfx} || ( $adb->{db_ext} || "db" );
795  
796 my $target_file;
797 if ($tpath) {
798 $target_file = "$tpath.$target_sfx";
799 }
800 else {
801 my $rel_path = $arch_path;
802 $rel_path =~ s{\.dump(?:db|del|aut|cnt)$}{.$target_sfx};
803 $target_file = "$base_dir/$rel_path";
804 }
805  
806 if ( my ($tdir) = $target_file =~ m{^(.*)[/\\]} ) {
807 $adb->make_path($tdir);
808 }
809  
810 if ( -e $target_file ) {
811 $adb->table_close($target_file);
812 unlink $target_file;
813 }
814  
815 my $db_handle = $adb->table_write($target_file);
816 unless ($db_handle) {
817 cluck "[DB_RESTORE] Cannot open target table $target_file for writing: $!\n";
818 return;
819 }
820  
821 for my $line ( split /\r?\n/, $dcontent ) {
822 next unless length $line;
823 my ( $k, $b64 ) = split /\t/, $line, 2;
824 next unless defined $k;
825 next unless $key_validator->( $k, $target_sfx );
826 my $v = defined $b64 ? MIME::Base64::decode_base64($b64) : '';
827 my $k_enc = $adb->utf_encode("$k");
828 $db_handle->put( $k_enc, $v );
829 }
830 $adb->table_close($target_file);
831 }
832 else {
833 # Legacy raw binary fallback
834 my $target_file = "$base_dir/$arch_path";
835 if ( my ($tdir) = $target_file =~ m{^(.*)[/\\]} ) {
836 $adb->make_path($tdir);
837 }
838  
839 open my $dfh, ">:raw", $target_file or do {
840 cluck "[DB_RESTORE] Cannot write data file $target_file: $!\n";
841 return;
842 };
843 print $dfh $dcontent;
844 close $dfh;
845 }
846 }
847  
848 push @restored_tables, $tid;
849 }
850  
851 # 5. Rebuild all indexes if reindex is requested (default: 1)
852 my $reindex = defined $opts{reindex} ? $opts{reindex} : 1;
853 if ($reindex) {
854 foreach my $tid (@restored_tables) {
855 eval {
856 my $idx = $self->can('set_index') ? $self : do {
857 require AmberDB::Tools::Index;
858 AmberDB::Tools::Index->new($adb);
859 };
860 $idx->set_index($tid);
861 };
862 if ($@) {
863 cluck "[DB_RESTORE] Reindexing failed for table '$tid': $@\n";
864 }
865 }
866 }
867  
868 $self->{say} .= "Restored " . scalar(@restored_tables) . " tables from $file.\n";
869  
870 return {
871 ok => 1,
872 file => $file,
873 tables => \@restored_tables,
874 manifest => $manifest,
875 reindexed => $reindex ? 1 : 0,
876 };
877 }
878  
879  
880 1;