DiaColloDB/Upgrade.pm

filter: Statementstmt 36.9% 24 / 65 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 58.3% 7 / 12 Podpod 80.0% 4 / 5 totaltotal 28.9% 35 / 121 CC 19 SCAR 33.4
CC
19
Cov
24%
which
42.0
upgrade
42.0
uobj
42.0
CRAP 28.2
« DiaColloDB/Compat.pm ↑ Summary DiaColloDB/PackedFile/MMap.pm »
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DiaColloDB::Upgrade.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: DiaColloDB utilities: auto-magic upgrades: top level
6  
7 package DiaColloDB::Upgrade;
8  
9 # "Attempt to reload DiaColloDB.pm aborted." on perl v5.31.7 (http://www.cpantesters.org/cpan/report/b8caf29a-4121-11ea-9d04-93d2cf6284ad)
10 # - maybe due to 'eval "use threads";' in Client/list.pm? --> nope, no joy
11 #use DiaColloDB;
12  
13 1 ▶use DiaColloDB::Upgrade::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use DiaColloDB::Upgrade::v0_04_dlimits;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use DiaColloDB::Upgrade::v0_09_multimap;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use DiaColloDB::Upgrade::v0_10_x2t;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use DiaColloDB::Upgrade::v0_12_sliceN;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 ##==============================================================================
22 ## Globals
23  
24 1 our @ISA = qw(DiaColloDB::Logger);
25  
26 ## @upgrades : list of available auto-magic upgrade sub-packages (suffixes)
27 1 our @upgrades = (
28 'v0_04_dlimits',
29 'v0_09_multimap',
30 'v0_10_x2t',
31 'v0_12_sliceN',
32 );
33  
34 ##==============================================================================
35 ## Top-level
36  
37 ## @upgrade_pkgs = $CLASS_OR_OBJECT->available()
38 ## + returns list of available upgrade-packages (suffixes)
39 0 ▶sub available {
Subroutine available: not called CC=1 SCAR=6.9
Pod documented
40 0 return @upgrades;
41 }
42  
43 ## @needed = $CLASS_OR_OBJECT->needed($dbdir, \%opts?, @upgrade_pkgs)
44 ## + returns list of those package-names in @upgrade_pkgs which are needed for DB in $dbdir
45 ## + %opts are passed to upgrade-package new() methods
46 0 ▶sub needed {
Subroutine needed: not called CC=4 SCAR=30.0
Pod documented
47 0 my $that = shift;
48 0 my $dbdir = shift;
49 0 ▶ my $opts = UNIVERSAL::isa($_[0],'HASH') ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'HASH') ? : 0 0 0
50 return grep {
51 0 my $pkg = $_;
52 0 ▶ $pkg = "DiaColloDB::Upgrade::$pkg" if (!UNIVERSAL::can($pkg,'needed'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($pkg, "needed") 0 0 0
53 0 ▶ $that->warn("unknown upgrade package $_") if (!UNIVERSAL::can($pkg,'needed'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($pkg, "needed") 0 0 0
54 0 $that->uobj($pkg,$dbdir,$opts)->needed();
55 } @_;
56 }
57  
58 ## @upgrades = $CLASS_OR_OBJECT->which($dbdir, \%opts?)
59 ## + returns a list of upgrades applied to $dbdir
60 ## + list is created by parsing "upgraded" field from "$dbdir/header.json"
61 ## + if the upgrade-item "by" keyword inherits from DiaColloDB::Upgrade::Base,
62 ## a new object will be created and returned in @upgrades; otherwise the
63 ## parsed HASH-ref is returned as-is
64 0 ▶sub which {
Subroutine which: not called CC=6 SCAR=37.4
Pod documented
65 0 my ($that,$dbdir,$opts) = @_;
66 0 ▶ $opts //= {};
Condition0%0/2
$opts //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
67 0 my $hdr = DiaColloDB::Upgrade::Base->dbheader($dbdir);
68 0 my @ups = qw();
69 0 ▶ foreach (@{$hdr->{upgraded}//[]}) {
Condition0%0/2
$hdr->{"upgraded"} // []
l!l
00
MC/DC0%0/1
$hdr->{"upgraded"} // []
$hdr->{"upgraded"}
Truth table0%0/2
A
A= $hdr->{"upgraded"}
Aresult
11
00
70 0 ▶ my $class = $_->{class} // $_->{by};
Condition0%0/3
$_->{"class"} // $_->{"by"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{"class"} // $_->{"by"}
$_->{"class"} $_->{"by"}
Truth table0%0/3
A // B
A= $_->{"class"}B= $_->{"by"}
ABresult
1X1
011
000
71 0 ▶ $class = "DiaColloDB::Upgrade::$class" if (!UNIVERSAL::isa($class,'DiaColloDB::Upgrade::Base'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($class, "DiaColloDB::Upgrade::Base") 0 0 0
72 0 push(@ups, $that->uobj($class, $dbdir, { %$opts, %$_ }));
73 }
74 0 return @ups;
75 }
76  
77 ## $bool = $CLASS_OR_OBJECT->upgrade($dbdir, \%opts?, \@upgrades_or_pkgs)
78 ## + applies upgrades in @upgrades to DB in $dbdir
79 ## + %opts are passed to upgrade-package new() methods
80 0 ▶sub upgrade {
Subroutine upgrade: not called CC=6 SCAR=37.4
Pod documented
81 0 my $that = shift;
82 0 my $dbdir = shift;
83 0 ▶ my $opts = UNIVERSAL::isa($_[0],'HASH') ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'HASH') ? : 0 0 0
84 0 foreach (@_) {
85 0 my $pkg = $_;
86 0 ▶ $pkg = "DiaColloDB::Upgrade::$pkg" if (!UNIVERSAL::can($pkg,'upgrade'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($pkg, "upgrade") 0 0 0
87 0 ▶ $that->logconfess("unknown upgrade package $_") if (!UNIVERSAL::can($pkg,'upgrade'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($pkg, "upgrade") 0 0 0
88 0 $that->info("applying upgrade package $_ to $dbdir/");
89 0 ▶ $that->uobj($pkg,$dbdir,$opts)->upgrade()
Branch: 1 branch
BranchTrueFalseTotal
$that->uobj($pkg, $dbdir, $opts)->upgrade or $that->logconfess("upgrade via package $pkg failed for $dbdir/") 0 0 0
90 or $that->logconfess("upgrade via package $pkg failed for $dbdir/");
91 }
92 0 return $that;
93 }
94  
95 ##==============================================================================
96 ## Utils
97  
98 ## $up = $CLASS_OR_OBJECT->uobj($pkg,$dbdir,\%opts)
99 ## + create or instantiate an upgrade-object $up as an instance of $pkg for $dbdir with options %opts
100 0 ▶sub uobj {
Subroutine uobj: not called CC=6 SCAR=37.4
Pod undocumented
101 0 my $that = shift;
102 0 my ($pkg,$dbdir,$opts) = @_;
103 0 my ($up);
104 0 ▶ if (ref($pkg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pkg) { } 0 0 0
elsif (UNIVERSAL::can($pkg, 'new')) { } 0 0 0
105 0 $up = $pkg;
106 0 ▶ @$up{keys %$opts} = values %$opts if ($opts);
Branch: 1 branch
BranchTrueFalseTotal
if $opts 0 0 0
107 0 $up->{dbdir} = $dbdir;
108 } elsif (UNIVERSAL::can($pkg,'new')) {
109 0 ▶ $up = $pkg->new(%{$opts//{}}, dbdir=>$dbdir);
Condition0%0/2
$opts // {}
l!l
00
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
110 } else {
111 0 ▶ $up = { %{$opts//{}}, dbdir=>$dbdir };
Condition0%0/2
$opts // {}
l!l
00
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
112 }
113 0 return $up;
114 }
115  
116 ##==============================================================================
117 ## Footer
118 1 1; ##-- be happy
« DiaColloDB/Compat.pm ↑ Summary DiaColloDB/PackedFile/MMap.pm »