lib/Data/Valve/BucketStore/Object.pm

filter: Statementstmt 16.3% 9 / 55 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 3 / 6 Podpod n/a totaltotal 14.8% 12 / 81 CC 11 SCAR 30.5
CC
11
Cov
12%
fill
30.0
try_push
30.0
reset
12.0
CRAP 21.2
↑ Summary lib/Data/Valve.pm »
1 # $Id: /mirror/coderepos/lang/perl/Data-Valve/trunk/lib/Data/Valve/BucketStore/Object.pm 86989 2008-10-01T17:20:18.893695Z daisuke $
2  
3 package Data::Valve::BucketStore::Object;
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 with 'Data::Valve::BucketStore';
8 with 'MooseX::KeyedMutex';
9  
10 # this is the storage object. it must support get()/set()
11 subtype 'Data::Valve::BucketStore::Object::StorageObject'
12 => as 'Object'
13 => where { $_->can('get') && $_->can('set') }
14 ;
15  
16 has 'store' => (
17 is => 'rw',
18 isa => 'Data::Valve::BucketStore::Object::StorageObject',
19 required => 1
20 );
21  
22 __PACKAGE__->meta->make_immutable;
23  
24 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 0 ▶sub reset {
Subroutine reset: not called CC=3 SCAR=24.8
27 0 my ($self, %args) = @_;
28 0 my $key = $args{key};
29  
30 0 my $rv;
31 0 my $done = 0;
32 0 my $store = $self->store;
33 0 ▶ while ( ! $done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
34 0 my $lock = $self->lock($key);
35 0 ▶ next unless $lock;
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 0 0 0
36  
37 0 $done = 1;
38 0 $rv = $store->remove($key);
39 }
40  
41 0 return $rv;
42 }
43  
44 0 ▶sub fill {
Subroutine fill: not called CC=5 SCAR=34.0
45 0 my ($self, %args) = @_;
46  
47 0 my $key = $args{key};
48  
49 0 my $rv;
50 0 my $done = 0;
51 0 my $store = $self->store;
52 0 ▶ while ( ! $done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
53 0 my $lock = $self->lock($key);
54 0 ▶ next unless $lock;
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 0 0 0
55  
56 0 $done = 1;
57 0 my $bucket_source = $store->get($key);
58 0 my $bucket;
59 0 ▶ if ($bucket_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket_source) { } 0 0 0
60 0 $bucket = Data::Valve::Bucket->deserialize($bucket_source, $self->interval, $self->max_items, $self->strict_interval);
61 } else {
62 0 $bucket = Data::Valve::Bucket->new(
63 interval => $self->interval,
64 max_items => $self->max_items,
65 strict_interval => $self->strict_interval
66 );
67 }
68  
69 0 ▶ 1 while ( $bucket->try_push() );
Branch: 1 branch
BranchTrueFalseTotal
$bucket->try_push and do { '???' } 0 0 0
70 0 $store->set($key, $bucket->serialize);
71 }
72  
73 0 return $rv;
74 }
75  
76 0 ▶sub try_push {
Subroutine try_push: not called CC=5 SCAR=34.0
77 0 my ($self, %args) = @_;
78  
79 0 my $key = $args{key};
80  
81 0 my $rv;
82 0 my $done = 0;
83 0 my $store = $self->store;
84 0 ▶ while ( ! $done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
85 0 my $lock = $self->lock($key);
86 0 ▶ next unless $lock;
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 0 0 0
87  
88 0 $done = 1;
89 0 my $bucket_source = $store->get($key);
90 0 my $bucket;
91 0 ▶ if ($bucket_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket_source) { } 0 0 0
92 0 $bucket = Data::Valve::Bucket->deserialize($bucket_source, $self->interval, $self->max_items, $self->strict_interval);
93 } else {
94 0 $bucket = Data::Valve::Bucket->new(
95 interval => $self->interval,
96 max_items => $self->max_items,
97 strict_interval => $self->strict_interval
98 );
99 }
100 0 $rv = $bucket->try_push();
101
102 # we only need to set if the value has changed, i.e., the throttle
103 # was successful
104 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 0 0
105 0 $store->set($key, $bucket->serialize);
106 }
107 }
108  
109 0 return $rv;
110 }
111  
112 1;
113  
114 __END__
↑ Summary lib/Data/Valve.pm »