line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
1
|
|
2135
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
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
|
|
|
|
|
|
|
no Moose; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub reset { |
27
|
|
|
|
|
|
|
my ($self, %args) = @_; |
28
|
|
|
|
|
|
|
my $key = $args{key}; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $rv; |
31
|
|
|
|
|
|
|
my $done = 0; |
32
|
|
|
|
|
|
|
my $store = $self->store; |
33
|
|
|
|
|
|
|
while ( ! $done) { |
34
|
|
|
|
|
|
|
my $lock = $self->lock($key); |
35
|
|
|
|
|
|
|
next unless $lock; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$done = 1; |
38
|
|
|
|
|
|
|
$rv = $store->remove($key); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
return $rv; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub fill { |
45
|
|
|
|
|
|
|
my ($self, %args) = @_; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $key = $args{key}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $rv; |
50
|
|
|
|
|
|
|
my $done = 0; |
51
|
|
|
|
|
|
|
my $store = $self->store; |
52
|
|
|
|
|
|
|
while ( ! $done) { |
53
|
|
|
|
|
|
|
my $lock = $self->lock($key); |
54
|
|
|
|
|
|
|
next unless $lock; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$done = 1; |
57
|
|
|
|
|
|
|
my $bucket_source = $store->get($key); |
58
|
|
|
|
|
|
|
my $bucket; |
59
|
|
|
|
|
|
|
if ($bucket_source) { |
60
|
|
|
|
|
|
|
$bucket = Data::Valve::Bucket->deserialize($bucket_source, $self->interval, $self->max_items, $self->strict_interval); |
61
|
|
|
|
|
|
|
} else { |
62
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
1 while ( $bucket->try_push() ); |
70
|
|
|
|
|
|
|
$store->set($key, $bucket->serialize); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
return $rv; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub try_push { |
77
|
|
|
|
|
|
|
my ($self, %args) = @_; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $key = $args{key}; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $rv; |
82
|
|
|
|
|
|
|
my $done = 0; |
83
|
|
|
|
|
|
|
my $store = $self->store; |
84
|
|
|
|
|
|
|
while ( ! $done) { |
85
|
|
|
|
|
|
|
my $lock = $self->lock($key); |
86
|
|
|
|
|
|
|
next unless $lock; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
$done = 1; |
89
|
|
|
|
|
|
|
my $bucket_source = $store->get($key); |
90
|
|
|
|
|
|
|
my $bucket; |
91
|
|
|
|
|
|
|
if ($bucket_source) { |
92
|
|
|
|
|
|
|
$bucket = Data::Valve::Bucket->deserialize($bucket_source, $self->interval, $self->max_items, $self->strict_interval); |
93
|
|
|
|
|
|
|
} else { |
94
|
|
|
|
|
|
|
$bucket = Data::Valve::Bucket->new( |
95
|
|
|
|
|
|
|
interval => $self->interval, |
96
|
|
|
|
|
|
|
max_items => $self->max_items, |
97
|
|
|
|
|
|
|
strict_interval => $self->strict_interval |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
$rv = $bucket->try_push(); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# we only need to set if the value has changed, i.e., the throttle |
103
|
|
|
|
|
|
|
# was successful |
104
|
|
|
|
|
|
|
if ($rv) { |
105
|
|
|
|
|
|
|
$store->set($key, $bucket->serialize); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
return $rv; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 NAME |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Data::Valve::BucketStore::Object - Basic Object Storage |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 SYNOPSIS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my $store = Data::Valve::BucketStore::Object->new( |
123
|
|
|
|
|
|
|
store => $object, |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 DESCRIPTION |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
This storage type only needs an object which supports a get()/set() methods |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 METHODS |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 fill |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 reset |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 try_push |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |