include/punk/punk_mrows.h

filter: Statementstmt 94.1% 484 / 514 Branchbran 68.9% 418 / 606 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.5% 902 / 1120 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/config.xs ↑ Summary include/punk/punk_health.h »
1 /* punk_mrows.h - the Punk::Model contract over rows held in memory, and the
2 * two backends built on it: Punk::Model::JSONFile (rows in a JSON file, read
3 * and write) and Punk::Model::Markdown (the pages of a `markdown` mount,
4 * read-only).
5 *
6 * Everything that is not SQL is punk_dbq.h's - the operator set, the column
7 * checks and their croaks, the ordering with its primary-key tie-breaker, the
8 * search options, the keyset token and the page - so a filter, an order_by or
9 * a `next` token means the same here as on Punk::Model::DBI. What this file
10 * adds is evaluating them against Perl rows instead of writing them as SQL:
11 *
12 * - three-valued logic, as SQL has it: NULL (undef) never equals, compares
13 * or is `in` anything; `!=` leaves NULLs out; `not_in` over a list that
14 * holds NULL matches nothing, and over an empty list matches everything;
15 * - values compare as numbers when both look like numbers, else as strings
16 * (sv_cmp), and NULL sorts before every value, as on SQLite;
17 * - `like` takes `%` and `_`, case-sensitively; `starts_with` is a literal
18 * prefix.
19 *
20 * The object is the blessed hash pdbi_build_self makes (opts/table/primary/
21 * columns/col), so pdbq_check_keys and the rest read it unchanged.
22 *
23 * Include after punk_dbq.h, punk_markdown.h and punk_app.h. */
24  
25 #ifndef PUNK_MROWS_H
26 #define PUNK_MROWS_H
27  
28 /* ---- comparing two values -------------------------------------------------- */
29  
30 /* A value's number, read WITHOUT caching one on the SV: SvNV would set NOK
31 * on a row held in the JSON cache, and the encoder would then write an
32 * integer id back as 2.0. */
33 228 static NV pmr_num(pTHX_ SV *sv) {
34 228 ▶ if (SvNOK(sv)) return SvNVX(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) return SvNVX(sv); 0 228 228
35 228 ▶ if (SvIOK(sv)) return SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(sv)) return SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); 228 0 228
if (SvIOK(sv)) return SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv); 0 228 228
36 0 ▶ return SvPOK(sv) ? Atof(SvPVX(sv)) : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
return SvPOK(sv) ? Atof(SvPVX(sv)) : 0.0; 0 0 0
37 }
38  
39 /* An integer key as an IV, without touching the SV's flags: an integer SV,
40 * or a string of digits with an optional sign. 0 when it is not one. */
41 5 static int pmr_int(pTHX_ SV *sv, IV *out) {
42 5 ▶ if (SvIOK(sv) && !SvNOK(sv) && !SvPOK(sv)) { *out = SvIVX(sv); return 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (SvIOK(sv) && !SvNOK(sv) && !SvPOK(sv)) { *out = SvIVX(sv); return 1; } 4 1 5
if (SvIOK(sv) && !SvNOK(sv) && !SvPOK(sv)) { *out = SvIVX(sv); return 1; } 4 0 4
if (SvIOK(sv) && !SvNOK(sv) && !SvPOK(sv)) { *out = SvIVX(sv); return 1; } 4 0 4
43 1 ▶ if (SvNOK(sv) && !SvPOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvNOK(sv) && !SvPOK(sv)) { 0 1 1
if (SvNOK(sv) && !SvPOK(sv)) { 0 0 0
44 0 NV v = SvNVX(sv);
45 0 ▶ if (v != (NV)(IV)v) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (v != (NV)(IV)v) return 0; 0 0 0
46 0 *out = (IV)v;
47 0 return 1;
48 }
49 1 ▶ if (SvPOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(sv)) { 1 0 1
50 1 STRLEN l, i = 0;
51 1 const char *p = SvPV_const(sv, l);
52 1 ▶ if (l && (p[0] == '-' || p[0] == '+')) i = 1;
Branch: 3 branches
BranchTrueFalseTotal
if (l && (p[0] == '-' || p[0] == '+')) i = 1; 1 0 1
if (l && (p[0] == '-' || p[0] == '+')) i = 1; 1 0 1
if (l && (p[0] == '-' || p[0] == '+')) i = 1; 0 1 1
53 1 ▶ if (i == l) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i == l) return 0; 0 1 1
54 1 ▶ for (; i < l; i++) if (!isDIGIT(p[i])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (; i < l; i++) if (!isDIGIT(p[i])) return 0; 1 0 1
for (; i < l; i++) if (!isDIGIT(p[i])) return 0; 1 0 1
55 0 *out = (IV)Atof(p);
56 0 return 1;
57 }
58 0 return 0;
59 }
60  
61 /* A value to string-compare, never the row's own SV unless it already is a
62 * string: sv_cmp stringifies, and a cached PV on a number changes how it is
63 * encoded. */
64 192 static SV *pmr_str(pTHX_ SV *sv) {
65 192 ▶ return SvPOK(sv) ? sv : sv_2mortal(newSVsv(sv));
Branch: 1 branch
BranchTrueFalseTotal
return SvPOK(sv) ? sv : sv_2mortal(newSVsv(sv)); 0 192 192
66 }
67  
68 /* <0, 0, >0 with NULL lowest. Numbers compare as numbers when both sides
69 * look like one ("10" after "9"), everything else as strings. Neither side
70 * is changed. */
71 218 static int pmr_cmp(pTHX_ SV *a, SV *b) {
72 218 ▶ int da = a && SvOK(a), db = b && SvOK(b);
Branch: 4 branches
BranchTrueFalseTotal
int da = a && SvOK(a), db = b && SvOK(b); 218 0 218
int da = a && SvOK(a), db = b && SvOK(b); 214 4 218
int da = a && SvOK(a), db = b && SvOK(b); 218 0 218
int da = a && SvOK(a), db = b && SvOK(b); 214 4 218
73 218 ▶ if (!da || !db) return da == db ? 0 : (da ? 1 : -1);
Branch: 4 branches
BranchTrueFalseTotal
if (!da || !db) return da == db ? 0 : (da ? 1 : -1); 214 4 218
if (!da || !db) return da == db ? 0 : (da ? 1 : -1); 4 210 214
if (!da || !db) return da == db ? 0 : (da ? 1 : -1); 8 0 8
if (!da || !db) return da == db ? 0 : (da ? 1 : -1); 4 4 8
74 210 ▶ if (!SvROK(a) && !SvROK(b) && looks_like_number(a) && looks_like_number(b)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!SvROK(a) && !SvROK(b) && looks_like_number(a) && looks_like_number(b)) { 210 0 210
if (!SvROK(a) && !SvROK(b) && looks_like_number(a) && looks_like_number(b)) { 210 0 210
if (!SvROK(a) && !SvROK(b) && looks_like_number(a) && looks_like_number(b)) { 114 96 210
if (!SvROK(a) && !SvROK(b) && looks_like_number(a) && looks_like_number(b)) { 114 0 114
75 114 NV x = pmr_num(aTHX_ a), y = pmr_num(aTHX_ b);
76 114 ▶ return x < y ? -1 : x > y ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return x < y ? -1 : x > y ? 1 : 0; 82 32 114
77 }
78 96 return sv_cmp(pmr_str(aTHX_ a), pmr_str(aTHX_ b));
79 }
80  
81 /* SQL LIKE: `%` any run, `_` one character, everything else literal and
82 * case-sensitive. Character-wise on a UTF-8 value, so `_` is one character
83 * and not one byte. */
84 30 static int pmr_like(const U8 *s, const U8 *se, const U8 *p, const U8 *pe,
85 int utf8) {
86 44 ▶ while (p < pe) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < pe) { 43 1 44
87 43 ▶ if (*p == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '%') { 11 32 43
88 22 ▶ while (p < pe && *p == '%') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < pe && *p == '%') p++; 16 6 22
while (p < pe && *p == '%') p++; 11 5 16
89 11 ▶ if (p == pe) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p == pe) return 1; 5 6 11
90 for (;;) {
91 10 ▶ if (pmr_like(s, se, p, pe, utf8)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pmr_like(s, se, p, pe, utf8)) return 1; 5 5 10
92 5 ▶ if (s >= se) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= se) return 0; 0 5 5
93 5 ▶ s += utf8 ? UTF8SKIP(s) : 1;
Branch: 1 branch
BranchTrueFalseTotal
s += utf8 ? UTF8SKIP(s) : 1; 1 4 5
94 }
95 }
96 32 ▶ if (s >= se) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= se) return 0; 0 32 32
97 32 ▶ if (*p == '_') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '_') { 3 29 32
98 3 ▶ s += utf8 ? UTF8SKIP(s) : 1;
Branch: 1 branch
BranchTrueFalseTotal
s += utf8 ? UTF8SKIP(s) : 1; 1 2 3
99 3 p++;
100 3 continue;
101 }
102 29 ▶ if (*p != *s) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*p != *s) return 0; 18 11 29
103 11 p++; s++;
104 }
105 1 return s == se;
106 }
107  
108 /* ---- the filter -------------------------------------------------------------
109 *
110 * Compiled once per call into terms (column, operator, value), validated
111 * exactly as pdbq_where validates - the same croaks, in the same order - and
112 * then run against each row. */
113  
114 typedef struct { SV *col; int op; SV *val; } pmr_term;
115  
116 /* Mortal AV of terms, each an AV [col, op, val]. */
117 68 static AV *pmr_compile(pTHX_ HV *col, SV *table, HV *filter, const char *cls) {
118 68 AV *terms = (AV *)sv_2mortal((SV *)newAV());
119 AV *keys;
120 SSize_t i, n;
121 68 ▶ if (!filter) return terms;
Branch: 1 branch
BranchTrueFalseTotal
if (!filter) return terms; 0 68 68
122 68 keys = pdbi_sorted_keys(aTHX_ filter);
123 68 n = av_len(keys) + 1;
124 107 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 45 62 107
125 45 SV *k = *av_fetch(keys, i, 0);
126 45 HE *he = hv_fetch_ent(filter, k, 0, 0);
127 45 ▶ SV *val = he ? HeVAL(he) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *val = he ? HeVAL(he) : NULL; 45 0 45
128 45 pdbq_check_col(aTHX_ col, k, table, cls, "the filter");
129 44 ▶ if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 44 0 44
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 24 20 44
if (val && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 23 1 24
130 23 HV *ops = (HV *)SvRV(val);
131 23 AV *onames = pdbi_sorted_keys(aTHX_ ops);
132 23 SSize_t j, m = av_len(onames) + 1;
133 23 ▶ if (m == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (m == 0) 1 22 23
134 3 ▶ croak("%s: the filter on %s is an empty hashref - it needs an "
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: the filter on %s is an empty hashref - it needs an " 1 0 1
135 "operator (%s)", cls, SvPV_nolen(k),
136 SvPV_nolen(pdbq_op_list(aTHX)));
137 42 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 23 19 42
138 23 SV *on = *av_fetch(onames, j, 0);
139 23 STRLEN ol; const char *op = SvPV_const(on, ol);
140 23 int idx = pdbq_op_index(op, ol);
141 HE *oe;
142 SV *v;
143 AV *t;
144 23 ▶ if (idx < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) 1 22 23
145 3 ▶ croak("%s: unknown operator '%s' on %s (known: %s)",
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: unknown operator '%s' on %s (known: %s)", 1 0 1
146 cls, op, SvPV_nolen(k),
147 SvPV_nolen(pdbq_op_list(aTHX)));
148 22 oe = hv_fetch_ent(ops, on, 0, 0);
149 22 ▶ v = oe ? HeVAL(oe) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
v = oe ? HeVAL(oe) : NULL; 22 0 22
150 22 switch (idx) {
151 13 case PDBQ_LT: case PDBQ_LE: case PDBQ_GT: case PDBQ_GE:
152 case PDBQ_LIKE: case PDBQ_STARTS_WITH:
153 13 ▶ if (!(v && SvOK(v)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(v && SvOK(v))) 13 0 13
if (!(v && SvOK(v))) 1 12 13
154 1 croak("%s: '%s' on %s needs a value, not undef",
155 cls, PDBQ_OPS[idx].name, SvPV_nolen(k));
156 12 ▶ if (SvROK(v))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) 0 12 12
157 0 croak("%s: '%s' on %s takes a plain value, not a %s",
158 cls, PDBQ_OPS[idx].name, SvPV_nolen(k),
159 sv_reftype(SvRV(v), 0));
160 12 break;
161 6 case PDBQ_IN: case PDBQ_NOT_IN:
162 6 ▶ if (!(v && SvOK(v) && SvROK(v)
Branch: 3 branches
BranchTrueFalseTotal
if (!(v && SvOK(v) && SvROK(v) 6 0 6
if (!(v && SvOK(v) && SvROK(v) 6 0 6
if (!(v && SvOK(v) && SvROK(v) 5 1 6
163 5 ▶ && SvTYPE(SvRV(v)) == SVt_PVAV))
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(v)) == SVt_PVAV)) 0 5 5
164 1 croak("%s: '%s' on %s takes an arrayref",
165 cls, PDBQ_OPS[idx].name, SvPV_nolen(k));
166 5 break;
167 3 default: break;
168 }
169 20 t = newAV();
170 20 av_push(t, newSVsv(k));
171 20 av_push(t, newSViv(idx));
172 20 ▶ av_push(t, v ? newSVsv(v) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(t, v ? newSVsv(v) : newSV(0)); 20 0 20
173 20 av_push(terms, newRV_noinc((SV *)t));
174 }
175 19 continue;
176 }
177 21 ▶ if (val && SvROK(val))
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvROK(val)) 21 0 21
if (val && SvROK(val)) 1 20 21
178 2 ▶ croak("%s: the filter on %s is a %s - a list is spelled "
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: the filter on %s is a %s - a list is spelled " 0 1 1
179 "{ in => [...] }, and everything else is a plain value or "
180 "{ operator => value }", cls, SvPV_nolen(k),
181 SvTYPE(SvRV(val)) == SVt_PVAV ? "plain arrayref"
182 : sv_reftype(SvRV(val), 0));
183 {
184 20 AV *t = newAV();
185 20 av_push(t, newSVsv(k));
186 20 av_push(t, newSViv(PDBQ_EQ));
187 20 ▶ av_push(t, val ? newSVsv(val) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(t, val ? newSVsv(val) : newSV(0)); 20 0 20
188 20 av_push(terms, newRV_noinc((SV *)t));
189 }
190 }
191 62 return terms;
192 }
193  
194 164 static int pmr_term_ok(pTHX_ int op, SV *rv, SV *val) {
195 164 ▶ int rdef = rv && SvOK(rv);
Branch: 2 branches
BranchTrueFalseTotal
int rdef = rv && SvOK(rv); 164 0 164
int rdef = rv && SvOK(rv); 149 15 164
196 164 switch (op) {
197 68 case PDBQ_EQ:
198 68 ▶ if (!SvOK(val)) return !rdef;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) return !rdef; 14 54 68
199 54 ▶ return rdef && pmr_cmp(aTHX_ rv, val) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return rdef && pmr_cmp(aTHX_ rv, val) == 0; 53 1 54
return rdef && pmr_cmp(aTHX_ rv, val) == 0; 15 38 53
200 14 case PDBQ_NE:
201 14 ▶ if (!SvOK(val)) return rdef;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(val)) return rdef; 9 5 14
202 5 ▶ return rdef && pmr_cmp(aTHX_ rv, val) != 0;
Branch: 2 branches
BranchTrueFalseTotal
return rdef && pmr_cmp(aTHX_ rv, val) != 0; 4 1 5
return rdef && pmr_cmp(aTHX_ rv, val) != 0; 2 2 4
203 10 ▶ case PDBQ_LT: return rdef && pmr_cmp(aTHX_ rv, val) < 0;
Branch: 2 branches
BranchTrueFalseTotal
case PDBQ_LT: return rdef && pmr_cmp(aTHX_ rv, val) < 0; 9 1 10
case PDBQ_LT: return rdef && pmr_cmp(aTHX_ rv, val) < 0; 6 3 9
204 5 ▶ case PDBQ_LE: return rdef && pmr_cmp(aTHX_ rv, val) <= 0;
Branch: 2 branches
BranchTrueFalseTotal
case PDBQ_LE: return rdef && pmr_cmp(aTHX_ rv, val) <= 0; 4 1 5
case PDBQ_LE: return rdef && pmr_cmp(aTHX_ rv, val) <= 0; 1 3 4
205 5 ▶ case PDBQ_GT: return rdef && pmr_cmp(aTHX_ rv, val) > 0;
Branch: 2 branches
BranchTrueFalseTotal
case PDBQ_GT: return rdef && pmr_cmp(aTHX_ rv, val) > 0; 4 1 5
case PDBQ_GT: return rdef && pmr_cmp(aTHX_ rv, val) > 0; 2 2 4
206 3 ▶ case PDBQ_GE: return rdef && pmr_cmp(aTHX_ rv, val) >= 0;
Branch: 2 branches
BranchTrueFalseTotal
case PDBQ_GE: return rdef && pmr_cmp(aTHX_ rv, val) >= 0; 3 0 3
case PDBQ_GE: return rdef && pmr_cmp(aTHX_ rv, val) >= 0; 2 1 3
207 25 case PDBQ_IN:
208 case PDBQ_NOT_IN: {
209 25 AV *list = (AV *)SvRV(val);
210 25 SSize_t i, n = av_len(list) + 1;
211 25 int found = 0, has_null = 0;
212 25 ▶ if (n == 0) return op == PDBQ_NOT_IN; /* 1=0 / 1=1 */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return op == PDBQ_NOT_IN; /* 1=0 / 1=1 */ 10 15 25
213 15 ▶ if (!rdef) return 0; /* NULL [NOT] IN (...) */
Branch: 1 branch
BranchTrueFalseTotal
if (!rdef) return 0; /* NULL [NOT] IN (...) */ 3 12 15
214 23 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 6 23
215 17 SV **e = av_fetch(list, i, 0);
216 17 ▶ if (!(e && *e && SvOK(*e))) { has_null = 1; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) { has_null = 1; continue; } 17 0 17
if (!(e && *e && SvOK(*e))) { has_null = 1; continue; } 17 0 17
if (!(e && *e && SvOK(*e))) { has_null = 1; continue; } 2 15 17
217 15 ▶ if (pmr_cmp(aTHX_ rv, *e) == 0) { found = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (pmr_cmp(aTHX_ rv, *e) == 0) { found = 1; break; } 6 9 15
218 }
219 12 ▶ if (op == PDBQ_IN) return found;
Branch: 1 branch
BranchTrueFalseTotal
if (op == PDBQ_IN) return found; 4 8 12
220 8 ▶ return !found && !has_null; /* x NOT IN (.., NULL) */
Branch: 2 branches
BranchTrueFalseTotal
return !found && !has_null; /* x NOT IN (.., NULL) */ 4 4 8
return !found && !has_null; /* x NOT IN (.., NULL) */ 2 2 4
221 }
222 34 case PDBQ_LIKE:
223 case PDBQ_STARTS_WITH: {
224 STRLEN sl, pl;
225 const U8 *s, *p;
226 34 ▶ if (!rdef) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!rdef) return 0; 0 34 34
227 34 ▶ s = (const U8 *)SvPV_const(rv, sl);
Branch: 1 branch
BranchTrueFalseTotal
s = (const U8 *)SvPV_const(rv, sl); 34 0 34
228 34 p = (const U8 *)SvPV_const(val, pl);
229 34 ▶ if (op == PDBQ_STARTS_WITH) return sl >= pl && memEQ(s, p, pl);
Branch: 3 branches
BranchTrueFalseTotal
if (op == PDBQ_STARTS_WITH) return sl >= pl && memEQ(s, p, pl); 14 20 34
if (op == PDBQ_STARTS_WITH) return sl >= pl && memEQ(s, p, pl); 14 0 14
if (op == PDBQ_STARTS_WITH) return sl >= pl && memEQ(s, p, pl); 4 10 14
230 20 return pmr_like(s, s + sl, p, p + pl, SvUTF8(rv) ? 1 : 0);
231 }
232 0 default: return 0;
233 }
234 }
235  
236 233 static int pmr_match(pTHX_ AV *terms, HV *row) {
237 233 SSize_t i, n = av_len(terms) + 1;
238 291 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 164 127 291
239 164 AV *t = (AV *)SvRV(*av_fetch(terms, i, 0));
240 164 SV *col = *av_fetch(t, 0, 0);
241 164 HE *he = hv_fetch_ent(row, col, 0, 0);
242 164 ▶ if (!pmr_term_ok(aTHX_ (int)SvIV(*av_fetch(t, 1, 0)),
Branch: 2 branches
BranchTrueFalseTotal
if (!pmr_term_ok(aTHX_ (int)SvIV(*av_fetch(t, 1, 0)), 164 0 164
if (!pmr_term_ok(aTHX_ (int)SvIV(*av_fetch(t, 1, 0)), 106 58 164
243 164 he ? HeVAL(he) : NULL, *av_fetch(t, 2, 0)))
244 106 return 0;
245 }
246 127 return 1;
247 }
248  
249 /* ---- copies ------------------------------------------------------------------
250 *
251 * A row handed out is the caller's to change, so it is copied all the way
252 * down: a JSON file's rows sit in a per-process cache, and a caller editing
253 * a nested hash must not edit the next caller's answer. */
254  
255 737 static SV *pmr_deep(pTHX_ SV *sv) {
256 737 ▶ if (!sv) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return newSV(0); 0 737 737
257 737 ▶ if (SvROK(sv) && !sv_isobject(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && !sv_isobject(sv)) { 177 560 737
if (SvROK(sv) && !sv_isobject(sv)) { 177 0 177
258 177 SV *r = SvRV(sv);
259 177 ▶ if (SvTYPE(r) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(r) == SVt_PVHV) { 153 24 177
260 153 HV *src = (HV *)r, *dst = newHV();
261 HE *he;
262 153 hv_iterinit(src);
263 713 ▶ while ((he = hv_iternext(src)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) 560 153 713
264 560 (void)hv_store_ent(dst, hv_iterkeysv(he),
265 pmr_deep(aTHX_ HeVAL(he)), 0);
266 153 return newRV_noinc((SV *)dst);
267 }
268 24 ▶ if (SvTYPE(r) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(r) == SVt_PVAV) { 24 0 24
269 24 AV *src = (AV *)r, *dst = newAV();
270 24 SSize_t i, n = av_len(src) + 1;
271 24 av_extend(dst, n > 0 ? n - 1 : 0);
272 81 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 57 24 81
273 57 SV **e = av_fetch(src, i, 0);
274 57 ▶ av_push(dst, pmr_deep(aTHX_ e ? *e : NULL));
Branch: 1 branch
BranchTrueFalseTotal
av_push(dst, pmr_deep(aTHX_ e ? *e : NULL)); 57 0 57
275 }
276 24 return newRV_noinc((SV *)dst);
277 }
278 }
279 560 return newSVsv(sv);
280 }
281  
282 /* ---- ordering ---------------------------------------------------------------- */
283  
284 typedef struct { AV *cols; AV *desc; } pmr_spec;
285 typedef struct { HV *row; const pmr_spec *spec; } pmr_item;
286  
287 110 static int pmr_rowcmp(pTHX_ const pmr_spec *sp, HV *a, HV *b, AV *bvals) {
288 110 SSize_t i, n = av_len(sp->cols) + 1;
289 116 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 113 3 116
290 113 SV *c = *av_fetch(sp->cols, i, 0);
291 113 HE *ha = hv_fetch_ent(a, c, 0, 0);
292 SV *vb;
293 int r;
294 113 ▶ if (bvals) { SV **e = av_fetch(bvals, i, 0); vb = e ? *e : NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (bvals) { SV **e = av_fetch(bvals, i, 0); vb = e ? *e : NULL; } 13 100 113
if (bvals) { SV **e = av_fetch(bvals, i, 0); vb = e ? *e : NULL; } 13 0 13
295 100 ▶ else { HE *hb = hv_fetch_ent(b, c, 0, 0); vb = hb ? HeVAL(hb) : NULL; }
Branch: 1 branch
BranchTrueFalseTotal
else { HE *hb = hv_fetch_ent(b, c, 0, 0); vb = hb ? HeVAL(hb) : NULL; } 100 0 100
296 113 ▶ r = pmr_cmp(aTHX_ ha ? HeVAL(ha) : NULL, vb);
Branch: 1 branch
BranchTrueFalseTotal
r = pmr_cmp(aTHX_ ha ? HeVAL(ha) : NULL, vb); 113 0 113
297 113 ▶ if (r) return SvIV(*av_fetch(sp->desc, i, 0)) ? -r : r;
Branch: 2 branches
BranchTrueFalseTotal
if (r) return SvIV(*av_fetch(sp->desc, i, 0)) ? -r : r; 107 6 113
if (r) return SvIV(*av_fetch(sp->desc, i, 0)) ? -r : r; 10 97 107
298 }
299 3 return 0;
300 }
301  
302 99 static int pmr_qsort_cmp(const void *x, const void *y) {
303 dTHX;
304 99 const pmr_item *a = (const pmr_item *)x, *b = (const pmr_item *)y;
305 99 return pmr_rowcmp(aTHX_ a->spec, a->row, b->row, NULL);
306 }
307  
308 /* ---- the contract over a row list -------------------------------------------- */
309  
310 207 static HV *pmr_self(pTHX_ SV *self, const char *cls) {
311 207 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 207 0 207
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 0 207 207
312 0 croak("%s: not a backend object", cls);
313 207 return (HV *)SvRV(self);
314 }
315  
316 106 static HV *pmr_colset(pTHX_ HV *h) {
317 106 SV *c = pdbi_get(aTHX_ h, "col");
318 106 ▶ return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVHV) ? (HV *)SvRV(c) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVHV) ? (HV *)SvRV(c) : NULL; 106 0 106
return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVHV) ? (HV *)SvRV(c) : NULL; 106 0 106
return (c && SvROK(c) && SvTYPE(SvRV(c)) == SVt_PVHV) ? (HV *)SvRV(c) : NULL; 106 0 106
319 }
320  
321 /* search(\%filter, \%opts) over `rows` (an AV of hashrefs): the page hash. */
322 44 static SV *pmr_search(pTHX_ SV *self, AV *rows, SV *filter, SV *opts,
323 const char *cls) {
324 44 HV *h = pmr_self(aTHX_ self, cls);
325 44 ▶ HV *f = (filter && SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *f = (filter && SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV) 44 0 44
HV *f = (filter && SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV) 44 0 44
326 88 ▶ ? (HV *)SvRV(filter) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(filter) : NULL; 44 0 44
327 44 ▶ HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 16 28 44
HV *o = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 16 0 16
328 88 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 44 0 44
329 44 SV *table = pdbi_get(aTHX_ h, "table");
330 44 SV *pk = pdbi_get(aTHX_ h, "primary");
331 44 HV *col = pmr_colset(aTHX_ h);
332 44 ▶ SV *lim_sv = o ? pdbi_get(aTHX_ o, "limit") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *lim_sv = o ? pdbi_get(aTHX_ o, "limit") : NULL; 16 28 44
333 44 ▶ SV *after = o ? pdbi_get(aTHX_ o, "after") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *after = o ? pdbi_get(aTHX_ o, "after") : NULL; 16 28 44
334 44 ▶ SV *order = o ? pdbi_get(aTHX_ o, "order_by") : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *order = o ? pdbi_get(aTHX_ o, "order_by") : NULL; 16 28 44
335 IV limit;
336 44 AV *cols, *desc, *terms, *vals = NULL, *out;
337 int pk_only;
338 pmr_spec spec;
339 pmr_item *items;
340 44 ▶ SSize_t i, n = rows ? av_len(rows) + 1 : 0, kept = 0, from = 0, taken;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = rows ? av_len(rows) + 1 : 0, kept = 0, from = 0, taken; 44 0 44
341  
342 44 pdbq_check_opts(aTHX_ o, cls);
343 43 ▶ limit = (lim_sv && SvOK(lim_sv)) ? SvIV(lim_sv) : 20;
Branch: 2 branches
BranchTrueFalseTotal
limit = (lim_sv && SvOK(lim_sv)) ? SvIV(lim_sv) : 20; 7 36 43
limit = (lim_sv && SvOK(lim_sv)) ? SvIV(lim_sv) : 20; 7 0 7
344 43 ▶ if (lim_sv && SvOK(lim_sv) && SvNV(lim_sv) >= (NV)IV_MAX) limit = IV_MAX - 1;
Branch: 3 branches
BranchTrueFalseTotal
if (lim_sv && SvOK(lim_sv) && SvNV(lim_sv) >= (NV)IV_MAX) limit = IV_MAX - 1; 7 36 43
if (lim_sv && SvOK(lim_sv) && SvNV(lim_sv) >= (NV)IV_MAX) limit = IV_MAX - 1; 7 0 7
if (lim_sv && SvOK(lim_sv) && SvNV(lim_sv) >= (NV)IV_MAX) limit = IV_MAX - 1; 0 7 7
345 43 ▶ if (limit < 1) limit = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (limit < 1) limit = 1; 1 42 43
346 43 terms = pmr_compile(aTHX_ col, table, f, cls);
347 37 pdbq_order(aTHX_ col, table, pk, order, cls, &cols, &desc, &pk_only);
348 37 ▶ if (after && SvOK(after) && SvCUR(after)) {
Branch: 3 branches
BranchTrueFalseTotal
if (after && SvOK(after) && SvCUR(after)) { 5 32 37
if (after && SvOK(after) && SvCUR(after)) { 5 0 5
if (after && SvOK(after) && SvCUR(after)) { 5 0 5
349 5 ▶ if (av_len(cols) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(cols) < 0) 0 5 5
350 0 croak("%s: pagination needs a primary key or an order_by", cls);
351 5 vals = pdbq_decode_token(aTHX_ after, cols, desc, pk_only, cls);
352 }
353  
354 35 ▶ Newx(items, n > 0 ? n : 1, pmr_item);
Branch: 3 branches
BranchTrueFalseTotal
Newx(items, n > 0 ? n : 1, pmr_item); 35 0 35
Newx(items, n > 0 ? n : 1, pmr_item); 0 35 35
Newx(items, n > 0 ? n : 1, pmr_item); 35 0 35
355 35 SAVEFREEPV(items);
356 35 spec.cols = cols;
357 35 spec.desc = desc;
358 203 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 168 35 203
359 168 SV **e = av_fetch(rows, i, 0);
360 HV *r;
361 168 ▶ if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 168 0 168
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 168 0 168
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 168 0 168
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 0 168 168
362 168 r = (HV *)SvRV(*e);
363 168 ▶ if (!pmr_match(aTHX_ terms, r)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pmr_match(aTHX_ terms, r)) continue; 68 100 168
364 100 items[kept].row = r;
365 100 items[kept].spec = &spec;
366 100 kept++;
367 }
368 35 ▶ if (kept > 1) qsort(items, (size_t)kept, sizeof *items, pmr_qsort_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (kept > 1) qsort(items, (size_t)kept, sizeof *items, pmr_qsort_cmp); 24 11 35
369 35 ▶ if (vals)
Branch: 1 branch
BranchTrueFalseTotal
if (vals) 3 32 35
370 3 while (from < kept
371 11 ▶ && pmr_rowcmp(aTHX_ &spec, items[from].row, NULL, vals) <= 0)
Branch: 2 branches
BranchTrueFalseTotal
&& pmr_rowcmp(aTHX_ &spec, items[from].row, NULL, vals) <= 0) 11 0 11
&& pmr_rowcmp(aTHX_ &spec, items[from].row, NULL, vals) <= 0) 8 3 11
372 8 from++;
373  
374 35 out = newAV();
375 119 ▶ for (taken = 0, i = from; i < kept && taken <= limit; i++, taken++)
Branch: 2 branches
BranchTrueFalseTotal
for (taken = 0, i = from; i < kept && taken <= limit; i++, taken++) 88 31 119
for (taken = 0, i = from; i < kept && taken <= limit; i++, taken++) 84 4 88
376 84 av_push(out, pmr_deep(aTHX_ sv_2mortal(newRV_inc((SV *)items[i].row))));
377 35 return pdbq_page(aTHX_ newRV_noinc((SV *)out), limit, cols, desc, pk_only);
378 }
379  
380 10 static IV pmr_count(pTHX_ SV *self, AV *rows, SV *filter, const char *cls) {
381 10 HV *h = pmr_self(aTHX_ self, cls);
382 10 ▶ HV *f = (filter && SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *f = (filter && SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV) 10 0 10
HV *f = (filter && SvROK(filter) && SvTYPE(SvRV(filter)) == SVt_PVHV) 10 0 10
383 20 ▶ ? (HV *)SvRV(filter) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(filter) : NULL; 10 0 10
384 10 AV *terms = pmr_compile(aTHX_ pmr_colset(aTHX_ h),
385 pdbi_get(aTHX_ h, "table"), f, cls);
386 10 ▶ SSize_t i, n = rows ? av_len(rows) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = rows ? av_len(rows) + 1 : 0; 4 6 10
387 10 IV c = 0;
388 27 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 10 27
389 17 SV **e = av_fetch(rows, i, 0);
390 17 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 17 0 17
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 17 0 17
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 17 0 17
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 17 0 17
391 17 ▶ && pmr_match(aTHX_ terms, (HV *)SvRV(*e)))
Branch: 1 branch
BranchTrueFalseTotal
&& pmr_match(aTHX_ terms, (HV *)SvRV(*e))) 15 2 17
392 15 c++;
393 }
394 10 return c;
395 }
396  
397 /* get(%key): the first row whose key columns equal the given values, copied,
398 * or undef. The keys are checked against the fields as DBI checks them. */
399 13 static SV *pmr_get(pTHX_ SV *self, AV *rows, SV **st, I32 items,
400 const char *cls) {
401 HV *key;
402 13 AV *keys = pdbi_key_args(aTHX_ st, items, &key, "get", cls);
403 AV *terms;
404 13 ▶ SSize_t i, n = rows ? av_len(rows) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = rows ? av_len(rows) + 1 : 0; 13 0 13
405 13 pdbq_check_keys(aTHX_ self, keys, cls, "get");
406 12 terms = pmr_compile(aTHX_ NULL, NULL, key, cls);
407 40 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 38 2 40
408 38 SV **e = av_fetch(rows, i, 0);
409 38 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 38 0 38
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 38 0 38
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 38 0 38
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 38 0 38
410 38 ▶ && pmr_match(aTHX_ terms, (HV *)SvRV(*e)))
Branch: 1 branch
BranchTrueFalseTotal
&& pmr_match(aTHX_ terms, (HV *)SvRV(*e))) 10 28 38
411 10 return pmr_deep(aTHX_ *e);
412 }
413 2 return newSV(0);
414 }
415  
416 /* ---- Punk::Model::JSONFile ----------------------------------------------------
417 *
418 * The file is a JSON object and a model's `table` names the array of rows in
419 * it, so one file can hold several tables (and values beside them, which
420 * `document` hands back). Read through the File::Raw::JSON ABI and kept per
421 * process until the file changes - its inode, size or mtime, because a write
422 * renames a new file into place and can land in the same second as the read
423 * before it. */
424  
425 #define PMJ "Punk::Model::JSONFile"
426  
427 static HV *PMJ_CACHE = NULL; /* file => [ stamp, doc-or-undef ] */
428  
429 79 static SV *pmj_file(pTHX_ HV *h) {
430 79 SV *o = pdbi_get(aTHX_ h, "opts");
431 79 ▶ SV *f = (o && SvROK(o)) ? pdbi_get(aTHX_ (HV *)SvRV(o), "file") : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *f = (o && SvROK(o)) ? pdbi_get(aTHX_ (HV *)SvRV(o), "file") : NULL; 79 0 79
SV *f = (o && SvROK(o)) ? pdbi_get(aTHX_ (HV *)SvRV(o), "file") : NULL; 79 0 79
432 79 ▶ if (!(f && SvOK(f) && SvCUR(f)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(f && SvOK(f) && SvCUR(f))) 78 1 79
if (!(f && SvOK(f) && SvCUR(f))) 78 0 78
if (!(f && SvOK(f) && SvCUR(f))) 0 78 78
433 1 croak(PMJ ": the database needs a `file` (database name => "
434 "{ backend => '" PMJ "', file => 'data.json' })");
435 78 return f;
436 }
437  
438 /* The bytes of the file decoded, or NULL with *err set to why. Owned. */
439 17 static SV *pmj_decode_file(pTHX_ const char *path, SV **err) {
440 17 SV *bytes = pmd_slurp(aTHX_ path);
441 17 SV *doc = NULL;
442 17 *err = NULL;
443 17 ▶ if (!bytes) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) return NULL; 0 17 17
444 /* the backend is XS registered at boot, so its .pm - and with it the
445 * module the decode is called in - may never have been required */
446 17 (void)pk_require_once(aTHX_ "File::Raw::JSON", TRUE);
447 {
448 17 dSP;
449 int count;
450 17 ENTER; SAVETMPS;
451 17 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(bytes); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(bytes); PUTBACK; 0 17 17
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(bytes); PUTBACK; 0 17 17
452 17 count = call_pv("File::Raw::JSON::file_json_decode", G_SCALAR | G_EVAL);
453 17 SPAGAIN;
454 17 ▶ doc = (count > 0) ? newSVsv(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
doc = (count > 0) ? newSVsv(POPs) : NULL; 17 0 17
455 17 PUTBACK;
456 17 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 17 0 17
if (SvTRUE(ERRSV)) { 2 15 17
457 2 ▶ *err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
*err = newSVsv(ERRSV); 2 0 2
458 2 ▶ if (doc) { SvREFCNT_dec(doc); doc = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (doc) { SvREFCNT_dec(doc); doc = NULL; } 2 0 2
459 }
460 17 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 17 0 17
461 }
462 17 ▶ if (doc && !(SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (doc && !(SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV)) { 15 2 17
if (doc && !(SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV)) { 15 0 15
if (doc && !(SvROK(doc) && SvTYPE(SvRV(doc)) == SVt_PVHV)) { 0 15 15
463 0 SvREFCNT_dec(doc);
464 0 doc = NULL;
465 0 *err = newSVpvs("it is not a JSON object\n");
466 }
467 17 return doc;
468 }
469  
470 /* The decoded file, borrowed from the cache, or NULL (no file, or a broken
471 * one - which warns once per version of it). */
472 59 static HV *pmj_document(pTHX_ SV *self) {
473 59 HV *h = pmr_self(aTHX_ self, PMJ);
474 59 SV *file = pmj_file(aTHX_ h);
475 59 const char *path = SvPV_nolen(file);
476 Stat_t st;
477 SV *stamp;
478 HE *he;
479 59 ▶ if (!PMJ_CACHE) PMJ_CACHE = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PMJ_CACHE) PMJ_CACHE = newHV(); 2 57 59
480 59 ▶ if (PerlLIO_stat(path, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_stat(path, &st) < 0) { 3 56 59
481 3 (void)hv_delete_ent(PMJ_CACHE, file, G_DISCARD, 0);
482 3 return NULL;
483 }
484 56 stamp = sv_2mortal(newSVpvf("%" UVuf ":%" IVdf ":%" IVdf,
485 (UV)st.st_ino, (IV)st.st_size, (IV)st.st_mtime));
486 56 he = hv_fetch_ent(PMJ_CACHE, file, 0, 0);
487 56 ▶ if (he && SvROK(HeVAL(he))) {
Branch: 2 branches
BranchTrueFalseTotal
if (he && SvROK(HeVAL(he))) { 54 2 56
if (he && SvROK(HeVAL(he))) { 54 0 54
488 54 AV *slot = (AV *)SvRV(HeVAL(he));
489 54 ▶ if (sv_eq(*av_fetch(slot, 0, 0), stamp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_eq(*av_fetch(slot, 0, 0), stamp)) { 51 3 54
490 51 SV *d = *av_fetch(slot, 1, 0);
491 51 ▶ return SvROK(d) ? (HV *)SvRV(d) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return SvROK(d) ? (HV *)SvRV(d) : NULL; 48 3 51
492 }
493 }
494 {
495 5 SV *err = NULL;
496 5 SV *doc = pmj_decode_file(aTHX_ path, &err);
497 5 AV *slot = newAV();
498 5 av_push(slot, newSVsv(stamp));
499 5 ▶ av_push(slot, doc ? doc : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(slot, doc ? doc : newSV(0)); 1 4 5
500 5 (void)hv_store_ent(PMJ_CACHE, file, newRV_noinc((SV *)slot), 0);
501 5 ▶ if (!doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) { 1 4 5
502 2 ▶ warn(PMJ ": %s is unusable, so it has no rows: %s", path,
Branch: 1 branch
BranchTrueFalseTotal
warn(PMJ ": %s is unusable, so it has no rows: %s", path, 1 0 1
503 err ? SvPV_nolen(err) : "it could not be read\n");
504 1 ▶ if (err) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) SvREFCNT_dec(err); 1 0 1
505 1 return NULL;
506 }
507 4 return (HV *)SvRV(doc);
508 }
509 }
510  
511 /* The table's rows, borrowed, or NULL. */
512 55 static AV *pmj_rows(pTHX_ SV *self) {
513 55 HV *doc = pmj_document(aTHX_ self);
514 SV *t, *rows;
515 55 ▶ if (!doc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return NULL; 6 49 55
516 49 t = pdbi_get(aTHX_ pmr_self(aTHX_ self, PMJ), "table");
517 49 rows = pdbi_get(aTHX_ doc, SvPV_nolen(t));
518 49 ▶ return (rows && SvROK(rows) && SvTYPE(SvRV(rows)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
return (rows && SvROK(rows) && SvTYPE(SvRV(rows)) == SVt_PVAV) 49 0 49
return (rows && SvROK(rows) && SvTYPE(SvRV(rows)) == SVt_PVAV) 49 0 49
519 98 ▶ ? (AV *)SvRV(rows) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(rows) : NULL; 49 0 49
520 }
521  
522 /* ---- writing ------------------------------------------------------------------
523 *
524 * Every write is read-modify-write of the whole file under an exclusive lock
525 * on `<file>.lock` (so two workers cannot lose each other's change), from a
526 * FRESH read rather than the cache, encoded pretty with sorted keys (so the
527 * file diffs well), written to a temporary file and renamed over the old one
528 * (so a reader never sees half of it). */
529  
530 typedef struct { int fd; } pmj_lock;
531  
532 /* The lock is fcntl's on every POSIX system and flock only on Windows, where
533 * perl emulates flock and there is no F_SETLKW. Solaris has no flock(2), so
534 * HAS_FLOCK is undefined there and a flock-only lock compiled to nothing: four
535 * forked writers kept about half of 100 rows (CPAN Testers, Punk 0.66). An
536 * fcntl lock belongs to the process and goes when it closes the descriptor,
537 * which is all this needs: the lock file is opened here and nowhere else. */
538 #if defined(F_SETLKW) && !defined(WIN32)
539 # define PMJ_LOCK_FCNTL
540 #elif defined(HAS_FLOCK) && defined(LOCK_EX)
541 # define PMJ_LOCK_FLOCK
542 #endif
543  
544 13 static void pmj_lock_take(pTHX_ const char *path, pmj_lock *l) {
545 13 SV *lp = sv_2mortal(newSVpvf("%s.lock", path));
546 13 l->fd = PerlLIO_open3(SvPV_nolen(lp), O_RDWR | O_CREAT, 0644);
547 13 ▶ if (l->fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (l->fd < 0) 0 13 13
548 0 croak(PMJ ": cannot open %s: %s", SvPV_nolen(lp), Strerror(errno));
549 #if defined(PMJ_LOCK_FCNTL)
550 {
551 struct flock fl;
552 13 Zero(&fl, 1, struct flock);
553 13 fl.l_type = F_WRLCK;
554 13 fl.l_whence = SEEK_SET;
555 13 ▶ while (fcntl(l->fd, F_SETLKW, &fl) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (fcntl(l->fd, F_SETLKW, &fl) < 0) { 0 13 13
556 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
557 0 PerlLIO_close(l->fd);
558 0 croak(PMJ ": cannot lock %s: %s", SvPV_nolen(lp), Strerror(errno));
559 }
560 }
561 #elif defined(PMJ_LOCK_FLOCK)
562 while (flock(l->fd, LOCK_EX) < 0) {
563 if (errno == EINTR) continue;
564 PerlLIO_close(l->fd);
565 croak(PMJ ": cannot lock %s: %s", SvPV_nolen(lp), Strerror(errno));
566 }
567 #else
568 PerlLIO_close(l->fd);
569 croak(PMJ ": no file locking on this system, so %s cannot be written "
570 "safely", path);
571 #endif
572 13 }
573  
574 13 static void pmj_lock_drop(pmj_lock *l) {
575 13 ▶ if (l->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->fd >= 0) { 13 0 13
576 #if defined(PMJ_LOCK_FLOCK)
577 (void)flock(l->fd, LOCK_UN);
578 #endif
579 13 PerlLIO_close(l->fd);
580 13 l->fd = -1;
581 }
582 13 }
583  
584 /* The lock, held until the enclosing LEAVE - on a croak as much as on a
585 * return, because the save stack unwinds either way. Call between ENTER and
586 * LEAVE. */
587 13 static void pmj_unlock_cb(pTHX_ void *p) {
588 13 pmj_lock *l = (pmj_lock *)p;
589 PERL_UNUSED_CONTEXT;
590 13 pmj_lock_drop(l);
591 13 Safefree(l);
592 13 }
593  
594 13 static void pmj_lock_scoped(pTHX_ const char *path) {
595 pmj_lock *l;
596 13 Newx(l, 1, pmj_lock);
597 13 l->fd = -1;
598 13 SAVEDESTRUCTOR_X(pmj_unlock_cb, l);
599 13 pmj_lock_take(aTHX_ path, l);
600 13 }
601  
602 /* Write `doc` to `path` atomically. Croaks on failure (the old file stays). */
603 8 static void pmj_write(pTHX_ const char *path, HV *doc) {
604 frj_opts o;
605 SV *json, *tmp;
606 PerlIO *fp;
607 STRLEN jl;
608 const char *jp;
609 8 const frj_abi *frj = punk_frj(aTHX);
610 8 frj->opts_init(&o);
611 8 o.pretty = 1;
612 8 o.sort_keys = 1;
613 8 json = sv_2mortal(frj->encode(aTHX_ sv_2mortal(newRV_inc((SV *)doc)), &o));
614 8 jp = SvPV_const(json, jl);
615 8 tmp = sv_2mortal(newSVpvf("%s.tmp.%" IVdf, path, (IV)PerlProc_getpid()));
616 8 fp = PerlIO_open(SvPV_nolen(tmp), "wb");
617 8 ▶ if (!fp) croak(PMJ ": cannot write %s: %s", SvPV_nolen(tmp), Strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) croak(PMJ ": cannot write %s: %s", SvPV_nolen(tmp), Strerror(errno)); 0 8 8
618 8 ▶ if (PerlIO_write(fp, jp, jl) != (SSize_t)jl || PerlIO_write(fp, "\n", 1) != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (PerlIO_write(fp, jp, jl) != (SSize_t)jl || PerlIO_write(fp, "\n", 1) != 1) { 8 0 8
if (PerlIO_write(fp, jp, jl) != (SSize_t)jl || PerlIO_write(fp, "\n", 1) != 1) { 0 8 8
619 0 PerlIO_close(fp);
620 0 (void)PerlLIO_unlink(SvPV_nolen(tmp));
621 0 croak(PMJ ": cannot write %s: %s", SvPV_nolen(tmp), Strerror(errno));
622 }
623 8 ▶ if (PerlIO_close(fp) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_close(fp) != 0) { 0 8 8
624 0 (void)PerlLIO_unlink(SvPV_nolen(tmp));
625 0 croak(PMJ ": cannot write %s: %s", SvPV_nolen(tmp), Strerror(errno));
626 }
627 8 ▶ if (PerlLIO_rename(SvPV_nolen(tmp), path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_rename(SvPV_nolen(tmp), path) != 0) { 0 8 8
628 0 int e = errno;
629 0 (void)PerlLIO_unlink(SvPV_nolen(tmp));
630 0 croak(PMJ ": cannot replace %s: %s", path, Strerror(e));
631 }
632 8 }
633  
634 /* The file as it is now, for a write: decoded fresh (never the cache), or a
635 * new empty object when there is no file. Croaks on a broken file rather
636 * than writing over what somebody may still want. Mortal HV. The table's
637 * array is created when missing; *rows_out points at it. */
638 13 static HV *pmj_fresh(pTHX_ SV *self, const char *path, AV **rows_out) {
639 13 HV *h = pmr_self(aTHX_ self, PMJ);
640 13 SV *t = pdbi_get(aTHX_ h, "table");
641 Stat_t st;
642 HV *doc;
643 SV *rows;
644 13 ▶ if (PerlLIO_stat(path, &st) < 0) doc = (HV *)sv_2mortal((SV *)newHV());
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_stat(path, &st) < 0) doc = (HV *)sv_2mortal((SV *)newHV()); 1 12 13
645 else {
646 12 SV *err = NULL;
647 12 SV *d = pmj_decode_file(aTHX_ path, &err);
648 12 ▶ if (!d) {
Branch: 1 branch
BranchTrueFalseTotal
if (!d) { 1 11 12
649 1 ▶ SV *why = sv_2mortal(err ? err : newSVpvs("it could not be read\n"));
Branch: 1 branch
BranchTrueFalseTotal
SV *why = sv_2mortal(err ? err : newSVpvs("it could not be read\n")); 0 1 1
650 1 croak(PMJ ": %s is unusable, so nothing was written: %s", path,
651 SvPV_nolen(why));
652 }
653 11 sv_2mortal(d);
654 11 doc = (HV *)SvRV(d);
655 }
656 12 rows = pdbi_get(aTHX_ doc, SvPV_nolen(t));
657 12 ▶ if (!(rows && SvROK(rows) && SvTYPE(SvRV(rows)) == SVt_PVAV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(rows && SvROK(rows) && SvTYPE(SvRV(rows)) == SVt_PVAV)) { 11 1 12
if (!(rows && SvROK(rows) && SvTYPE(SvRV(rows)) == SVt_PVAV)) { 11 0 11
if (!(rows && SvROK(rows) && SvTYPE(SvRV(rows)) == SVt_PVAV)) { 0 11 11
658 1 AV *a = newAV();
659 1 (void)hv_store_ent(doc, t, newRV_noinc((SV *)a), 0);
660 1 *rows_out = a;
661 }
662 11 else *rows_out = (AV *)SvRV(rows);
663 12 return doc;
664 }
665  
666 /* The known, defined columns of `data`, as a fresh HV. */
667 11 static HV *pmj_known(pTHX_ HV *col, HV *d, int keep_undef) {
668 11 HV *out = newHV();
669 HE *he;
670 11 ▶ if (!d) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return out; 0 11 11
671 11 hv_iterinit(d);
672 32 ▶ while ((he = hv_iternext(d))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(d))) { 21 11 32
673 21 SV *k = hv_iterkeysv(he);
674 21 SV *v = HeVAL(he);
675 21 ▶ if (col && HvUSEDKEYS(col) && !hv_exists_ent(col, k, 0)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (col && HvUSEDKEYS(col) && !hv_exists_ent(col, k, 0)) continue; 21 0 21
if (col && HvUSEDKEYS(col) && !hv_exists_ent(col, k, 0)) continue; 0 21 21
if (col && HvUSEDKEYS(col) && !hv_exists_ent(col, k, 0)) continue; 21 0 21
if (col && HvUSEDKEYS(col) && !hv_exists_ent(col, k, 0)) continue; 3 18 21
676 18 ▶ if (!keep_undef && !SvOK(v)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!keep_undef && !SvOK(v)) continue; 11 7 18
if (!keep_undef && !SvOK(v)) continue; 1 10 11
677 17 (void)hv_store_ent(out, k, pmr_deep(aTHX_ v), 0);
678 }
679 11 return out;
680 }
681  
682 /* The index of the row whose `pk` equals `id`, or -1. */
683 5 static SSize_t pmj_find(pTHX_ AV *rows, SV *pk, SV *id) {
684 5 SSize_t i, n = av_len(rows) + 1;
685 15 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 13 2 15
686 13 SV **e = av_fetch(rows, i, 0);
687 HE *he;
688 13 ▶ if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 13 0 13
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 13 0 13
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 13 0 13
if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV)) continue; 0 13 13
689 13 he = hv_fetch_ent((HV *)SvRV(*e), pk, 0, 0);
690 13 ▶ if (he && SvOK(HeVAL(he)) && pmr_cmp(aTHX_ HeVAL(he), id) == 0) return i;
Branch: 3 branches
BranchTrueFalseTotal
if (he && SvOK(HeVAL(he)) && pmr_cmp(aTHX_ HeVAL(he), id) == 0) return i; 13 0 13
if (he && SvOK(HeVAL(he)) && pmr_cmp(aTHX_ HeVAL(he), id) == 0) return i; 13 0 13
if (he && SvOK(HeVAL(he)) && pmr_cmp(aTHX_ HeVAL(he), id) == 0) return i; 3 10 13
691 }
692 2 return -1;
693 }
694  
695 8 static SV *pmj_create(pTHX_ SV *self, SV *data) {
696 8 HV *h = pmr_self(aTHX_ self, PMJ);
697 8 ▶ HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) ? (HV *)SvRV(data) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) ? (HV *)SvRV(data) : NULL; 8 0 8
HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) ? (HV *)SvRV(data) : NULL; 8 0 8
698 8 SV *pk = pdbi_get(aTHX_ h, "primary");
699 8 const char *path = SvPV_nolen(pmj_file(aTHX_ h));
700 8 HV *row = (HV *)sv_2mortal((SV *)pmj_known(aTHX_ pmr_colset(aTHX_ h), d, 0));
701 AV *rows;
702 HV *doc;
703 SV *out;
704 8 ▶ if (!HvUSEDKEYS(row)) croak(PMJ ": create with no known columns");
Branch: 2 branches
BranchTrueFalseTotal
if (!HvUSEDKEYS(row)) croak(PMJ ": create with no known columns"); 0 8 8
if (!HvUSEDKEYS(row)) croak(PMJ ": create with no known columns"); 1 7 8
705 7 ENTER;
706 7 pmj_lock_scoped(aTHX_ path);
707 {
708 {
709 7 doc = pmj_fresh(aTHX_ self, path, &rows);
710 6 ▶ if (pk && SvOK(pk)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pk && SvOK(pk)) { 6 0 6
if (pk && SvOK(pk)) { 6 0 6
711 6 HE *he = hv_fetch_ent(row, pk, 0, 0);
712 6 ▶ if (he && SvOK(HeVAL(he))) {
Branch: 2 branches
BranchTrueFalseTotal
if (he && SvOK(HeVAL(he))) { 2 4 6
if (he && SvOK(HeVAL(he))) { 2 0 2
713 2 ▶ if (pmj_find(aTHX_ rows, pk, HeVAL(he)) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pmj_find(aTHX_ rows, pk, HeVAL(he)) >= 0) 1 1 2
714 3 ▶ croak(PMJ ": a row with %s = %s already exists in %s",
Branch: 1 branch
BranchTrueFalseTotal
croak(PMJ ": a row with %s = %s already exists in %s", 1 0 1
715 SvPV_nolen(pk), SvPV_nolen(HeVAL(he)), path);
716 }
717 else {
718 /* the next integer key, when every key so far is one */
719 4 SSize_t i, n = av_len(rows) + 1;
720 4 IV max = 0;
721 8 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 3 8
722 5 SV **e = av_fetch(rows, i, 0);
723 HE *ke;
724 SV *k;
725 5 ▶ if (!(e && *e && SvROK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvROK(*e))) continue; 5 0 5
if (!(e && *e && SvROK(*e))) continue; 5 0 5
if (!(e && *e && SvROK(*e))) continue; 0 5 5
726 5 ke = hv_fetch_ent((HV *)SvRV(*e), pk, 0, 0);
727 5 ▶ k = ke ? HeVAL(ke) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
k = ke ? HeVAL(ke) : NULL; 5 0 5
728 5 ▶ if (!(k && SvOK(k))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(k && SvOK(k))) continue; 5 0 5
if (!(k && SvOK(k))) continue; 0 5 5
729 {
730 IV kv;
731 5 ▶ if (!pmr_int(aTHX_ k, &kv))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmr_int(aTHX_ k, &kv)) 1 4 5
732 1 croak(PMJ ": create needs a value for %s - its "
733 "existing keys are not all integers, so "
734 "there is no next one", SvPV_nolen(pk));
735 4 ▶ if (kv > max) max = kv;
Branch: 1 branch
BranchTrueFalseTotal
if (kv > max) max = kv; 4 0 4
736 }
737 }
738 3 (void)hv_store_ent(row, pk, newSViv(max + 1), 0);
739 }
740 }
741 4 av_push(rows, newRV_inc((SV *)row));
742 4 pmj_write(aTHX_ path, doc);
743 }
744 }
745 4 LEAVE;
746 4 out = pmr_deep(aTHX_ sv_2mortal(newRV_inc((SV *)row)));
747 4 return out;
748 }
749  
750 4 static SV *pmj_update(pTHX_ SV *self, SV *data) {
751 4 HV *h = pmr_self(aTHX_ self, PMJ);
752 4 ▶ HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) ? (HV *)SvRV(data) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) ? (HV *)SvRV(data) : NULL; 4 0 4
HV *d = (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) ? (HV *)SvRV(data) : NULL; 4 0 4
753 4 SV *pk = pdbi_get(aTHX_ h, "primary");
754 const char *path;
755 HV *changes;
756 HE *pke;
757 SV *id;
758 4 SV *out = NULL;
759 4 ▶ if (!(pk && SvOK(pk)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(pk && SvOK(pk))) 4 0 4
if (!(pk && SvOK(pk))) 0 4 4
760 0 croak(PMJ ": update needs a primary key");
761 4 ▶ pke = d ? hv_fetch_ent(d, pk, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
pke = d ? hv_fetch_ent(d, pk, 0, 0) : NULL; 4 0 4
762 4 ▶ if (!(pke && SvOK(HeVAL(pke))))
Branch: 2 branches
BranchTrueFalseTotal
if (!(pke && SvOK(HeVAL(pke)))) 3 1 4
if (!(pke && SvOK(HeVAL(pke)))) 0 3 3
763 1 croak(PMJ ": update needs the primary key in the data");
764 3 id = sv_2mortal(newSVsv(HeVAL(pke)));
765 3 path = SvPV_nolen(pmj_file(aTHX_ h));
766 3 changes = (HV *)sv_2mortal((SV *)pmj_known(aTHX_ pmr_colset(aTHX_ h), d, 1));
767 3 (void)hv_delete_ent(changes, pk, G_DISCARD, 0);
768 3 ENTER;
769 3 pmj_lock_scoped(aTHX_ path);
770 {
771 {
772 AV *rows;
773 3 HV *doc = pmj_fresh(aTHX_ self, path, &rows);
774 3 SSize_t at = pmj_find(aTHX_ rows, pk, id);
775 3 ▶ if (at >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (at >= 0) { 2 1 3
776 2 HV *row = (HV *)SvRV(*av_fetch(rows, at, 0));
777 HE *he;
778 2 hv_iterinit(changes);
779 5 ▶ while ((he = hv_iternext(changes)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(changes))) 3 2 5
780 3 (void)hv_store_ent(row, hv_iterkeysv(he),
781 newSVsv(HeVAL(he)), 0);
782 2 ▶ if (HvUSEDKEYS(changes)) pmj_write(aTHX_ path, doc);
Branch: 2 branches
BranchTrueFalseTotal
if (HvUSEDKEYS(changes)) pmj_write(aTHX_ path, doc); 0 2 2
if (HvUSEDKEYS(changes)) pmj_write(aTHX_ path, doc); 2 0 2
783 2 out = pmr_deep(aTHX_ *av_fetch(rows, at, 0));
784 }
785 }
786 }
787 3 LEAVE;
788 3 ▶ return out ? out : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return out ? out : newSV(0); 1 2 3
789 }
790  
791 3 static IV pmj_delete(pTHX_ SV *self, SV **st, I32 items) {
792 3 HV *h = pmr_self(aTHX_ self, PMJ);
793 HV *key;
794 3 AV *keys = pdbi_key_args(aTHX_ st, items, &key, "delete", PMJ);
795 AV *terms;
796 const char *path;
797 3 IV gone = 0;
798 3 pdbq_check_keys(aTHX_ self, keys, PMJ, "delete");
799 3 terms = pmr_compile(aTHX_ NULL, NULL, key, PMJ);
800 3 path = SvPV_nolen(pmj_file(aTHX_ h));
801 3 ENTER;
802 3 pmj_lock_scoped(aTHX_ path);
803 {
804 {
805 Stat_t st0;
806 3 ▶ if (PerlLIO_stat(path, &st0) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_stat(path, &st0) == 0) { 3 0 3
807 AV *rows;
808 3 HV *doc = pmj_fresh(aTHX_ self, path, &rows);
809 3 AV *keep = newAV();
810 3 SSize_t i, n = av_len(rows) + 1;
811 13 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 3 13
812 10 SV **e = av_fetch(rows, i, 0);
813 10 ▶ if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV
Branch: 4 branches
BranchTrueFalseTotal
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 10 0 10
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 10 0 10
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 10 0 10
if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVHV 10 0 10
814 10 ▶ && pmr_match(aTHX_ terms, (HV *)SvRV(*e))) {
Branch: 1 branch
BranchTrueFalseTotal
&& pmr_match(aTHX_ terms, (HV *)SvRV(*e))) { 2 8 10
815 2 gone++;
816 2 continue;
817 }
818 8 ▶ av_push(keep, e && *e ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(keep, e && *e ? newSVsv(*e) : newSV(0)); 8 0 8
av_push(keep, e && *e ? newSVsv(*e) : newSV(0)); 8 0 8
819 }
820 3 (void)hv_store_ent(doc, pdbi_get(aTHX_ h, "table"),
821 newRV_noinc((SV *)keep), 0);
822 3 ▶ if (gone) pmj_write(aTHX_ path, doc);
Branch: 1 branch
BranchTrueFalseTotal
if (gone) pmj_write(aTHX_ path, doc); 2 1 3
823 }
824 }
825 }
826 3 LEAVE;
827 3 return gone;
828 }
829  
830 /* ---- Punk::Model::Markdown ----------------------------------------------------
831 *
832 * The pages of a `markdown` mount, as rows: what $app->markdown_pages gives,
833 * read straight from the mount's page table. The files are the pages, so the
834 * backend does not write. */
835  
836 #define PMM "Punk::Model::Markdown"
837  
838 /* The capture of the markdown mount at `prefix` (trailing slash ignored), or
839 * a croak naming why: not compiled yet, or no such mount (listing the ones
840 * there are). `who` prefixes the croak. Borrowed. */
841 29 static AV *pmd_mount_cap(pTHX_ SV *app, SV *prefix, const char *who) {
842 29 HV *h = app_hv(aTHX_ app);
843 29 ▶ SV *mp = h ? app_get(aTHX_ h, K_MOUNTS_C) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *mp = h ? app_get(aTHX_ h, K_MOUNTS_C) : NULL; 29 0 29
844 29 SV *pf = sv_2mortal(app_strip_slash(aTHX_ prefix));
845 29 SV *have = sv_2mortal(newSVpvs(""));
846 AV *mounts;
847 SSize_t i, n;
848 29 ▶ if (!(mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV)) 28 1 29
if (!(mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV)) 28 0 28
if (!(mp && SvROK(mp) && SvTYPE(SvRV(mp)) == SVt_PVAV)) 0 28 28
849 1 croak("%s: the application has not been compiled yet (call it from "
850 "a route, or after to_app)", who);
851 28 mounts = (AV *)SvRV(mp);
852 28 n = av_len(mounts) + 1;
853 35 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 31 4 35
854 31 SV **e = av_fetch(mounts, i, 0);
855 HV *m;
856 SV **flag, **mpf, **mapp;
857 AV *cap;
858 31 ▶ if (!e || !*e || !SvROK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvROK(*e)) continue; 31 0 31
if (!e || !*e || !SvROK(*e)) continue; 31 0 31
if (!e || !*e || !SvROK(*e)) continue; 0 31 31
859 31 m = (HV *)SvRV(*e);
860 31 flag = hv_fetchs(m, K_MD_MOUNT, 0);
861 31 ▶ if (!(flag && *flag && SvTRUE(*flag))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(flag && *flag && SvTRUE(*flag))) continue; 29 2 31
if (!(flag && *flag && SvTRUE(*flag))) continue; 29 0 29
if (!(flag && *flag && SvTRUE(*flag))) continue; 0 29 29
862 29 mpf = hv_fetchs(m, K_PREFIX, 0);
863 29 ▶ if (!(mpf && *mpf)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(mpf && *mpf)) continue; 29 0 29
if (!(mpf && *mpf)) continue; 0 29 29
864 29 ▶ if (sv_cmp(*mpf, pf) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_cmp(*mpf, pf) != 0) { 5 24 29
865 5 ▶ if (SvCUR(have)) sv_catpvs(have, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(have)) sv_catpvs(have, ", "); 2 3 5
866 5 ▶ sv_catsv(have, SvCUR(*mpf) ? *mpf : sv_2mortal(newSVpvs("/")));
Branch: 1 branch
BranchTrueFalseTotal
sv_catsv(have, SvCUR(*mpf) ? *mpf : sv_2mortal(newSVpvs("/"))); 5 0 5
867 5 continue;
868 }
869 24 mapp = hv_fetchs(m, K_APP, 0);
870 24 ▶ cap = (mapp && *mapp && SvROK(*mapp) && SvTYPE(SvRV(*mapp)) == SVt_PVCV)
Branch: 3 branches
BranchTrueFalseTotal
cap = (mapp && *mapp && SvROK(*mapp) && SvTYPE(SvRV(*mapp)) == SVt_PVCV) 24 0 24
cap = (mapp && *mapp && SvROK(*mapp) && SvTYPE(SvRV(*mapp)) == SVt_PVCV) 24 0 24
cap = (mapp && *mapp && SvROK(*mapp) && SvTYPE(SvRV(*mapp)) == SVt_PVCV) 24 0 24
871 48 ▶ ? punk_clos_cap(aTHX_ (CV *)SvRV(*mapp)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? punk_clos_cap(aTHX_ (CV *)SvRV(*mapp)) : NULL; 24 0 24
872 24 ▶ if (!cap)
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) 0 24 24
873 0 croak("%s: the mount at '%s' was not built", who, SvPV_nolen(*mpf));
874 24 return cap;
875 }
876 8 ▶ croak("%s: no markdown mount at '%s' (%s%s)", who,
Branch: 2 branches
BranchTrueFalseTotal
croak("%s: no markdown mount at '%s' (%s%s)", who, 2 2 4
croak("%s: no markdown mount at '%s' (%s%s)", who, 4 0 4
877 SvCUR(pf) ? SvPV_nolen(pf) : "/",
878 SvCUR(have) ? "markdown mounts: " : "there are none",
879 SvPV_nolen(have));
880 return NULL; /* not reached */
881 }
882  
883 /* The mount's pages as rows. Mortal AV. */
884 13 static AV *pmm_rows(pTHX_ SV *self) {
885 13 HV *h = pmr_self(aTHX_ self, PMM);
886 13 SV *app = pdbi_get(aTHX_ h, "app");
887 13 SV *o = pdbi_get(aTHX_ h, "opts");
888 13 ▶ SV *prefix = (o && SvROK(o)) ? pdbi_get(aTHX_ (HV *)SvRV(o), "prefix") : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *prefix = (o && SvROK(o)) ? pdbi_get(aTHX_ (HV *)SvRV(o), "prefix") : NULL; 13 0 13
SV *prefix = (o && SvROK(o)) ? pdbi_get(aTHX_ (HV *)SvRV(o), "prefix") : NULL; 13 0 13
889 13 ▶ if (!(app && SvROK(app)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(app && SvROK(app))) 13 0 13
if (!(app && SvROK(app))) 0 13 13
890 0 croak(PMM ": the application has gone away");
891 13 return (AV *)sv_2mortal((SV *)pmd_pages_av(aTHX_
892 pmd_mount_cap(aTHX_ app, prefix, PMM)));
893 }
894  
895 #endif /* PUNK_MROWS_H */
« xs/config.xs ↑ Summary include/punk/punk_health.h »