reverse r4290 since we -do- -not- currently want these namespaces indexed
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema / QueryInterval.pm
1 package DBIx::Class::Schema::QueryInterval;
2
3 use Moose;
4
5 =head1 NAME
6
7 DBIx::Class::Schema::Role::QueryInterval; Defines a job control interval.
8
9 =head1 SYNOPSIS
10
11 The following example shows how to define a job control interval and assign it
12 to a particular L<DBIx::Class::Schema::Job> for a L<DBIx::Class::Schema>
13
14     my $job = DBIx::Class::Schema->new(runs => sub { print 'did job'});
15     my $interval = DBIx::Class::Schema::Interval->new(every => 10);
16     
17     if($interval->matches($query_count)) {
18         print "I indentified the query count as matching";
19     }
20     
21     ## $schema->isa(DBIx::Class::Schema);
22     $schema->create_and_add_at_query_intervals($interval => $job);
23     
24 =head1 DESCRIPTION
25
26 A Query Interval defines a reoccuring period based on the query count from a
27 given offset.  For example, you can define a query interval of 10 queries
28 with an offset of 1 query.  This interval identifies query number 11, 21, 31,
29 and so on.
30
31 =head1 ATTRIBUTES
32
33 This package defines the following attributes.
34
35 =head2 every (Int)
36
37 This is the 'size' of the gap identifying a query as matching a particular
38 interval.  Think, "I match every X queries".
39
40 =cut
41
42 has 'every' => (
43   is=>'ro',
44   isa=>'Int',
45   required=>1,
46 );
47
48
49 =head2 offset (Int)
50
51 This is a number of queries from the start of all queries to offset the match
52 counting mechanism.  This is basically added to the L</every> attribute to 
53 identify a query as matching the interval we wish to define.
54
55 =cut
56
57 has 'offset' => (
58   is=>'ro',
59   isa=>'Int',
60   required=>1,
61   default=>0,
62 );
63
64
65 =head1 METHODS
66
67 This module defines the following methods.
68
69 =head2 matches ($query_count)
70
71 Does the $query_count match the defined interval?  Returns a Boolean.
72
73 =cut
74
75 sub matches {
76   my ($self, $query_count) = @_;
77   my $offset_count = $query_count - $self->offset;
78   return $offset_count % $self->every ? 0:1;
79 }
80
81
82 =head1 AUTHORS
83
84 See L<DBIx::Class> for more information regarding authors.
85
86 =head1 LICENSE
87
88 You may distribute this code under the same terms as Perl itself.
89
90 =cut
91
92
93 1;