| 1 | package DBIx::CouchLike::Iterator; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use base qw/ Class::Accessor::Fast /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | __PACKAGE__->mk_accessors(qw/ couch sth query reduce /); | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | my $Sub = {}; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 0 | ▶ | sub _next { | ||||||||||||||||||
|
Subroutine
_next: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 12 | 0 | my $self = shift; | |||||||||||||||||||
| 13 | 0 | my $couch = $self->{couch}; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | my $r = $self->{sth}->fetchrow_arrayref; | |||||||||||||||||||
| 16 | 0 | ▶ | return unless $r; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 0 | ▶ | my $res = { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | id => $r->[0], | ||||||||||||||||||||
| 20 | key => $r->[1], | ||||||||||||||||||||
| 21 | value => ( $r->[2] =~ /^[{\[]/ ) | ||||||||||||||||||||
| 22 | ? $couch->from_json($r->[2]) | ||||||||||||||||||||
| 23 | : $r->[2], | ||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||
| 25 | 0 | ▶ | if ( $self->{query}->{include_docs} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 0 | $res->{document} = $couch->from_json($r->[3]); | |||||||||||||||||||
| 27 | 0 | $res->{document}->{_id} = $r->[0]; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | 0 | ▶ | delete $res->{key} unless defined $res->{key}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | return $res; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | ▶ | sub next { | ||||||||||||||||||
|
Subroutine
next: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||||||||
| 36 | 0 | ▶ | return $self->{reduce} ? $self->_next_reduce() | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | : $self->_next(); | ||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | sub all { | ||||||||||||||||||
|
Subroutine
all: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||||
| 42 | 0 | my @res; | |||||||||||||||||||
| 43 | 0 | ▶ | if ( $self->{reduce} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 0 | ▶ | push @res, $_ while $_ = $self->_next_reduce(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | else { | ||||||||||||||||||||
| 47 | 0 | ▶ | push @res, $_ while $_ = $self->_next(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | 0 | return @res; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | sub _next_reduce { | ||||||||||||||||||
|
Subroutine
_next_reduce: not called CC=11 SCAR=48.8
| |||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||
| 54 | 0 | my $sub_str = $self->{reduce}; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | ▶ | return if $self->{_exit}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | ▶ | my $sub = ( $Sub->{$sub_str} ||= eval $sub_str ); ## no critic | ||||||||||||||||||
|
Condition0%0/3
$Sub->{$sub_str} ||= eval $sub_str
MC/DC0%0/2
$Sub->{$sub_str} ||= eval $sub_str
$Sub->{$sub_str}
eval $sub_str
Truth table0%0/3
A ||= B
A= $Sub->{$sub_str}B= eval $sub_str
| |||||||||||||||||||||
| 59 | 0 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 0 | carp $@; | |||||||||||||||||||
| 61 | 0 | return; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | ▶ | my $keys = $self->{_pre_key} || []; | ||||||||||||||||||
|
Condition0%0/2
$self->{'_pre_key'} || []
MC/DC0%0/1
$self->{'_pre_key'} || []
$self->{'_pre_key'}
Truth table0%0/2
A
A= $self->{'_pre_key'}
| |||||||||||||||||||||
| 65 | 0 | ▶ | my $values = $self->{_pre_value} || []; | ||||||||||||||||||
|
Condition0%0/2
$self->{'_pre_value'} || []
MC/DC0%0/1
$self->{'_pre_value'} || []
$self->{'_pre_value'}
Truth table0%0/2
A
A= $self->{'_pre_value'}
| |||||||||||||||||||||
| 66 | 0 | ▶ | my $pre_key = $self->{_pre_key} ? $self->{_pre_key}->[0]->[0] : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | ▶ | while ( my $r = $self->_next ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 0 | ▶ | $pre_key = $r->{key} unless defined $pre_key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | ▶ | if ( $r->{key} eq $pre_key ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | # key が同じうちは貯めていく | ||||||||||||||||||||
| 71 | 0 | push @$keys, [ $r->{key}, $r->{id} ]; | |||||||||||||||||||
| 72 | 0 | push @$values, $r->{value}; | |||||||||||||||||||
| 73 | 0 | $pre_key = $r->{key}; | |||||||||||||||||||
| 74 | 0 | next; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | # key が変わったら貯めていた分を return | ||||||||||||||||||||
| 77 | 0 | $self->{_pre_key} = [ [ $r->{key}, $r->{id} ] ]; | |||||||||||||||||||
| 78 | 0 | $self->{_pre_value} = [ $r->{value} ]; | |||||||||||||||||||
| 79 | 0 | return $self->_do_reduce( $sub, $keys, $values ); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | $self->{_exit} = 1; | |||||||||||||||||||
| 83 | 0 | ▶ | return unless defined $keys->[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # 最後の | ||||||||||||||||||||
| 86 | 0 | return $self->_do_reduce( $sub, $keys, $values ); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | sub _do_reduce { | ||||||||||||||||||
|
Subroutine
_do_reduce: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||
| 91 | 0 | my ( $sub, $keys, $values ) = @_; | |||||||||||||||||||
| 92 | return { | ||||||||||||||||||||
| 93 | key => $keys->[0]->[0], | ||||||||||||||||||||
| 94 | 0 | value => eval { $sub->( $keys, $values ) }, | |||||||||||||||||||
| 95 | }; | ||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 99 | 0 | my $self = shift; | |||||||||||||||||||
| 100 | 0 | ▶ | $self->{sth}->finish() if $self->{sth}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 1 | 1; | |||||||||||||||||||
| 104 | |||||||||||||||||||||