| 1 |
|
| #ifndef DBIL_LOOP_H |
| 2 |
|
| #define DBIL_LOOP_H |
| 3 |
|
| |
| 4 |
|
| /* The DBIx::Loop instance and its dispatch, in C. |
| 5 |
|
| * |
| 6 |
|
| * The object is a blessed HV { dbh, loop, capability } - not a hot struct (one |
| 7 |
|
| * per connection), so a hash is fine; the hot object is the future. query/do |
| 8 |
|
| * build a future, run the statement (dbil_run_dbi), and settle it. Phase 1 is |
| 9 |
|
| * synchronous (the stub path); phases 02/03 branch here on capability to the |
| 10 |
|
| * worker pool / native fd backends. */ |
| 11 |
|
| |
| 12 |
|
| /* driver name via $dbh->{Driver}{Name} (FETCH from C). Mortal SV or NULL. */ |
| 13 |
21 |
| static SV *dbil_driver_name(pTHX_ SV *dbh) { |
| 14 |
21 |
| SV *drv = dbil_h_fetch(aTHX_ dbh, "Driver"); |
| 15 |
21 |
▶ | if (drv && SvROK(drv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (drv && SvROK(drv)) { |
21 |
0 |
21 |
| if (drv && SvROK(drv)) { |
21 |
0 |
21 |
|
| 16 |
21 |
| SV *nm = dbil_h_fetch(aTHX_ drv, "Name"); |
| 17 |
21 |
▶ | if (nm && SvOK(nm)) return nm; |
Branch: 2 branches
| Branch | True | False | Total |
| if (nm && SvOK(nm)) return nm; |
21 |
0 |
21 |
| if (nm && SvOK(nm)) return nm; |
21 |
0 |
21 |
|
| 18 |
|
| } |
| 19 |
0 |
| return NULL; |
| 20 |
|
| } |
| 21 |
|
| |
| 22 |
|
| /* 'native' for socket drivers with non-blocking execute, else 'pool'. */ |
| 23 |
26 |
| static const char *dbil_capability(pTHX_ SV *driver_name) { |
| 24 |
26 |
▶ | if (driver_name && SvOK(driver_name)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (driver_name && SvOK(driver_name)) { |
26 |
0 |
26 |
| if (driver_name && SvOK(driver_name)) { |
26 |
0 |
26 |
|
| 25 |
|
| STRLEN l; |
| 26 |
26 |
| const char *p = SvPV_const(driver_name, l); |
| 27 |
26 |
▶ | if ((l == 2 && memEQ(p, "Pg", 2)) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((l == 2 && memEQ(p, "Pg", 2)) |
2 |
24 |
26 |
| if ((l == 2 && memEQ(p, "Pg", 2)) |
0 |
2 |
2 |
|
| 28 |
24 |
▶ | || (l == 5 && memEQ(p, "mysql", 5)) |
Branch: 2 branches
| Branch | True | False | Total |
| || (l == 5 && memEQ(p, "mysql", 5)) |
1 |
23 |
24 |
| || (l == 5 && memEQ(p, "mysql", 5)) |
0 |
1 |
1 |
|
| 29 |
23 |
▶ | || (l == 7 && memEQ(p, "MariaDB", 7))) |
Branch: 2 branches
| Branch | True | False | Total |
| || (l == 7 && memEQ(p, "MariaDB", 7))) |
1 |
22 |
23 |
| || (l == 7 && memEQ(p, "MariaDB", 7))) |
1 |
0 |
1 |
|
| 30 |
4 |
| return "native"; |
| 31 |
|
| } |
| 32 |
22 |
| return "pool"; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
|
| /* fetch a stored field of the blessed-HV object, or NULL */ |
| 36 |
1529 |
| static SV *dbil_field(pTHX_ SV *self, const char *k, STRLEN kl) { |
| 37 |
|
| HV *h; |
| 38 |
|
| SV **e; |
| 39 |
1529 |
▶ | if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
1529 |
0 |
1529 |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
0 |
1529 |
1529 |
|
| 40 |
0 |
| croak("DBIx::Loop: not a DBIx::Loop object"); |
| 41 |
1529 |
| h = (HV *)SvRV(self); |
| 42 |
1529 |
| e = hv_fetch(h, k, (I32)kl, 0); |
| 43 |
1529 |
▶ | return (e && *e) ? *e : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (e && *e) ? *e : NULL; |
1512 |
17 |
1529 |
| return (e && *e) ? *e : NULL; |
1512 |
0 |
1512 |
|
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| /* Build the object: the blessed HV behind both DBIx::Loop->new (xs/loop.xs, |
| 47 |
|
| * which parses the option pairs) and the C ABI's connect (dbil_abi_impl.h), so |
| 48 |
|
| * the capability probe and the connect-args retention have one home. |
| 49 |
|
| * |
| 50 |
|
| * `loop` and `dbh` are required; the dsn/user/pass/attr group is optional and |
| 51 |
|
| * stored only when a dsn is given, since it is what lets the pool's forked |
| 52 |
|
| * workers open their own handles - a live dbh cannot cross a fork. Croaks on a |
| 53 |
|
| * missing loop or dbh; callers that must not croak check first. */ |
| 54 |
22 |
| static SV *dbil_new(pTHX_ const char *cls, SV *dbh, SV *loop, |
| 55 |
|
| SV *dsn, SV *user, SV *pass, SV *attr, |
| 56 |
|
| int nworkers, int max_queue) { |
| 57 |
|
| HV *self; |
| 58 |
22 |
▶ | if (!loop || !SvOK(loop)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!loop || !SvOK(loop)) |
21 |
1 |
22 |
| if (!loop || !SvOK(loop)) |
0 |
21 |
21 |
|
| 59 |
1 |
| croak("DBIx::Loop->new: a 'loop' is required - DBIx::Loop is not " |
| 60 |
|
| "an event loop, it runs on one. Pass loop => $adapter."); |
| 61 |
21 |
▶ | if (!dbh || !SvROK(dbh)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!dbh || !SvROK(dbh)) |
21 |
0 |
21 |
| if (!dbh || !SvROK(dbh)) |
0 |
21 |
21 |
|
| 62 |
0 |
| croak("DBIx::Loop->new: a 'dbh' is required (or use connect)"); |
| 63 |
21 |
| self = newHV(); |
| 64 |
21 |
| (void)hv_stores(self, "dbh", newSVsv(dbh)); |
| 65 |
21 |
| (void)hv_stores(self, "loop", newSVsv(loop)); |
| 66 |
21 |
▶ | (void)hv_stores(self, "workers", newSViv(nworkers > 0 ? nworkers : 4)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(self, "workers", newSViv(nworkers > 0 ? nworkers : 4)); |
21 |
0 |
21 |
|
| 67 |
21 |
| (void)hv_stores(self, "max_queue", newSViv(max_queue > 0 ? max_queue : 0)); |
| 68 |
|
| { |
| 69 |
21 |
| SV *dn = dbil_driver_name(aTHX_ dbh); |
| 70 |
21 |
| const char *cap = dbil_capability(aTHX_ dn); |
| 71 |
|
| /* 'native' only when the driver's async surface is actually loaded |
| 72 |
|
| * (DBD::Pg's PG_ASYNC resolves); otherwise the pool backend serves it |
| 73 |
|
| * like any other driver */ |
| 74 |
21 |
▶ | if (strEQ(cap, "native") |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(cap, "native") |
1 |
20 |
21 |
|
| 75 |
2 |
▶ | && !(dn && SvOK(dn) && dbil_native_available(aTHX_ SvPV_nolen(dn)))) |
Branch: 3 branches
| Branch | True | False | Total |
| && !(dn && SvOK(dn) && dbil_native_available(aTHX_ SvPV_nolen(dn)))) |
1 |
0 |
1 |
| && !(dn && SvOK(dn) && dbil_native_available(aTHX_ SvPV_nolen(dn)))) |
1 |
0 |
1 |
| && !(dn && SvOK(dn) && dbil_native_available(aTHX_ SvPV_nolen(dn)))) |
0 |
1 |
1 |
|
| 76 |
0 |
| cap = "pool"; |
| 77 |
21 |
| (void)hv_stores(self, "capability", newSVpv(cap, 0)); |
| 78 |
|
| } |
| 79 |
21 |
▶ | if (dsn && SvOK(dsn)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (dsn && SvOK(dsn)) { |
18 |
3 |
21 |
| if (dsn && SvOK(dsn)) { |
18 |
0 |
18 |
|
| 80 |
18 |
| AV *ca = newAV(); |
| 81 |
18 |
| av_push(ca, newSVsv(dsn)); |
| 82 |
18 |
▶ | av_push(ca, (user && SvOK(user)) ? newSVsv(user) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(ca, (user && SvOK(user)) ? newSVsv(user) : newSV(0)); |
17 |
1 |
18 |
| av_push(ca, (user && SvOK(user)) ? newSVsv(user) : newSV(0)); |
17 |
0 |
17 |
|
| 83 |
18 |
▶ | av_push(ca, (pass && SvOK(pass)) ? newSVsv(pass) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(ca, (pass && SvOK(pass)) ? newSVsv(pass) : newSV(0)); |
17 |
1 |
18 |
| av_push(ca, (pass && SvOK(pass)) ? newSVsv(pass) : newSV(0)); |
17 |
0 |
17 |
|
| 84 |
18 |
▶ | av_push(ca, (attr && SvROK(attr)) ? newSVsv(attr) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(ca, (attr && SvROK(attr)) ? newSVsv(attr) : newSV(0)); |
18 |
0 |
18 |
| av_push(ca, (attr && SvROK(attr)) ? newSVsv(attr) : newSV(0)); |
18 |
0 |
18 |
|
| 85 |
18 |
| (void)hv_stores(self, "connect_args", newRV_noinc((SV *)ca)); |
| 86 |
|
| } |
| 87 |
21 |
▶ | return sv_bless(newRV_noinc((SV *)self), |
Branch: 2 branches
| Branch | True | False | Total |
| return sv_bless(newRV_noinc((SV *)self), |
21 |
0 |
21 |
| return sv_bless(newRV_noinc((SV *)self), |
21 |
0 |
21 |
|
| 88 |
|
| gv_stashpv(cls && *cls ? cls : "DBIx::Loop", GV_ADD)); |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| /* get-or-create the worker pool for this object (cached as _pool IV). |
| 92 |
|
| * |
| 93 |
|
| * Every path to the pool - exec and txn alike - comes through here, so this is |
| 94 |
|
| * where fork is caught. A cached pool that belongs to another process was |
| 95 |
|
| * inherited across a fork: using it would have two processes reading one |
| 96 |
|
| * socketpair, so it is disowned (fds closed, workers left alone) and a fresh |
| 97 |
|
| * pool is forked for this process. */ |
| 98 |
698 |
| static dbil_pool *dbil_pool_of(pTHX_ SV *self, SV *cargs) { |
| 99 |
698 |
| HV *h = (HV *)SvRV(self); |
| 100 |
698 |
| SV **pp = hv_fetchs(h, "_pool", 0); |
| 101 |
|
| dbil_pool *p; |
| 102 |
698 |
▶ | if (pp && *pp && SvIOK(*pp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (pp && *pp && SvIOK(*pp)) { |
680 |
18 |
698 |
| if (pp && *pp && SvIOK(*pp)) { |
680 |
0 |
680 |
| if (pp && *pp && SvIOK(*pp)) { |
680 |
0 |
680 |
|
| 103 |
680 |
| p = INT2PTR(dbil_pool *, SvIV(*pp)); |
| 104 |
680 |
▶ | if (dbil_pool_owned(p)) return p; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dbil_pool_owned(p)) return p; |
675 |
5 |
680 |
|
| 105 |
5 |
| dbil_pool_disown(aTHX_ p); |
| 106 |
5 |
| (void)hv_delete(h, "_pool", 5, G_DISCARD); |
| 107 |
|
| } |
| 108 |
|
| { |
| 109 |
23 |
| SV *loop = dbil_field(aTHX_ self, "loop", 4); |
| 110 |
23 |
| SV *wv = dbil_field(aTHX_ self, "workers", 7); |
| 111 |
23 |
| SV *mq = dbil_field(aTHX_ self, "max_queue", 9); |
| 112 |
23 |
▶ | int nw = (wv && SvIOK(wv)) ? (int)SvIV(wv) : 4; |
Branch: 2 branches
| Branch | True | False | Total |
| int nw = (wv && SvIOK(wv)) ? (int)SvIV(wv) : 4; |
23 |
0 |
23 |
| int nw = (wv && SvIOK(wv)) ? (int)SvIV(wv) : 4; |
23 |
0 |
23 |
|
| 113 |
23 |
| p = dbil_pool_new(aTHX_ loop, cargs, nw); |
| 114 |
23 |
▶ | p->max_queue = (mq && SvIOK(mq)) ? (int)SvIV(mq) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| p->max_queue = (mq && SvIOK(mq)) ? (int)SvIV(mq) : 0; |
23 |
0 |
23 |
| p->max_queue = (mq && SvIOK(mq)) ? (int)SvIV(mq) : 0; |
23 |
0 |
23 |
|
| 115 |
23 |
| (void)hv_stores(h, "_pool", newSViv(PTR2IV(p))); |
| 116 |
|
| } |
| 117 |
23 |
| return p; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| /* get-or-create the native connection state (cached as _native IV) */ |
| 121 |
9 |
| static dbil_native *dbil_native_of(pTHX_ SV *self) { |
| 122 |
9 |
| HV *h = (HV *)SvRV(self); |
| 123 |
9 |
| SV **pp = hv_fetchs(h, "_native", 0); |
| 124 |
|
| dbil_native *nc; |
| 125 |
9 |
▶ | if (pp && *pp && SvIOK(*pp)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (pp && *pp && SvIOK(*pp)) { |
8 |
1 |
9 |
| if (pp && *pp && SvIOK(*pp)) { |
8 |
0 |
8 |
| if (pp && *pp && SvIOK(*pp)) { |
8 |
0 |
8 |
|
| 126 |
8 |
| nc = INT2PTR(dbil_native *, SvIV(*pp)); |
| 127 |
8 |
▶ | if (dbil_native_owned(nc)) return nc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dbil_native_owned(nc)) return nc; |
8 |
0 |
8 |
|
| 128 |
|
| /* inherited across a fork: the libpq socket is the parent's */ |
| 129 |
0 |
| dbil_native_disown(aTHX_ nc); |
| 130 |
0 |
| (void)hv_delete(h, "_native", 7, G_DISCARD); |
| 131 |
|
| } |
| 132 |
1 |
| nc = dbil_native_new(aTHX_ dbil_field(aTHX_ self, "dbh", 3), |
| 133 |
|
| dbil_field(aTHX_ self, "loop", 4)); |
| 134 |
1 |
| (void)hv_stores(h, "_native", newSViv(PTR2IV(nc))); |
| 135 |
1 |
| return nc; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| /* build a future, run the statement, settle it; return the future (+1). |
| 139 |
|
| * 'native' -> the fd-async backend (Pg); 'pool' with connect args -> the |
| 140 |
|
| * forked worker pool; otherwise the synchronous path (a bare dbh). */ |
| 141 |
701 |
| static SV *dbil_exec(pTHX_ SV *self, int is_query, SV *sql, AV *bind) { |
| 142 |
701 |
| SV *cap = dbil_field(aTHX_ self, "capability", 10); |
| 143 |
701 |
| SV *cargs = dbil_field(aTHX_ self, "connect_args", 12); |
| 144 |
701 |
| SV *dbh, *fut, *err = NULL, *res; |
| 145 |
|
| /* v2 observers. Fired here, once, because this is the only place all |
| 146 |
|
| * three backends have in common - and attached to whatever comes back, so |
| 147 |
|
| * none of them has to know it is being watched. */ |
| 148 |
701 |
| dbil_obs_tokens *obs = dbil_obs_start(aTHX_ is_query, sql, bind); |
| 149 |
|
| |
| 150 |
701 |
▶ | if (cap && SvPOK(cap) && strEQ(SvPVX(cap), "native")) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (cap && SvPOK(cap) && strEQ(SvPVX(cap), "native")) { |
701 |
0 |
701 |
| if (cap && SvPOK(cap) && strEQ(SvPVX(cap), "native")) { |
701 |
0 |
701 |
| if (cap && SvPOK(cap) && strEQ(SvPVX(cap), "native")) { |
9 |
692 |
701 |
|
| 151 |
9 |
| dbil_native *nc = dbil_native_of(aTHX_ self); |
| 152 |
9 |
| fut = dbil_native_run(aTHX_ nc, is_query, sql, bind); |
| 153 |
9 |
| dbil_obs_attach(aTHX_ fut, obs); |
| 154 |
9 |
| return fut; |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
692 |
▶ | if (cap && SvPOK(cap) && strEQ(SvPVX(cap), "pool") |
Branch: 3 branches
| Branch | True | False | Total |
| if (cap && SvPOK(cap) && strEQ(SvPVX(cap), "pool") |
692 |
0 |
692 |
| if (cap && SvPOK(cap) && strEQ(SvPVX(cap), "pool") |
692 |
0 |
692 |
| if (cap && SvPOK(cap) && strEQ(SvPVX(cap), "pool") |
692 |
0 |
692 |
|
| 158 |
692 |
▶ | && cargs && SvROK(cargs)) { |
Branch: 2 branches
| Branch | True | False | Total |
| && cargs && SvROK(cargs)) { |
684 |
8 |
692 |
| && cargs && SvROK(cargs)) { |
684 |
0 |
684 |
|
| 159 |
684 |
| dbil_pool *p = dbil_pool_of(aTHX_ self, cargs); |
| 160 |
684 |
| fut = dbil_pool_run(aTHX_ p, is_query, sql, bind); |
| 161 |
684 |
| dbil_obs_attach(aTHX_ fut, obs); |
| 162 |
684 |
| return fut; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
8 |
| dbh = dbil_field(aTHX_ self, "dbh", 3); |
| 166 |
8 |
| fut = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 167 |
8 |
▶ | if (!dbh || !SvROK(dbh)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!dbh || !SvROK(dbh)) { |
8 |
0 |
8 |
| if (!dbh || !SvROK(dbh)) { |
0 |
8 |
8 |
|
| 168 |
0 |
| dbil_future_settle_fail(aTHX_ fut, sv_2mortal(newSVpvs("no database handle"))); |
| 169 |
0 |
| dbil_obs_attach(aTHX_ fut, obs); |
| 170 |
0 |
| return fut; |
| 171 |
|
| } |
| 172 |
8 |
| res = dbil_run_dbi(aTHX_ dbh, is_query, sql, bind, &err); |
| 173 |
8 |
▶ | if (res) dbil_future_settle_done1(aTHX_ fut, res); |
Branch: 1 branch
| Branch | True | False | Total |
| if (res) dbil_future_settle_done1(aTHX_ fut, res); |
5 |
3 |
8 |
|
| 174 |
3 |
| else dbil_future_settle_fail(aTHX_ fut, |
| 175 |
3 |
▶ | err ? err : sv_2mortal(newSVpvs("query failed"))); |
Branch: 1 branch
| Branch | True | False | Total |
| err ? err : sv_2mortal(newSVpvs("query failed"))); |
0 |
3 |
3 |
|
| 176 |
8 |
| dbil_obs_attach(aTHX_ fut, obs); |
| 177 |
8 |
| return fut; |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| #endif /* DBIL_LOOP_H */ |