| 1 | package App::SimpleBackuper::DB; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Const::Fast; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use App::SimpleBackuper::DB::BackupsTable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use App::SimpleBackuper::DB::FilesTable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use App::SimpleBackuper::DB::PartsTable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use App::SimpleBackuper::DB::BlocksTable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use App::SimpleBackuper::DB::UidsGidsTable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | const my $FORMAT_VERSION => 2; | |||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | sub _unpack_tmpl { | ||||||||
|
Subroutine
_unpack_tmpl: not called CC=1 SCAR=6.9
| |||||||||||
| 15 | 0 | my($self, $tmpl) = @_; | |||||||||
| 16 | 0 | my $length = length pack $tmpl; | |||||||||
| 17 | 0 | my $buf = substr $self->{dump}, $self->{offset}, $length; | |||||||||
| 18 | 0 | $self->{offset} += $length; | |||||||||
| 19 | 0 | return unpack $tmpl, $buf; | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | sub _unpack_record { | ||||||||
|
Subroutine
_unpack_record: not called CC=1 SCAR=6.9
| |||||||||||
| 23 | 0 | my($self) = @_; | |||||||||
| 24 | 0 | my $length = $self->_unpack_tmpl("J"); | |||||||||
| 25 | 0 | return $self->_unpack_tmpl("a$length"); | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 29 | 0 | my($class, $dump_ref) = @_; | |||||||||
| 30 | |||||||||||
| 31 | 0 | my $self = bless { | |||||||||
| 32 | backups => App::SimpleBackuper::DB::BackupsTable->new(), | ||||||||||
| 33 | files => App::SimpleBackuper::DB::FilesTable->new(), | ||||||||||
| 34 | parts => App::SimpleBackuper::DB::PartsTable->new(), | ||||||||||
| 35 | blocks => App::SimpleBackuper::DB::BlocksTable->new(), | ||||||||||
| 36 | uids_gids => App::SimpleBackuper::DB::UidsGidsTable->new(), | ||||||||||
| 37 | } => $class; | ||||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | if($dump_ref) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | $self->{dump} = $$dump_ref; | |||||||||
| 41 | 0 | $self->{offset} = 0; | |||||||||
| 42 | |||||||||||
| 43 | 0 | my $format_version = $self->_unpack_tmpl('J'); | |||||||||
| 44 | 0 | my $parse_method = "parse_format_v$format_version"; | |||||||||
| 45 | 0 | ▶ | die "Unsupported database format version $format_version" if ! $self->can($parse_method); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | $self->$parse_method(); | |||||||||
| 47 | 0 | delete $self->{ $_ } foreach qw(dump offset); | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 0 | return $self; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub dump { | ||||||||
|
Subroutine
dump: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 54 | 0 | my($self) = @_; | |||||||||
| 55 | 0 | my $dump_method = "dump_format_v$FORMAT_VERSION"; | |||||||||
| 56 | 0 | return $self->$dump_method(); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub parse_format_v2 { | ||||||||
|
Subroutine
parse_format_v2: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 60 | 0 | my($self) = @_; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my($backups_cnt, $files_cnt, $parts_cnt, $blocks_cnt, $uids_gids_cnt) = $self->_unpack_tmpl("JJJJJ"); | |||||||||
| 63 | |||||||||||
| 64 | 0 | $self->{backups} = App::SimpleBackuper::DB::BackupsTable->new($backups_cnt); | |||||||||
| 65 | 0 | $self->{backups} ->[$_ - 1] = $self->_unpack_record() for 1 .. $backups_cnt; | |||||||||
| 66 | 0 | $self->{files} = App::SimpleBackuper::DB::FilesTable->new($files_cnt); | |||||||||
| 67 | 0 | $self->{files} ->[$_ - 1] = $self->_unpack_record() for 1 .. $files_cnt; | |||||||||
| 68 | 0 | $self->{parts} = App::SimpleBackuper::DB::PartsTable->new($parts_cnt); | |||||||||
| 69 | 0 | $self->{parts} ->[$_ - 1] = $self->_unpack_record() for 1 .. $parts_cnt; | |||||||||
| 70 | 0 | $self->{blocks} = App::SimpleBackuper::DB::BlocksTable->new($blocks_cnt); | |||||||||
| 71 | 0 | $self->{blocks} ->[$_ - 1] = $self->_unpack_record() for 1 .. $blocks_cnt; | |||||||||
| 72 | 0 | $self->{uids_gids} = App::SimpleBackuper::DB::UidsGidsTable->new($uids_gids_cnt); | |||||||||
| 73 | 0 | $self->{uids_gids} ->[$_ - 1] = $self->_unpack_record() for 1 .. $uids_gids_cnt; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sub dump_format_v2 { | ||||||||
|
Subroutine
dump_format_v2: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 77 | 0 | my($self) = @_; | |||||||||
| 78 | |||||||||||
| 79 | return \ join('', | ||||||||||
| 80 | 0 | pack("JJJJJJ", $FORMAT_VERSION, map {scalar @{ $self->{$_} }} qw(backups files parts blocks uids_gids)), | |||||||||
| 81 | 0 | map { pack("Ja".length($_), length($_), $_) } map {@{ $self->{$_} }} qw(backups files parts blocks uids_gids) | |||||||||
| 82 | ); | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub parse_format_v1 { | ||||||||
|
Subroutine
parse_format_v1: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||
| 86 | 0 | my($self) = @_; | |||||||||
| 87 | |||||||||||
| 88 | 0 | my($backups_cnt, $files_cnt, $uids_gids_cnt) = $self->_unpack_tmpl("JJJ"); | |||||||||
| 89 | |||||||||||
| 90 | 0 | $self->{backups} = App::SimpleBackuper::DB::BackupsTable->new($backups_cnt); | |||||||||
| 91 | 0 | ▶ | foreach(my $q = 0; $q < $backups_cnt; $q++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | # upgrade backups format | ||||||||||
| 93 | 0 | my $record = $self->_unpack_record(); | |||||||||
| 94 | 0 | $record = $self->{backups}->unpack_format_v1($record); | |||||||||
| 95 | 0 | $record = $self->{backups}->pack($record); | |||||||||
| 96 | 0 | $self->{backups}->[ $q ] = $record; | |||||||||
| 97 | } | ||||||||||
| 98 | 0 | $self->{files} = App::SimpleBackuper::DB::FilesTable->new($files_cnt); | |||||||||
| 99 | 0 | $self->{files} ->[$_ - 1] = $self->_unpack_record() for 1 .. $files_cnt; | |||||||||
| 100 | 0 | $self->{uids_gids} = App::SimpleBackuper::DB::UidsGidsTable->new($uids_gids_cnt); | |||||||||
| 101 | 0 | $self->{uids_gids} ->[$_ - 1] = $self->_unpack_record() for 1 .. $uids_gids_cnt; | |||||||||
| 102 | |||||||||||
| 103 | 0 | delete $self->{ $_ } foreach qw(dump offset); | |||||||||
| 104 | |||||||||||
| 105 | 0 | my %backups_files_cnt = map {$self->{backups}->unpack($_)->{id} => 0} @{ $self->{backups} }; | |||||||||
| 106 | 0 | for my $q (0 .. $#{ $self->{files} }) { | |||||||||
| 107 | 0 | my $file = $self->{files}->unpack( $self->{files}->[ $q ] ); | |||||||||
| 108 | |||||||||||
| 109 | 0 | foreach my $version (@{ $file->{versions} }) { | |||||||||
| 110 | 0 | foreach my $backup_id ( $version->{backup_id_min} .. $version->{backup_id_max} ) { | |||||||||
| 111 | 0 | $backups_files_cnt{ $backup_id }++; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | foreach my $part ( @{ $version->{parts} } ) { | |||||||||
| 115 | 0 | $self->{parts}->upsert({hash => $part->{hash}}, {%$part, block_id => $version->{block_id}}); | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 0 | my $block = $self->{blocks}->find_row({ id => $version->{block_id} }); | |||||||||
| 119 | 0 | ▶ | if(! $block) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | $self->{blocks}->upsert( | ||||||||||
| 121 | {id => $version->{block_id}}, | ||||||||||
| 122 | { | ||||||||||
| 123 | id => $version->{block_id}, | ||||||||||
| 124 | last_backup_id => $version->{backup_id_max}, | ||||||||||
| 125 | 0 | parts_cnt => scalar @{ $version->{parts} }, | |||||||||
| 126 | } | ||||||||||
| 127 | ); | ||||||||||
| 128 | } else { | ||||||||||
| 129 | 0 | ▶ | $block->{last_backup_id} = $version->{backup_id_max} if $block->{last_backup_id} < $version->{backup_id_max}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | $block->{parts_cnt} += @{ $version->{parts} }; | |||||||||
| 131 | 0 | $self->{blocks}->upsert({ id => $version->{block_id} }, $block); | |||||||||
| 132 | } | ||||||||||
| 133 | } | ||||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 0 | ▶ | while(my($backup_id, $files_cnt) = each %backups_files_cnt) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 0 | my $backup = $self->{backups}->find_row({ id => $backup_id }); | |||||||||
| 138 | 0 | $backup->{files_cnt} = $files_cnt; | |||||||||
| 139 | 0 | $self->{backups}->upsert({ id => $backup_id }, $backup ); | |||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 0 | ▶ | sub dump_format_v1 { | ||||||||
|
Subroutine
dump_format_v1: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 144 | 0 | my $self = shift; | |||||||||
| 145 | |||||||||||
| 146 | return \ join('', | ||||||||||
| 147 | 0 | pack("JJJJ", $FORMAT_VERSION, scalar @{ $self->{backups} }, scalar @{ $self->{files} }, scalar @{ $self->{uids_gids} }), | |||||||||
| 148 | 0 | map { pack("Ja".length($_), length($_), $_) } @{ $self->{backups} }, @{ $self->{files} }, @{ $self->{uids_gids} } | |||||||||
| 149 | ); | ||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 1 | 1; | |||||||||