DiaColloDB/EnumFile/Tied.pm

filter: Statementstmt 28.0% 28 / 100 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 27.5% 8 / 29 Podpod 0.0% 0 / 4 totaltotal 20.2% 36 / 178 CC 16 SCAR 25.4
CC
16
Cov
21%
tienew
56.0
tiepair
12.0
NEXTKEY
12.0
CRAP 12.7
« DiaColloDB/Document/Storable.pm ↑ Summary DiaColloDB/Persistent.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::EnumFile::Tied.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db: file-based enums: tied interface
5  
6 package DiaColloDB::EnumFile::Tied;
7 1 1;
8  
9 package DiaColloDB::EnumFile;
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ##==============================================================================
14 ## Global Wrappers
15  
16 ## $enum = $CLASS->tienew(%opts,class=>$enumFileClass)
17 ## $enum = $CLASS->tienew($enum)
18 ## + returns $enum if specified, otherwise a new EnumFile object for %opts
19 0 ▶sub tienew {
Subroutine tienew: not called CC=7 SCAR=40.3
Pod undocumented
20 0 my $that = shift;
21 0 my $enum;
22 0 ▶ if (@_==1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
23 0 $enum = shift;
24 } else {
25 0 my %opts = @_;
26 0 ▶ my $class = $opts{class} || ref($that) || $that || __PACKAGE__;
Condition0%0/8
$opts{'class'} || ref $that || $that || __PACKAGE__
l!l
00
$opts{"class"} or ref $that or $that
l!l&&r!l&&!r
000
$opts{"class"} or ref $that
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts{'class'} || ref $that || $that || __PACKAGE__
$opts{"class"} ref $that $that
Truth table0%0/4
A or B or C
A= $opts{"class"}B= ref $thatC= $that
ABCresult
1XX1
01X1
0011
0000
27 0 ▶ $class = "DiaColloDB::$class" if (!UNIVERSAL::isa($class,'DiaColloDB::EnumFile'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($class, "DiaColloDB::EnumFile") 0 0 0
28 0 delete $opts{class};
29 0 ▶ $enum = $class->new(%opts)
Branch: 1 branch
BranchTrueFalseTotal
$enum = $class->new(%opts) or $that->logconfess("tienew(): could not create enum object of class '${class}'") 0 0 0
30 or $that->logconfess("tienew(): could not create enum object of class '$class'");
31 }
32 #$enum->{shared} = 1; ##-- refs are shared, so we should be o.k. with auto-close
33 0 return $enum;
34 }
35  
36 ## (\@id2sym,\%sym2id) = $CLASS->tiepair(%opts)
37 ## (\@id2sym,\%sym2id) = $CLASS->tiepair($enum)
38 ## (\@id2sym,\%sym2id) = $OBJECT->tiepair()
39 ## + returns pair of tied objects suitable for simulating e.g. MUDL::Enum
40 ## + %opts: passed to $CLASS->tienew()
41 0 ▶sub tiepair {
Subroutine tiepair: not called CC=3 SCAR=24.8
Pod undocumented
42 0 my $that = shift;
43 0 ▶ my $enum = ref($that) ? $that : $that->tienew(@_)
Branch: 2 branches
BranchTrueFalseTotal
my $enum = ref $that ? $that : $that->tienew(@_) or $that->logconfess("tiepair(): could not create EnumFile object") 0 0 0
ref $that ? : 0 0 0
44 or $that->logconfess("tiepair(): could not create EnumFile object");
45  
46 0 my (@id2sym,%sym2id);
47 0 tie(@id2sym, $enum->tieArrayClass, $enum);
48 0 tie(%sym2id, $enum->tieHashClass, $enum);
49 0 return (\@id2sym,\%sym2id);
50 }
51  
52 ## $class = $CLASS_OR_OBJECT->tieArrayClass()
53 ## + returns class for tied arrays to be returned by tiepair() method
54 ## + default just returns "DiaColloDB::EnumFile::TiedArray"
55 0 ▶sub tieArrayClass {
Subroutine tieArrayClass: not called CC=1 SCAR=6.9
Pod undocumented
56 0 return "DiaColloDB::EnumFile::TiedArray";
57 }
58  
59 ## $class = $CLASS_OR_OBJECT->tieHashClass()
60 ## + returns class for tied arrays to be returned by tiepair() method
61 ## + default just returns "DiaColloDB::EnumFile::TiedHash"
62 0 ▶sub tieHashClass {
Subroutine tieHashClass: not called CC=1 SCAR=6.9
Pod undocumented
63 0 return "DiaColloDB::EnumFile::TiedHash";
64 }
65  
66 ##==============================================================================
67 ## API: TiedArray
68  
69 package DiaColloDB::EnumFile::TiedArray;
70 1 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 our @ISA = qw(Tie::Array);
74  
75 ##--------------------------------------------------------------
76 ## API: TiedArray: mandatory methods
77  
78 ## $tied = tie(@array, $tieClass, $enum)
79 ## $tied = tie(@array, $tieClass, %opts)
80 ## $tied = TIEARRAY($class, $tieClass, %opts, class=>$enumFileClass)
81 ## $tied = TIEARRAY($class, $tieClass, $enum)
82 ## + %opts as for DiaColloDB::EnumFile::tienew()
83 ## + returns $tied = \$enum
84 0 ▶sub TIEARRAY {
Subroutine TIEARRAY: not called CC=2 SCAR=17.9
85 0 my $that = shift;
86 0 my $enum = DiaColloDB::EnumFile->tienew(@_);
87 0 ▶ return bless \$enum, ref($that)||$that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
88 }
89  
90  
91 ## $val = $tied->FETCH($index)
92 0 ▶sub FETCH {
Subroutine FETCH: not called CC=1 SCAR=6.9
93 0 return ${$_[0]}->i2s($_[1]);
94 }
95  
96 ## $count = $tied->FETCHSIZE()
97 ## + like scalar(@array)
98 0 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
99 0 return ${$_[0]}->size();
100 }
101  
102 ## $val = $tied->STORE($index,$val)
103 0 ▶sub STORE {
Subroutine STORE: not called CC=2 SCAR=17.9
104 0 ${$_[0]}->{dirty} = 1;
105 0 ▶ ${$_[0]}->setsize($_[1]+1) if ($_[1] >= ${$_[0]}->size);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] >= ${$_[0];}->size 0 0 0
106 0 return ${$_[0]}->{i2s}[$_[1]] = $_[2];
107 }
108  
109 ## $count = $tied->STORESIZE($count)
110 ## + not quite safe
111 0 ▶sub STORESIZE {
Subroutine STORESIZE: not called CC=1 SCAR=6.9
112 0 ${$_[0]}->{dirty} = 1;
113 0 return ${$_[0]}->setsize($_[1]);
114 }
115  
116 ## $bool = $tied->EXISTS($index)
117 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
118 0 return $_[1] < ${$_[0]}->size();
119 }
120  
121 ## undef = $tied->DELETE($index)
122 ## + not properly supported; just deletes from in-memory cache
123 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
124 0 return delete ${$_[0]}->{i2s}[$_[1]];
125 }
126  
127 ##--------------------------------------------------------------
128 ## API: TiedArray: optional methods
129  
130 ## undef = $tied->CLEAR()
131 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
132 0 ${$_[0]}->fromArray([]);
133 }
134  
135 #sub PUSH { ... }
136 #sub POP { ... }
137 #sub SHIFT { ... }
138 #sub UNSHIFT { ... }
139 #sub SPLICE { ... }
140 #sub EXTEND { ... }
141 #sub DESTROY { ... }
142  
143  
144 ##==============================================================================
145 ## API: TiedHash
146  
147 package DiaColloDB::EnumFile::TiedHash;
148 1 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 our @ISA = qw(Tie::Hash);
152  
153 ##--------------------------------------------------------------
154 ## API: TiedHash: mandatory methods
155  
156 ## $tied = tie(%hash, $tieClass, $enum)
157 ## $tied = tie(%hash, $tieClass, %opts)
158 ## $tied = TIEHASH($class, $tieClass, %opts, class=>$enumFileClass)
159 ## $tied = TIEHASH($class, $tieClass, $enum)
160 ## + %opts as for DiaColloDB::EnumFile::tienew()
161 ## + returned $tied = \$enum
162 0 ▶sub TIEHASH {
Subroutine TIEHASH: not called CC=2 SCAR=17.9
163 0 my $that = shift;
164 0 my $enum = DiaColloDB::EnumFile->tienew(@_);
165 0 ▶ return bless \$enum, ref($that)||$that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
166 }
167  
168 ##--------------------------------------------------------------
169 ## API: TiedArray: optional methods
170  
171 ## $val = $tied->STORE($key, $value)
172 0 ▶sub STORE {
Subroutine STORE: not called CC=2 SCAR=17.9
173 0 ${$_[0]}->{dirty} = 1;
174 0 ▶ ${$_[0]}->setsize($_[2]+1) if ($_[2] >= ${$_[0]}->size);
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] >= ${$_[0];}->size 0 0 0
175 0 return ${$_[0]}->{s2i}{$_[1]} = $_[2];
176 }
177  
178 ## $val = $tied->FETCH($key)
179 0 ▶sub FETCH {
Subroutine FETCH: not called CC=1 SCAR=6.9
180 0 return ${$_[0]}->s2i($_[1]);
181 }
182  
183 ## $key = $tied->FIRSTKEY()
184 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=2 SCAR=17.9
185 0 ▶ return ${$_[0]}->i2s(0) // '';
Condition0%0/2
${$_[0];}->i2s(0) // ""
l!l
00
MC/DC0%0/1
${$_[0];}->i2s(0) // ""
${$_[0];}->i2s(0)
Truth table0%0/2
A
A= ${$_[0];}->i2s(0)
Aresult
11
00
186 }
187  
188 ## $key = $tied->NEXTKEY($lastkey)
189 ## + only works for enums without index-gaps
190 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=3 SCAR=24.8
191 0 my $i = ${$_[0]}->s2i($_[1]);
192 0 ▶ return undef if (!defined($i) || ++$i >= ${$_[0]}->size);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $i or ++$i >= ${$_[0];}->size 0 0 0
Condition0%0/3
not defined $i or ++$i >= ${$_[0];}->size
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $i or ++$i >= ${$_[0];}->size
not defined $i ++$i >= ${$_[0];}->size
Truth table0%0/3
not(A) or B
A= not defined $iB= ++$i >= ${$_[0];}->size
ABresult
1X1
011
000
193 0 return ${$_[0]}->i2s($i);
194 }
195  
196 ## $bool = $tied->EXISTS($key)
197 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
198 0 return ${$_[0]}->s2i($_[1]);
199 }
200  
201 ## undef = $tied->DELETE($key)
202 ## + not properly supported; just deletes from in-memory cache
203 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
204 0 ${$_[0]}->{dirty} = 1;
205 0 delete ${$_[0]}->{s2i}{$_[1]};
206 }
207  
208 ## undef = $tied->CLEAR()
209 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
210 0 ${$_[0]}->fromArray([]);
211 }
212  
213 ## $scalar = $tied->SCALAR()
214 ## + returns key count
215 0 ▶sub SCALAR {
Subroutine SCALAR: not called CC=1 SCAR=6.9
216 0 return ${$_[0]}->size();
217 }
218  
219  
220 ##==============================================================================
221 ## Footer
222 1 1;
223  
224 __END__
« DiaColloDB/Document/Storable.pm ↑ Summary DiaColloDB/Persistent.pm »