Async/Storage/DBI/Cursor.pm

filter: Statementstmt 100% 41 / 41 Branchbran 100% 8 / 8 Conditioncond 50.0% 3 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 92.7% 64 / 69 CC 7 SCAR 9.1
CC
7
Cov
95%
new
4.2
next
3.0
__ANON__
2.0
CRAP 2.5
« Async/ResultSet/Pager.pm ↑ Summary Async/Storage.pm »
1 package DBIx::Class::Async::Storage::DBI::Cursor;
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 Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 DBIx::Class::Async::Storage::DBI::Cursor - Asynchronous cursor for DBIx::Class ResultSets using Futures
15  
16 =head1 VERSION
17  
18 Version v1.2.0
19  
20 =cut
21  
22 =head1 SYNOPSIS
23  
24 # Typically obtained from the storage or resultset
25 my $cursor = $rs->cursor;
26  
27 # Standard Async Iteration Pattern
28 my $process_next;
29 $process_next = sub {
30 return $cursor->next->then(sub {
31 my ($row_data) = @_;
32  
33 # If no data, we've reached the end of the ResultSet
34 return Future->done unless $row_data;
35  
36 # Process the raw row data (hashref)
37 say "Processing: " . $row_data->{email};
38  
39 # Recurse to fetch the next record
40 return $process_next->();
41 });
42 };
43  
44 # Start the stream
45 $process_next->()->on_fail(sub { warn "Streaming failed: @_" });
46  
47 # Reset the cursor to the beginning if needed
48 $cursor->reset;
49  
50 =head1 DESCRIPTION
51  
52 This module implements an asynchronous cursor abstraction for
53 L<DBIx::Class> ResultSets backed by DBI storage.
54  
55 It fetches rows from a ResultSet incrementally in fixed-size batches
56 (pages) and exposes a C<next> method that returns a L<Future>.
57 Each call to C<next> resolves to a single row object or C<undef>
58 when the result set has been exhausted.
59  
60 The cursor maintains an internal buffer and transparently issues
61 paginated queries using the ResultSet's C<page> and C<rows>
62 attributes.
63  
64 This class is primarily intended for use by asynchronous storage
65 layers or consumers that wish to process large result sets without
66 loading all rows into memory at once.
67  
68 =cut
69  
70 =head1 CONSTRUCTOR
71  
72 =head2 new
73  
74 my $cursor = DBIx::Class::Async::Storage::DBI::Cursor->new(%args);
75  
76 Creates a new asynchronous cursor.
77  
78 B<Arguments>
79  
80 =over 4
81  
82 =item * C<storage>
83  
84 The storage object associated with the ResultSet.
85 This value is currently stored but not actively used by the cursor.
86  
87 =item * C<rs>
88  
89 A L<DBIx::Class::ResultSet> to iterate over.
90 If the ResultSet has a C<rows> attribute set, it will be used
91 as the batch size for pagination.
92  
93 =back
94  
95 B<Batch Size>
96  
97 The cursor fetches rows in batches. The batch size is determined as follows:
98  
99 =over 4
100  
101 =item * If the ResultSet has C<< $rs->{_attrs}{rows} >> defined, that value is used
102  
103 =item * Otherwise, a default batch size of 20 rows is used
104  
105 =back
106  
107 =cut
108  
109 9 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
110 9 my ($class, %args) = @_;
111  
112 9 my $rs = $args{rs};
113 9 my $batch_size = 20; # default
114  
115 # Try to get rows attribute from ResultSet
116 9 ▶ if ($rs && $rs->{_attrs} && defined $rs->{_attrs}{rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rs and $rs->{"_attrs"} and defined $rs->{"_attrs"}{"rows"}) 4 5 9
Condition50%3/6
$rs and $rs->{"_attrs"} and defined $rs->{"_attrs"}{"rows"}
!ll&&!rl&&r
054
$rs and $rs->{"_attrs"}
!ll&&!rl&&r
009
MC/DC33%1/3
$rs and $rs->{"_attrs"} and defined $rs->{"_attrs"}{"rows"}
$rs $rs->{"_attrs"} defined $rs->{"_attrs"}{"rows"}
Truth table50%2/4
A and B and C
A= $rsB= $rs->{"_attrs"}C= defined $rs->{"_attrs"}{"rows"}
ABCresult
0XX0
10X0
1100
1111
117 4 $batch_size = $rs->{_attrs}{rows};
118 }
119  
120 return bless {
121 storage => $args{storage},
122 9 rs => $rs,
123 buffer => [],
124 page => 1,
125 finished => 0,
126 batch => $batch_size,
127 }, $class;
128 }
129  
130 =head1 METHODS
131  
132 =head2 reset
133  
134 $cursor->reset;
135  
136 Resets the cursor to the beginning of the ResultSet.
137  
138 This clears the internal buffer, resets the page counter to 1,
139 and marks the cursor as not finished.
140  
141 The cursor may then be iterated again from the start.
142  
143 Returns the cursor itself.
144  
145 B<INTERNAL STATE>
146  
147 The cursor maintains the following internal state:
148  
149 =over 4
150  
151 =item * C<buffer>
152  
153 An array reference holding prefetched rows.
154  
155 =item * C<page>
156  
157 The current page number used for pagination.
158  
159 =item * C<finished>
160  
161 Boolean flag indicating whether the result set has been exhausted.
162  
163 =item * C<batch>
164  
165 The number of rows fetched per page.
166  
167 =back
168  
169 These attributes are considered internal implementation details
170 and are not part of the public API.
171  
172 =cut
173  
174 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
175 1 my ($self) = @_;
176  
177 1 $self->{buffer} = [];
178 1 $self->{page} = 1;
179 1 $self->{finished} = 0;
180  
181 1 return $self;
182 }
183  
184 =head2 next
185  
186 my $future = $cursor->next;
187  
188 Returns a L<Future> that resolves to the next row in the ResultSet.
189  
190 B<Behaviour>
191  
192 =over 4
193  
194 =item * If buffered rows are available, the Future resolves immediately
195  
196 =item * If the buffer is empty, the next page of rows is fetched asynchronously
197  
198 =item * If no more rows are available, the Future resolves to C<undef>
199  
200 =back
201  
202 Each resolved value is a single row object (typically a
203 L<DBIx::Class::Row>), or C<undef> when iteration is complete.
204  
205 This method never throws synchronously; all results are delivered
206 via the returned Future.
207  
208 =cut
209  
210 18 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
Pod documented
211 18 my ($self) = @_;
212  
213 # 1. Return from buffer if available
214 18 ▶ if (@{$self->{buffer}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"buffer"};}) 8 10 18
215 8 return Future->done(shift @{$self->{buffer}});
216 }
217  
218 # 2. Stop if exhausted
219 10 ▶ return Future->done(undef) if $self->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"finished"} 1 9 10
220  
221 # 3. Fetch next chunk
222 return $self->{rs}->search(undef, {
223 page => $self->{page},
224 rows => $self->{batch}
225 9 ▶ })->all->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
226 9 my ($rows) = @_;
227  
228 9 ▶ if (!@$rows) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$rows) 2 7 9
229 2 $self->{finished} = 1;
230 2 return Future->done(undef);
231 }
232  
233 7 $self->{page}++;
234 7 $self->{buffer} = [ @$rows ];
235 7 return Future->done(shift @{$self->{buffer}});
236 9 });
237 }
238  
239 =head1 LIMITATIONS
240  
241 =head2 Stateless Iteration
242  
243 Unlike traditional DBI cursors, C<DBIx::Class::Async::Cursor> operates over a
244 stateless worker pool. To prevent "Worker Starvation," it does not hold
245 database-level cursors open. Instead:
246  
247 * It requires a ResultSet that supports L<DBIx::Class> paging (C<page> and C<rows>).
248  
249 * It fetches data in discrete batches to keep workers available for other tasks.
250  
251 =head2 Non-Blocking Requirement
252  
253 The cursor assumes that the underlying C<search> operation returns an
254 L<IO::Async::Future>. It is designed to work exclusively with the
255 C<DBIx::Class::Async> bridge.
256  
257 =head2 Concurrency Safety
258  
259 The Cursor object is intended for use within a single event loop iteration
260 or C<Future> chain. While the underlying worker pool is process-safe,
261 sharing a single Cursor instance across multiple C<ithreads> is not
262 supported and may result in undefined behavior.
263  
264 =cut
265  
266 =head1 SEE ALSO
267  
268 =over 4
269  
270 =item * L<DBIx::Class::Async>
271  
272 =item * L<DBIx::Class::Async::Storage>
273  
274 =item * L<DBIx::Class::Async::Storage::DBI>
275  
276 =item * L<Future>
277  
278 =back
279  
280 =head1 AUTHOR
281  
282 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
283  
284 =head1 REPOSITORY
285  
286 L<https://github.com/manwar/DBIx-Class-Async>
287  
288 =head1 BUGS
289  
290 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/DBIx-Class-Async/issues>.
291 I will be notified and then you'll automatically be notified of progress on your
292 bug as I make changes.
293  
294 =head1 SUPPORT
295  
296 You can find documentation for this module with the perldoc command.
297  
298 perldoc DBIx::Class::Async::Storage::DBI::Cursor
299  
300 You can also look for information at:
301  
302 =over 4
303  
304 =item * BUG Report
305  
306 L<https://github.com/manwar/DBIx-Class-Async/issues>
307  
308 =item * CPAN Ratings
309  
310 L<http://cpanratings.perl.org/d/DBIx-Class-Async>
311  
312 =item * Search MetaCPAN
313  
314 L<https://metacpan.org/dist/DBIx-Class-Async/>
315  
316 =back
317  
318 =head1 LICENSE AND COPYRIGHT
319  
320 Copyright (C) 2026 Mohammad Sajid Anwar.
321  
322 This program is free software; you can redistribute it and / or modify it under
323 the terms of the the Artistic License (2.0). You may obtain a copy of the full
324 license at:
325  
326 L<http://www.perlfoundation.org/artistic_license_2_0>
327  
328 Any use, modification, and distribution of the Standard or Modified Versions is
329 governed by this Artistic License.By using, modifying or distributing the Package,
330 you accept this license. Do not use, modify, or distribute the Package, if you do
331 not accept this license.
332  
333 If your Modified Version has been derived from a Modified Version made by someone
334 other than you,you are nevertheless required to ensure that your Modified Version
335 complies with the requirements of this license.
336  
337 This license does not grant you the right to use any trademark, service mark,
338 tradename, or logo of the Copyright Holder.
339  
340 This license includes the non-exclusive, worldwide, free-of-charge patent license
341 to make, have made, use, offer to sell, sell, import and otherwise transfer the
342 Package with respect to any patent claims licensable by the Copyright Holder that
343 are necessarily infringed by the Package. If you institute patent litigation
344 (including a cross-claim or counterclaim) against any party alleging that the
345 Package constitutes direct or contributory patent infringement,then this Artistic
346 License to you shall terminate on the date that such litigation is filed.
347  
348 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
349 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
350 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
351 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
352 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
353 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
354 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
355  
356 =cut
357  
358 100 1; # End of DBIx::Class::Async::Storage::DBI::Cursor
« Async/ResultSet/Pager.pm ↑ Summary Async/Storage.pm »