| 1 | package Fey::Object::Iterator::FromSelect::Caching; | ||||||||||
| 2 | { | ||||||||||
| 3 | 11 | $Fey::Object::Iterator::FromSelect::Caching::VERSION = '0.46'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 11 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 11 | ▶ | use Fey::ORM::Types qw( ArrayRef Bool ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 11 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 11 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 11 | ▶ | use MooseX::StrictConstructor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 11 | extends 'Fey::Object::Iterator::FromSelect'; | |||||||||
| 17 | |||||||||||
| 18 | has _cached_results => ( | ||||||||||
| 19 | traits => ['Array'], | ||||||||||
| 20 | is => 'ro', | ||||||||||
| 21 | isa => ArrayRef[ArrayRef], | ||||||||||
| 22 | lazy => 1, | ||||||||||
| 23 | 0 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 24 | 11 | init_arg => undef, | |||||||||
| 25 | handles => { | ||||||||||
| 26 | _cache_result => 'push', | ||||||||||
| 27 | _get_cached_result => 'get', | ||||||||||
| 28 | }, | ||||||||||
| 29 | |||||||||||
| 30 | # for cloning | ||||||||||
| 31 | writer => '_set_cached_results', | ||||||||||
| 32 | |||||||||||
| 33 | # for testability | ||||||||||
| 34 | clearer => '_clear_cached_results', | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 11 | has '_sth_is_exhausted' => ( | |||||||||
| 38 | is => 'rw', | ||||||||||
| 39 | isa => Bool, | ||||||||||
| 40 | init_arg => undef, | ||||||||||
| 41 | ); | ||||||||||
| 42 | |||||||||||
| 43 | override _get_next_result => sub { | ||||||||||
| 44 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 45 | |||||||||||
| 46 | 0 | my $result = $self->_get_cached_result( $self->index() ); | |||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | unless ($result) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | # Some drivers (DBD::Pg, at least) will blow up if we try to | ||||||||||
| 50 | # call a ->fetch type method on an exhausted statement | ||||||||||
| 51 | # handle. DBD::SQLite can handle this, so it is not tested. | ||||||||||
| 52 | 0 | ▶ | return if $self->_sth_is_exhausted(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 0 | $result = super(); | |||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | unless ($result) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | $self->_set_sth_is_exhausted(1); | |||||||||
| 58 | 0 | return; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | $self->_cache_result($result); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | return $result; | |||||||||
| 65 | 11 | }; | |||||||||
| 66 | |||||||||||
| 67 | sub reset { | ||||||||||
| 68 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 69 | |||||||||||
| 70 | 0 | $self->_reset_index(); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | sub clone { | ||||||||||
| 74 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
clone: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 75 | |||||||||||
| 76 | 0 | my $clone = $self->meta()->clone_object($self); | |||||||||
| 77 | |||||||||||
| 78 | # It'd be nice to actually share the array reference between | ||||||||||
| 79 | # multiple objects, but that causes problems because the sth may | ||||||||||
| 80 | # not be shared (if it has not yet been created). That means that | ||||||||||
| 81 | # the two sth's pull the same data twice and stuff it into the | ||||||||||
| 82 | # same array reference, so the data ends up in there twice. | ||||||||||
| 83 | 0 | $clone->_set_cached_results( [ @{ $self->_cached_results() } ] ); | |||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | $clone->_set_sth( $self->sth() ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | if $self->_has_sth(); | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | $clone->_set_sth_is_exhausted(1) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | if $self->_sth_is_exhausted(); | ||||||||||
| 90 | |||||||||||
| 91 | 0 | $clone->reset(); | |||||||||
| 92 | |||||||||||
| 93 | 0 | return $clone; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 11 | __PACKAGE__->meta()->make_immutable(); | |||||||||
| 97 | |||||||||||
| 98 | 11 | 1; | |||||||||
| 99 | |||||||||||
| 100 | # ABSTRACT: A caching subclass of Fey::Object::Iterator::FromSelect | ||||||||||
| 101 | |||||||||||
| 102 | __END__ | ||||||||||
| 103 | |||||||||||
| 104 | =pod | ||||||||||
| 105 | |||||||||||
| 106 | =head1 NAME | ||||||||||
| 107 | |||||||||||
| 108 | Fey::Object::Iterator::FromSelect::Caching - A caching subclass of Fey::Object::Iterator::FromSelect | ||||||||||
| 109 | |||||||||||
| 110 | =head1 VERSION | ||||||||||
| 111 | |||||||||||
| 112 | version 0.46 | ||||||||||
| 113 | |||||||||||
| 114 | =head1 SYNOPSIS | ||||||||||
| 115 | |||||||||||
| 116 | use Fey::Object::Iterator::FromSelect::Caching; | ||||||||||
| 117 | |||||||||||
| 118 | my $iter = Fey::Object::Iterator::FromSelect::Caching->new( | ||||||||||
| 119 | classes => 'MyApp::User', | ||||||||||
| 120 | select => $select, | ||||||||||
| 121 | dbh => $dbh, | ||||||||||
| 122 | bind_params => \@bind, | ||||||||||
| 123 | ); | ||||||||||
| 124 | |||||||||||
| 125 | print $iter->index(); # 0 | ||||||||||
| 126 | |||||||||||
| 127 | while ( my $user = $iter->next() ) { | ||||||||||
| 128 | print $iter->index(); # 1, 2, 3, ... | ||||||||||
| 129 | print $user->username(); | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | # will return cached objects now | ||||||||||
| 133 | $iter->reset(); | ||||||||||
| 134 | |||||||||||
| 135 | =head1 DESCRIPTION | ||||||||||
| 136 | |||||||||||
| 137 | This class implements a caching subclass of | ||||||||||
| 138 | L<Fey::Object::Iterator::FromSelect>. This means that it caches | ||||||||||
| 139 | objects it creates internally. When C<< $iterator->reset() >> is | ||||||||||
| 140 | called it will re-use those objects before fetching more data from the | ||||||||||
| 141 | DBMS. | ||||||||||
| 142 | |||||||||||
| 143 | =head1 METHODS | ||||||||||
| 144 | |||||||||||
| 145 | This class provides the following methods: | ||||||||||
| 146 | |||||||||||
| 147 | =head2 $iterator->reset() | ||||||||||
| 148 | |||||||||||
| 149 | Resets the iterator so that the next call to C<< $iterator->next() >> | ||||||||||
| 150 | returns the first objects. Internally, this I<does not> reset the | ||||||||||
| 151 | L<DBI> statement handle, it simply makes the iterator use cached | ||||||||||
| 152 | objects. | ||||||||||
| 153 | |||||||||||
| 154 | =head2 $iterator->clone() | ||||||||||
| 155 | |||||||||||
| 156 | Clones the iterator while sharing its cached data with the original | ||||||||||
| 157 | object. This is really intended for internal use, so I<use at your own | ||||||||||
| 158 | risk>. | ||||||||||
| 159 | |||||||||||
| 160 | =head1 ROLES | ||||||||||
| 161 | |||||||||||
| 162 | This class does the L<Fey::ORM::Role::Iterator> role. | ||||||||||
| 163 | |||||||||||
| 164 | =head1 AUTHOR | ||||||||||
| 165 | |||||||||||
| 166 | Dave Rolsky <autarch@urth.org> | ||||||||||
| 167 | |||||||||||
| 168 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 169 | |||||||||||
| 170 | This software is copyright (c) 2011 by Dave Rolsky. | ||||||||||
| 171 | |||||||||||
| 172 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 173 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 174 | |||||||||||
| 175 | =cut | ||||||||||