DiaColloDB/Upgrade/v0_12_sliceN.pm

filter: Statementstmt 14.4% 17 / 118 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 5 / 10 Podpod 100% 5 / 5 totaltotal 15.2% 27 / 177 CC 24 SCAR 51.4
CC
24
Cov
11%
upgrade
306.0
backup
56.0
revert_created
6.0
CRAP 170.2
« DiaColloDB/Document/TCF.pm ↑ Summary DiaColloDB/EnumFile.pm »
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DiaColloDB::Upgrade::v0_12_sliceN.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: DiaColloDB utilities: auto-magic upgrade: v0.11.x -> v0.12.x: allow slice-wise N
6  
7 package DiaColloDB::Upgrade::v0_12_sliceN;
8 1 ▶use DiaColloDB::Upgrade::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DiaColloDB::Compat::v0_11;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DiaColloDB::Utils qw(:pack :env :run :file :pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 our @ISA = qw(DiaColloDB::Upgrade::Base);
14  
15 ##==============================================================================
16 ## API
17  
18 ## $version = $CLASS_OR_OBJECT->toversion()
19 ## + returns default target version; default just returns $DiaColloDB::VERSION
20 0 ▶sub toversion {
Subroutine toversion: not called CC=1 SCAR=6.9
Pod documented
21 0 return '0.12.000';
22 }
23  
24 ## $bool = $CLASS_OR_OBJECT->upgrade()
25 ## + performs upgrade
26 0 ▶sub upgrade {
Subroutine upgrade: not called CC=17 SCAR=57.2
Pod documented
27 0 my $up = shift;
28  
29 ##-- backup
30 0 ▶ $up->backup() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$up->backup or return undef 0 0 0
31  
32 ##-- read header
33 0 my $dbdir = $up->{dbdir};
34 0 my $hdr = $up->dbheader();
35  
36 ##-- convert relations: unigrams
37 {
38 0 ▶ my $ug = DiaColloDB::Relation::Unigrams->new(base=>"$dbdir/xf", logCompat=>'off')
Branch: 1 branch
BranchTrueFalseTotal
my $ug = "DiaColloDB::Relation::Unigrams"->new("base", "$dbdir/xf", "logCompat", "off") or $up->logconfess("failed to open unigram index $dbdir/xf.*: $!") 0 0 0
39 or $up->logconfess("failed to open unigram index $dbdir/xf.*: $!");
40 0 $up->info("upgrading unigram index $dbdir/xf.*");
41 0 ▶ $up->warn("unigram data in $dbdir/xf.* doesn't seem to be v0.11 format; trying to upgrade anyways")
Branch: 1 branch
BranchTrueFalseTotal
unless $ug->isa("DiaColloDB::Compat::v0_11::Relation::Unigrams") 0 0 0
42 if (!$ug->isa('DiaColloDB::Compat::v0_11::Relation::Unigrams'));
43  
44 ##-- xf: extract total counts by date
45 0 my $r2 = $ug->{r2}; ##-- pf: [$d1,$f1]* @ end2($i1-1)..(end2($i1+1)-1)
46 0 my $packas = $r2->{packas};
47 0 my ($buf, $d,$f);
48 0 my %fN = qw();
49 0 ▶ for (my $i=0; $i < $ug->{size2}; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ug->{"size2"}) 0 0 0
50 0 $r2->read(\$buf);
51 0 ($d,$f) = unpack($packas,$buf);
52 0 $fN{$d} += $f;
53 }
54  
55 ##-- xf: create $rN by date
56 0 my @dates = sort {$a<=>$b} keys %fN;
57 0 my $ymin = $dates[0];
58 0 my $rN = $ug->{rN} = DiaColloDB::PackedFile->new(file=>"$dbdir/xf.dbaN", flags=>'rw', perms=>$ug->{perms}, packas=>"$ug->{pack_f}");
59 0 $rN->store(($_-$ymin)=>$fN{$_}) foreach (@dates);
60 0 $rN->flush();
61  
62 ##-- xf: update header
63 0 $ug->{ymin} = $ymin;
64 0 $ug->{sizeN} = $rN->size;
65 0 $ug->{version} = $up->toversion;
66 0 ▶ $ug->saveHeader()
Branch: 1 branch
BranchTrueFalseTotal
$ug->saveHeader or $up->logconfess("failed to save new unigram index header $dbdir/xf.hdr") 0 0 0
67 or $up->logconfess("failed to save new unigram index header $dbdir/xf.hdr");
68 }
69  
70 ##-- convert relations: cofreqs
71 {
72 0 ▶ my $cof = DiaColloDB::Relation::Cofreqs->new(base=>"$dbdir/cof", logCompat=>'off')
Branch: 1 branch
BranchTrueFalseTotal
my $cof = "DiaColloDB::Relation::Cofreqs"->new("base", "$dbdir/cof", "logCompat", "off") or $up->logconfess("failed to open co-frequency index $dbdir/cof.*: $!") 0 0 0
73 or $up->logconfess("failed to open co-frequency index $dbdir/cof.*: $!");
74 0 $up->info("upgrading co-frequency index $dbdir/cof.*");
75 0 ▶ $up->warn("co-frequency data in $dbdir/cof.* doesn't seem to be v0.11 format; trying to upgrade anyways")
Branch: 1 branch
BranchTrueFalseTotal
unless $cof->isa("DiaColloDB::Compat::v0_11::Relation::Cofreqs") 0 0 0
76 if (!$cof->isa('DiaColloDB::Compat::v0_11::Relation::Cofreqs'));
77  
78 ##-- cof: extract total counts by date
79 0 my $r2 = $cof->{r2}; ##-- pf: [$end3,$d1,$f1]* @ end2($i1-1)..(end2($i1+1)-1)
80 0 my $packas = $r2->{packas};
81 0 my ($buf, $end3,$d,$f);
82 0 my %fN = qw();
83 0 ▶ for (my $i=0; $i < $cof->{size2}; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cof->{"size2"}) 0 0 0
84 0 $r2->read(\$buf);
85 0 ($end3,$d,$f) = unpack($packas,$buf);
86 0 $fN{$d} += $f;
87 }
88  
89 ##-- cof: create $rN by date
90 0 my @dates = sort {$a<=>$b} keys %fN;
91 0 my $ymin = $dates[0];
92 0 my $rN = $cof->{rN} = DiaColloDB::PackedFile->new(file=>"$dbdir/cof.dbaN", flags=>'rw', perms=>$cof->{perms}, packas=>"$cof->{pack_f}");
93 0 $rN->store(($_-$ymin)=>$fN{$_}) foreach (@dates);
94 0 $rN->flush();
95  
96 ##-- cof: update header
97 0 $cof->{ymin} = $ymin;
98 0 $cof->{sizeN} = $rN->size;
99 0 $cof->{version} = $up->toversion;
100 0 ▶ $cof->saveHeader()
Branch: 1 branch
BranchTrueFalseTotal
$cof->saveHeader or $up->logconfess("failed to save new co-frequency index header $dbdir/cof.hdr") 0 0 0
101 or $up->logconfess("failed to save new co-frequency index header $dbdir/cof.hdr");
102 }
103  
104 ##-- convert relations: tdf
105 0 ▶ if ($hdr->{index_tdf} && -r "$dbdir/tdf.hdr") {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr->{"index_tdf"} and -r "$dbdir/tdf.hdr") 0 0 0
Condition0%0/3
$hdr->{"index_tdf"} and -r "$dbdir/tdf.hdr"
!ll&&!rl&&r
000
MC/DC0%0/2
$hdr->{"index_tdf"} and -r "$dbdir/tdf.hdr"
$hdr->{"index_tdf"} -r "$dbdir/tdf.hdr"
Truth table0%0/3
A and B
A= $hdr->{"index_tdf"}B= -r "$dbdir/tdf.hdr"
ABresult
0X0
100
111
106 0 DiaColloDB::Compat->usecompat('v0_11::Relation::TDF');
107 0 ▶ my $vs = DiaColloDB::Relation::TDF->new(base=>"$dbdir/tdf", logCompat=>'off')
Branch: 1 branch
BranchTrueFalseTotal
my $vs = "DiaColloDB::Relation::TDF"->new("base", "$dbdir/tdf", "logCompat", "off") or $up->logconfess("failed to open (term x document) index $dbdir/tdf.*: $!") 0 0 0
108 or $up->logconfess("failed to open (term x document) index $dbdir/tdf.*: $!");
109 0 $up->info("upgrading (term x document) index $dbdir/tdf.*");
110 0 ▶ $up->warn("term-document data in $dbdir/tdf.* doesn't seem to be v0.11 format; trying to upgrade anyways")
Branch: 1 branch
BranchTrueFalseTotal
unless $vs->isa("DiaColloDB::Compat::v0_11::Relation::TDF") 0 0 0
111 if (!$vs->isa('DiaColloDB::Compat::v0_11::Relation::TDF'));
112  
113 ##-- tdf: create {yf}, {ymin}
114 0 my ($cf,$c2date) = @$vs{qw(cf c2date)};
115 0 my ($ymin,$ymax) = $c2date->minmax;
116 0 my $NY = $ymax-$ymin+1;
117 0 $cf->indadd( ($c2date-$ymin), my $yf=mmzeroes("$dbdir/tdf.d/yf.pdl",$vs->vtype,$NY) );
118 0 undef $yf;
119  
120 ##-- tdf: update header
121 0 $vs->{y0} = $ymin;
122 0 $vs->{version} = $up->toversion;
123 0 ▶ $vs->saveHeader()
Branch: 1 branch
BranchTrueFalseTotal
$vs->saveHeader or $up->logconfess("failed to save new (term x document) index header $dbdir/tdf.hdr") 0 0 0
124 or $up->logconfess("failed to save new (term x document) index header $dbdir/tdf.hdr");
125 }
126  
127 ##-- cleanup
128 0 if (0 && !$up->{keep}) {
129 $up->info("removing temporary file(s)");
130 }
131  
132 ##-- update header
133 0 return $up->updateHeader();
134 }
135  
136 ##==============================================================================
137 ## Backup & Revert
138  
139 ## $bool = $up->backup()
140 ## + perform backup any files we expect to change to $up->backupdir()
141 0 ▶sub backup {
Subroutine backup: not called CC=7 SCAR=40.3
Pod documented
142 0 my $up = shift;
143 0 ▶ $up->SUPER::backup() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$up->SUPER::backup or return undef 0 0 0
144 0 ▶ return 1 if (!$up->{backup});
Branch: 1 branch
BranchTrueFalseTotal
unless $up->{"backup"} 0 0 0
145  
146 0 my $dbdir = $up->{dbdir};
147 0 my $hdr = $up->dbheader;
148 0 my $backd = $up->backupdir;
149  
150 ##-- backup: top-level
151 0 foreach my $base (map {"$dbdir/$_"} qw(xf cof tdf)) {
152 0 $up->info("backing up $base.*");
153 0 ▶ copyto_a([grep {-e $_} map {($_,"$_.hdr")} ($base,"$base.dbaN")], $backd)
Branch: 1 branch
BranchTrueFalseTotal
copyto_a([grep({-e $_;} map({$_, "$_.hdr";} $base, "$base.dbaN"))], $backd) or $up->logconfess("backup failed for $base.*: $!") 0 0 0
154 or $up->logconfess("backup failed for $base.*: $!");
155 }
156  
157 ##-- backup: tdf.d
158 0 foreach my $base (map {"$dbdir/tdf.d/$_"} qw(yf.pdl)) {
159 0 $up->info("backing up $base.*");
160 0 ▶ copyto_a([grep {-e $_} map {($_,"$_.hdr")} ($base)], "$backd/tdf.d")
Branch: 1 branch
BranchTrueFalseTotal
copyto_a([grep({-e $_;} map({$_, "$_.hdr";} $base))], "$backd/tdf.d") or $up->logconfess("backup failed for $base*: $!") 0 0 0
161 or $up->logconfess("backup failed for $base*: $!");
162 }
163  
164 0 return 1;
165 }
166  
167 ## @files = $up->revert_created()
168 ## + returns list of files created by this upgrade, for use with default revert() implementation
169 0 ▶sub revert_created {
Subroutine revert_created: not called CC=2 SCAR=17.9
Pod documented
170 0 my $up = shift;
171 0 my $hdr = $up->dbheader;
172  
173 return (
174 ##-- unigrams
175 0 (grep {!-e $up->backupdir."/$_"} map {"xf.$_"} qw(dbaN dbaN.hdr)),
176  
177 ##-- cofreqs
178 0 (grep {!-e $up->backupdir."/$_"} map {"cof.$_"} qw(dbaN dbaN.hdr)),
179  
180 ##-- tdf
181 0 ▶ (grep {-e "$up->{dbdir}/$_" && !-e $up->backupdir."/$_"} map {"tdf.d/$_"} qw(yf.pdl yf.pdl.hdr)),
Condition0%0/3
-e "$up->{'dbdir'}/$_" && !(-e $up->backupdir . "/$_")
!ll&&!rl&&r
000
MC/DC0%0/2
-e "$up->{'dbdir'}/$_" && !(-e $up->backupdir . "/$_")
-e "$up->{'dbdir'}/$_" !(-e $up->backupdir . "/$_")
Truth table0%0/3
A && not(B)
A= -e "$up->{'dbdir'}/$_"B= !(-e $up->backupdir . "/$_")
ABresult
0X0
100
111
182  
183 ##-- header
184 #'header.json',
185 );
186 }
187  
188 ## @files = $up->revert_updated()
189 ## + returns list of files updated by this upgrade, for use with default revert() implementation
190 0 ▶sub revert_updated {
Subroutine revert_updated: not called CC=1 SCAR=6.9
Pod documented
191 0 my $up = shift;
192 0 my $hdr = $up->dbheader;
193  
194 return (
195 ##-- unigrams
196 "xf.hdr",
197 0 (grep {-e $up->backupdir."/$_"} map {"xf.$_"} qw(dbaN dbaN.hdr)),
198  
199 ##-- cofreqs
200 "cof.hdr",
201 0 (grep {-e $up->backupdir."/$_"} map {"cof.$_"} qw(dbaN dbaN.hdr)),
202  
203 ##-- tdf
204 0 (grep {-e $up->backupdir."/$_"} map {"tdf.$_"} qw(hdr d/yf.pdl d/yf.pdl.hdr)),
205  
206 ##-- header
207 'header.json',
208 );
209 }
210  
211  
212 ##==============================================================================
213 ## Footer
214 1 1; ##-- be happy
« DiaColloDB/Document/TCF.pm ↑ Summary DiaColloDB/EnumFile.pm »