File Coverage

blib/lib/Spread/Queue/FIFO.pm
Criterion Covered Total %
statement 32 42 76.1
branch 2 4 50.0
condition 1 3 33.3
subroutine 9 12 75.0
pod 4 7 57.1
total 48 68 70.5


line stmt bran cond sub pod time code
1             package Spread::Queue::FIFO;
2              
3             require 5.005_03;
4 1     1   14322 use strict;
  1         2  
  1         31  
5 1     1   4 use vars qw($VERSION);
  1         1  
  1         57  
6             $VERSION = '0.3';
7              
8 1     1   764 use Time::HiRes qw( time ); # for queue pending time metrics
  1         1616  
  1         4  
9 1     1   920 use Log::Channel;
  1         3395835  
  1         91  
10              
11             =head1 NAME
12              
13             Spread::Queue::FIFO - basic FIFO queue
14              
15             =head1 SYNOPSIS
16              
17             use Spread::Queue::FIFO;
18              
19             my $q = new Spread::Queue::FIFO ("to-do list");
20             enqueue $q, "eat breakfast", "go to work";
21             my $task = dequeue $q;
22              
23             =head1 DESCRIPTION
24              
25             Basic FIFO queue service. Not thread-safe.
26              
27             Logging via Log::Channel.
28              
29             =head1 METHODS
30              
31             =cut
32              
33             BEGIN {
34 1     1   9 my $qlog = new Log::Channel;
35 2     2 0 26 sub qlog { $qlog->(@_) }
36             }
37              
38             =item B
39              
40             my $q = new Spread::Queue::FIFO ("to-do list");
41              
42             Creates a named FIFO queue. Name will be included in each log message.
43              
44             =cut
45              
46             sub new {
47 1     1 1 13 my $proto = shift;
48 1   33     10 my $class = ref ($proto) || $proto;
49              
50 1         2 my $self = {};
51 1         3 bless ($self, $class);
52              
53 1         8 $self->{NAME} = shift; # optional
54 1         3 $self->{QUEUE} = [];
55              
56 1         3 return $self;
57             }
58              
59             =item B
60              
61             enqueue $q, "eat breakfast", "go to work";
62              
63             Append one or more items to the end of a queue.
64              
65             =cut
66              
67             sub enqueue {
68 1     1 1 6 my $self = shift;
69              
70 1         8 qlog "enqueue $self->{NAME}\n";
71              
72 1         6 push (@{$self->{QUEUE}},
  1         9  
73             [ shift, time ]
74             );
75             }
76              
77             =item B
78              
79             my $node = dequeue $q;
80             my ($queue_pending_time, $task) = @$node;
81              
82             Remove the first item from the front of the queue and return it.
83              
84             =cut
85              
86             sub dequeue {
87 1     1 1 100225 my $self = shift;
88              
89 1         23 qlog "dequeue $self->{NAME}\n";
90              
91 1         16 my $node = shift @{$self->{QUEUE}};
  1         8  
92              
93 1 50       14 return if !$node;
94              
95 1         8 my ($data, $timestamp) = @$node;
96             return wantarray ?
97 1 50       28 ( $node->[0], time - $node->[1] )
98             : $node->[0];
99             }
100              
101             =item B
102              
103             my $tasks = $q->pending;
104              
105             Retrieve number of items in the queue.
106              
107             =cut
108              
109             sub pending {
110 0     0 1   my $self = shift;
111              
112 0           return scalar @{$self->{QUEUE}};
  0            
113             }
114              
115             =item B
116              
117             foreach my $item ($q->all) { ... }
118              
119             Return the queue contents as a list, for inspection.
120              
121             =cut
122              
123             sub all {
124 0     0 0   my $self = shift;
125              
126 0           return map { $_->[0] } @{$self->{QUEUE}};
  0            
  0            
127             }
128              
129             sub length {
130 0     0 0   my $self = shift;
131              
132 0           return scalar(@{$self->{QUEUE}});
  0            
133             }
134              
135             1;
136              
137              
138             =head1 AUTHOR
139              
140             Jason W. May
141              
142             =head1 COPYRIGHT
143              
144             Copyright (C) 2002 Jason W. May. All rights reserved.
145             This module is free software; you can redistribute it and/or
146             modify it under the same terms as Perl itself.
147              
148             =head1 SEE ALSO
149              
150             L
151              
152             =cut