include/punk/punk_computed.h

filter: Statementstmt 97.5% 400 / 410 Branchbran 69.3% 423 / 606 (25 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.7% 823 / 1016 (25 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_sitemap.h ↑ Summary include/punk/punk_dbi.h »
1 /* punk_computed.h - Punk::Model::Computed, a base class for read-only model
2 * backends whose rows are worked out from the question rather than stored,
3 * and Punk::Model::Computed::Error, the error a visitor's input raises there.
4 *
5 * A subclass declares its tables (`answers`): which fields each may be asked
6 * by, which operators a field takes, and which fields are taken whole. The
7 * base checks every filter and option against that, as Punk's own backends
8 * check theirs, and hands the normalised question ({ field => { op => value }})
9 * to the subclass's _get_<table>, _search_<table> and _count_<table>. Nothing
10 * is stored, so create, update and delete croak.
11 *
12 * The declarations live in the subclass's own package (%PUNK_COMPUTED), not
13 * in a C static: they are made at compile time and a static would hold the
14 * parent interpreter's SVs in a pseudo-forked child.
15 *
16 * The object is the blessed hash pdbi_build_self makes, plus spec (the
17 * table's declaration), app (weak), and the cache's slots.
18 *
19 * Include after punk_dbq.h. */
20  
21 #ifndef PUNK_COMPUTED_H
22 #define PUNK_COMPUTED_H
23  
24 #define PMC "Punk::Model::Computed"
25 #define PMCE "Punk::Model::Computed::Error"
26 #define PMC_CACHE_DEFAULT 256
27  
28 /* ---- the error ----------------------------------------------------------------- */
29  
30 15 static SV *pmce_new(pTHX_ SV *cls, SV *field, SV *message) {
31 15 HV *h = newHV();
32 HV *stash;
33 /*
34 # uncoverable branch true
35 # uncoverable condition left count:1 class:ignore_covered_err
36 # uncoverable condition left count:2 class:ignore_covered_err
37 # uncoverable condition left count:3 class:ignore_covered_err
38 # uncoverable condition right count:3 class:ignore_covered_err */
39 15 ▶ if (cls && SvROK(cls) && SvOBJECT(SvRV(cls))) stash = SvSTASH(SvRV(cls));
Branch: 3 branches
BranchTrueFalseTotal
if (cls && SvROK(cls) && SvOBJECT(SvRV(cls))) stash = SvSTASH(SvRV(cls)); 5 10 15
if (cls && SvROK(cls) && SvOBJECT(SvRV(cls))) stash = SvSTASH(SvRV(cls)); 0 5 5
if (cls && SvROK(cls) && SvOBJECT(SvRV(cls))) stash = SvSTASH(SvRV(cls)); 0 0 0
40 15 ▶ else stash = gv_stashsv(cls ? cls : sv_2mortal(newSVpvs(PMCE)), GV_ADD);
Branch: 1 branch
BranchTrueFalseTotal
else stash = gv_stashsv(cls ? cls : sv_2mortal(newSVpvs(PMCE)), GV_ADD); 10 5 15
41 15 ▶ (void)hv_stores(h, "field", field ? newSVsv(field) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "field", field ? newSVsv(field) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 0 15
42 15 ▶ (void)hv_stores(h, "message", message ? newSVsv(message) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "message", message ? newSVsv(message) : newSV(0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 0 15
43 15 return sv_bless(newRV_noinc((SV *)h), stash);
44 }
45  
46 5 static int pmce_is(pTHX_ SV *e) {
47 /*
48 # uncoverable condition right count:1 class:ignore_covered_err
49 # uncoverable condition right count:3 class:ignore_covered_err */
50 5 ▶ return e && sv_isobject(e) && sv_derived_from(e, PMCE);
Branch: 3 branches
BranchTrueFalseTotal
return e && sv_isobject(e) && sv_derived_from(e, PMCE); 5 0 5
return e && sv_isobject(e) && sv_derived_from(e, PMCE); 2 3 5
return e && sv_isobject(e) && sv_derived_from(e, PMCE); 2 0 2
51 }
52  
53 9 static void pmce_throw(pTHX_ SV *cls, SV *field, SV *message) {
54 9 croak_sv(sv_2mortal(pmce_new(aTHX_ cls, field, message)));
55 }
56  
57 6 static void pmce_throw_pv(pTHX_ const char *field, SV *message) {
58 6 pmce_throw(aTHX_ NULL, sv_2mortal(newSVpv(field, 0)), message);
59 0 }
60  
61 94 ▶static int pmce_word(char c) { return isALNUM(c) || c == '_'; } /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
static int pmce_word(char c) { return isALNUM(c) || c == '_'; } /* # uncoverable condition left count:2 class:ignore_covered_err */ 15 79 94
static int pmce_word(char c) { return isALNUM(c) || c == '_'; } /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 15 15
62  
63 /* The length of `s` without a trailing " at FILE line N." (with perl's
64 * optional ", <FH> line N" or "chunk N" after it), the place a die names. */
65 10 static STRLEN pmce_strip_at(const char *s, STRLEN len) {
66 STRLEN p;
67 139 ▶ for (p = 0; p + 4 <= len; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p + 4 <= len; p++) { 136 3 139
68 STRLEN q;
69 136 ▶ if (memNE(s + p, " at ", 4)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (memNE(s + p, " at ", 4)) continue; 128 8 136
70 8 q = p + 4;
71 /*
72 # uncoverable branch true
73 # uncoverable condition left count:1 class:ignore_covered_err
74 # uncoverable condition left count:2 class:ignore_covered_err */
75 8 ▶ if (q >= len || isSPACE(s[q])) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= len || isSPACE(s[q])) continue; 8 0 8
if (q >= len || isSPACE(s[q])) continue; 0 8 8
76 60 ▶ while (q < len && !isSPACE(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (q < len && !isSPACE(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 60 0 60
while (q < len && !isSPACE(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 52 8 60
77 /*
78 # uncoverable branch true
79 # uncoverable condition left count:1 class:ignore_covered_err
80 # uncoverable condition left count:2 class:ignore_covered_err */
81 8 ▶ if (q + 6 > len || memNE(s + q, " line ", 6)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (q + 6 > len || memNE(s + q, " line ", 6)) continue; 8 0 8
if (q + 6 > len || memNE(s + q, " line ", 6)) continue; 0 8 8
82 8 q += 6;
83 /*
84 # uncoverable branch true
85 # uncoverable condition left count:1 class:ignore_covered_err
86 # uncoverable condition left count:2 class:ignore_covered_err */
87 8 ▶ if (q >= len || !isDIGIT(s[q])) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= len || !isDIGIT(s[q])) continue; 8 0 8
if (q >= len || !isDIGIT(s[q])) continue; 0 8 8
88 18 ▶ while (q < len && isDIGIT(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (q < len && isDIGIT(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 18 0 18
while (q < len && isDIGIT(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 8 18
89 8 ▶ if (q + 3 <= len && memEQ(s + q, ", <", 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if (q + 3 <= len && memEQ(s + q, ", <", 3)) { 3 5 8
if (q + 3 <= len && memEQ(s + q, ", <", 3)) { 2 1 3
90 2 STRLEN r = q + 3;
91 10 ▶ while (r < len && s[r] != '>') r++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (r < len && s[r] != '>') r++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
while (r < len && s[r] != '>') r++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 8 2 10
92 /*
93 # uncoverable branch false
94 # uncoverable condition right count:1 class:ignore_covered_err
95 # uncoverable condition right count:2 class:ignore_covered_err
96 # uncoverable condition right count:3 class:ignore_covered_err */
97 2 ▶ if (r < len && r + 2 <= len && s[r + 1] == ' ') {
Branch: 3 branches
BranchTrueFalseTotal
if (r < len && r + 2 <= len && s[r + 1] == ' ') { 2 0 2
if (r < len && r + 2 <= len && s[r + 1] == ' ') { 2 0 2
if (r < len && r + 2 <= len && s[r + 1] == ' ') { 2 0 2
98 2 r += 2;
99 /*
100 # uncoverable branch false
101 # uncoverable condition right count:1 class:ignore_covered_err
102 # uncoverable condition right count:2 class:ignore_covered_err
103 # uncoverable condition right count:3 class:ignore_covered_err */
104 2 ▶ if (r + 5 <= len && (memEQ(s + r, "line ", 5) || memEQ(s + r, "chunk", 5))) {
Branch: 3 branches
BranchTrueFalseTotal
if (r + 5 <= len && (memEQ(s + r, "line ", 5) || memEQ(s + r, "chunk", 5))) { 2 0 2
if (r + 5 <= len && (memEQ(s + r, "line ", 5) || memEQ(s + r, "chunk", 5))) { 1 1 2
if (r + 5 <= len && (memEQ(s + r, "line ", 5) || memEQ(s + r, "chunk", 5))) { 1 0 1
105 2 r += 5;
106 2 ▶ if (r < len && s[r] == ' ') r++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (r < len && s[r] == ' ') r++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
if (r < len && s[r] == ' ') r++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 1 2
107 /*
108 # uncoverable branch false
109 # uncoverable condition right count:1 class:ignore_covered_err
110 # uncoverable condition right count:2 class:ignore_covered_err */
111 2 ▶ if (r < len && isDIGIT(s[r])) {
Branch: 2 branches
BranchTrueFalseTotal
if (r < len && isDIGIT(s[r])) { 2 0 2
if (r < len && isDIGIT(s[r])) { 2 0 2
112 4 ▶ while (r < len && isDIGIT(s[r])) r++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (r < len && isDIGIT(s[r])) r++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
while (r < len && isDIGIT(s[r])) r++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 2 4
113 2 q = r;
114 }
115 }
116 }
117 }
118 8 ▶ if (q < len && s[q] == '.') q++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (q < len && s[q] == '.') q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 8 0 8
if (q < len && s[q] == '.') q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 1 8
119 8 ▶ if (q < len && s[q] == '\n') q++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (q < len && s[q] == '\n') q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 8 0 8
if (q < len && s[q] == '\n') q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 1 8
120 8 ▶ if (q == len) return p;
Branch: 1 branch
BranchTrueFalseTotal
if (q == len) return p; 7 1 8
121 }
122 3 ▶ if (len && s[len - 1] == '\n') return len - 1; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (len && s[len - 1] == '\n') return len - 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
if (len && s[len - 1] == '\n') return len - 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 2 3
123 2 return len;
124 }
125  
126 /* The length of a leading "Class->method: ", the prefix a dist's croak
127 * names itself with, or 0. */
128 10 static STRLEN pmce_prefix(const char *s, STRLEN len) {
129 10 STRLEN q = 0;
130 55 ▶ while (q < len && (pmce_word(s[q]) || s[q] == ':')) q++;
Branch: 3 branches
BranchTrueFalseTotal
while (q < len && (pmce_word(s[q]) || s[q] == ':')) q++; 52 3 55
while (q < len && (pmce_word(s[q]) || s[q] == ':')) q++; 41 11 52
while (q < len && (pmce_word(s[q]) || s[q] == ':')) q++; 4 7 11
131 /*
132 # uncoverable condition left count:1 class:ignore_covered_err
133 # uncoverable condition left count:4 class:ignore_covered_err */
134 10 ▶ if (!q || q + 2 > len || s[q] != '-' || s[q + 1] != '>') return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!q || q + 2 > len || s[q] != '-' || s[q + 1] != '>') return 0; 10 0 10
if (!q || q + 2 > len || s[q] != '-' || s[q + 1] != '>') return 0; 7 3 10
if (!q || q + 2 > len || s[q] != '-' || s[q + 1] != '>') return 0; 3 4 7
if (!q || q + 2 > len || s[q] != '-' || s[q + 1] != '>') return 0; 0 3 3
135 3 q += 2;
136 /*
137 # uncoverable branch true
138 # uncoverable condition left count:1 class:ignore_covered_err
139 # uncoverable condition left count:2 class:ignore_covered_err */
140 3 ▶ if (q >= len || !pmce_word(s[q])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= len || !pmce_word(s[q])) return 0; 3 0 3
if (q >= len || !pmce_word(s[q])) return 0; 0 3 3
141 12 ▶ while (q < len && pmce_word(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (q < len && pmce_word(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 12 0 12
while (q < len && pmce_word(s[q])) q++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 9 3 12
142 /*
143 # uncoverable branch true
144 # uncoverable condition left count:1 class:ignore_covered_err
145 # uncoverable condition left count:2 class:ignore_covered_err
146 # uncoverable condition left count:3 class:ignore_covered_err */
147 3 ▶ if (q + 2 > len || s[q] != ':' || s[q + 1] != ' ') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (q + 2 > len || s[q] != ':' || s[q + 1] != ' ') return 0; 3 0 3
if (q + 2 > len || s[q] != ':' || s[q + 1] != ' ') return 0; 3 0 3
if (q + 2 > len || s[q] != ':' || s[q + 1] != ' ') return 0; 0 3 3
148 3 return q + 2;
149 }
150  
151 /* A die's text in the dist's own words, for a visitor: without where it was
152 * raised and without the method that raised it. An object other than the
153 * error is its string. Mortal. */
154 11 static SV *pmce_said(pTHX_ SV *e) {
155 11 SV *s = sv_2mortal(newSVsv(e));
156 const char *p;
157 STRLEN len, end, start;
158 SV *out;
159 11 ▶ if (SvROK(e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(e)) { 1 10 11
160 (void)SvPV_nolen(s);
161 1 return s;
162 }
163 10 p = SvPV_const(s, len);
164 10 end = pmce_strip_at(p, len);
165 10 start = pmce_prefix(p, end);
166 10 out = sv_2mortal(newSVpvn(p + start, end - start));
167 10 ▶ if (SvUTF8(s)) SvUTF8_on(out);
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(s)) SvUTF8_on(out); 2 8 10
168 10 return out;
169 }
170  
171 /* The error a die becomes: itself when it is one already, else a new one for
172 * `field` in the dist's words. Mortal. */
173 5 static SV *pmce_from(pTHX_ SV *cls, SV *field, SV *e) {
174 5 ▶ if (pmce_is(aTHX_ e)) return e;
Branch: 1 branch
BranchTrueFalseTotal
if (pmce_is(aTHX_ e)) return e; 2 3 5
175 3 return sv_2mortal(pmce_new(aTHX_ cls, field, pmce_said(aTHX_ e)));
176 }
177  
178 31 static HV *pmce_hv(pTHX_ SV *self) {
179 /*
180 # uncoverable branch true
181 # uncoverable condition left count:1 class:ignore_covered_err
182 # uncoverable condition left count:2 class:ignore_covered_err */
183 31 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 31 0 31
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 0 31 31
184 0 croak(PMCE ": not an error object"); /* # uncoverable statement */
185 31 return (HV *)SvRV(self);
186 }
187  
188 /* ---- the declarations ------------------------------------------------------------ */
189  
190 77 static const char *pmc_cls(pTHX_ SV *self) {
191 77 ▶ if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 8 branches
BranchTrueFalseTotal
if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 63 14 77
if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 63 0 63
if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 63 0 63
if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 63 0 63
if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 0 63 63
if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 0 0 0
if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 63 0 63
if (SvROK(self) && SvOBJECT(SvRV(self))) return HvNAME(SvSTASH(SvRV(self))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 0 63 63
192 14 return SvPV_nolen(self);
193 }
194  
195 23 static HV *pmc_decl_hv(pTHX_ const char *cls, I32 flags) {
196 23 SV *name = sv_2mortal(newSVpvf("%s::PUNK_COMPUTED", cls));
197 23 return get_hv(SvPV_nolen(name), flags);
198 }
199  
200 /* The declaration of `table` for `cls` or the nearest class it inherits one
201 * from, or NULL. */
202 7 static HV *pmc_spec(pTHX_ const char *cls, SV *table) {
203 7 HV *stash = gv_stashpv(cls, 0);
204 AV *isa;
205 SSize_t i, n;
206 /*
207 # uncoverable branch true
208 # uncoverable condition left count:1 class:ignore_covered_err */
209 7 ▶ if (!stash) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) return NULL; 0 7 7
210 7 isa = mro_get_linear_isa(stash);
211 7 n = av_len(isa) + 1;
212 11 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 2 11
213 9 SV **c = av_fetch(isa, i, 0);
214 /*
215 # uncoverable condition right count:1 class:ignore_covered_err
216 # uncoverable condition right count:2 class:ignore_covered_err */
217 18 ▶ HV *d = c && *c ? pmc_decl_hv(aTHX_ SvPV_nolen(*c), 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *d = c && *c ? pmc_decl_hv(aTHX_ SvPV_nolen(*c), 0) : NULL; 9 0 9
HV *d = c && *c ? pmc_decl_hv(aTHX_ SvPV_nolen(*c), 0) : NULL; 9 0 9
218 9 ▶ HE *he = d ? hv_fetch_ent(d, table, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = d ? hv_fetch_ent(d, table, 0, 0) : NULL; 6 3 9
219 9 ▶ if (he && SvROK(HeVAL(he))) return (HV *)SvRV(HeVAL(he)); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (he && SvROK(HeVAL(he))) return (HV *)SvRV(HeVAL(he)); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 4 9
if (he && SvROK(HeVAL(he))) return (HV *)SvRV(HeVAL(he)); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
220 }
221 2 return NULL;
222 }
223  
224 /* Every table `cls` answers, its own and inherited, sorted. Mortal AV. */
225 5 static AV *pmc_tables(pTHX_ const char *cls) {
226 5 HV *seen = (HV *)sv_2mortal((SV *)newHV());
227 5 HV *stash = gv_stashpv(cls, 0);
228 /*
229 # uncoverable branch false
230 # uncoverable condition right count:1 class:ignore_covered_err */
231 5 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 5 0 5
232 5 AV *isa = mro_get_linear_isa(stash);
233 5 SSize_t i, n = av_len(isa) + 1;
234 16 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11 5 16
235 11 SV **c = av_fetch(isa, i, 0);
236 /*
237 # uncoverable condition right count:1 class:ignore_covered_err
238 # uncoverable condition right count:2 class:ignore_covered_err */
239 22 ▶ HV *d = c && *c ? pmc_decl_hv(aTHX_ SvPV_nolen(*c), 0) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *d = c && *c ? pmc_decl_hv(aTHX_ SvPV_nolen(*c), 0) : NULL; 11 0 11
HV *d = c && *c ? pmc_decl_hv(aTHX_ SvPV_nolen(*c), 0) : NULL; 11 0 11
240 HE *he;
241 11 ▶ if (!d) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) continue; 6 5 11
242 5 hv_iterinit(d);
243 14 ▶ while ((he = hv_iternext(d)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(d))) 9 5 14
244 9 ▶ (void)hv_store_ent(seen, HeSVKEY_force(he), newSViv(1), 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store_ent(seen, HeSVKEY_force(he), newSViv(1), 0); 9 0 9
(void)hv_store_ent(seen, HeSVKEY_force(he), newSViv(1), 0); 0 9 9
245 }
246 }
247 5 return pdbi_sorted_keys(aTHX_ seen);
248 }
249  
250 3 static SV *pmc_join(pTHX_ AV *av) {
251 3 SV *s = sv_2mortal(newSVpvs(""));
252 3 SSize_t i, n = av_len(av) + 1;
253 7 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 3 7
254 4 ▶ if (i) sv_catpvs(s, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(s, ", "); 2 2 4
255 4 sv_catsv(s, *av_fetch(av, i, 0));
256 }
257 3 return s;
258 }
259  
260 11 static int pmc_ident(pTHX_ SV *sv) {
261 STRLEN l, i;
262 const char *p;
263 /*
264 # uncoverable branch true
265 # uncoverable condition left count:1 class:ignore_covered_err
266 # uncoverable condition left count:2 class:ignore_covered_err
267 # uncoverable condition left count:3 class:ignore_covered_err */
268 11 ▶ if (!(sv && SvOK(sv) && !SvROK(sv))) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(sv && SvOK(sv) && !SvROK(sv))) return 0; 11 0 11
if (!(sv && SvOK(sv) && !SvROK(sv))) return 0; 11 0 11
if (!(sv && SvOK(sv) && !SvROK(sv))) return 0; 0 11 11
269 11 p = SvPV_const(sv, l);
270 /*
271 # uncoverable branch true
272 # uncoverable condition left count:1 class:ignore_covered_err
273 # uncoverable condition left count:2 class:ignore_covered_err */
274 11 ▶ if (!l || isDIGIT(p[0])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!l || isDIGIT(p[0])) return 0; 11 0 11
if (!l || isDIGIT(p[0])) return 0; 0 11 11
275 37 ▶ for (i = 0; i < l; i++) if (!pmce_word(p[i])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < l; i++) if (!pmce_word(p[i])) return 0; 1 26 27
for (i = 0; i < l; i++) if (!pmce_word(p[i])) return 0; 27 10 37
276 10 return 1;
277 }
278  
279 /* A list of field names as a set, each checked. Owned HV. */
280 11 static HV *pmc_name_set(pTHX_ const char *cls, SV *table, const char *what,
281 SV *list) {
282 HV *set;
283 AV *av;
284 SSize_t i, n;
285 /*
286 # uncoverable condition left count:1 class:ignore_covered_err
287 # uncoverable condition left count:3 class:ignore_covered_err */
288 11 ▶ if (!(list && SvROK(list) && SvTYPE(SvRV(list)) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(list && SvROK(list) && SvTYPE(SvRV(list)) == SVt_PVAV)) 11 0 11
if (!(list && SvROK(list) && SvTYPE(SvRV(list)) == SVt_PVAV)) 10 1 11
if (!(list && SvROK(list) && SvTYPE(SvRV(list)) == SVt_PVAV)) 0 10 10
289 1 croak("%s: %s's %s is a list of field names", cls, SvPV_nolen(table), what);
290 10 set = newHV();
291 10 av = (AV *)SvRV(list);
292 10 n = av_len(av) + 1;
293 23 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 13 10 23
294 13 SV **e = av_fetch(av, i, 0);
295 /*
296 # uncoverable branch true
297 # uncoverable condition left count:1 class:ignore_covered_err
298 # uncoverable condition left count:2 class:ignore_covered_err
299 # uncoverable condition left count:3 class:ignore_covered_err
300 # uncoverable condition left count:4 class:ignore_covered_err
301 # uncoverable condition left count:5 class:ignore_covered_err */
302 13 ▶ if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) {
Branch: 5 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) { 13 0 13
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) { 13 0 13
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) { 13 0 13
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) { 13 0 13
if (!(e && *e && SvOK(*e) && !SvROK(*e) && SvCUR(*e))) { 0 13 13
303 0 SvREFCNT_dec((SV *)set); /* # uncoverable statement */
304 0 croak("%s: %s's %s is a list of field names", cls, SvPV_nolen(table), what); /* # uncoverable statement */
305 }
306 13 (void)hv_store_ent(set, *e, newSViv(1), 0);
307 }
308 10 return set;
309 }
310  
311 /* `answers(table => { ask => [...], ops => { f => [...] }, as_is => [...] })`:
312 * one table's declaration, checked and stored normalised as
313 * { ask => {f=>1}, ops => { f => {op=>1} }, as_is => {f=>1} }. */
314 11 static void pmc_declare(pTHX_ const char *cls, SV *table, SV *spec) {
315 HV *s, *out, *ask, *ops, *as_is;
316 HE *he;
317 SV *v;
318 11 ▶ if (!pmc_ident(aTHX_ table))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmc_ident(aTHX_ table)) 1 10 11
319 2 ▶ croak("%s: a table's name is a word, not '%s'", cls,
Branch: 2 branches
BranchTrueFalseTotal
croak("%s: a table's name is a word, not '%s'", cls, 1 0 1
croak("%s: a table's name is a word, not '%s'", cls, 1 0 1
320 table && SvOK(table) ? SvPV_nolen(table) : "undef");
321 /*
322 # uncoverable condition left count:1 class:ignore_covered_err
323 # uncoverable condition left count:2 class:ignore_covered_err */
324 10 ▶ if (!(spec && SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(spec && SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV)) 10 0 10
if (!(spec && SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV)) 10 0 10
if (!(spec && SvROK(spec) && SvTYPE(SvRV(spec)) == SVt_PVHV)) 1 9 10
325 1 croak("%s: %s's declaration is a hash reference", cls, SvPV_nolen(table));
326 9 s = (HV *)SvRV(spec);
327 9 hv_iterinit(s);
328 24 ▶ while ((he = hv_iternext(s))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(s))) { 16 8 24
329 16 ▶ STRLEN kl; const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(he, kl); 0 16 16
330 /*
331 # uncoverable condition right count:2 class:ignore_covered_err
332 # uncoverable condition right count:4 class:ignore_covered_err
333 # uncoverable condition right count:6 class:ignore_covered_err */
334 16 ▶ if ((kl == 3 && memEQ(k, "ask", 3)) || (kl == 3 && memEQ(k, "ops", 3))
Branch: 4 branches
BranchTrueFalseTotal
if ((kl == 3 && memEQ(k, "ask", 3)) || (kl == 3 && memEQ(k, "ops", 3)) 12 4 16
if ((kl == 3 && memEQ(k, "ask", 3)) || (kl == 3 && memEQ(k, "ops", 3)) 4 8 12
if ((kl == 3 && memEQ(k, "ask", 3)) || (kl == 3 && memEQ(k, "ops", 3)) 4 4 8
if ((kl == 3 && memEQ(k, "ask", 3)) || (kl == 3 && memEQ(k, "ops", 3)) 0 4 4
335 16 ▶ || (kl == 5 && memEQ(k, "as_is", 5))) continue;
Branch: 2 branches
BranchTrueFalseTotal
|| (kl == 5 && memEQ(k, "as_is", 5))) continue; 3 1 4
|| (kl == 5 && memEQ(k, "as_is", 5))) continue; 3 0 3
336 2 croak("%s: %s's declaration has no '%.*s' (known: ask, as_is, ops)",
337 cls, SvPV_nolen(table), (int)kl, k);
338 }
339 8 v = pdbi_get(aTHX_ s, "ask");
340 8 ▶ ask = v ? pmc_name_set(aTHX_ cls, table, "ask", v) : newHV(); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ask = v ? pmc_name_set(aTHX_ cls, table, "ask", v) : newHV(); /* # uncoverable condition right count:1 class:ignore_covered_err */ 8 0 8
341 7 sv_2mortal((SV *)ask);
342 7 v = pdbi_get(aTHX_ s, "as_is");
343 7 ▶ as_is = v ? pmc_name_set(aTHX_ cls, table, "as_is", v) : newHV();
Branch: 1 branch
BranchTrueFalseTotal
as_is = v ? pmc_name_set(aTHX_ cls, table, "as_is", v) : newHV(); 3 4 7
344 7 sv_2mortal((SV *)as_is);
345 7 hv_iterinit(as_is);
346 9 ▶ while ((he = hv_iternext(as_is)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(as_is))) 3 6 9
347 /*
348 # uncoverable condition right count:1 class:ignore_covered_err
349 # uncoverable condition left count:2 class:ignore_covered_err */
350 3 ▶ if (!hv_exists_ent(ask, HeSVKEY_force(he), 0))
Branch: 3 branches
BranchTrueFalseTotal
if (!hv_exists_ent(ask, HeSVKEY_force(he), 0)) 3 0 3
if (!hv_exists_ent(ask, HeSVKEY_force(he), 0)) 0 3 3
if (!hv_exists_ent(ask, HeSVKEY_force(he), 0)) 1 2 3
351 3 ▶ croak("%s: %s's as_is names '%s', which it is not asked by",
Branch: 3 branches
BranchTrueFalseTotal
croak("%s: %s's as_is names '%s', which it is not asked by", 1 0 1
croak("%s: %s's as_is names '%s', which it is not asked by", 0 1 1
croak("%s: %s's as_is names '%s', which it is not asked by", 1 0 1
352 cls, SvPV_nolen(table), SvPV_nolen(HeSVKEY_force(he)));
353 6 ops = (HV *)sv_2mortal((SV *)newHV());
354 6 v = pdbi_get(aTHX_ s, "ops");
355 6 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 4 2 6
356 HV *o;
357 /*
358 # uncoverable branch true
359 # uncoverable condition left count:1 class:ignore_covered_err
360 # uncoverable condition left count:2 class:ignore_covered_err */
361 4 ▶ if (!(SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV)) 4 0 4
if (!(SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV)) 0 4 4
362 0 croak("%s: %s's ops is { field => [operators] }", cls, SvPV_nolen(table)); /* # uncoverable statement */
363 4 o = (HV *)SvRV(v);
364 4 hv_iterinit(o);
365 5 ▶ while ((he = hv_iternext(o))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(o))) { 4 1 5
366 4 ▶ SV *f = HeSVKEY_force(he), *l = HeVAL(he);
Branch: 2 branches
BranchTrueFalseTotal
SV *f = HeSVKEY_force(he), *l = HeVAL(he); 4 0 4
SV *f = HeSVKEY_force(he), *l = HeVAL(he); 0 4 4
367 HV *set;
368 AV *av;
369 SSize_t i, n;
370 4 ▶ if (!hv_exists_ent(ask, f, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists_ent(ask, f, 0)) 1 3 4
371 2 ▶ croak("%s: %s's ops names '%s', which it is not asked by",
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: %s's ops names '%s', which it is not asked by", 1 0 1
372 cls, SvPV_nolen(table), SvPV_nolen(f));
373 3 ▶ if (hv_exists_ent(as_is, f, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(as_is, f, 0)) 1 2 3
374 2 ▶ croak("%s: %s's '%s' is taken as it is and takes no operators",
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: %s's '%s' is taken as it is and takes no operators", 1 0 1
375 cls, SvPV_nolen(table), SvPV_nolen(f));
376 /*
377 # uncoverable branch true
378 # uncoverable condition left count:1 class:ignore_covered_err
379 # uncoverable condition left count:2 class:ignore_covered_err */
380 2 ▶ if (!(SvROK(l) && SvTYPE(SvRV(l)) == SVt_PVAV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(l) && SvTYPE(SvRV(l)) == SVt_PVAV)) 2 0 2
if (!(SvROK(l) && SvTYPE(SvRV(l)) == SVt_PVAV)) 0 2 2
381 0 croak("%s: %s's ops is { field => [operators] }", cls, SvPV_nolen(table)); /* # uncoverable statement */
382 2 set = (HV *)sv_2mortal((SV *)newHV());
383 2 av = (AV *)SvRV(l);
384 2 n = av_len(av) + 1;
385 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 1 4
386 3 SV **e = av_fetch(av, i, 0);
387 STRLEN ol;
388 /*
389 # uncoverable condition right count:1 class:ignore_covered_err
390 # uncoverable condition right count:2 class:ignore_covered_err
391 # uncoverable condition right count:3 class:ignore_covered_err */
392 6 ▶ const char *op = (e && *e && SvOK(*e)) ? SvPV_const(*e, ol) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *op = (e && *e && SvOK(*e)) ? SvPV_const(*e, ol) : NULL; 3 0 3
const char *op = (e && *e && SvOK(*e)) ? SvPV_const(*e, ol) : NULL; 3 0 3
const char *op = (e && *e && SvOK(*e)) ? SvPV_const(*e, ol) : NULL; 3 0 3
393 3 ▶ if (!op || pdbq_op_index(op, ol) < 0) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!op || pdbq_op_index(op, ol) < 0) /* # uncoverable condition left count:1 class:ignore_covered_err */ 3 0 3
if (!op || pdbq_op_index(op, ol) < 0) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 2 3
394 4 ▶ croak("%s: %s's '%s' names an operator '%s' (known: %s)",
Branch: 2 branches
BranchTrueFalseTotal
croak("%s: %s's '%s' names an operator '%s' (known: %s)", 1 0 1
croak("%s: %s's '%s' names an operator '%s' (known: %s)", 1 0 1
395 cls, SvPV_nolen(table), SvPV_nolen(f), op ? op : "undef",
396 SvPV_nolen(pdbq_op_list(aTHX)));
397 2 (void)hv_store_ent(set, *e, newSViv(1), 0);
398 }
399 1 (void)hv_store_ent(ops, f, newRV_inc((SV *)set), 0);
400 }
401 }
402 3 out = newHV();
403 3 (void)hv_stores(out, "ask", newRV_inc((SV *)ask));
404 3 (void)hv_stores(out, "ops", newRV_inc((SV *)ops));
405 3 (void)hv_stores(out, "as_is", newRV_inc((SV *)as_is));
406 3 (void)hv_store_ent(pmc_decl_hv(aTHX_ cls, GV_ADD | GV_ADDMULTI), table,
407 newRV_noinc((SV *)out), 0);
408 3 }
409  
410 /* ---- the object ---------------------------------------------------------------- */
411  
412 149 static HV *pmc_self(pTHX_ SV *self) {
413 /*
414 # uncoverable branch true
415 # uncoverable condition left count:1 class:ignore_covered_err
416 # uncoverable condition left count:2 class:ignore_covered_err */
417 149 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 149 0 149
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 0 149 149
418 0 croak(PMC ": not a backend object"); /* # uncoverable statement */
419 149 return (HV *)SvRV(self);
420 }
421  
422 164 static HV *pmc_sub(pTHX_ HV *spec, const char *k) {
423 164 SV *v = pdbi_get(aTHX_ spec, k);
424 /*
425 # uncoverable condition right count:1 class:ignore_covered_err
426 # uncoverable condition right count:2 class:ignore_covered_err */
427 164 ▶ return v && SvROK(v) ? (HV *)SvRV(v) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return v && SvROK(v) ? (HV *)SvRV(v) : NULL; 164 0 164
return v && SvROK(v) ? (HV *)SvRV(v) : NULL; 164 0 164
428 }
429  
430 7 static SV *pmc_build(pTHX_ SV *class, SV **st, I32 items) {
431 7 SV *self = pdbi_build_self(aTHX_ class, st, items, SvPV_nolen(class));
432 7 HV *h = (HV *)SvRV(self);
433 7 ▶ const char *cls = HvNAME(SvSTASH((SV *)h));
Branch: 6 branches
BranchTrueFalseTotal
const char *cls = HvNAME(SvSTASH((SV *)h)); 7 0 7
const char *cls = HvNAME(SvSTASH((SV *)h)); 7 0 7
const char *cls = HvNAME(SvSTASH((SV *)h)); 0 7 7
const char *cls = HvNAME(SvSTASH((SV *)h)); 0 0 0
const char *cls = HvNAME(SvSTASH((SV *)h)); 7 0 7
const char *cls = HvNAME(SvSTASH((SV *)h)); 0 7 7
434 7 SV *table = pdbi_get(aTHX_ h, "table");
435 7 SV *o = pdbi_get(aTHX_ h, "opts");
436 7 SV *app = NULL, *size = NULL;
437 HV *spec;
438 7 IV max = PMC_CACHE_DEFAULT;
439 I32 i;
440 7 sv_2mortal(self);
441 7 spec = pmc_spec(aTHX_ cls, table);
442 7 ▶ if (!spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (!spec) { 2 5 7
443 2 AV *t = pmc_tables(aTHX_ cls);
444 6 ▶ croak("%s: no table '%s' (%s%s)", cls, SvPV_nolen(table),
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: no table '%s' (%s%s)", cls, SvPV_nolen(table), 1 1 2
445 av_len(t) >= 0 ? "it answers " : "it declares none: answers(table => {...})",
446 SvPV_nolen(pmc_join(aTHX_ t)));
447 }
448 /*
449 # uncoverable branch false
450 # uncoverable condition right count:1 class:ignore_covered_err
451 # uncoverable condition right count:2 class:ignore_covered_err */
452 5 ▶ if (o && SvROK(o)) size = pdbi_get(aTHX_ (HV *)SvRV(o), "cache");
Branch: 2 branches
BranchTrueFalseTotal
if (o && SvROK(o)) size = pdbi_get(aTHX_ (HV *)SvRV(o), "cache"); 5 0 5
if (o && SvROK(o)) size = pdbi_get(aTHX_ (HV *)SvRV(o), "cache"); 5 0 5
453 5 ▶ if (size && SvOK(size)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (size && SvOK(size)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 1 5
if (size && SvOK(size)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 4 0 4
454 STRLEN l, j;
455 4 const char *p = SvPV_const(size, l);
456 7 ▶ for (j = 0; j < l; j++) if (!isDIGIT(p[j])) break;
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < l; j++) if (!isDIGIT(p[j])) break; 1 3 4
for (j = 0; j < l; j++) if (!isDIGIT(p[j])) break; 4 3 7
457 4 ▶ if (!l || j < l) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!l || j < l) /* # uncoverable condition left count:1 class:ignore_covered_err */ 4 0 4
if (!l || j < l) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 3 4
458 1 croak("%s: the database's cache is how many answers to keep, a whole "
459 "number (0 for none), not '%s'", cls, p);
460 3 max = SvIV(size);
461 }
462 21 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 17 4 21
463 17 STRLEN kl; const char *k = SvPV_const(st[i], kl);
464 17 ▶ if (kl == 3 && memEQ(k, "app", 3)) app = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 3 && memEQ(k, "app", 3)) app = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 14 17
if (kl == 3 && memEQ(k, "app", 3)) app = st[i + 1]; /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
465 }
466 4 ▶ if (app && SvROK(app)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (app && SvROK(app)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 1 4
if (app && SvROK(app)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
467 3 SV *w = newSVsv(app);
468 3 sv_rvweaken(w);
469 3 (void)hv_stores(h, "app", w);
470 }
471 4 (void)hv_stores(h, "spec", newRV_inc((SV *)spec));
472 4 (void)hv_stores(h, "cache", newRV_noinc((SV *)newHV()));
473 4 (void)hv_stores(h, "cache_max", newSViv(max));
474 4 (void)hv_stores(h, "cache_tick", newSViv(0));
475 4 return SvREFCNT_inc_simple_NN(self);
476 }
477  
478 /* ---- the question --------------------------------------------------------------- */
479  
480 /* A filter as { field => { op => value } }: each field one of the model's,
481 * one the table is asked by, each operator one the field takes. A field
482 * taken as it is keeps its value whole, a hash or a list included. A
483 * program's fault croaks. Mortal RV. */
484 41 static SV *pmc_filter(pTHX_ SV *self, SV *filter) {
485 41 HV *h = pmc_self(aTHX_ self);
486 41 const char *cls = pmc_cls(aTHX_ self);
487 41 SV *table = pdbi_get(aTHX_ h, "table");
488 41 HV *spec = pmc_sub(aTHX_ h, "spec");
489 41 ▶ HV *ask = spec ? pmc_sub(aTHX_ spec, "ask") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HV *ask = spec ? pmc_sub(aTHX_ spec, "ask") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 41 0 41
490 41 ▶ HV *ops = spec ? pmc_sub(aTHX_ spec, "ops") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HV *ops = spec ? pmc_sub(aTHX_ spec, "ops") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 41 0 41
491 41 ▶ HV *as_is = spec ? pmc_sub(aTHX_ spec, "as_is") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HV *as_is = spec ? pmc_sub(aTHX_ spec, "as_is") : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 41 0 41
492 41 HV *col = pmr_colset(aTHX_ h);
493 41 HV *out = newHV();
494 41 SV *rv = sv_2mortal(newRV_noinc((SV *)out));
495 AV *keys;
496 SSize_t i, n;
497 /*
498 # uncoverable condition right count:1 class:ignore_covered_err
499 # uncoverable condition left count:3 class:ignore_covered_err */
500 41 ▶ if (filter && SvOK(filter) && !(SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (filter && SvOK(filter) && !(SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)) 41 0 41
if (filter && SvOK(filter) && !(SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)) 40 1 41
if (filter && SvOK(filter) && !(SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)) 40 0 40
if (filter && SvOK(filter) && !(SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)) 1 39 40
501 1 croak("%s: a filter is a hash reference", cls);
502 /*
503 # uncoverable branch true class:ignore_covered_err
504 # uncoverable condition left count:1 class:ignore_covered_err */
505 40 ▶ if (!(filter && SvROK(filter))) return rv;
Branch: 2 branches
BranchTrueFalseTotal
if (!(filter && SvROK(filter))) return rv; 40 0 40
if (!(filter && SvROK(filter))) return rv; 1 39 40
506 39 keys = pdbi_sorted_keys(aTHX_ (HV *)SvRV(filter));
507 39 n = av_len(keys) + 1;
508 76 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 43 33 76
509 43 SV *k = *av_fetch(keys, i, 0);
510 43 HE *he = hv_fetch_ent((HV *)SvRV(filter), k, 0, 0);
511 43 ▶ SV *v = he ? HeVAL(he) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *v = he ? HeVAL(he) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 43 0 43
512 HV *term;
513 43 pdbq_check_col(aTHX_ col, k, table, cls, "a filter");
514 42 ▶ if (!(ask && hv_exists_ent(ask, k, 0))) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(ask && hv_exists_ent(ask, k, 0))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 42 0 42
if (!(ask && hv_exists_ent(ask, k, 0))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 41 42
515 2 ▶ croak("%s: '%s' is an answer of %s, not something to ask it by",
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: '%s' is an answer of %s, not something to ask it by", 1 0 1
516 cls, SvPV_nolen(k), SvPV_nolen(table));
517 41 term = newHV();
518 41 (void)hv_store_ent(out, k, newRV_noinc((SV *)term), 0);
519 41 ▶ if (as_is && hv_exists_ent(as_is, k, 0)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (as_is && hv_exists_ent(as_is, k, 0)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 41 0 41
if (as_is && hv_exists_ent(as_is, k, 0)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 40 41
520 1 (void)hv_stores(term, "=", newSVsv(v));
521 1 continue;
522 }
523 47 ▶ if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV && !SvOBJECT(SvRV(v))) { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV && !SvOBJECT(SvRV(v))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 11 29 40
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV && !SvOBJECT(SvRV(v))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 10 1 11
if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV && !SvOBJECT(SvRV(v))) { /* # uncoverable condition right count:3 class:ignore_covered_err */ 10 0 10
524 10 HV *given = (HV *)SvRV(v);
525 10 ▶ HE *oe = ops ? hv_fetch_ent(ops, k, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
HE *oe = ops ? hv_fetch_ent(ops, k, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
526 10 ▶ HV *takes = oe && SvROK(HeVAL(oe)) ? (HV *)SvRV(HeVAL(oe)) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
HV *takes = oe && SvROK(HeVAL(oe)) ? (HV *)SvRV(HeVAL(oe)) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 9 1 10
HV *takes = oe && SvROK(HeVAL(oe)) ? (HV *)SvRV(HeVAL(oe)) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 9 0 9
527 10 AV *gk = pdbi_sorted_keys(aTHX_ given);
528 10 SSize_t j, m = av_len(gk) + 1;
529 11 ▶ if (!m) croak("%s: an empty operator hash for '%s'", cls, SvPV_nolen(k));
Branch: 1 branch
BranchTrueFalseTotal
if (!m) croak("%s: an empty operator hash for '%s'", cls, SvPV_nolen(k)); 1 9 10
530 16 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 9 7 16
531 9 SV *op = *av_fetch(gk, j, 0);
532 9 STRLEN ol; const char *o = SvPV_const(op, ol);
533 HE *ve;
534 /*
535 # uncoverable condition left count:1 class:ignore_covered_err
536 # uncoverable condition right count:1 class:ignore_covered_err
537 # uncoverable condition right count:2 class:ignore_covered_err */
538 9 ▶ if (!(ol == 1 && *o == '=') && !(takes && hv_exists_ent(takes, op, 0))) {
Branch: 4 branches
BranchTrueFalseTotal
if (!(ol == 1 && *o == '=') && !(takes && hv_exists_ent(takes, op, 0))) { 7 2 9
if (!(ol == 1 && *o == '=') && !(takes && hv_exists_ent(takes, op, 0))) { 7 0 7
if (!(ol == 1 && *o == '=') && !(takes && hv_exists_ent(takes, op, 0))) { 8 1 9
if (!(ol == 1 && *o == '=') && !(takes && hv_exists_ent(takes, op, 0))) { 1 7 8
539 2 ▶ AV *t = takes ? pdbi_sorted_keys(aTHX_ takes) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *t = takes ? pdbi_sorted_keys(aTHX_ takes) : NULL; 1 1 2
540 5 ▶ croak("%s: '%s' takes no operator '%s' here (%s%s)", cls,
Branch: 4 branches
BranchTrueFalseTotal
croak("%s: '%s' takes no operator '%s' here (%s%s)", cls, 1 1 2
croak("%s: '%s' takes no operator '%s' here (%s%s)", cls, 1 0 1
croak("%s: '%s' takes no operator '%s' here (%s%s)", cls, 1 1 2
croak("%s: '%s' takes no operator '%s' here (%s%s)", cls, 1 0 1
541 SvPV_nolen(k), o, t && av_len(t) >= 0 ? "it takes =, " : "only =",
542 t && av_len(t) >= 0 ? SvPV_nolen(pmc_join(aTHX_ t)) : "");
543 }
544 7 ve = hv_fetch_ent(given, op, 0, 0);
545 7 ▶ (void)hv_store_ent(term, op, newSVsv(ve ? HeVAL(ve) : &PL_sv_undef), 0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store_ent(term, op, newSVsv(ve ? HeVAL(ve) : &PL_sv_undef), 0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 7 0 7
546 }
547 }
548 /*
549 # uncoverable condition right count:2 class:ignore_covered_err
550 # uncoverable condition right count:3 class:ignore_covered_err */
551 30 ▶ else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV && !SvOBJECT(SvRV(v)))
Branch: 3 branches
BranchTrueFalseTotal
else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV && !SvOBJECT(SvRV(v))) 1 29 30
else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV && !SvOBJECT(SvRV(v))) 1 0 1
else if (SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV && !SvOBJECT(SvRV(v))) 1 0 1
552 1 croak("%s: a list for '%s' is { in => [...] }", cls, SvPV_nolen(k));
553 29 else (void)hv_stores(term, "=", newSVsv(v));
554 }
555 33 return rv;
556 }
557  
558 /* The search options: limit and after. order_by croaks, the rows coming in
559 * the order the table works them out in. Mortal RV, a copy. */
560 14 static SV *pmc_options(pTHX_ SV *self, SV *opts) {
561 14 HV *h = pmc_self(aTHX_ self);
562 14 const char *cls = pmc_cls(aTHX_ self);
563 14 HV *out = newHV();
564 14 SV *rv = sv_2mortal(newRV_noinc((SV *)out));
565 HE *he;
566 /*
567 # uncoverable condition right count:1 class:ignore_covered_err
568 # uncoverable condition left count:3 class:ignore_covered_err */
569 14 ▶ if (opts && SvOK(opts) && !(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (opts && SvOK(opts) && !(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)) 14 0 14
if (opts && SvOK(opts) && !(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)) 12 2 14
if (opts && SvOK(opts) && !(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)) 12 0 12
if (opts && SvOK(opts) && !(SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)) 1 11 12
570 1 croak("%s: options are a hash reference", cls);
571 13 ▶ if (!(opts && SvROK(opts))) return rv; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(opts && SvROK(opts))) return rv; /* # uncoverable condition left count:1 class:ignore_covered_err */ 13 0 13
if (!(opts && SvROK(opts))) return rv; /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 11 13
572 11 hv_iterinit((HV *)SvRV(opts));
573 20 ▶ while ((he = hv_iternext((HV *)SvRV(opts)))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext((HV *)SvRV(opts)))) { 11 9 20
574 11 ▶ STRLEN kl; const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN kl; const char *k = HePV(he, kl); 0 11 11
575 11 ▶ if (kl == 8 && memEQ(k, "order_by", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 8 && memEQ(k, "order_by", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 9 11
if (kl == 8 && memEQ(k, "order_by", 8)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
576 2 ▶ if (!SvOK(HeVAL(he))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(HeVAL(he))) continue; 1 1 2
577 2 croak("%s: the %s rows come in their own order, which order_by "
578 "cannot change", cls, SvPV_nolen(pdbi_get(aTHX_ h, "table")));
579 }
580 /*
581 # uncoverable condition left count:2 class:ignore_covered_err
582 # uncoverable condition left count:4 class:ignore_covered_err */
583 9 ▶ if (!((kl == 5 && memEQ(k, "limit", 5)) || (kl == 5 && memEQ(k, "after", 5))))
Branch: 4 branches
BranchTrueFalseTotal
if (!((kl == 5 && memEQ(k, "limit", 5)) || (kl == 5 && memEQ(k, "after", 5)))) 8 1 9
if (!((kl == 5 && memEQ(k, "limit", 5)) || (kl == 5 && memEQ(k, "after", 5)))) 2 6 8
if (!((kl == 5 && memEQ(k, "limit", 5)) || (kl == 5 && memEQ(k, "after", 5)))) 2 1 3
if (!((kl == 5 && memEQ(k, "limit", 5)) || (kl == 5 && memEQ(k, "after", 5)))) 0 2 2
584 1 croak("%s: unknown search option '%.*s' (known: after, limit)", cls, (int)kl, k);
585 8 ▶ (void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); 8 0 8
(void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); 0 8 8
586 }
587 9 return rv;
588 }
589  
590 /* ---- the contract --------------------------------------------------------------- */
591  
592 static const char *const PMC_VERBS[] = { "get", "search", "count" };
593  
594 34 static CV *pmc_hook(pTHX_ SV *self, int verb) {
595 34 HV *h = pmc_self(aTHX_ self);
596 34 SV *table = pdbi_get(aTHX_ h, "table");
597 34 SV *name = sv_2mortal(newSVpvf("_%s_%s", PMC_VERBS[verb], SvPV_nolen(table)));
598 34 GV *gv = gv_fetchmethod_autoload(SvSTASH(SvRV(self)), SvPV_nolen(name), FALSE);
599 34 ▶ return gv && isGV(gv) ? GvCV(gv) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return gv && isGV(gv) ? GvCV(gv) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 28 6 34
return gv && isGV(gv) ? GvCV(gv) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 28 0 28
600 }
601  
602 /* `$self->_<verb>_<table>(@args)` in scalar context, or a croak naming what
603 * the table does answer. A new SV. */
604 28 static SV *pmc_call(pTHX_ SV *self, int verb, SV *a, SV *b) {
605 28 CV *cv = pmc_hook(aTHX_ self, verb);
606 SV *ret;
607 int count;
608 28 ▶ if (!cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) { 2 26 28
609 2 SV *have = sv_2mortal(newSVpvs(""));
610 int i;
611 8 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 6 2 8
612 6 ▶ if (!pmc_hook(aTHX_ self, i)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmc_hook(aTHX_ self, i)) continue; 4 2 6
613 /*
614 # uncoverable branch true
615 # uncoverable condition left count:1 class:ignore_covered_err */
616 2 ▶ if (SvCUR(have)) sv_catpvs(have, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(have)) sv_catpvs(have, ", "); 0 2 2
617 2 sv_catpv(have, PMC_VERBS[i]);
618 }
619 8 ▶ croak("%s: the %s table answers %s, not %s (define _%s_%s)",
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: the %s table answers %s, not %s (define _%s_%s)", 2 0 2
620 pmc_cls(aTHX_ self), SvPV_nolen(pdbi_get(aTHX_ pmc_self(aTHX_ self), "table")),
621 SvCUR(have) ? SvPV_nolen(have) : "nothing", PMC_VERBS[verb], /* # uncoverable condition right count:1 class:ignore_covered_err */
622 PMC_VERBS[verb], SvPV_nolen(pdbi_get(aTHX_ pmc_self(aTHX_ self), "table")));
623 }
624 {
625 26 dSP;
626 26 ENTER; SAVETMPS;
627 26 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 26 26
628 26 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 26 26
629 26 ▶ XPUSHs(a);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(a); 0 26 26
630 26 ▶ if (b) XPUSHs(b); /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (b) XPUSHs(b); /* # uncoverable condition left count:2 class:ignore_covered_err */ 8 18 26
if (b) XPUSHs(b); /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 8 8
631 26 PUTBACK;
632 26 count = call_sv((SV *)cv, G_SCALAR);
633 18 SPAGAIN;
634 18 ▶ ret = count ? POPs : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ret = count ? POPs : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 18 0 18
635 18 ret = newSVsv(ret);
636 18 PUTBACK;
637 18 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 18 0 18
638 }
639 18 return ret;
640 }
641  
642 18 static SV *pmc_search(pTHX_ SV *self, SV *filter, SV *opts) {
643 18 SV *f = pmc_filter(aTHX_ self, filter);
644 13 SV *o = pmc_options(aTHX_ self, opts);
645 10 return pmc_call(aTHX_ self, 1, f, o);
646 }
647  
648 /* ---- limit -------------------------------------------------------------------- */
649  
650 12 static IV pmc_limit(pTHX_ SV *opts, SV *dflt, IV max, SV *what) {
651 12 SV *n = NULL;
652 STRLEN l, j;
653 const char *p;
654 /*
655 # uncoverable branch false class:ignore_covered_err
656 # uncoverable condition right count:1 class:ignore_covered_err
657 # uncoverable condition right count:2 class:ignore_covered_err
658 # uncoverable condition right count:3 class:ignore_covered_err */
659 12 ▶ if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 12 0 12
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 12 0 12
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 12 0 12
660 12 n = pdbi_get(aTHX_ (HV *)SvRV(opts), "limit");
661 12 ▶ if (!(n && SvOK(n))) n = dflt; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(n && SvOK(n))) n = dflt; /* # uncoverable condition left count:2 class:ignore_covered_err */ 7 5 12
if (!(n && SvOK(n))) n = dflt; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 7 7
662 /*
663 # uncoverable condition left count:1 class:ignore_covered_err
664 # uncoverable condition left count:3 class:ignore_covered_err */
665 12 ▶ if (!(n && SvOK(n) && !SvROK(n)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(n && SvOK(n) && !SvROK(n))) 12 0 12
if (!(n && SvOK(n) && !SvROK(n))) 11 1 12
if (!(n && SvOK(n) && !SvROK(n))) 0 11 11
666 1 pmce_throw_pv(aTHX_ "limit", sv_2mortal(newSVpvf(
667 "limit must be a whole number from 1 to %" IVdf, max)));
668 11 p = SvPV_const(n, l);
669 41 ▶ for (j = 0; j < l; j++) if (!isDIGIT(p[j])) break;
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; j < l; j++) if (!isDIGIT(p[j])) break; 1 30 31
for (j = 0; j < l; j++) if (!isDIGIT(p[j])) break; 31 10 41
670 11 ▶ if (!l || j < l || !SvIV(n)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!l || j < l || !SvIV(n)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 11 0 11
if (!l || j < l || !SvIV(n)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 10 1 11
if (!l || j < l || !SvIV(n)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 9 10
671 2 pmce_throw_pv(aTHX_ "limit", sv_2mortal(newSVpvf(
672 "limit must be a whole number from 1 to %" IVdf, max)));
673 9 ▶ if (l > 18 || SvIV(n) > max)
Branch: 2 branches
BranchTrueFalseTotal
if (l > 18 || SvIV(n) > max) 8 1 9
if (l > 18 || SvIV(n) > max) 2 6 8
674 5 ▶ pmce_throw_pv(aTHX_ "limit", sv_2mortal(newSVpvf(
Branch: 2 branches
BranchTrueFalseTotal
pmce_throw_pv(aTHX_ "limit", sv_2mortal(newSVpvf( 3 0 3
pmce_throw_pv(aTHX_ "limit", sv_2mortal(newSVpvf( 2 1 3
675 "at most %" IVdf " %s at a time", max,
676 what && SvOK(what) ? SvPV_nolen(what) : "rows")));
677 6 return SvIV(n);
678 }
679  
680 /* ---- the cache ---------------------------------------------------------------- */
681  
682 /* One part of a cache key, written so that two different values never write
683 * the same bytes: each scalar its length first, hashes by sorted key, an
684 * object as its string. Read without changing the value. */
685 163 static void pmc_key_part(pTHX_ SV *out, SV *v, int depth) {
686 163 ▶ if (depth > 64) croak(PMC ": a cache key nested deeper than 64 (a cycle?)");
Branch: 1 branch
BranchTrueFalseTotal
if (depth > 64) croak(PMC ": a cache key nested deeper than 64 (a cycle?)"); 1 162 163
687 162 SvGETMAGIC(v);
688 162 ▶ if (!SvOK(v)) { sv_catpvs(out, "u"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(v)) { sv_catpvs(out, "u"); return; } 4 158 162
689 158 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 95 63 158
690 95 SV *r = SvRV(v);
691 95 ▶ if (SvOBJECT(r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(r)) { 1 94 95
692 1 SV *s = sv_2mortal(newSVsv(v));
693 1 STRLEN l; const char *p = SvPVutf8(s, l);
694 1 ▶ sv_catpvf(out, "o%s=%" UVuf ":", HvNAME(SvSTASH(r)), (UV)l);
Branch: 6 branches
BranchTrueFalseTotal
sv_catpvf(out, "o%s=%" UVuf ":", HvNAME(SvSTASH(r)), (UV)l); 1 0 1
sv_catpvf(out, "o%s=%" UVuf ":", HvNAME(SvSTASH(r)), (UV)l); 1 0 1
sv_catpvf(out, "o%s=%" UVuf ":", HvNAME(SvSTASH(r)), (UV)l); 0 1 1
sv_catpvf(out, "o%s=%" UVuf ":", HvNAME(SvSTASH(r)), (UV)l); 0 0 0
sv_catpvf(out, "o%s=%" UVuf ":", HvNAME(SvSTASH(r)), (UV)l); 1 0 1
sv_catpvf(out, "o%s=%" UVuf ":", HvNAME(SvSTASH(r)), (UV)l); 0 1 1
695 1 sv_catpvn(out, p, l);
696 1 return;
697 }
698 94 ▶ if (SvTYPE(r) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(r) == SVt_PVHV) { 25 69 94
699 25 AV *k = pdbi_sorted_keys(aTHX_ (HV *)r);
700 25 SSize_t i, n = av_len(k) + 1;
701 25 sv_catpvs(out, "{");
702 52 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 27 25 52
703 27 SV *key = *av_fetch(k, i, 0);
704 27 HE *he = hv_fetch_ent((HV *)r, key, 0, 0);
705 27 pmc_key_part(aTHX_ out, key, depth + 1);
706 27 ▶ pmc_key_part(aTHX_ out, he ? HeVAL(he) : &PL_sv_undef, depth + 1); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pmc_key_part(aTHX_ out, he ? HeVAL(he) : &PL_sv_undef, depth + 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ 27 0 27
707 }
708 25 sv_catpvs(out, "}");
709 25 return;
710 }
711 69 ▶ if (SvTYPE(r) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(r) == SVt_PVAV) { 68 1 69
712 68 SSize_t i, n = av_len((AV *)r) + 1;
713 68 sv_catpvs(out, "[");
714 73 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 70 3 73
715 70 SV **e = av_fetch((AV *)r, i, 0);
716 /*
717 # uncoverable condition right count:1 class:ignore_covered_err
718 # uncoverable condition right count:2 class:ignore_covered_err */
719 70 ▶ pmc_key_part(aTHX_ out, e && *e ? *e : &PL_sv_undef, depth + 1);
Branch: 2 branches
BranchTrueFalseTotal
pmc_key_part(aTHX_ out, e && *e ? *e : &PL_sv_undef, depth + 1); 70 0 70
pmc_key_part(aTHX_ out, e && *e ? *e : &PL_sv_undef, depth + 1); 70 0 70
720 }
721 3 sv_catpvs(out, "]");
722 3 return;
723 }
724 /*
725 # uncoverable branch true
726 # uncoverable condition left count:1 class:ignore_covered_err */
727 1 ▶ if (SvTYPE(r) < SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(r) < SVt_PVAV) { 0 1 1
728 0 sv_catpvs(out, "\\"); /* # uncoverable statement */
729 0 pmc_key_part(aTHX_ out, r, depth + 1); /* # uncoverable statement */
730 0 return; /* # uncoverable statement */
731 }
732 1 croak(PMC ": a cache key is made of plain values, hashes, lists and "
733 "objects, not a %s reference", sv_reftype(r, 0));
734 }
735 {
736 63 SV *s = sv_2mortal(newSVsv(v));
737 63 STRLEN l; const char *p = SvPVutf8(s, l);
738 63 sv_catpvf(out, "s%" UVuf ":", (UV)l);
739 63 sv_catpvn(out, p, l);
740 }
741 }
742  
743 /* What `code` answers, kept under `key` among the last cache_max answers.
744 * An undef key runs it every time. A new SV. */
745 18 static SV *pmc_cached(pTHX_ SV *self, SV *key, SV *code) {
746 18 HV *h = pmc_self(aTHX_ self);
747 18 SV *cs = pdbi_get(aTHX_ h, "cache");
748 18 SV *mx = pdbi_get(aTHX_ h, "cache_max");
749 18 SV *tk = pdbi_get(aTHX_ h, "cache_tick");
750 /*
751 # uncoverable condition right count:1 class:ignore_covered_err
752 # uncoverable condition right count:2 class:ignore_covered_err */
753 18 ▶ HV *cache = cs && SvROK(cs) ? (HV *)SvRV(cs) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *cache = cs && SvROK(cs) ? (HV *)SvRV(cs) : NULL; 18 0 18
HV *cache = cs && SvROK(cs) ? (HV *)SvRV(cs) : NULL; 18 0 18
754 18 ▶ IV max = mx ? SvIV(mx) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
IV max = mx ? SvIV(mx) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 18 0 18
755 HE *he;
756 SV *v;
757 int count;
758 /*
759 # uncoverable condition left count:1 class:ignore_covered_err
760 # uncoverable condition left count:3 class:ignore_covered_err */
761 18 ▶ if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 18 0 18
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 17 1 18
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 0 17 17
762 1 croak("%s: cached takes a key and a code reference", pmc_cls(aTHX_ self));
763 /*
764 # uncoverable condition right count:1 class:ignore_covered_err
765 # uncoverable condition right count:3 class:ignore_covered_err */
766 17 ▶ if (cache && max > 0 && key && SvOK(key)
Branch: 4 branches
BranchTrueFalseTotal
if (cache && max > 0 && key && SvOK(key) 17 0 17
if (cache && max > 0 && key && SvOK(key) 15 2 17
if (cache && max > 0 && key && SvOK(key) 15 0 15
if (cache && max > 0 && key && SvOK(key) 12 3 15
767 12 ▶ && (he = hv_fetch_ent(cache, key, 0, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
&& (he = hv_fetch_ent(cache, key, 0, 0))) { 6 6 12
768 6 AV *slot = (AV *)SvRV(HeVAL(he));
769 6 sv_setiv(tk, SvIV(tk) + 1);
770 6 sv_setiv(*av_fetch(slot, 1, 0), SvIV(tk));
771 6 return newSVsv(*av_fetch(slot, 0, 0));
772 }
773 {
774 11 dSP;
775 11 ENTER; SAVETMPS;
776 11 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 11 11
777 11 PUTBACK;
778 11 count = call_sv(code, G_SCALAR);
779 10 SPAGAIN;
780 10 ▶ v = count ? POPs : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
v = count ? POPs : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
781 10 v = newSVsv(v);
782 10 PUTBACK;
783 10 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 10 0 10
784 }
785 /*
786 # uncoverable condition left count:1 class:ignore_covered_err
787 # uncoverable condition left count:3 class:ignore_covered_err */
788 10 ▶ if (!(cache && max > 0 && key && SvOK(key))) return v;
Branch: 4 branches
BranchTrueFalseTotal
if (!(cache && max > 0 && key && SvOK(key))) return v; 10 0 10
if (!(cache && max > 0 && key && SvOK(key))) return v; 8 2 10
if (!(cache && max > 0 && key && SvOK(key))) return v; 8 0 8
if (!(cache && max > 0 && key && SvOK(key))) return v; 3 5 8
789 7 ▶ while ((IV)HvUSEDKEYS(cache) >= max) { /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while ((IV)HvUSEDKEYS(cache) >= max) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 7 7
while ((IV)HvUSEDKEYS(cache) >= max) { /* # uncoverable condition left count:1 class:ignore_covered_err */ 2 5 7
790 2 SV *oldest = NULL;
791 2 IV at = 0;
792 2 hv_iterinit(cache);
793 6 ▶ while ((he = hv_iternext(cache))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(cache))) { 4 2 6
794 4 IV t = SvIV(*av_fetch((AV *)SvRV(HeVAL(he)), 1, 0));
795 /*
796 # uncoverable branch false class:ignore_covered_err
797 # uncoverable condition left count:2 class:ignore_covered_err */
798 4 ▶ if (!oldest || t < at) { oldest = HeSVKEY_force(he); at = t; }
Branch: 4 branches
BranchTrueFalseTotal
if (!oldest || t < at) { oldest = HeSVKEY_force(he); at = t; } 2 2 4
if (!oldest || t < at) { oldest = HeSVKEY_force(he); at = t; } 1 1 2
if (!oldest || t < at) { oldest = HeSVKEY_force(he); at = t; } 3 0 3
if (!oldest || t < at) { oldest = HeSVKEY_force(he); at = t; } 0 3 3
799 }
800 /*
801 # uncoverable branch true
802 # uncoverable condition left count:1 class:ignore_covered_err */
803 2 ▶ if (!oldest) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!oldest) break; 0 2 2
804 2 oldest = sv_2mortal(newSVsv(oldest));
805 2 (void)hv_delete_ent(cache, oldest, G_DISCARD, 0);
806 }
807 {
808 5 AV *slot = newAV();
809 5 sv_setiv(tk, SvIV(tk) + 1);
810 5 av_push(slot, newSVsv(v));
811 5 av_push(slot, newSViv(SvIV(tk)));
812 5 (void)hv_store_ent(cache, key, newRV_noinc((SV *)slot), 0);
813 }
814 5 return v;
815 }
816  
817 #endif /* PUNK_COMPUTED_H */
« include/punk/punk_sitemap.h ↑ Summary include/punk/punk_dbi.h »