| 1 | package WWW::Crawler::Mojo::Queue::Memory; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use Mojo::Base 'WWW::Crawler::Mojo::Queue'; | ||
| 6 | use List::Util; | ||
| 7 | |||
| 8 | has 'cap'; | ||
| 9 | has jobs => sub { [] }; | ||
| 10 | has redundancy_storage => sub { {} }; | ||
| 11 | |||
| 12 | sub dequeue { | ||
| 13 | return shift(@{shift->jobs}); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub enqueue { | ||
| 17 | return shift->_enqueue(@_); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub length { | ||
| 21 | return scalar(@{shift->jobs}); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub next { | ||
| 25 | return shift->jobs->[shift || 0]; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub requeue { | ||
| 29 | shift->_enqueue(@_, 1); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub shuffle { | ||
| 33 | my $self = shift; | ||
| 34 | @{$self->jobs} = List::Util::shuffle @{$self->jobs}; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _enqueue { | ||
| 38 | my ($self, $job, $requeue) = @_; | ||
| 39 | my $digest = $job->digest; | ||
| 40 | my $redund = $self->redundancy_storage; | ||
| 41 | return if !$requeue && $redund->{$digest}; | ||
| 42 | return if $self->cap && $self->cap < $self->length; | ||
| 43 | push(@{$self->jobs}, $job); | ||
| 44 | $redund->{$digest} = 1; | ||
| 45 | return $job; | ||
| 46 | } | ||
| 47 | |||
| 48 | 1; | ||
| 49 | |||
| 50 | =head1 NAME | ||
| 51 | |||
| 52 | WWW::Crawler::Mojo::Queue::Memory - Crawler queue with memory | ||
| 53 | |||
| 54 | =head1 SYNOPSIS | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | Crawler queue with memory. | ||
| 59 | |||
| 60 | =head1 ATTRIBUTES | ||
| 61 | |||
| 62 | This class inherits all methods from L<WWW::Crawler::Mojo::Queue> and implements | ||
| 63 | following new ones. | ||
| 64 | |||
| 65 | =head2 cap | ||
| 66 | |||
| 67 | Capacity of queue, indecating how many jobs can be kept in queue at a time. | ||
| 68 | If you enqueue over capacity, the oldest job will be automatically disposed. | ||
| 69 | |||
| 70 | =head2 jobs | ||
| 71 | |||
| 72 | jobs. | ||
| 73 | |||
| 74 | =head2 redundancy_storage | ||
| 75 | |||
| 76 | A hash ref in which the class keeps DONE flags for each jobs | ||
| 77 | in order to avoid to perform resembling jobs multiple times. | ||
| 78 | |||
| 79 | # Mark a job as DONE | ||
| 80 | $queue->redundancy_storage->{$job->digest} = 1; | ||
| 81 | |||
| 82 | # Delete the mark | ||
| 83 | delete($queue->redundancy_storage->{$job->digest}); | ||
| 84 | |||
| 85 | =head1 METHODS | ||
| 86 | |||
| 87 | This class inherits all methods from L<WWW::Crawler::Mojo::Queue> class and | ||
| 88 | implements following new ones. | ||
| 89 | |||
| 90 | =head2 dequeue | ||
| 91 | |||
| 92 | Implementation for L<WWW::Crawler::Mojo::Queue> interface. | ||
| 93 | |||
| 94 | =head2 enqueue | ||
| 95 | |||
| 96 | Implementation for L<WWW::Crawler::Mojo::Queue> interface. | ||
| 97 | |||
| 98 | =head2 length | ||
| 99 | |||
| 100 | Implementation for L<WWW::Crawler::Mojo::Queue> interface. | ||
| 101 | |||
| 102 | =head2 next | ||
| 103 | |||
| 104 | Implementation for L<WWW::Crawler::Mojo::Queue> interface. | ||
| 105 | |||
| 106 | =head2 requeue | ||
| 107 | |||
| 108 | Implementation for L<WWW::Crawler::Mojo::Queue> interface. | ||
| 109 | |||
| 110 | =head2 shuffle | ||
| 111 | |||
| 112 | Implementation for L<WWW::Crawler::Mojo::Queue> interface. | ||
| 113 | |||
| 114 | =head1 AUTHOR | ||
| 115 | |||
| 116 | Keita Sugama, E<lt>sugama@jamadam.comE<gt> | ||
| 117 | |||
| 118 | =head1 COPYRIGHT AND LICENSE | ||
| 119 | |||
| 120 | Copyright (C) Keita Sugama. | ||
| 121 | |||
| 122 | This program is free software; you can redistribute it and/or | ||
| 123 | modify it under the same terms as Perl itself. | ||
| 124 | |||
| 125 | =cut |