include/dbil_future.h

filter: Statementstmt 96.3% 234 / 243 Branchbran 62.5% 180 / 288 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.9% 414 / 531 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/dbil_loop.h ↑ Summary include/dbil_obs_perl.h »
1 #ifndef DBIL_FUTURE_H
2 #define DBIL_FUTURE_H
3  
4 /* DBIx::Loop::Future - the canonical future, in C.
5 *
6 * A pending future carries a queue of on_ready callbacks. It settles exactly
7 * once: done() stores an arrayref of result values, fail() stores an error SV;
8 * either fires and then clears the queue (one-shot). The struct hangs off a
9 * blessed scalar via ext magic, freed in the magic-free hook - no DESTROY
10 * needed.
11 *
12 * Chaining (then/else) is C too. A continuation is not a Perl closure: it is a
13 * plain AV of [ next, on_done, on_fail ] pushed onto the same queue, and the
14 * fire loop tells the two apart by type - on_ready only ever accepts a
15 * coderef, so an AV in the queue is unambiguously ours. That means a `then`
16 * costs one array rather than a compiled closure, and settling a chain runs no
17 * Perl frame except the user's own callbacks. */
18  
19 typedef struct dbil_future {
20 int state; /* 0 pending, 1 done, 2 failed */
21 SV *result; /* done: an arrayref of values (+1) */
22 SV *error; /* failed: the error SV (+1) */
23 AV *cbs; /* pending on_ready coderefs (+1 each) */
24 } dbil_future;
25  
26 2850 static int dbil_future_free(pTHX_ SV *sv, MAGIC *mg) {
27 2850 dbil_future *f = (dbil_future *)mg->mg_ptr;
28 PERL_UNUSED_ARG(sv);
29 2850 ▶ if (f) {
Branch: 1 branch
BranchTrueFalseTotal
if (f) { 2850 0 2850
30 2850 ▶ if (f->result) SvREFCNT_dec(f->result);
Branch: 1 branch
BranchTrueFalseTotal
if (f->result) SvREFCNT_dec(f->result); 2804 46 2850
31 2850 ▶ if (f->error) SvREFCNT_dec(f->error);
Branch: 1 branch
BranchTrueFalseTotal
if (f->error) SvREFCNT_dec(f->error); 40 2810 2850
32 2850 ▶ if (f->cbs) SvREFCNT_dec((SV *)f->cbs);
Branch: 1 branch
BranchTrueFalseTotal
if (f->cbs) SvREFCNT_dec((SV *)f->cbs); 2850 0 2850
33 2850 Safefree(f);
34 }
35 2850 return 0;
36 }
37 static MGVTBL dbil_future_vtbl =
38 { NULL, NULL, NULL, NULL, dbil_future_free, NULL, NULL, NULL };
39  
40 /* the struct behind a DBIx::Loop::Future object, or croak */
41 9297 static dbil_future *dbil_future_of(pTHX_ SV *self) {
42 MAGIC *mg;
43 9297 ▶ if (!SvROK(self)) croak("DBIx::Loop::Future: not an object");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) croak("DBIx::Loop::Future: not an object"); 0 9297 9297
44 9297 mg = mg_findext(SvRV(self), PERL_MAGIC_ext, &dbil_future_vtbl);
45 9297 ▶ if (!mg) croak("DBIx::Loop::Future: not a DBIx::Loop::Future");
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) croak("DBIx::Loop::Future: not a DBIx::Loop::Future"); 0 9297 9297
46 9297 return (dbil_future *)mg->mg_ptr;
47 }
48  
49 /* new pending future, blessed into `class` (RV +1) */
50 2850 static SV *dbil_future_new(pTHX_ const char *class) {
51 dbil_future *f;
52 SV *inner, *rv;
53 2850 Newxz(f, 1, dbil_future);
54 2850 f->cbs = newAV();
55 2850 inner = newSV(0);
56 2850 sv_magicext(inner, NULL, PERL_MAGIC_ext, &dbil_future_vtbl, (char *)f, 0);
57 2850 rv = newRV_noinc(inner);
58 2850 ▶ sv_bless(rv, gv_stashpv(class && *class ? class : "DBIx::Loop::Future",
Branch: 2 branches
BranchTrueFalseTotal
sv_bless(rv, gv_stashpv(class && *class ? class : "DBIx::Loop::Future", 2850 0 2850
sv_bless(rv, gv_stashpv(class && *class ? class : "DBIx::Loop::Future", 2850 0 2850
59 GV_ADD));
60 2850 return rv;
61 }
62  
63 /* settle a future from C (no XSUB round-trip): done with a single value, or
64 * failed with an error SV. Both are no-ops if already settled. `fire` runs
65 * the on_ready queue. */
66 static void dbil_future_fire(pTHX_ SV *self, dbil_future *f); /* below */
67 static void dbil_then_run(pTHX_ SV *self, dbil_future *f, AV *k);
68  
69 /* Builtin result transforms. The DBI select* family is a fixed set of reshapes
70 * of one query result, so instead of each being a Perl closure the handler
71 * slot may hold one of these integers and the reshape happens in C. A
72 * continuation is [ next, on_done, on_fail, arg ]: a handler that is a
73 * reference is a coderef, one that is a plain integer is a builtin, and `arg`
74 * carries whatever that builtin needs (the key field, for ALL_HASHREF). */
75 #define DBIL_T_ALL_ARRAYREF 1
76 #define DBIL_T_ROW_ARRAYREF 2
77 #define DBIL_T_ROW_ARRAY 3
78 #define DBIL_T_ROW_HASHREF 4
79 #define DBIL_T_COL_ARRAYREF 5
80 #define DBIL_T_ALL_HASHREF 6
81 /* ALL_ROWHASH is DBI's selectall_arrayref($sql, { Slice => {} }): every row as
82 * a hashref, in the order the server returned them. ALL_HASHREF cannot stand
83 * in for it - keying by a column destroys the ordering, which breaks keyset
84 * pagination - so this is a genuinely missing member of the select* family
85 * rather than a convenience over one. */
86 #define DBIL_T_ALL_ROWHASH 7
87  
88 /* settle done from a list of values (copied, so the caller keeps its own) */
89 2033 static void dbil_future_settle_done_av(pTHX_ SV *self, AV *vals) {
90 2033 dbil_future *f = dbil_future_of(aTHX_ self);
91 AV *res;
92 SSize_t i, n;
93 2033 ▶ if (f->state) return;
Branch: 1 branch
BranchTrueFalseTotal
if (f->state) return; 0 2033 2033
94 2033 res = newAV();
95 2033 ▶ n = vals ? av_len(vals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = vals ? av_len(vals) + 1 : 0; 2033 0 2033
96 4069 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2036 2033 4069
97 2036 SV **v = av_fetch(vals, i, 0);
98 2036 ▶ av_push(res, newSVsv(v && *v ? *v : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(res, newSVsv(v && *v ? *v : &PL_sv_undef)); 2036 0 2036
av_push(res, newSVsv(v && *v ? *v : &PL_sv_undef)); 2036 0 2036
99 }
100 2033 f->result = newRV_noinc((SV *)res);
101 2033 f->state = 1;
102 2033 dbil_future_fire(aTHX_ self, f);
103 }
104  
105 743 static void dbil_future_settle_done1(pTHX_ SV *self, SV *val) {
106 743 dbil_future *f = dbil_future_of(aTHX_ self);
107 AV *res;
108 743 ▶ if (f->state) return;
Branch: 1 branch
BranchTrueFalseTotal
if (f->state) return; 1 742 743
109 742 res = newAV();
110 742 av_push(res, newSVsv(val));
111 742 f->result = newRV_noinc((SV *)res);
112 742 f->state = 1;
113 742 dbil_future_fire(aTHX_ self, f);
114 }
115  
116 32 static void dbil_future_settle_fail(pTHX_ SV *self, SV *err) {
117 32 dbil_future *f = dbil_future_of(aTHX_ self);
118 32 ▶ if (f->state) return;
Branch: 1 branch
BranchTrueFalseTotal
if (f->state) return; 0 32 32
119 32 f->error = newSVsv(err);
120 32 f->state = 2;
121 32 dbil_future_fire(aTHX_ self, f);
122 }
123  
124 /* Attach a continuation to `target`: when it settles, run $on_done or
125 * $on_fail and settle `next` with the result. An already-settled target runs
126 * it now rather than queueing it, exactly as on_ready does.
127 *
128 * With both callbacks undef this is adoption - done passes the values
129 * through, fail passes the error through - which is how a callback that
130 * returns a future is flattened, with no second code path. */
131 2048 static void dbil_then_attach4(pTHX_ SV *target, SV *next,
132 SV *on_done, SV *on_fail, SV *arg) {
133 2048 dbil_future *tf = dbil_future_of(aTHX_ target);
134 2048 AV *k = newAV();
135 2048 av_push(k, newSVsv(next));
136 2048 ▶ av_push(k, (on_done && SvOK(on_done)) ? newSVsv(on_done) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(k, (on_done && SvOK(on_done)) ? newSVsv(on_done) : newSV(0)); 2040 8 2048
av_push(k, (on_done && SvOK(on_done)) ? newSVsv(on_done) : newSV(0)); 2039 1 2040
137 2048 ▶ av_push(k, (on_fail && SvOK(on_fail)) ? newSVsv(on_fail) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(k, (on_fail && SvOK(on_fail)) ? newSVsv(on_fail) : newSV(0)); 2015 33 2048
av_push(k, (on_fail && SvOK(on_fail)) ? newSVsv(on_fail) : newSV(0)); 1 2014 2015
138 2048 ▶ av_push(k, (arg && SvOK(arg)) ? newSVsv(arg) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(k, (arg && SvOK(arg)) ? newSVsv(arg) : newSV(0)); 2 2046 2048
av_push(k, (arg && SvOK(arg)) ? newSVsv(arg) : newSV(0)); 2 0 2
139 2048 ▶ if (tf->state) {
Branch: 1 branch
BranchTrueFalseTotal
if (tf->state) { 2 2046 2048
140 2 dbil_then_run(aTHX_ target, tf, k);
141 2 SvREFCNT_dec((SV *)k);
142 }
143 2046 else av_push(tf->cbs, newRV_noinc((SV *)k));
144 2048 }
145  
146 2023 static void dbil_then_attach(pTHX_ SV *target, SV *next,
147 SV *on_done, SV *on_fail) {
148 2023 dbil_then_attach4(aTHX_ target, next, on_done, on_fail, NULL);
149 2023 }
150  
151 /* One of the DBI select* reshapes, applied to a query result hashref. Pushes
152 * the values to settle the next future with onto `out`.
153 *
154 * Returns an error SV (+1) instead of croaking: in the Perl these reshapes ran
155 * inside then's eval, so a bad key field produced a FAILED FUTURE rather than
156 * a die at settle time. Croaking from here would escape through whatever
157 * settled the query future, which is a different thing entirely. */
158 /* one row (an AV of values) against the column names, as a fresh HV (+1) */
159 26 static HV *dbil_row_to_hv(pTHX_ AV *ra, AV *cols) {
160 26 HV *h = newHV();
161 26 ▶ SSize_t j, ncol = cols ? av_len(cols) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t j, ncol = cols ? av_len(cols) + 1 : 0; 26 0 26
162 77 ▶ for (j = 0; j < ncol; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ncol; j++) { 51 26 77
163 51 SV **c = av_fetch(cols, j, 0);
164 51 SV **v = av_fetch(ra, j, 0);
165 STRLEN cl;
166 const char *cp;
167 51 ▶ if (!(c && *c)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(c && *c)) continue; 51 0 51
if (!(c && *c)) continue; 0 51 51
168 51 cp = SvPV_const(*c, cl);
169 51 ▶ (void)hv_store(h, cp, (I32)cl, newSVsv(v && *v ? *v : &PL_sv_undef), 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store(h, cp, (I32)cl, newSVsv(v && *v ? *v : &PL_sv_undef), 0); 51 0 51
(void)hv_store(h, cp, (I32)cl, newSVsv(v && *v ? *v : &PL_sv_undef), 0); 51 0 51
170 }
171 26 return h;
172 }
173  
174 31 static SV *dbil_then_builtin(pTHX_ IV op, SV *res, SV *arg, AV *out) {
175 HV *r;
176 SV **e;
177 31 AV *rows = NULL, *cols = NULL;
178  
179 31 ▶ if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) { 31 0 31
if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) { 31 0 31
if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) { 0 31 31
180 0 av_push(out, newSV(0));
181 0 return NULL;
182 }
183 31 r = (HV *)SvRV(res);
184 31 ▶ if ((e = hv_fetchs(r, "rows", 0)) && *e && SvROK(*e)
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(r, "rows", 0)) && *e && SvROK(*e) 31 0 31
if ((e = hv_fetchs(r, "rows", 0)) && *e && SvROK(*e) 31 0 31
if ((e = hv_fetchs(r, "rows", 0)) && *e && SvROK(*e) 31 0 31
185 31 ▶ && SvTYPE(SvRV(*e)) == SVt_PVAV) rows = (AV *)SvRV(*e);
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*e)) == SVt_PVAV) rows = (AV *)SvRV(*e); 31 0 31
186 31 ▶ if ((e = hv_fetchs(r, "columns", 0)) && *e && SvROK(*e)
Branch: 3 branches
BranchTrueFalseTotal
if ((e = hv_fetchs(r, "columns", 0)) && *e && SvROK(*e) 31 0 31
if ((e = hv_fetchs(r, "columns", 0)) && *e && SvROK(*e) 31 0 31
if ((e = hv_fetchs(r, "columns", 0)) && *e && SvROK(*e) 31 0 31
187 31 ▶ && SvTYPE(SvRV(*e)) == SVt_PVAV) cols = (AV *)SvRV(*e);
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*e)) == SVt_PVAV) cols = (AV *)SvRV(*e); 31 0 31
188  
189 31 switch (op) {
190 7 case DBIL_T_ALL_ARRAYREF:
191 7 ▶ av_push(out, rows ? newRV_inc((SV *)rows) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(out, rows ? newRV_inc((SV *)rows) : newSV(0)); 7 0 7
192 7 break;
193  
194 1 case DBIL_T_ROW_ARRAYREF: {
195 1 ▶ SV **row = rows ? av_fetch(rows, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **row = rows ? av_fetch(rows, 0, 0) : NULL; 1 0 1
196 1 ▶ av_push(out, (row && *row) ? newSVsv(*row) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, (row && *row) ? newSVsv(*row) : newSV(0)); 1 0 1
av_push(out, (row && *row) ? newSVsv(*row) : newSV(0)); 1 0 1
197 1 break;
198 }
199  
200 2 case DBIL_T_ROW_ARRAY: { /* the first row as a list, or the empty list */
201 2 ▶ SV **row = rows ? av_fetch(rows, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **row = rows ? av_fetch(rows, 0, 0) : NULL; 2 0 2
202 2 ▶ if (row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV) { 2 0 2
if (row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV) { 2 0 2
if (row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV) { 2 0 2
if (row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV) { 2 0 2
203 2 AV *ra = (AV *)SvRV(*row);
204 2 SSize_t i, n = av_len(ra) + 1;
205 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
206 4 SV **v = av_fetch(ra, i, 0);
207 4 ▶ av_push(out, newSVsv(v && *v ? *v : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, newSVsv(v && *v ? *v : &PL_sv_undef)); 4 0 4
av_push(out, newSVsv(v && *v ? *v : &PL_sv_undef)); 4 0 4
208 }
209 }
210 2 break;
211 }
212  
213 4 case DBIL_T_ROW_HASHREF: {
214 4 ▶ SV **row = rows ? av_fetch(rows, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **row = rows ? av_fetch(rows, 0, 0) : NULL; 4 0 4
215 4 ▶ if (!(row && *row && SvROK(*row) && cols)) { av_push(out, newSV(0)); break; }
Branch: 4 branches
BranchTrueFalseTotal
if (!(row && *row && SvROK(*row) && cols)) { av_push(out, newSV(0)); break; } 3 1 4
if (!(row && *row && SvROK(*row) && cols)) { av_push(out, newSV(0)); break; } 3 0 3
if (!(row && *row && SvROK(*row) && cols)) { av_push(out, newSV(0)); break; } 3 0 3
if (!(row && *row && SvROK(*row) && cols)) { av_push(out, newSV(0)); break; } 0 3 3
216 3 av_push(out, newRV_noinc(
217 (SV *)dbil_row_to_hv(aTHX_ (AV *)SvRV(*row), cols)));
218 3 break;
219 }
220  
221 /* every row as a hashref, order preserved */
222 10 case DBIL_T_ALL_ROWHASH: {
223 10 AV *o = newAV();
224 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; 10 0 10
225 28 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 18 10 28
226 18 SV **row = av_fetch(rows, i, 0);
227 18 ▶ if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV)) 18 0 18
if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV)) 18 0 18
if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV)) 18 0 18
if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV)) 0 18 18
228 0 continue;
229 18 av_push(o, newRV_noinc(
230 (SV *)dbil_row_to_hv(aTHX_ (AV *)SvRV(*row), cols)));
231 }
232 10 av_push(out, newRV_noinc((SV *)o));
233 10 break;
234 }
235  
236 2 case DBIL_T_COL_ARRAYREF: {
237 2 AV *o = newAV();
238 2 ▶ SSize_t i, n = rows ? av_len(rows) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = rows ? av_len(rows) + 1 : 0; 2 0 2
239 7 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 2 7
240 5 SV **row = av_fetch(rows, i, 0);
241 5 ▶ SV **v = (row && *row && SvROK(*row)
Branch: 2 branches
BranchTrueFalseTotal
SV **v = (row && *row && SvROK(*row) 5 0 5
SV **v = (row && *row && SvROK(*row) 5 0 5
242 5 ▶ && SvTYPE(SvRV(*row)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*row)) == SVt_PVAV) 5 0 5
243 10 ▶ ? av_fetch((AV *)SvRV(*row), 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? av_fetch((AV *)SvRV(*row), 0, 0) : NULL; 5 0 5
244 5 ▶ av_push(o, newSVsv(v && *v ? *v : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(o, newSVsv(v && *v ? *v : &PL_sv_undef)); 5 0 5
av_push(o, newSVsv(v && *v ? *v : &PL_sv_undef)); 5 0 5
245 }
246 2 av_push(out, newRV_noinc((SV *)o));
247 2 break;
248 }
249  
250 5 case DBIL_T_ALL_HASHREF: {
251 5 HV *o = newHV();
252 5 ▶ SSize_t i, n = rows ? av_len(rows) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = rows ? av_len(rows) + 1 : 0; 5 0 5
253 5 ▶ SSize_t ncol = cols ? av_len(cols) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t ncol = cols ? av_len(cols) + 1 : 0; 5 0 5
254 5 SSize_t ki = -1, j;
255 STRLEN keylen;
256 9 ▶ const char *key = (arg && SvOK(arg)) ? SvPV_const(arg, keylen) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *key = (arg && SvOK(arg)) ? SvPV_const(arg, keylen) : NULL; 4 1 5
const char *key = (arg && SvOK(arg)) ? SvPV_const(arg, keylen) : NULL; 4 0 4
257 5 ▶ if (!key) {
Branch: 1 branch
BranchTrueFalseTotal
if (!key) { 1 4 5
258 1 SvREFCNT_dec((SV *)o);
259 3 return newSVpvs("DBIx::Loop->selectall_hashref: "
260 "a key field is required");
261 }
262 8 ▶ for (j = 0; j < ncol; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ncol; j++) { 6 2 8
263 6 SV **c = av_fetch(cols, j, 0);
264 STRLEN cl;
265 const char *cp;
266 6 ▶ if (!(c && *c)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(c && *c)) continue; 6 0 6
if (!(c && *c)) continue; 0 6 6
267 6 cp = SvPV_const(*c, cl);
268 6 ▶ if (cl == keylen && memEQ(cp, key, cl)) { ki = j; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (cl == keylen && memEQ(cp, key, cl)) { ki = j; break; } 2 4 6
if (cl == keylen && memEQ(cp, key, cl)) { ki = j; break; } 2 0 2
269 }
270 4 ▶ if (ki < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ki < 0) { 2 2 4
271 2 SV *err = newSVpvs("DBIx::Loop->selectall_hashref: no such column '");
272 2 sv_catpvn(err, key, keylen);
273 2 sv_catpvs(err, "'");
274 2 SvREFCNT_dec((SV *)o);
275 2 return err;
276 }
277 7 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 2 7
278 5 SV **row = av_fetch(rows, i, 0);
279 AV *ra;
280 HV *h;
281 SV **kv;
282 5 ▶ if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV)) 5 0 5
if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV)) 5 0 5
if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV)) 5 0 5
if (!(row && *row && SvROK(*row) && SvTYPE(SvRV(*row)) == SVt_PVAV)) 0 5 5
283 0 continue;
284 5 ra = (AV *)SvRV(*row);
285 5 h = dbil_row_to_hv(aTHX_ ra, cols);
286 5 kv = av_fetch(ra, ki, 0);
287 10 ▶ if (kv && *kv) {
Branch: 2 branches
BranchTrueFalseTotal
if (kv && *kv) { 5 0 5
if (kv && *kv) { 5 0 5
288 STRLEN kl2;
289 5 const char *kp2 = SvPV_const(*kv, kl2);
290 5 (void)hv_store(o, kp2, (I32)kl2, newRV_noinc((SV *)h), 0);
291 }
292 0 else SvREFCNT_dec((SV *)h);
293 }
294 2 av_push(out, newRV_noinc((SV *)o));
295 2 break;
296 }
297  
298 0 default:
299 0 av_push(out, newSVsv(res));
300 0 break;
301 }
302 28 return NULL;
303 }
304  
305 /* One settled future's continuation: call the handler for the outcome, then
306 * settle the next future from what it returned. */
307 2048 static void dbil_then_run(pTHX_ SV *self, dbil_future *f, AV *k) {
308 2048 int failed = (f->state == 2);
309 2048 SV **e = av_fetch(k, 0, 0);
310 2048 ▶ SV *next = (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
SV *next = (e && *e) ? *e : NULL; 2048 0 2048
SV *next = (e && *e) ? *e : NULL; 2048 0 2048
311 2048 SV *cb = NULL;
312 2048 ▶ if (!next) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!next) return; 0 2048 2048
313 2048 ▶ e = av_fetch(k, failed ? 2 : 1, 0);
Branch: 1 branch
BranchTrueFalseTotal
e = av_fetch(k, failed ? 2 : 1, 0); 6 2042 2048
314 2048 ▶ if (e && *e && SvOK(*e)) cb = *e;
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) cb = *e; 2048 0 2048
if (e && *e && SvOK(*e)) cb = *e; 2048 0 2048
if (e && *e && SvOK(*e)) cb = *e; 2037 11 2048
315  
316 /* a builtin reshape: no callback, no Perl frame */
317 2048 ▶ if (cb && !SvROK(cb) && SvIOK(cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (cb && !SvROK(cb) && SvIOK(cb)) { 2037 11 2048
if (cb && !SvROK(cb) && SvIOK(cb)) { 23 2014 2037
if (cb && !SvROK(cb) && SvIOK(cb)) { 23 0 23
318 23 AV *out = newAV();
319 23 ▶ SV **v = (!failed && f->result)
Branch: 1 branch
BranchTrueFalseTotal
SV **v = (!failed && f->result) 23 0 23
320 46 ▶ ? av_fetch((AV *)SvRV(f->result), 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? av_fetch((AV *)SvRV(f->result), 0, 0) : NULL; 23 0 23
321 23 SV *arg = NULL, *err;
322 23 e = av_fetch(k, 3, 0);
323 23 ▶ if (e && *e && SvOK(*e)) arg = *e;
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) arg = *e; 23 0 23
if (e && *e && SvOK(*e)) arg = *e; 23 0 23
if (e && *e && SvOK(*e)) arg = *e; 2 21 23
324 23 ▶ err = dbil_then_builtin(aTHX_ SvIV(cb), (v && *v) ? *v : NULL, arg, out);
Branch: 2 branches
BranchTrueFalseTotal
err = dbil_then_builtin(aTHX_ SvIV(cb), (v && *v) ? *v : NULL, arg, out); 23 0 23
err = dbil_then_builtin(aTHX_ SvIV(cb), (v && *v) ? *v : NULL, arg, out); 23 0 23
325 23 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 1 22 23
326 1 dbil_future_settle_fail(aTHX_ next, err);
327 1 SvREFCNT_dec(err);
328 }
329 22 else dbil_future_settle_done_av(aTHX_ next, out);
330 23 SvREFCNT_dec((SV *)out);
331 23 return;
332 }
333  
334 /* no handler for this outcome: the result passes straight through */
335 2025 ▶ if (!cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cb) { 11 2014 2025
336 11 ▶ if (failed)
Branch: 1 branch
BranchTrueFalseTotal
if (failed) 5 6 11
337 5 dbil_future_settle_fail(aTHX_ next,
338 5 ▶ f->error ? f->error : sv_2mortal(newSVpvs("failed")));
Branch: 1 branch
BranchTrueFalseTotal
f->error ? f->error : sv_2mortal(newSVpvs("failed"))); 5 0 5
339 else
340 6 dbil_future_settle_done_av(aTHX_ next,
341 6 ▶ f->result ? (AV *)SvRV(f->result) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
f->result ? (AV *)SvRV(f->result) : NULL); 6 0 6
342 11 return;
343 }
344  
345 {
346 2014 dSP;
347 int count;
348 SSize_t i;
349 AV *out;
350  
351 2014 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2014 2014
352 2014 ▶ if (failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (failed) { 1 2013 2014
353 1 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 1 1
354 1 ▶ PUSHs(f->error ? sv_2mortal(newSVsv(f->error)) : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(f->error ? sv_2mortal(newSVsv(f->error)) : &PL_sv_undef); 1 0 1
355 }
356 else {
357 2013 ▶ AV *res = f->result ? (AV *)SvRV(f->result) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *res = f->result ? (AV *)SvRV(f->result) : NULL; 2013 0 2013
358 2013 ▶ SSize_t n = res ? av_len(res) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = res ? av_len(res) + 1 : 0; 2013 0 2013
359 2013 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 2013 0 2013
EXTEND(SP, n); 0 2013 2013
360 4028 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2015 2013 4028
361 2015 SV **v = av_fetch(res, i, 0);
362 2015 ▶ PUSHs(v && *v ? sv_2mortal(newSVsv(*v)) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
PUSHs(v && *v ? sv_2mortal(newSVsv(*v)) : &PL_sv_undef); 2015 0 2015
PUSHs(v && *v ? sv_2mortal(newSVsv(*v)) : &PL_sv_undef); 2015 0 2015
363 }
364 }
365 2014 PUTBACK;
366 2014 count = call_sv(cb, G_ARRAY | G_EVAL);
367 2014 SPAGAIN;
368  
369 2014 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 2014 0 2014
if (SvTRUE(ERRSV)) { 1 2013 2014
370 /* The error is stringified, as the Perl chaining did with "$@":
371 * an exception object arrives at the next future as its string.
372 * NOT mortal - the FREETMPS below would free it before it is
373 * used, which is a freed-string panic rather than a wrong value. */
374 2 ▶ SV *err = newSVpv(SvPV_nolen(ERRSV), 0);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = newSVpv(SvPV_nolen(ERRSV), 0); 1 0 1
375 1 SP -= count;
376 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
377 1 dbil_future_settle_fail(aTHX_ next, err);
378 1 SvREFCNT_dec(err);
379 1 return;
380 }
381  
382 2013 out = newAV();
383 4028 ▶ for (i = (SSize_t)count - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = (SSize_t)count - 1; i >= 0; i--) { 2015 2013 4028
384 2015 SV *v = POPs;
385 2015 (void)av_store(out, i, newSVsv(v));
386 }
387 2013 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 2013 0 2013
388  
389 /* a lone returned future is adopted rather than stored as a value */
390 2013 ▶ if (av_len(out) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(out) == 0) { 2012 1 2013
391 2012 SV **v = av_fetch(out, 0, 0);
392 2012 ▶ if (v && *v && SvROK(*v) && sv_isobject(*v)
Branch: 4 branches
BranchTrueFalseTotal
if (v && *v && SvROK(*v) && sv_isobject(*v) 2012 0 2012
if (v && *v && SvROK(*v) && sv_isobject(*v) 2012 0 2012
if (v && *v && SvROK(*v) && sv_isobject(*v) 8 2004 2012
if (v && *v && SvROK(*v) && sv_isobject(*v) 8 0 8
393 8 ▶ && sv_derived_from(*v, "DBIx::Loop::Future")) {
Branch: 1 branch
BranchTrueFalseTotal
&& sv_derived_from(*v, "DBIx::Loop::Future")) { 8 0 8
394 8 dbil_then_attach(aTHX_ *v, next, NULL, NULL);
395 8 SvREFCNT_dec((SV *)out);
396 8 return;
397 }
398 }
399 2005 dbil_future_settle_done_av(aTHX_ next, out);
400 2005 SvREFCNT_dec((SV *)out);
401 }
402 }
403  
404 /* Run and clear the queue, passing $self to each callback. Entries are either
405 * an on_ready coderef or a then/else continuation (an AV); on_ready refuses
406 * anything but a coderef, so the type is the discriminator. */
407 2841 static void dbil_future_fire(pTHX_ SV *self, dbil_future *f) {
408 2841 SSize_t i, n = av_len(f->cbs) + 1;
409 4952 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2111 2841 4952
410 2111 SV **cb = av_fetch(f->cbs, i, 0);
411 2111 ▶ if (!cb || !*cb || !SvROK(*cb)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!cb || !*cb || !SvROK(*cb)) continue; 2111 0 2111
if (!cb || !*cb || !SvROK(*cb)) continue; 2111 0 2111
if (!cb || !*cb || !SvROK(*cb)) continue; 0 2111 2111
412 2111 ▶ if (SvTYPE(SvRV(*cb)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*cb)) == SVt_PVAV) { 2046 65 2111
413 2046 dbil_then_run(aTHX_ self, f, (AV *)SvRV(*cb));
414 2046 continue;
415 }
416 {
417 65 dSP;
418 65 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 65 65
419 65 ▶ EXTEND(SP, 1); PUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); PUSHs(self); 0 65 65
420 65 PUTBACK;
421 65 call_sv(*cb, G_DISCARD | G_EVAL);
422 65 ▶ if (SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) 65 0 65
if (SvTRUE(ERRSV)) 0 65 65
423 0 ▶ warn("DBIx::Loop::Future: on_ready callback died: %s",
Branch: 1 branch
BranchTrueFalseTotal
warn("DBIx::Loop::Future: on_ready callback died: %s", 0 0 0
424 SvPV_nolen(ERRSV));
425 65 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 65 65
426 }
427 }
428 2841 av_clear(f->cbs);
429 2841 }
430  
431 #endif /* DBIL_FUTURE_H */
« include/dbil_loop.h ↑ Summary include/dbil_obs_perl.h »