Indexer.pm

filter: Statementstmt 32.6% 62 / 190 Branchbran 3.1% 3 / 94 Conditioncond 2.0% 1 / 50 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 55.1% 16 / 29 Podpod 0.0% 0 / 10 totaltotal 20.4% 82 / 402 CC 78 SCAR 63.1
CC
78
Cov
20%
search
1482.0
add_index
110.0
get_value
42.0
CRAP 551.1
↑ Summary SymlinkTree.pm »
1 package Tie::Indexer;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 my $dp = new B::Deparse("-sCi0");
5 1 our $VERSION="0.1";
6  
7 BEGIN {
8 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶ use constant Equals => do { package main; sub { $_[0] eq $_[1] } };
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=0.1
11 0 ▶ use constant Not => do { package main; sub { !exists $_[1]->{$_[0]}; } };
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=0.1
12 0 ▶ use constant Exists => do { package main; sub { exists $_[0]->{$_[1]}; } };
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=0.1
13 0 ▶ use constant IndexSimple => do { package main; sub { Tie::Indexer::get_value(@_); } };
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=0.1
14 0 ▶ use constant IndexExists => do{ package main; sub { keys %{Tie::Indexer::get_value(@_)}; } };
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine BEGIN: not called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=1 SCAR=0.4
15 }
16  
17 1 my %operators = (
18 '=' => Equals,
19 '!' => Not,
20 'E' => Exists,
21 );
22  
23 1 my %codeindex;
24 2 ▶sub code2text {
Subroutine code2text: called CC=3 SCAR=11.2
Pod undocumented
25 2 my ($operator) = @_;
26 2 ▶ return undef if !defined $operator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $operator 0 2 2
27 2 ▶ if (!exists $codeindex{$operator}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $codeindex{$operator}) 2 0 2
28 2 $codeindex{$operator} = $dp->coderef2text($operator);
29 2 $codeindex{$operator} =~ s/\n/ /g;
30 }
31 2 return $codeindex{$operator};
32 }
33  
34 1 my %indexers = (
35 code2text(Equals) => IndexSimple,
36 code2text(Exists) => IndexExists,
37 );
38  
39 0 ▶sub get_value {
Subroutine get_value: not called CC=6 SCAR=37.4
Pod undocumented
40 0 my ($tie, $expr, $node) = @_;
41 0 ▶ return $node if !defined $expr || $expr eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $expr or $expr eq "" 0 0 0
Condition0%0/3
not defined $expr or $expr eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $expr or $expr eq ""
not defined $expr $expr eq ""
Truth table0%0/3
not(A) or B
A= not defined $exprB= $expr eq ""
ABresult
1X1
011
000
42 0 my ($prefix) = ($expr =~ m/^([a-z]+);/);
43 0 $expr =~ s/^[a-z]+;//;
44 0 my @path = split("/",$expr);
45 0 my $value = $node;
46 0 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 0 0 0
47 0 ▶ if (UNIVERSAL::isa($value,'HASH')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, 'HASH')) { } 0 0 0
elsif (UNIVERSAL::isa($value, 'ARRAY')) { } 0 0 0
48 0 $value = $value->{shift @path};
49 } elsif (UNIVERSAL::isa($value,'ARRAY')) {
50 0 $value = $value->[shift @path];
51 } else {
52 0 undef $value;
53 0 undef @path;
54 }
55 }
56 0 return $value;
57 }
58  
59 3 ▶sub deindex_node {
Subroutine deindex_node: called CC=5 SCAR=26.5
Pod undocumented
60 3 my ($tie, $node, $nodeid) = @_;
61 3 foreach my $index (get_indices($tie)) {
62 0 ▶ if ($$index[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index->[3]) { } 0 0 0
63 0 delete $$index[0]{$_}{$nodeid} foreach ($$index[3]->($tie,$$index[2],$node,$$index[0]));
64 } else {
65 0 delete $$index[0]{$_}{$nodeid} foreach ($$index[4]->($tie,$$index[2],$node,$$index[0]));
66 }
67 }
68 }
69  
70 0 ▶sub index_node_single {
Subroutine index_node_single: not called CC=5 SCAR=27.4
Pod undocumented
71 0 my ($tie, $node, $nodeid, $index) = @_;
72 0 ▶ if ($$index[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index->[3]) { } 0 0 0
73 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 0 $$index[0]{$_}{$nodeid} = undef foreach ($$index[3]->($tie,$$index[2],$node,$$index[0]));
75 } else {
76 0 foreach my $value ($$index[4]->($tie,$$index[2],$node,$$index[0])) {
77 0 ▶ $$index[0]{$value}{$nodeid} = undef if ($$index[1]->($value,get_value($tie,$$index[2],$node)));
Branch: 1 branch
BranchTrueFalseTotal
if $index->[1]->($value, get_value($tie, $index->[2], $node)) 0 0 0
78 }
79 }
80 }
81  
82 3 ▶sub index_node {
Subroutine index_node: called CC=2 SCAR=7.6
Pod undocumented
83 3 my ($tie, $node, $nodeid) = @_;
84 3 foreach my $index (get_indices($tie)) {
85 0 index_node_single(@_,$index);
86 }
87 }
88  
89 6 ▶sub get_indices {
Subroutine get_indices: called CC=8 SCAR=33.9
Pod undocumented
90 6 my ($tie) = @_;
91 6 my @res;
92 6 ▶ my $index = $tie->_get_index() || {};
Condition50%1/2
$tie->_get_index || {}
l!l
06
MC/DC0%0/1
$tie->_get_index || {}
$tie->_get_index
Truth table50%1/2
A
A= $tie->_get_index
Aresult
11
00
93 6 ▶ while (my ($expr, $eindex) = each %$index) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$index) 0 6 6
94 0 ▶ next if $expr =~ m/^\x{200b}[^\x{200b}]/;
Branch: 1 branch
BranchTrueFalseTotal
if $expr =~ /^\x{200b}[^\x{200b}]/ 0 0 0
95 0 $expr =~ s/^\x{200b}\x{200b}/\x{200b}/;
96 0 ▶ my $values = eval "no strict; package main; return sub ".$$eindex{"\x{200b}values"} if exists $$eindex{"\x{200b}values"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $eindex->{"\x{200b}values"} 0 0 0
97 0 ▶ $values ||= do { no strict; package main; sub { keys %{$_[3]} } };
Subroutine BEGIN: called CC=1 SCAR=2.9
Subroutine __ANON__: not called CC=1 SCAR=2.9
Condition0%0/3
$values ||= do { package main; no strict; sub { keys %{$_[3];}; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$values ||= do { package main; no strict; sub { keys %{$_[3];}; } }
$values do { package main; no strict; sub { keys %{$_[3];}; } }
Truth table0%0/3
A ||= B
A= $valuesB= do { package main; no strict; sub { keys %{$_[3];}; } }
ABresult
1X1
011
000
98 0 ▶ while (my ($operator, $oindex) = each %$eindex) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$eindex) 0 0 0
99 0 ▶ my $indexer = eval "no strict; package main; return sub ".$$index{"\x{200b}indexers"}{$operator} if exists $$index{"\x{200b}indexers"}{$operator};
Branch: 1 branch
BranchTrueFalseTotal
if exists $index->{"\x{200b}indexers"}{$operator} 0 0 0
100 0 push @res, [$oindex, eval "no strict; package main; return sub ".$operator, $expr, $indexer, $values];
101 }
102 }
103 6 return @res;
104 }
105  
106 0 ▶sub get_index {
Subroutine get_index: not called CC=6 SCAR=37.4
Pod undocumented
107 0 my ($tie, $expr, $operator, $force) = @_;
108 0 $operator = code2text($operator);
109 0 ▶ my $index = $tie->_get_index() || return undef;
Condition0%0/2
$tie->_get_index || (return undef)
l!l
00
MC/DC0%0/1
$tie->_get_index || (return undef)
$tie->_get_index
Truth table0%0/2
A
A= $tie->_get_index
Aresult
11
00
110 0 ▶ $expr = '' if (!defined $expr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 0 0 0
111 0 $expr =~ s/^\x{200b}/\x{200b}\x{200b}/;
112 0 ▶ return undef if (!$force && (!exists $$index{$expr} || !exists $$index{$expr}{$operator}));
Branch: 1 branch
BranchTrueFalseTotal
unless $force or exists $index->{$expr} and exists $index->{$expr}{$operator} 0 0 0
Condition0%0/6
$force or exists $index->{$expr} and exists $index->{$expr}{$operator}
l!l&&r!l&&!r
000
exists $index->{$expr} and exists $index->{$expr}{$operator}
!ll&&!rl&&r
000
MC/DC0%0/3
$force or exists $index->{$expr} and exists $index->{$expr}{$operator}
$force exists $index->{$expr} exists $index->{$expr}{$operator}
Truth table0%0/4
A or B and C
A= $forceB= exists $index->{$expr}C= exists $index->{$expr}{$operator}
ABCresult
1XX1
0111
00X0
0100
113 0 return $$index{$expr}{$operator};
114 }
115  
116 0 ▶sub add_index {
Subroutine add_index: not called CC=10 SCAR=47.0
Pod undocumented
117 0 my ($tie, $operator, $expr, $indexer, $values) = @_;
118 0 ▶ $operator = $operators{$operator} if (exists $operators{$operator});
Branch: 1 branch
BranchTrueFalseTotal
if exists $operators{$operator} 0 0 0
119 0 $operator = code2text($operator);
120 0 ▶ $indexer ||= $indexers{$operator} if (exists $indexers{$operator});
Branch: 1 branch
BranchTrueFalseTotal
if exists $indexers{$operator} 0 0 0
Condition0%0/3
$indexer ||= $indexers{$operator}
l!l&&r!l&&!r
000
MC/DC0%0/2
$indexer ||= $indexers{$operator}
$indexer $indexers{$operator}
Truth table0%0/3
A ||= B
A= $indexerB= $indexers{$operator}
ABresult
1X1
011
000
121 0 $indexer = code2text($indexer);
122 0 $values = code2text($values);
123  
124 0 my $index = $tie->_get_index(1);
125 0 ▶ $$index{"\x{200b}indexers"}{$operator} ||= $indexer if (defined $indexer);
Branch: 1 branch
BranchTrueFalseTotal
if defined $indexer 0 0 0
Condition0%0/3
$index->{"\x{200b}indexers"}{$operator} ||= $indexer
l!l&&r!l&&!r
000
MC/DC0%0/2
$index->{"\x{200b}indexers"}{$operator} ||= $indexer
$index->{"\x{200b}indexers"}{$operator} $indexer
Truth table0%0/3
A ||= B
A= $index->{"\x{200b}indexers"}{$operator}B= $indexer
ABresult
1X1
011
000
126 0 $expr =~ s/^\x{200b}/\x{200b}\x{200b}/;
127 0 ▶ $$index{$expr} ||= {};
Condition0%0/2
$index->{$expr} ||= {}
l!l
00
MC/DC0%0/1
$index->{$expr} ||= {}
$index->{$expr}
Truth table0%0/2
A
A= $index->{$expr}
Aresult
11
00
128 0 $index = $$index{$expr};
129 0 ▶ $$index{"\x{200b}values"} ||= $values if (defined $values);
Branch: 1 branch
BranchTrueFalseTotal
if defined $values 0 0 0
Condition0%0/3
$index->{"\x{200b}values"} ||= $values
l!l&&r!l&&!r
000
MC/DC0%0/2
$index->{"\x{200b}values"} ||= $values
$index->{"\x{200b}values"} $values
Truth table0%0/3
A ||= B
A= $index->{"\x{200b}values"}B= $values
ABresult
1X1
011
000
130 0 ▶ $$index{$operator} ||= {};
Condition0%0/2
$index->{$operator} ||= {}
l!l
00
MC/DC0%0/1
$index->{$operator} ||= {}
$index->{$operator}
Truth table0%0/2
A
A= $index->{$operator}
Aresult
11
00
131 }
132  
133 0 ▶sub search {
Subroutine search: not called CC=38 SCAR=73.0
Pod undocumented
134 0 my $tie = shift;
135 0 my $base = shift;
136 0 my ($expr, $operator, $value) = ($base, Equals, undef);
137 0 ▶ if (ref($base) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $base eq 'HASH') { } 0 0 0
138 0 ▶ return (wantarray?values(%$base):(values %$base)[0]) if !@_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 0 0 0
wantarray ? : 0 0 0
139 0 $expr = shift;
140 } else {
141 0 undef $base;
142 }
143  
144 0 ▶ $expr = $operators{$expr} if (exists $operators{$expr});
Branch: 1 branch
BranchTrueFalseTotal
if exists $operators{$expr} 0 0 0
145  
146 0 ▶ if (!ref($expr) || ref($expr) ne 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $expr or ref $expr ne 'CODE') { } 0 0 0
elsif (ref $expr eq 'CODE') { } 0 0 0
Condition0%0/3
not ref $expr or ref $expr ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $expr or ref $expr ne "CODE"
not ref $expr ref $expr ne "CODE"
Truth table0%0/3
not(A) or B
A= not ref $exprB= ref $expr ne "CODE"
ABresult
1X1
011
000
147 0 $value = shift;
148 0 ▶ if (ref($value) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'CODE') { } 0 0 0
elsif (exists $operators{$value}) { } 0 0 0
149 0 $operator = $value;
150 0 $value = shift;
151 } elsif (exists $operators{$value}) {
152 0 $operator = $operators{$value};
153 0 $value = shift;
154 }
155 } elsif (ref($expr) eq 'CODE') {
156 0 $operator = $expr;
157 0 $expr = undef;
158 0 $value = undef;
159 }
160 0 my $index;
161 0 ▶ if ($operator ne Not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator ne sub { no warnings; not exists $_[1]{$_[0]};}) 0 0 0
162 0 ▶ if (defined ($index = get_index($tie, $expr, $operator))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($index = get_index($tie, $expr, $operator))) { } 0 0 0
elsif (not $base and defined($index = get_index($tie, $expr, sub { package main; no warnings; no strict; $_[0] eq $_[1]; } ))) { } 0 0 0
163 # do nothing
164 ▶ } elsif (!$base && defined ($index = get_index($tie, $expr, Equals))) {
Condition0%0/3
not $base and defined($index = get_index($tie, $expr, sub { no warnings; $_[0] eq $_[1];}))
!ll&&!rl&&r
000
MC/DC0%0/2
not $base and defined($index = get_index($tie, $expr, sub { no warnings; $_[0] eq $_[1];}))
not $base defined($index = get_index($tie, $expr, sub { no warnings; $_[0] eq $_[1];}))
Truth table0%0/3
not(A) and B
A= not $baseB= defined($index = get_index($tie, $expr, sub { no warnings; $_[0] eq $_[1];}))
ABresult
0X0
100
111
165 # TODO: use an Equals index for base matches as well?
166 # perhaps some heuristic: if (keys %$base > keys %$index)
167 0 my %res;
168 0 foreach my $exp (keys %$index) {
169 0 local $_ = $exp;
170 0 ▶ if ($operator->($exp,$value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$operator($exp, $value)) 0 0 0
171 0 ▶ $res{$_} ||= $tie->FETCH($_) foreach (keys %{$$index{$exp}});
Condition0%0/3
$res{$_} ||= $tie->FETCH($_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$res{$_} ||= $tie->FETCH($_)
$res{$_} $tie->FETCH($_)
Truth table0%0/3
A ||= B
A= $res{$_}B= $tie->FETCH($_)
ABresult
1X1
011
000
172 0 ▶ last if !wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
173 }
174 }
175 0 return search($tie,\%res,@_);
176 }
177 }
178 0 ▶ if (!$index && !$base && (wantarray || $operator eq Not)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $index || $base and wantarray || $operator eq sub { no warnings; not exists $_[1]{$_[0]};}) 0 0 0
Condition0%0/9
not $index || $base and wantarray || $operator eq sub { no warnings; not exists $_[1]{$_[0]};}
!ll&&!rl&&r
000
$index || $base
l!l&&r!l&&!r
000
wantarray or $operator eq sub { no warnings; not exists $_[1]{$_[0]};}
l!l&&r!l&&!r
000
MC/DC0%0/4
not $index || $base and wantarray || $operator eq sub { no warnings; not exists $_[1]{$_[0]};}
$index $base wantarray $operator eq sub { no warnings; not exists $_[1]{$_[0]};}
Truth table0%0/5
not(A || B) and C or D
A= $indexB= $baseC= wantarrayD= $operator eq sub { no warnings; not exists $_[1]{$_[0]};}
ABCDresult
1XXX0
01XX0
00000
001X1
00011
179 0 $base = {};
180 0 my $key = $tie->FIRSTKEY;
181 0 ▶ while (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
182 0 $$base{$key} = $tie->FETCH($key);
183 0 $key = $tie->NEXTKEY;
184 }
185 }
186  
187 0 ▶ if ($operator eq Not) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator eq sub { no warnings; not exists $_[1]{$_[0]};}) 0 0 0
188 0 ▶ if (!$expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $expr) { } 0 0 0
189 0 return search($tie,$base,@_,{ %$base },Not);
190 } else {
191 0 delete $$expr{$_} foreach (keys %$base);
192 0 return search($tie,$expr);
193 }
194 }
195 0 ▶ if ($index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index) 0 0 0
196 0 ▶ if (defined ($index = $$index{$value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($index = $index->{$value})) 0 0 0
197 0 ▶ if (!$base) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $base) { } 0 0 0
198 0 $base = { map { ($_ => $tie->FETCH($_)) } keys %$index };
199 } else {
200 0 foreach my $key (keys %$base) {
201 0 ▶ delete $$base{$key} if !exists $$index{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $index->{$key} 0 0 0
202 }
203 }
204 0 return search($tie,$base,@_);
205 }
206 0 return ();
207 }
208  
209 0 ▶ if (!$base) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base) 0 0 0
210 0 my $key = $tie->FIRSTKEY;
211 0 ▶ while (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
212 0 my $node = $tie->FETCH($key);
213 0 local $_ = get_value($tie,$expr,$node);
214 0 ▶ return $node if $operator->($_,$value) && search($tie,{$key => $node},@_);
Branch: 1 branch
BranchTrueFalseTotal
if &$operator($_, $value) and search($tie, {$key, $node}, @_) 0 0 0
Condition0%0/3
&$operator($_, $value) and search($tie, {$key, $node}, @_)
!ll&&!rl&&r
000
MC/DC0%0/2
&$operator($_, $value) and search($tie, {$key, $node}, @_)
&$operator($_, $value) search($tie, {$key, $node}, @_)
Truth table0%0/3
A and B
A= &$operator($_, $value)B= search($tie, {$key, $node}, @_)
ABresult
0X0
100
111
215 0 $key = $tie->NEXTKEY;
216 }
217 0 return undef;
218 }
219  
220 0 ▶ while (my ($key, $node) = each %$base) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$base) 0 0 0
221 0 local $_ = get_value($tie,$expr,$node);
222 0 ▶ delete $$base{$key} if !$operator->($_,$value);
Branch: 1 branch
BranchTrueFalseTotal
unless &$operator($_, $value) 0 0 0
223 }
224 0 return search($tie,$base,@_);
225 }
226  
227 0 ▶sub build_index {
Subroutine build_index: not called CC=4 SCAR=30.0
Pod undocumented
228 0 my ($tie) = @_;
229 0 foreach my $index (get_indices($tie)) {
230 0 foreach my $key (keys %{$$index[0]}) {
231 0 undef $$index[0]{$key};
232 }
233 0 my $key = $tie->FIRSTKEY;
234 0 ▶ while (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
235 0 index_node_single($tie,$tie->FETCH($key),$key,$index);
236 0 $key = $tie->NEXTKEY;
237 }
238 }
239 }
240  
241 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 1 "Dahut!";
243 __END__
↑ Summary SymlinkTree.pm »