line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Hash::SharedMem::Handle - handle for efficient shared mutable hash |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Hash::SharedMem::Handle; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
if(Hash::SharedMem::Handle->referential_handle) { ... |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$shash = Hash::SharedMem::Handle->open($filename, "rwc"); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
if($shash->is_readable) { ... |
14
|
|
|
|
|
|
|
if($shash->is_writable) { ... |
15
|
|
|
|
|
|
|
$mode = $shash->mode; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
if($shash->exists($key)) { ... |
18
|
|
|
|
|
|
|
$length = $shash->length($key); |
19
|
|
|
|
|
|
|
$value = $shash->get($key); |
20
|
|
|
|
|
|
|
$shash->set($key, $newvalue); |
21
|
|
|
|
|
|
|
$oldvalue = $shash->gset($key, $newvalue); |
22
|
|
|
|
|
|
|
if($shash->cset($key, $chkvalue, $newvalue)) { ... |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
if($shash->occupied) { ... |
25
|
|
|
|
|
|
|
$count = $shash->count; |
26
|
|
|
|
|
|
|
$size = $shash->size; |
27
|
|
|
|
|
|
|
$key = $shash->key_min; |
28
|
|
|
|
|
|
|
$key = $shash->key_max; |
29
|
|
|
|
|
|
|
$key = $shash->key_ge($key); |
30
|
|
|
|
|
|
|
$key = $shash->key_gt($key); |
31
|
|
|
|
|
|
|
$key = $shash->key_le($key); |
32
|
|
|
|
|
|
|
$key = $shash->key_lt($key); |
33
|
|
|
|
|
|
|
$keys = $shash->keys_array; |
34
|
|
|
|
|
|
|
$keys = $shash->keys_hash; |
35
|
|
|
|
|
|
|
$group = $shash->group_get_hash; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$snap_shash = $shash->snapshot; |
38
|
|
|
|
|
|
|
if($shash->is_snapshot) { ... |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$shash->idle; |
41
|
|
|
|
|
|
|
$shash->tidy; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$tally = $shash->tally_get; |
44
|
|
|
|
|
|
|
$shash->tally_zero; |
45
|
|
|
|
|
|
|
$tally = $shash->tally_gzero; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
tie %shash, "Hash::SharedMem::Handle", $shash; |
48
|
|
|
|
|
|
|
tie %shash, "Hash::SharedMem::Handle", $filename, "rwc"; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$shash = tied(%shash); |
51
|
|
|
|
|
|
|
if(exists($shash{$key})) { ... |
52
|
|
|
|
|
|
|
$value = $shash{$key}; |
53
|
|
|
|
|
|
|
$shash{$key} = $newvalue; |
54
|
|
|
|
|
|
|
$oldvalue = delete($shash{$key}); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 DESCRIPTION |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
An object of this class is a handle referring to a memory-mapped shared |
59
|
|
|
|
|
|
|
hash object of the kind described in L. It can be |
60
|
|
|
|
|
|
|
passed to the functions of that module, or the same operations can be |
61
|
|
|
|
|
|
|
performed by calling the methods described below. Uses of the function |
62
|
|
|
|
|
|
|
and method interfaces may be intermixed arbitrarily; they are completely |
63
|
|
|
|
|
|
|
equivalent in function. They are not equivalent in performance, however, |
64
|
|
|
|
|
|
|
with the method interface being somewhat slower. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
This class also supplies a tied-hash interface to shared hashes. The tied |
67
|
|
|
|
|
|
|
interface is much slower than the function and method interfaces. |
68
|
|
|
|
|
|
|
The behaviour of a tied hash more resembles the function and method |
69
|
|
|
|
|
|
|
interfaces to shared hashes than it resembles the syntactically-similar |
70
|
|
|
|
|
|
|
use of ordinary Perl hashes. Using a non-string as a key will result |
71
|
|
|
|
|
|
|
in an exception, rather than stringification of the key. Using a |
72
|
|
|
|
|
|
|
string containing a non-octet codepoint as a key will also result in an |
73
|
|
|
|
|
|
|
exception, rather than merely referring to an absent hash element. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
package Hash::SharedMem::Handle; |
78
|
|
|
|
|
|
|
|
79
|
8
|
|
|
8
|
|
721962
|
{ use 5.006; } |
|
8
|
|
|
|
|
22
|
|
|
8
|
|
|
|
|
307
|
|
80
|
8
|
|
|
8
|
|
37
|
use warnings; |
|
8
|
|
|
|
|
11
|
|
|
8
|
|
|
|
|
214
|
|
81
|
8
|
|
|
8
|
|
31
|
use strict; |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
236
|
|
82
|
|
|
|
|
|
|
|
83
|
8
|
|
|
8
|
|
956
|
use Hash::SharedMem (); |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
554
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
our $VERSION = "0.004"; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 CLASS METHODS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=over |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=item Hash::SharedMem::Handle->referential_handle |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Returns a truth value indicating whether each shared hash handle |
94
|
|
|
|
|
|
|
contains a first-class reference to the shared hash to which it refers. |
95
|
|
|
|
|
|
|
See L for discussion |
96
|
|
|
|
|
|
|
of the significance of this. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=back |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=over |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item Hash::SharedMem::Handle->open(FILENAME, MODE) |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Opens and returns a handle referring to a shared hash object, |
107
|
|
|
|
|
|
|
or Cs if the shared hash can't be opened as specified. |
108
|
|
|
|
|
|
|
See L for details. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=back |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 METHODS |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=over |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item $shash->is_readable |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item $shash->is_writable |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item $shash->mode |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item $shash->exists(KEY) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item $shash->getd(KEY) |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item $shash->length(KEY) |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item $shash->get(KEY) |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item $shash->set(KEY, NEWVALUE) |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item $shash->gset(KEY, NEWVALUE) |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item $shash->cset(KEY, CHKVALUE, NEWVALUE) |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item $shash->occupied |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item $shash->count |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item $shash->size |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item $shash->key_min |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item $shash->key_max |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item $shash->key_ge(KEY) |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item $shash->key_gt(KEY) |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item $shash->key_le(KEY) |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item $shash->key_lt(KEY) |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item $shash->keys_array |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item $shash->keys_hash |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item $shash->group_get_hash |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item $shash->snapshot |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item $shash->is_snapshot |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item $shash->idle |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item $shash->tidy |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item $shash->tally_get |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item $shash->tally_zero |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item $shash->tally_gzero |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
These methods are each equivalent to the corresponding |
175
|
|
|
|
|
|
|
"C"-prefixed function in L. See that document |
176
|
|
|
|
|
|
|
for details. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=back |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 TIE CONSTRUCTORS |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=over |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item tie(VARIABLE, "Hash::SharedMem::Handle", SHASH) |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
I must be a hash variable, and I must be a handle |
187
|
|
|
|
|
|
|
referring to a shared hash object. The call binds the variable to the |
188
|
|
|
|
|
|
|
shared hash, so that the variable provides a view of the shared hash |
189
|
|
|
|
|
|
|
that resembles an ordinary Perl hash. The shared hash handle is returned. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item tie(VARIABLE, "Hash::SharedMem::Handle", FILENAME, MODE) |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
I must be a hash variable. The call opens a handle referring |
194
|
|
|
|
|
|
|
to a shared hash object, as described in L, |
195
|
|
|
|
|
|
|
and binds the variable to the shared hash, so that the variable provides a |
196
|
|
|
|
|
|
|
view of the shared hash that resembles an ordinary Perl hash. The shared |
197
|
|
|
|
|
|
|
hash handle is returned. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=back |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head1 TIED OPERATORS |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
For all of these operators, the key of interest (I parameter) |
204
|
|
|
|
|
|
|
and values can each be any octet (Latin-1) string. Strings containing |
205
|
|
|
|
|
|
|
non-octets (Unicode characters above U+FF) and items other than strings |
206
|
|
|
|
|
|
|
cannot be used as keys or values. If a dualvar (scalar with independent |
207
|
|
|
|
|
|
|
string and numeric values) is supplied, only its string value will |
208
|
|
|
|
|
|
|
be used. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=over |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item tied(%SHASH) |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Returns the handle via which I<%SHASH> is bound to the shared hash. |
215
|
|
|
|
|
|
|
This is a shared hash handle that can be used by calling the methods |
216
|
|
|
|
|
|
|
described above or by passing it to the functions of L. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=item exists($SHASH{KEY}) |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Returns a truth value indicating whether the specified key is currently |
221
|
|
|
|
|
|
|
present in the shared hash. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item $SHASH{KEY} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Returns the value currently referenced by the specified key in the shared |
226
|
|
|
|
|
|
|
hash, or C if the key is absent. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item $SHASH{KEY} = NEWVALUE |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Modifies the shared hash so that the specified key henceforth references |
231
|
|
|
|
|
|
|
the specified value. The new value must be a string. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item delete($SHASH{KEY}) |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Modifies the shared hash so that the specified key is henceforth absent, |
236
|
|
|
|
|
|
|
and returns the value that the key previously referenced, or C |
237
|
|
|
|
|
|
|
if the key was already absent. This swap is performed atomically. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=item scalar(%SHASH) |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
From Perl 5.8.3 onwards, returns a truth value indicating whether |
242
|
|
|
|
|
|
|
there are currently any items in the shared hash. Does not supply any |
243
|
|
|
|
|
|
|
additional information corresponding to the hash bucket usage information |
244
|
|
|
|
|
|
|
that untied hashes supply in this situation. Prior to Perl 5.8.3, |
245
|
|
|
|
|
|
|
returns a meaningless value, due to a limitation of the tying system. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item scalar(keys(%SHASH)) |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=item scalar(values(%SHASH)) |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Returns the number of items that are currently in the shared hash. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Due to a limitation of the tying system, the item count is not extracted |
254
|
|
|
|
|
|
|
atomically, but is derived by means equivalent to a loop using C. |
255
|
|
|
|
|
|
|
If the set of keys in the shared hash changes during this process, |
256
|
|
|
|
|
|
|
the count of keys visited (which is what is actually returned) does not |
257
|
|
|
|
|
|
|
necessarily match any state that the shared hash has ever been in. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item each(%SHASH) |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
Iterates over the shared hash. On each call, returns either the next key |
262
|
|
|
|
|
|
|
(in scalar context) or the next key and the value that it references |
263
|
|
|
|
|
|
|
(in list context). The iterator state, preserved between calls, is |
264
|
|
|
|
|
|
|
attached to C<%SHASH>. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
The iteration process always visits the keys in lexicographical order. |
267
|
|
|
|
|
|
|
Unlike iteration of untied hashes, it is safe to make any changes at all |
268
|
|
|
|
|
|
|
to the shared hash content between calls to C. Subsequent calls |
269
|
|
|
|
|
|
|
see the new content, and the iteration process resumes with whatever key |
270
|
|
|
|
|
|
|
(in the new content) follows the key most recently visited (from the |
271
|
|
|
|
|
|
|
old content). |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
When using C in list context, the fetching of the next key and its |
274
|
|
|
|
|
|
|
corresponding value is not an atomic operation, due to a limitation of the |
275
|
|
|
|
|
|
|
tying system. The key and value are fetched as two separate operations |
276
|
|
|
|
|
|
|
(each one individually atomic), and it is possible for the shared hash |
277
|
|
|
|
|
|
|
content to change between them. This is noticeable if the key that was |
278
|
|
|
|
|
|
|
fetched gets deleted before the value is fetched: it will appear that |
279
|
|
|
|
|
|
|
the value is C, which is not a permitted value in a shared hash. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=item keys(%SHASH) |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=item values(%SHASH) |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=item %SHASH |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Enumerates the shared hash's content (keys alone, values alone, or |
288
|
|
|
|
|
|
|
keys with values), and as a side effect resets the iterator state used |
289
|
|
|
|
|
|
|
by C. Always returns the content in lexicographical order of key. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Due to a limitation of the tying system, the content is not extracted |
292
|
|
|
|
|
|
|
atomically, and so the content returned as a whole does not necessarily |
293
|
|
|
|
|
|
|
match any state that the shared hash has ever been in. The content |
294
|
|
|
|
|
|
|
is extracted by means equivalent to a loop using C, and the |
295
|
|
|
|
|
|
|
inconsistencies that may be seen follow therefrom. |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=item %SHASH = LIST |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
Setting the entire content of the shared hash (throwing away the previous |
300
|
|
|
|
|
|
|
content) is not supported. |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=back |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head1 BUGS |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
Due to details of the Perl implementation, this object-oriented interface |
307
|
|
|
|
|
|
|
to the shared hash mechanism is somewhat slower than the function |
308
|
|
|
|
|
|
|
interface, and the tied interface is much slower. The functions in |
309
|
|
|
|
|
|
|
L are the recommended interface. |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
Limitations of the tying system mean that whole-hash operations (including |
312
|
|
|
|
|
|
|
iteration and enumeration) performed on shared hashes via the tied |
313
|
|
|
|
|
|
|
interface are not as atomic as they appear. If it is necessary to see |
314
|
|
|
|
|
|
|
a consistent state of a shared hash, one must create and use a snapshot |
315
|
|
|
|
|
|
|
handle. A snapshot may be iterated over or enumerated at leisure via |
316
|
|
|
|
|
|
|
any of the interfaces. |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=head1 SEE ALSO |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
L |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=head1 AUTHOR |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
Andrew Main (Zefram) |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=head1 COPYRIGHT |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
Copyright (C) 2014, 2015 PhotoBox Ltd |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
Copyright (C) 2014, 2015 Andrew Main (Zefram) |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
=head1 LICENSE |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
335
|
|
|
|
|
|
|
under the same terms as Perl itself. |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=cut |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
1; |