| 1 | #!/usr/bin/perl | ||||||||||||||
| 2 | |||||||||||||||
| 3 | package KiokuDB::Role::Scan; | ||||||||||||||
| 4 | 3 | ▶ | use MooseX::Role::Parameterized; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 3 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | parameter result_class => ( | |||||||||||||
| 9 | isa => "Str", | ||||||||||||||
| 10 | is => "ro", | ||||||||||||||
| 11 | required => 1, | ||||||||||||||
| 12 | ); | ||||||||||||||
| 13 | |||||||||||||||
| 14 | role { | ||||||||||||||
| 15 | 4 | ▶ | my $meta = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 4 | my $result_class = $meta->result_class; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 4 | with qw(KiokuDB::Role::Verbosity); | |||||||||||||
| 20 | |||||||||||||||
| 21 | 4 | has backend => ( | |||||||||||||
| 22 | does => "KiokuDB::Backend::Role::Scan", | ||||||||||||||
| 23 | is => "ro", | ||||||||||||||
| 24 | required => 1, | ||||||||||||||
| 25 | ); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 4 | has scan_all => ( | |||||||||||||
| 28 | isa => "Bool", | ||||||||||||||
| 29 | is => "ro", | ||||||||||||||
| 30 | default => 1, | ||||||||||||||
| 31 | ); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 4 | has scan_ids => ( | |||||||||||||
| 34 | isa => "Bool", | ||||||||||||||
| 35 | is => "ro", | ||||||||||||||
| 36 | ); | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 4 | has entries => ( | |||||||||||||
| 39 | does => "Data::Stream::Bulk", | ||||||||||||||
| 40 | is => "ro", | ||||||||||||||
| 41 | lazy_build => 1, | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | sub _build_entries { | ||||||||||||||
| 45 | 24 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_entries: called CC=3 SCAR=11.0
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | 24 | my $backend = $self->backend; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 24 | ▶ | my $set = $self->scan_all ? "all" : "root"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 24 | ▶ | my $type = $self->scan_ids ? "entry_ids" : "entries"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | 24 | my $method = join("_", $set, $type); | |||||||||||||
| 53 | |||||||||||||||
| 54 | 24 | $backend->$method; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 4 | has [qw(block_callback entry_callback)] => ( | |||||||||||||
| 58 | isa => "CodeRef|Str", | ||||||||||||||
| 59 | is => "ro", | ||||||||||||||
| 60 | ); | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 4 | has results => ( | |||||||||||||
| 63 | isa => $result_class, | ||||||||||||||
| 64 | is => "ro", | ||||||||||||||
| 65 | handles => qr/.*/, | ||||||||||||||
| 66 | lazy_build => 1, | ||||||||||||||
| 67 | ); | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 4 | requires "process_block"; | |||||||||||||
| 70 | |||||||||||||||
| 71 | method _build_results => sub { | ||||||||||||||
| 72 | 21 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_results: called CC=3 SCAR=11.0
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | 21 | my $res = $result_class->new; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 21 | my $i = my $j = 0; | |||||||||||||
| 77 | |||||||||||||||
| 78 | 21 | ▶ | while ( my $next = $self->entries->next ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 79 | 21 | $i += @$next; | |||||||||||||
| 80 | 21 | $j += @$next; | |||||||||||||
| 81 | |||||||||||||||
| 82 | 21 | ▶ | if ( $j > 13 ) { # luv primes | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 83 | 5 | $j = 0; | |||||||||||||
| 84 | 5 | $self->v("\rscanning... $i"); | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 21 | $self->process_block( block => $next, results => $res ); | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 21 | $self->v("\rscanned $i entries \n"); | |||||||||||||
| 91 | |||||||||||||||
| 92 | 21 | return $res; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | 4 | }; | |||||||||||||
| 95 | |||||||||||||||
| 96 | 2 | __PACKAGE__ | |||||||||||||
| 97 | |||||||||||||||
| 98 | __END__ | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =pod | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =head1 NAME | ||||||||||||||
| 103 | |||||||||||||||
| 104 | KiokuDB::Role::Scan - A role for entry scanning. | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =head1 SYNOPSIS | ||||||||||||||
| 107 | |||||||||||||||
| 108 | package My::Entry::Processor; | ||||||||||||||
| 109 | use Moose; | ||||||||||||||
| 110 | |||||||||||||||
| 111 | with 'KiokuDB::Role::Scan' => { result_class => "My::Entry::Processor::Results" }; | ||||||||||||||
| 112 | |||||||||||||||
| 113 | sub process_block { | ||||||||||||||
| 114 | my ( $self, %args ) = @_; | ||||||||||||||
| 115 | |||||||||||||||
| 116 | $args{results}; # intermediate results | ||||||||||||||
| 117 | |||||||||||||||
| 118 | foreach my $entry ( @{ $args{block} } ) { | ||||||||||||||
| 119 | |||||||||||||||
| 120 | } | ||||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | |||||||||||||||
| 124 | |||||||||||||||
| 125 | my $scan = My::Entry::Processor->new( | ||||||||||||||
| 126 | backend => $some_backend, | ||||||||||||||
| 127 | ); | ||||||||||||||
| 128 | |||||||||||||||
| 129 | my $res = $scan->results; | ||||||||||||||
| 130 | |||||||||||||||
| 131 | $res->foo; | ||||||||||||||
| 132 | |||||||||||||||
| 133 | $scan->foo; # delegates to result | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =head1 DESCRIPTION | ||||||||||||||
| 136 | |||||||||||||||
| 137 | This role is used by classes like L<KiokuDB::LinkChecker> to scan the whole | ||||||||||||||
| 138 | database and computing summary results. | ||||||||||||||
| 139 | |||||||||||||||
| 140 | =head1 ROLE PARAMETERS | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =over 4 | ||||||||||||||
| 143 | |||||||||||||||
| 144 | =item result_class | ||||||||||||||
| 145 | |||||||||||||||
| 146 | The class of the results. | ||||||||||||||
| 147 | |||||||||||||||
| 148 | Will be used when creating the results initially by calling C<new> and also | ||||||||||||||
| 149 | sets up an attribute with delegations. | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =back | ||||||||||||||
| 152 | |||||||||||||||
| 153 | =cut | ||||||||||||||
| 154 | |||||||||||||||
| 155 | |||||||||||||||