| 1 | package Data::Stream::Bulk; | ||||||||||||||
| 2 | 15 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 15 | $Data::Stream::Bulk::AUTHORITY = 'cpan:NUFFIN'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | { | ||||||||||||||
| 6 | 15 | $Data::Stream::Bulk::VERSION = '0.11'; | |||||||||||||
| 7 | } | ||||||||||||||
| 8 | 15 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | # ABSTRACT: N at a time iteration API | ||||||||||||||
| 10 | |||||||||||||||
| 11 | 15 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 15 | requires qw(next is_done); | |||||||||||||
| 14 | |||||||||||||||
| 15 | 28 | ▶ | sub items { | ||||||||||||
|
Subroutine
items: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 16 | 28 | my $self = shift; | |||||||||||||
| 17 | |||||||||||||||
| 18 | 28 | ▶ | if ( my $a = $self->next ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 19 | 22 | return @$a; | |||||||||||||
| 20 | } else { | ||||||||||||||
| 21 | 6 | return (); | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | } | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 12 | ▶ | sub all { | ||||||||||||
|
Subroutine
all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 26 | 12 | my $self = shift; | |||||||||||||
| 27 | |||||||||||||||
| 28 | 12 | my @ret; | |||||||||||||
| 29 | |||||||||||||||
| 30 | 12 | ▶ | while ( my $next = $self->next ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 269 | push @ret, @$next; | |||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 12 | return @ret; | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 20 | ▶ | sub cat { | ||||||||||||
|
Subroutine
cat: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 38 | 20 | my ( $self, @streams ) = @_; | |||||||||||||
| 39 | |||||||||||||||
| 40 | 20 | ▶ | return $self unless @streams; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | 16 | my @cat = $self->list_cat(@streams); | |||||||||||||
| 43 | |||||||||||||||
| 44 | 16 | ▶ | unless ( @cat ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 45 | 2 | return Data::Stream::Bulk::Nil->new; | |||||||||||||
| 46 | } elsif ( @cat == 1 ) { | ||||||||||||||
| 47 | 5 | return $cat[0]; | |||||||||||||
| 48 | } else { | ||||||||||||||
| 49 | 9 | return Data::Stream::Bulk::Cat->new( | |||||||||||||
| 50 | streams => \@cat, | ||||||||||||||
| 51 | ); | ||||||||||||||
| 52 | } | ||||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 10 | ▶ | sub list_cat { | ||||||||||||
|
Subroutine
list_cat: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 56 | 10 | my ( $self, $head, @tail ) = @_; | |||||||||||||
| 57 | |||||||||||||||
| 58 | 10 | ▶ | return $self unless $head; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 10 | return ( $self, $head->list_cat(@tail) ); | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 12 | ▶ | sub filter { | ||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 63 | 12 | my ( $self, $filter ) = @_; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 12 | return Data::Stream::Bulk::Filter->new( | |||||||||||||
| 66 | filter => $filter, | ||||||||||||||
| 67 | stream => $self, | ||||||||||||||
| 68 | ); | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 1 | ▶ | sub chunk { | ||||||||||||
|
Subroutine
chunk: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 72 | 1 | my ( $self, $chunk_size ) = @_; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 1 | return Data::Stream::Bulk::Chunked->new( | |||||||||||||
| 75 | chunk_size => $chunk_size, | ||||||||||||||
| 76 | stream => $self, | ||||||||||||||
| 77 | ); | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 2 | ▶ | sub loaded { 0 } | ||||||||||||
|
Subroutine
loaded: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 81 | |||||||||||||||
| 82 | # load it *after* the entire role is defined | ||||||||||||||
| 83 | 15 | require Data::Stream::Bulk::Cat; | |||||||||||||
| 84 | 15 | require Data::Stream::Bulk::Chunked; | |||||||||||||
| 85 | 15 | require Data::Stream::Bulk::Nil; | |||||||||||||
| 86 | 15 | require Data::Stream::Bulk::Filter; | |||||||||||||
| 87 | |||||||||||||||
| 88 | 15 | __PACKAGE__; | |||||||||||||
| 89 | |||||||||||||||
| 90 | |||||||||||||||
| 91 | |||||||||||||||
| 92 | =pod | ||||||||||||||
| 93 | |||||||||||||||
| 94 | =head1 NAME | ||||||||||||||
| 95 | |||||||||||||||
| 96 | Data::Stream::Bulk - N at a time iteration API | ||||||||||||||
| 97 | |||||||||||||||
| 98 | =head1 VERSION | ||||||||||||||
| 99 | |||||||||||||||
| 100 | version 0.11 | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =head1 SYNOPSIS | ||||||||||||||
| 103 | |||||||||||||||
| 104 | # get a bulk stream from somewere | ||||||||||||||
| 105 | my $s = Data::Stream::Bulk::Foo->new( ... ); | ||||||||||||||
| 106 | |||||||||||||||
| 107 | # can be used like this: | ||||||||||||||
| 108 | until ( $s->is_done ) { | ||||||||||||||
| 109 | foreach my $item ( $s->items ) { | ||||||||||||||
| 110 | process($item); | ||||||||||||||
| 111 | } | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | # or like this: | ||||||||||||||
| 115 | while( my $block = $s->next ) { | ||||||||||||||
| 116 | foreach my $item ( @$block ) { | ||||||||||||||
| 117 | process($item); | ||||||||||||||
| 118 | } | ||||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =head1 DESCRIPTION | ||||||||||||||
| 122 | |||||||||||||||
| 123 | This module tries to find middle ground between one at a time and all at once | ||||||||||||||
| 124 | processing of data sets. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | The purpose of this module is to avoid the overhead of implementing an | ||||||||||||||
| 127 | iterative api when this isn't necessary, without breaking forward | ||||||||||||||
| 128 | compatibility in case that becomes necessary later on. | ||||||||||||||
| 129 | |||||||||||||||
| 130 | The API optimizes for when a data set typically fits in memory and is returned | ||||||||||||||
| 131 | as an array, but the consumer cannot assume that the data set is bounded. | ||||||||||||||
| 132 | |||||||||||||||
| 133 | The API is destructive in order to minimize the chance that resultsets are | ||||||||||||||
| 134 | leaked due to improper usage. | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =head1 API | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =head2 Required Methods | ||||||||||||||
| 139 | |||||||||||||||
| 140 | The API requires two methods to be implemented: | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =over 4 | ||||||||||||||
| 143 | |||||||||||||||
| 144 | =item is_done | ||||||||||||||
| 145 | |||||||||||||||
| 146 | Should return true if the stream is exhausted. | ||||||||||||||
| 147 | |||||||||||||||
| 148 | As long as this method returns a false value (not done) C<next> could | ||||||||||||||
| 149 | potentially return another block. | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =item next | ||||||||||||||
| 152 | |||||||||||||||
| 153 | Returns the next block. | ||||||||||||||
| 154 | |||||||||||||||
| 155 | Note that C<next> is not guaranteed to return an array reference, even if | ||||||||||||||
| 156 | C<is_done> returned false prior to calling it. | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =back | ||||||||||||||
| 159 | |||||||||||||||
| 160 | =head2 Convenience Methods | ||||||||||||||
| 161 | |||||||||||||||
| 162 | =over 4 | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =item items | ||||||||||||||
| 165 | |||||||||||||||
| 166 | This method calls C<next> and dereferences the result if there are pending | ||||||||||||||
| 167 | items. | ||||||||||||||
| 168 | |||||||||||||||
| 169 | =item all | ||||||||||||||
| 170 | |||||||||||||||
| 171 | Force evaluation of the entire resultset. | ||||||||||||||
| 172 | |||||||||||||||
| 173 | Note that for large data sets this might cause swap thrashing of various other | ||||||||||||||
| 174 | undesired effects. Use with caution. | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =item cat @streams | ||||||||||||||
| 177 | |||||||||||||||
| 178 | Concatenates this stream with @streams, returning a single stream. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =item list_cat @tail | ||||||||||||||
| 181 | |||||||||||||||
| 182 | Returns a possibly cleaned up list of streams. | ||||||||||||||
| 183 | |||||||||||||||
| 184 | Used by C<cat>. | ||||||||||||||
| 185 | |||||||||||||||
| 186 | Overridden by L<Data::Stream::Bulk::Array>, L<Data::Stream::Bulk::Cat> and | ||||||||||||||
| 187 | L<Data::Stream::Bulk::Nil> to implement some simple short circuiting. | ||||||||||||||
| 188 | |||||||||||||||
| 189 | =item filter $filter | ||||||||||||||
| 190 | |||||||||||||||
| 191 | Applies a per-block block filter to the stream. | ||||||||||||||
| 192 | |||||||||||||||
| 193 | Returns a possibly new stream with the filtering layered. | ||||||||||||||
| 194 | |||||||||||||||
| 195 | C<$filter> is invoked once per block and should return an array reference to | ||||||||||||||
| 196 | the filtered block. | ||||||||||||||
| 197 | |||||||||||||||
| 198 | =item chunk $chunk_size | ||||||||||||||
| 199 | |||||||||||||||
| 200 | Chunks the input stream so that each block returned by C<next> will have at | ||||||||||||||
| 201 | least C<$chunk_size> items. | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =item loaded | ||||||||||||||
| 204 | |||||||||||||||
| 205 | Should be overridden to return true if all the items are already realized (e.g. | ||||||||||||||
| 206 | in the case of L<Data::Stream::Bulk::Array>). | ||||||||||||||
| 207 | |||||||||||||||
| 208 | Returns false by default. | ||||||||||||||
| 209 | |||||||||||||||
| 210 | When true calling C<all> is supposed to be safe (memory usage should be in the | ||||||||||||||
| 211 | same order of magnitude as stream's own usage). | ||||||||||||||
| 212 | |||||||||||||||
| 213 | This is typically useful when tranforming an array is easier than transorming a | ||||||||||||||
| 214 | stream (e.g. optional duplicate filtering). | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =back | ||||||||||||||
| 217 | |||||||||||||||
| 218 | =head1 CLASSES | ||||||||||||||
| 219 | |||||||||||||||
| 220 | =over 4 | ||||||||||||||
| 221 | |||||||||||||||
| 222 | =item L<Data::Stream::Bulk::Array> | ||||||||||||||
| 223 | |||||||||||||||
| 224 | This class is not a stream at all, but just one block. When the data set easily | ||||||||||||||
| 225 | fits in memory this class can be used, while retaining forward compatibility | ||||||||||||||
| 226 | with larger data sets. | ||||||||||||||
| 227 | |||||||||||||||
| 228 | =item L<Data::Stream::Bulk::Callback> | ||||||||||||||
| 229 | |||||||||||||||
| 230 | Callback driven iteration. | ||||||||||||||
| 231 | |||||||||||||||
| 232 | =item L<Data::Stream::Bulk::Chunked> | ||||||||||||||
| 233 | |||||||||||||||
| 234 | Wrapper to return larger blocks from an existing stream. | ||||||||||||||
| 235 | |||||||||||||||
| 236 | =item L<Data::Stream::Bulk::DBI> | ||||||||||||||
| 237 | |||||||||||||||
| 238 | Bulk fetching of data from L<DBI> statement handles. | ||||||||||||||
| 239 | |||||||||||||||
| 240 | =item L<Data::Stream::Bulk::DBIC> | ||||||||||||||
| 241 | |||||||||||||||
| 242 | L<DBIx::Class::ResultSet> iteration. | ||||||||||||||
| 243 | |||||||||||||||
| 244 | =item L<Data::Stream::Bulk::FileHandle> | ||||||||||||||
| 245 | |||||||||||||||
| 246 | Iterates over lines in a text file. | ||||||||||||||
| 247 | |||||||||||||||
| 248 | =item L<Data::Stream::Bulk::Nil> | ||||||||||||||
| 249 | |||||||||||||||
| 250 | An empty result set. | ||||||||||||||
| 251 | |||||||||||||||
| 252 | =item L<Data::Stream::Bulk::Cat> | ||||||||||||||
| 253 | |||||||||||||||
| 254 | A concatenation of several streams. | ||||||||||||||
| 255 | |||||||||||||||
| 256 | =item L<Data::Stream::Bulk::Filter> | ||||||||||||||
| 257 | |||||||||||||||
| 258 | A filter wrapping a stream. | ||||||||||||||
| 259 | |||||||||||||||
| 260 | =back | ||||||||||||||
| 261 | |||||||||||||||
| 262 | =head1 SEE ALSO | ||||||||||||||
| 263 | |||||||||||||||
| 264 | L<HOP::Stream>, L<Iterator>, L<Class::Iterator> etc for one by one iteration | ||||||||||||||
| 265 | |||||||||||||||
| 266 | L<DBI>, L<DBIx::Class::ResultSet> | ||||||||||||||
| 267 | |||||||||||||||
| 268 | L<POE::Filter> | ||||||||||||||
| 269 | |||||||||||||||
| 270 | L<Data::Page> | ||||||||||||||
| 271 | |||||||||||||||
| 272 | L<Parallel::Iterator> | ||||||||||||||
| 273 | |||||||||||||||
| 274 | L<http://en.wikipedia.org/wiki/MapReduce>, LISP, and all that other kool aid | ||||||||||||||
| 275 | |||||||||||||||
| 276 | =head1 TODO | ||||||||||||||
| 277 | |||||||||||||||
| 278 | =over 4 | ||||||||||||||
| 279 | |||||||||||||||
| 280 | =item Sorted streams | ||||||||||||||
| 281 | |||||||||||||||
| 282 | Add a hint for sorted streams (like C<loaded> but as an attribute in the base | ||||||||||||||
| 283 | role). | ||||||||||||||
| 284 | |||||||||||||||
| 285 | Introduce a C<merge> operation for merging of sorted streams. | ||||||||||||||
| 286 | |||||||||||||||
| 287 | Optimize C<unique> to make use of sorting hints for constant space uniquing. | ||||||||||||||
| 288 | |||||||||||||||
| 289 | =item More utility functions | ||||||||||||||
| 290 | |||||||||||||||
| 291 | To assist in proccessing and creating streams. | ||||||||||||||
| 292 | |||||||||||||||
| 293 | =item Coercion tables | ||||||||||||||
| 294 | |||||||||||||||
| 295 | L<Moose::Util::TypeConstraints> | ||||||||||||||
| 296 | |||||||||||||||
| 297 | =back | ||||||||||||||
| 298 | |||||||||||||||
| 299 | =head1 VERSION CONTROL | ||||||||||||||
| 300 | |||||||||||||||
| 301 | This module is maintained using git. You can get the latest version from | ||||||||||||||
| 302 | L<http://github.com/nothingmuch/data-stream-bulk/>. | ||||||||||||||
| 303 | |||||||||||||||
| 304 | =head1 AUTHOR | ||||||||||||||
| 305 | |||||||||||||||
| 306 | Yuval Kogman <nothingmuch@woobling.org> | ||||||||||||||
| 307 | |||||||||||||||
| 308 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 309 | |||||||||||||||
| 310 | This software is copyright (c) 2012 by Yuval Kogman. | ||||||||||||||
| 311 | |||||||||||||||
| 312 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 313 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 314 | |||||||||||||||
| 315 | =cut | ||||||||||||||
| 316 | |||||||||||||||
| 317 | |||||||||||||||
| 318 | __END__ | ||||||||||||||