| 1 | package DBIO::MySQL::EV; | ||
| 2 | 5 | our $VERSION = '0.900001'; | |
| 3 | # ABSTRACT: Async MySQL/MariaDB storage for DBIO via EV::MariaDB | ||
| 4 | |||
| 5 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 5 | ▶ | use base 'DBIO::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | |||
| 11 | # Note for cross-repo readers (karr #14 / dbio-mysql-ev): | ||
| 12 | # A previous version of this file overrode `connection()` to force | ||
| 13 | # `storage_type('+DBIO::MySQL::EV::Storage')` on every connect. That hijack | ||
| 14 | # contradicted the inert-component model (ADR 0030): a loadable component | ||
| 15 | # must not switch the schema's storage class on its own. PostgreSQL's | ||
| 16 | # equivalent `DBIO::PostgreSQL::EV` has never had such an override; the | ||
| 17 | # MySQL copy was removed for parity. The cross-repo handoff that registers | ||
| 18 | # the `ev` mode on `DBIO::MySQL::Storage` is tracked in the dbio-mysql | ||
| 19 | # board as its own karr ticket and is intentionally NOT implemented here. | ||
| 20 | |||
| 21 | 5 | 1; | |
| 22 | |||
| 23 | __END__ | ||
| 24 | |||
| 25 | =pod | ||
| 26 | |||
| 27 | =encoding UTF-8 | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | DBIO::MySQL::EV - Async MySQL/MariaDB storage for DBIO via EV::MariaDB | ||
| 32 | |||
| 33 | =head1 VERSION | ||
| 34 | |||
| 35 | version 0.900001 | ||
| 36 | |||
| 37 | =head1 SYNOPSIS | ||
| 38 | |||
| 39 | # Schema setup | ||
| 40 | package MyApp::Schema; | ||
| 41 | use base 'DBIO::Schema'; | ||
| 42 | __PACKAGE__->load_components(qw(MySQL MySQL::EV)); | ||
| 43 | |||
| 44 | # Async is opt-in per connection | ||
| 45 | my $schema = MyApp::Schema->connect( | ||
| 46 | $dsn, $user, $pass, | ||
| 47 | { async => 'ev' }, | ||
| 48 | ); | ||
| 49 | |||
| 50 | # Async queries return Futures | ||
| 51 | $schema->resultset('Artist')->all_async->then(sub { | ||
| 52 | my @artists = @_; | ||
| 53 | say $_->name for @artists; | ||
| 54 | }); | ||
| 55 | |||
| 56 | # Automatic wire pipelining — issue several *_async calls without awaiting | ||
| 57 | # between them; EV::MariaDB batches them in a single round-trip. | ||
| 58 | my @futures = map { | ||
| 59 | $schema->resultset('Artist')->create_async({ name => $_ }) | ||
| 60 | } @names; | ||
| 61 | Future->needs_all(@futures)->then(sub { ... }); | ||
| 62 | |||
| 63 | # Sync methods still work (block the event loop) | ||
| 64 | my @all = $schema->resultset('Artist')->all; | ||
| 65 | |||
| 66 | =head1 DESCRIPTION | ||
| 67 | |||
| 68 | Async MySQL/MariaDB support for DBIO using L<EV::MariaDB>, a non-blocking | ||
| 69 | MariaDB client built on MariaDB's C client library. Bypasses DBI | ||
| 70 | entirely for maximum performance. | ||
| 71 | |||
| 72 | EV::MariaDB pipelines queries B<automatically> at the wire level (consecutive | ||
| 73 | issued queries are batched, up to 64 in flight) and uses prepared statements | ||
| 74 | for bound queries. There is no explicit pipeline-mode API to bracket, so | ||
| 75 | L<DBIO::MySQL::EV::Storage> declares no C<pipeline> transport capability; | ||
| 76 | throughput comes for free from issuing several C<*_async> calls without | ||
| 77 | awaiting between them. | ||
| 78 | |||
| 79 | This module is an B<inert marker component> for async MySQL/MariaDB. Loading | ||
| 80 | it via C<< load_components('MySQL::EV') >> does B<not> by itself switch the | ||
| 81 | storage to async: it only tags the schema so the L<DBIO::MySQL::Storage> MRO | ||
| 82 | arm can resolve the C<ev> mode. Async is an explicit, per-connection choice | ||
| 83 | (ADR 0030); you opt in at C<connect> time with C<< { async => 'ev' } >>: | ||
| 84 | |||
| 85 | my $schema = MyApp::Schema->connect( | ||
| 86 | $dsn, $user, $pass, | ||
| 87 | { async => 'ev' }, | ||
| 88 | ); | ||
| 89 | |||
| 90 | The C<ev> mode resolves to L<DBIO::MySQL::EV::Storage> (this distribution) | ||
| 91 | and is registered by L<DBIO::MySQL::Storage> (the sync driver); the | ||
| 92 | registration is its own karr ticket and lives in the L<DBIO::MySQL> | ||
| 93 | distribution, not here. With C<{ async => 'ev' }> on a schema that has loaded | ||
| 94 | this component, the C<*_async> methods on the resulting storage run real | ||
| 95 | non-blocking queries over L<EV::MariaDB>. | ||
| 96 | |||
| 97 | C<< $storage->insert_async >> resolves with the returned-columns | ||
| 98 | I<hashref> (autoinc PK overlaid onto the supplied insert data), per ADR 0031 | ||
| 99 | §3 -- MySQL has no RETURNING clause, so the EV storage assembles the hashref | ||
| 100 | from C<LAST_INSERT_ID()> on the pinned connection. C<< select_async >> | ||
| 101 | resolves with the raw row arrayrefs (cursor C<< ->all >> shape) and | ||
| 102 | C<< select_single_async >> with a single row arrayref, matching the sync | ||
| 103 | cursor shape. Backend L<Future> C<< ->then >> callbacks auto-wrap a plain | ||
| 104 | return into a resolved Future (ADR 0031 §4), which is the native | ||
| 105 | L<Future.pm|Future> behaviour. | ||
| 106 | |||
| 107 | =head1 EVENT LOOP COMPATIBILITY | ||
| 108 | |||
| 109 | L<EV::MariaDB> uses the L<EV> event loop. This works with: | ||
| 110 | |||
| 111 | =over 4 | ||
| 112 | |||
| 113 | =item * L<EV> directly | ||
| 114 | |||
| 115 | =item * L<AnyEvent> (uses EV as backend when available) | ||
| 116 | |||
| 117 | =item * L<IO::Async> via L<IO::Async::Loop::EV> | ||
| 118 | |||
| 119 | =item * L<Mojolicious> via L<Mojo::Reactor::EV> | ||
| 120 | |||
| 121 | =back | ||
| 122 | |||
| 123 | =head1 SEE ALSO | ||
| 124 | |||
| 125 | L<DBIO::MySQL::EV::Storage>, L<DBIO::MySQL::Storage>, L<DBIO::Storage::Async>. | ||
| 126 | |||
| 127 | ADRs 0030 and 0031 in the L<DBIO> distribution's C<docs/adr/>. | ||
| 128 | |||
| 129 | =head1 AUTHOR | ||
| 130 | |||
| 131 | DBIO Authors | ||
| 132 | |||
| 133 | =head1 COPYRIGHT AND LICENSE | ||
| 134 | |||
| 135 | Copyright (C) 2026 DBIO Authors | ||
| 136 | |||
| 137 | This is free software; you can redistribute it and/or modify it under | ||
| 138 | the same terms as the Perl 5 programming language system itself. | ||
| 139 | |||
| 140 | =cut | ||