Async/Storage.pm

filter: Statementstmt 86.2% 25 / 29 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 100% 5 / 5 totaltotal 82.9% 39 / 47 CC 3 SCAR 4.4
CC
3
Cov
82%
new
2.0
disconnect
2.0
cursor
2.0
CRAP 1.6
« Async/Storage/DBI/Cursor.pm ↑ Summary Async/Exception/RelationshipAsColumn.pm »
1 package DBIx::Class::Async::Storage;
2  
3 100 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 100 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 100 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 100 our $VERSION = qv('v1.2.0');
8 100 our $AUTHORITY = 'cpan:MANWAR';
9  
10 100 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 DBIx::Class::Async::Storage - Storage Layer for DBIx::Class::Async
15  
16 =head1 VERSION
17  
18 Version v1.2.0
19  
20 =cut
21  
22 =head1 SYNOPSIS
23  
24 use DBIx::Class::Async::Storage;
25  
26 # Typically instantiated automatically via DBIx::Class::Async::Schema
27 my $storage = $schema->storage;
28  
29 # Connection Management
30  
31 # Check if the worker pool is currently connected
32 if ( $storage->connected ) {
33 say "Worker pool is active and ready.";
34 }
35  
36 # Gracefully shut down all background worker processes and disconnect
37 $storage->disconnect;
38  
39 # Metadata & Configuration
40  
41 # Access the underlying schema instance
42 my $schema = $storage->schema;
43  
44 # Inspect the DSN (Data Source Name) currently in use
45 my $dsn = $storage->connect_info->[0];
46  
47 # Debugging
48  
49 # Storage acts as the gateway for the debug environment
50 $storage->debug(1) if $ENV{DBIC_TRACE};
51  
52 =head1 DESCRIPTION
53  
54 C<DBIx::Class::Async::Storage> is the orchestration layer for the asynchronous
55 bridge. Unlike standard L<DBIx::Class::Storage::DBI>, this class does not
56 connect to the database directly. Instead, it manages the life-cycle of a
57 background worker pool.
58  
59 =head1 CONSTRUCTOR
60  
61 =head2 new
62  
63 my $storage = DBIx::Class::Async::Storage->new(
64 schema => $schema,
65 async_db => $bridge_engine,
66 );
67  
68 B<async_db> is the internal engine (the 'bridge') that handles
69 communication with the worker processes.
70  
71 This constructor automatically weakens the reference to the parent L<schema>
72 to prevent circular reference leaks, ensuring that worker processes are
73 correctly reaped when the schema object goes out of scope.
74  
75 =cut
76  
77 148 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
78 148 my ($class, %args) = @_;
79  
80 # Standard DBIC storage expects a reference to the schema
81 my $self = bless {
82 _schema => $args{schema},
83 _async_db => $args{async_db}, # The worker pool engine
84 148 }, $class;
85  
86 # WEAKEN the schema reference to prevent circular memory leaks
87 # that caused your "5 vs 2 processes" test failure.
88 148 ▶ weaken($self->{_schema}) if $self->{_schema};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_schema"} 148 0 148
89  
90 148 return $self;
91 }
92  
93 =head1 METHODS
94  
95 =head2 cursor
96  
97 my $cursor = $storage->cursor($resultset);
98  
99 Creates and returns a cursor object for iterating through a ResultSet's rows.
100  
101 This is an abstract method that must be implemented by storage subclasses
102 (such as L<DBIx::Class::Async::Storage::DBI>). Calling this method directly
103 on the base storage class will throw an error.
104  
105 B<Arguments>
106  
107 =over 4
108  
109 =item * C<$resultset> - A L<DBIx::Class::Async::ResultSet> object
110  
111 =back
112  
113 B<Returns>
114  
115 A cursor object appropriate for the storage type. For DBI-based storage,
116 this returns a L<DBIx::Class::Async::Storage::DBI::Cursor> object.
117  
118 # Don't call on base class directly
119 my $cursor = $storage->cursor($rs); # Dies!
120  
121 # Use through a DBI storage subclass
122 my $dbi_storage = DBIx::Class::Async::Storage::DBI->new(...);
123 my $cursor = $dbi_storage->cursor($rs); # Works!
124  
125 Subclasses should override this method to return storage-specific cursor
126 implementations.
127  
128 =cut
129  
130 0 ▶sub cursor {
Subroutine cursor: not called CC=1 SCAR=6.9
Pod documented
131 0 my $self = shift;
132 0 die "Method 'cursor' must be implemented by a subclass of " . ref($self);
133 }
134  
135 =head2 dbh
136  
137 my $dbh = $storage->dbh; # Returns undef
138  
139 In this architecture, the Parent process B<must not> touch the database. This
140 method always returns C<undef>. Any code relying on C<$schema-E<gt>storage-E<gt>dbh>
141 will fail, which is intentional to prevent accidental blocking I/O in the
142 main event loop.
143  
144 =cut
145  
146 0 ▶sub dbh {
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
147 0 my $self = shift;
148 # In Async mode, the parent process doesn't hold a DBH.
149 0 return undef;
150 }
151  
152 =head2 schema
153  
154 my $schema = $storage->schema;
155  
156 Returns the L<DBIx::Class::Async::Schema> object that this storage layer
157 is associated with.
158  
159 This provides a back-reference from the storage to its parent schema,
160 allowing storage components to access schema-level information and other
161 ResultSources when needed.
162  
163 Note: The schema reference is weakened internally to prevent circular
164 reference memory leaks between the schema and storage objects.
165  
166 my $storage = $schema->storage;
167 my $parent_schema = $storage->schema;
168  
169 # Access schema configuration
170 my $source = $parent_schema->source('User');
171  
172 =cut
173  
174 3 ▶sub schema {
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
175 3 my $self = shift;
176 3 return $self->{_schema};
177 }
178  
179 =head2 disconnect
180  
181 $storage->disconnect;
182  
183 Signals the background worker pool to shut down. This is an asynchronous-safe
184 cleanup:
185  
186 1. It stops the worker processes.
187  
188 2. It closes IPC pipes or sockets used by the bridge.
189  
190 3. It clears the local query cache.
191  
192 =cut
193  
194 1 ▶sub disconnect {
Subroutine disconnect: called CC=2 SCAR=7.0
Pod documented
195 1 my $self = shift;
196  
197 # Delegate cleanup to the functional library
198 1 ▶ if ($self->{_async_db}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_async_db"}) 1 0 1
199 1 DBIx::Class::Async::disconnect($self->{_async_db});
200 }
201  
202 1 return 1;
203 }
204  
205 =head1 INTERNAL NOTES
206  
207 This class implements a specialised B<"Proxy Storage"> layer for the L<DBIx::Class::Async>
208 ecosystem. Unlike standard DBIC storage, this layer does not hold a local
209 database connection. Instead, it acts as a B<Command Dispatcher>.
210  
211 B<Key Architectural Differences>:
212  
213 =over 4
214  
215 =item * Proxied Execution
216  
217 SQL generation and execution occur within persistent background workers
218 via L<IO::Async::Function>.
219  
220 =item * Asynchronous Transactions
221  
222 Transactional integrity is maintained by routing related commands (C<txn_do>,
223 C<txn_batch>) to a single dedicated worker to ensure atomicity.
224  
225 =item * Distributed State
226  
227 Database handle management is isolated to the worker process, utilising
228  
229 B<InactiveDestroy> to prevent handle leakage or cross-process corruption.
230  
231 =item * Compatibility Shim
232  
233 It provides enough of the L<DBIx::Class::Storage> API to allow standard
234 ResultSets to function while redirecting all I/O to the L<IO::Async> event
235 loop.
236  
237 =back
238  
239 =head1 SEE ALSO
240  
241 =over 4
242  
243 =item *
244  
245 L<DBIx::Class::Async> - Asynchronous DBIx::Class interface
246  
247 =item *
248  
249 L<DBIx::Class::Async::Schema> - Asynchronous schema class
250  
251 =item *
252  
253 L<DBIx::Class::Storage> - Standard DBIx::Class storage interface
254  
255 =back
256  
257 =head1 AUTHOR
258  
259 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
260  
261 =head1 REPOSITORY
262  
263 L<https://github.com/manwar/DBIx-Class-Async>
264  
265 =head1 BUGS
266  
267 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/DBIx-Class-Async/issues>.
268 I will be notified and then you'll automatically be notified of progress on your
269 bug as I make changes.
270  
271 =head1 SUPPORT
272  
273 You can find documentation for this module with the perldoc command.
274  
275 perldoc DBIx::Class::Async::Storage
276  
277 You can also look for information at:
278  
279 =over 4
280  
281 =item * BUG Report
282  
283 L<https://github.com/manwar/DBIx-Class-Async/issues>
284  
285 =item * CPAN Ratings
286  
287 L<http://cpanratings.perl.org/d/DBIx-Class-Async>
288  
289 =item * Search MetaCPAN
290  
291 L<https://metacpan.org/dist/DBIx-Class-Async/>
292  
293 =back
294  
295 =head1 LICENSE AND COPYRIGHT
296  
297 Copyright (C) 2026 Mohammad Sajid Anwar.
298  
299 This program is free software; you can redistribute it and / or modify it under
300 the terms of the the Artistic License (2.0). You may obtain a copy of the full
301 license at:
302  
303 L<http://www.perlfoundation.org/artistic_license_2_0>
304  
305 Any use, modification, and distribution of the Standard or Modified Versions is
306 governed by this Artistic License.By using, modifying or distributing the Package,
307 you accept this license. Do not use, modify, or distribute the Package, if you do
308 not accept this license.
309  
310 If your Modified Version has been derived from a Modified Version made by someone
311 other than you,you are nevertheless required to ensure that your Modified Version
312 complies with the requirements of this license.
313  
314 This license does not grant you the right to use any trademark, service mark,
315 tradename, or logo of the Copyright Holder.
316  
317 This license includes the non-exclusive, worldwide, free-of-charge patent license
318 to make, have made, use, offer to sell, sell, import and otherwise transfer the
319 Package with respect to any patent claims licensable by the Copyright Holder that
320 are necessarily infringed by the Package. If you institute patent litigation
321 (including a cross-claim or counterclaim) against any party alleging that the
322 Package constitutes direct or contributory patent infringement,then this Artistic
323 License to you shall terminate on the date that such litigation is filed.
324  
325 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
326 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
327 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
328 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
329 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
330 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
331 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
332  
333 =cut
334  
335 100 1; # End of DBIx::Class::Async::Storage
« Async/Storage/DBI/Cursor.pm ↑ Summary Async/Exception/RelationshipAsColumn.pm »