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