line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Stack::Persistent; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
37322
|
use 5.008; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
50
|
|
4
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
42
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
5
|
use File::Spec; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
8
|
1
|
|
|
1
|
|
388
|
use Cache::FastMmap; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(); |
11
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
14
|
|
|
|
|
|
|
# Public Methods |
15
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
18
|
|
|
|
|
|
|
my $proto = shift; |
19
|
|
|
|
|
|
|
my %params = @_; |
20
|
|
|
|
|
|
|
my $class = ref($proto) || $proto; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $self = {}; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$self->{handle} = undef; |
25
|
|
|
|
|
|
|
$self->{initialize} = 0; |
26
|
|
|
|
|
|
|
$self->{num_pages} = 64; |
27
|
|
|
|
|
|
|
$self->{page_size} = "64k"; |
28
|
|
|
|
|
|
|
$self->{expiration} = 0; |
29
|
|
|
|
|
|
|
$self->{cachefile} = File::Spec->catfile('tmp', 'stack-persistent.cache'); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
bless($self, $class); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my ($k, $v); |
34
|
|
|
|
|
|
|
local $_; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
if (defined($v = delete $params{'-initialize'})) { |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$self->{initialize} = $v; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
if (defined($v = delete $params{'-pages'})) { |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$self->{num_pages} = $v; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
if (defined($v = delete $params{'-size'})) { |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$self->{page_size} = $v; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if (defined($v = delete $params{'-expiration'})) { |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$self->{expiration} = $v; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
if (defined($v = delete $params{'-filename'})) { |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$self->{cachefile} = $v; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$self->{handle} = Cache::FastMmap->new(init_file => $self->{initialize}, |
67
|
|
|
|
|
|
|
num_pages => $self->{num_pages}, |
68
|
|
|
|
|
|
|
page_size => $self->{page_size}, |
69
|
|
|
|
|
|
|
expire_time => $self->{expiration}, |
70
|
|
|
|
|
|
|
share_file => $self->{cachefile}, |
71
|
|
|
|
|
|
|
unlink_on_exit => 0); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$self->{handle}->purge(); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
return $self; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub push { |
80
|
|
|
|
|
|
|
my ($self, $stack, $data) = @_; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my ($ckey, $skey, $counter); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$skey = $stack . ':counter'; |
85
|
|
|
|
|
|
|
$counter = $self->{handle}->get($skey) || 0; |
86
|
|
|
|
|
|
|
$self->{handle}->remove($skey); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
$counter++; |
89
|
|
|
|
|
|
|
$ckey = $stack . ':' . $counter; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
$self->{handle}->set($ckey, $data); |
92
|
|
|
|
|
|
|
$self->{handle}->set($skey, $counter); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub pop { |
97
|
|
|
|
|
|
|
my ($self, $stack) = @_; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my ($ckey, $skey, $data, $counter); |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
$skey = $stack . ':counter'; |
102
|
|
|
|
|
|
|
$counter = $self->{handle}->get($skey) || 0; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$ckey = $stack . ':' . $counter; |
105
|
|
|
|
|
|
|
$data = $self->{handle}->get($ckey); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
$self->{handle}->remove($skey); |
108
|
|
|
|
|
|
|
$self->{handle}->remove($ckey); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$counter--; |
111
|
|
|
|
|
|
|
$self->{handle}->set($skey, $counter); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
return($data); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub peek { |
118
|
|
|
|
|
|
|
my ($self, $stack) = @_; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my ($ckey, $skey, $data, $counter); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$skey = $stack . ':counter'; |
123
|
|
|
|
|
|
|
$counter = $self->{handle}->get($skey) || 0; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$ckey = $stack . ':' . $counter; |
126
|
|
|
|
|
|
|
$data = $self->{handle}->get($ckey); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
return($data); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub items { |
133
|
|
|
|
|
|
|
my ($self, $stack) = @_; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my ($skey, $counter); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$skey = $stack . ':counter'; |
138
|
|
|
|
|
|
|
$counter = $self->{handle}->get($skey) || 0; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
return($counter); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub clear { |
145
|
|
|
|
|
|
|
my ($self, $stack) = @_; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
my ($skey, $ckey, $counter); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
$skey = $stack . ':counter'; |
150
|
|
|
|
|
|
|
$counter = $self->{handle}->get($skey) || 0; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
for (; $counter > 0; $counter--) { |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
$ckey = $stack . ':' . $counter; |
155
|
|
|
|
|
|
|
$self->{handle}->remove($ckey); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
$self->{handle}->set($skey, $counter); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub dump { |
164
|
|
|
|
|
|
|
my ($self, $stack) = @_; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
my $item; |
167
|
|
|
|
|
|
|
my @items = $self->{handle}->get_keys(2); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
foreach $item (@items) { |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
if ($stack eq substr($item->{key}, 0, index($item->{key}, ':'))) { |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
printf("key: %s; last_access: %s; expiration: %s; flags: %s\n", |
174
|
|
|
|
|
|
|
$item->{key}, $item->{last_access}, $item->{expire_time}, |
175
|
|
|
|
|
|
|
$item->{flags}); |
176
|
|
|
|
|
|
|
printf(" value: %s\n", $item->{value}); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
return 0; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub handle { |
187
|
|
|
|
|
|
|
my $self = shift; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
return($self->{handle}); |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
1; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
__END__ |