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
|
9
|
|
|
9
|
|
942426
|
{ use 5.006; } |
|
9
|
|
|
|
|
32
|
|
80
|
9
|
|
|
9
|
|
46
|
use warnings; |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
283
|
|
81
|
9
|
|
|
9
|
|
42
|
use strict; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
184
|
|
82
|
|
|
|
|
|
|
|
83
|
9
|
|
|
9
|
|
1074
|
use Hash::SharedMem (); |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
562
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
our $VERSION = "0.005"; |
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.25.3 onwards, returns the number of items that are currently |
242
|
|
|
|
|
|
|
in the shared hash. This matches the behaviour of untied hashes on |
243
|
|
|
|
|
|
|
these Perl versions. Prior to Perl 5.25.3, from Perl 5.8.3 onwards, |
244
|
|
|
|
|
|
|
returns a truth value indicating whether |
245
|
|
|
|
|
|
|
there are currently any items in the shared hash. Does not supply any |
246
|
|
|
|
|
|
|
additional information corresponding to the hash bucket usage information |
247
|
|
|
|
|
|
|
that untied hashes supply in this situation. Prior to Perl 5.8.3, |
248
|
|
|
|
|
|
|
returns a meaningless value, due to a limitation of the tying system. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
If the hash is evaluated in a truth value context, with the expectation of |
251
|
|
|
|
|
|
|
this testing whether the shared hash is occupied, there is a performance |
252
|
|
|
|
|
|
|
concern. Prior to Perl 5.25.3 only the truth value would be determined, |
253
|
|
|
|
|
|
|
quite cheaply. From Perl 5.25.3 onwards, a more expensive operation is |
254
|
|
|
|
|
|
|
performed, counting all the keys. If this is a problem, one can evaluate |
255
|
|
|
|
|
|
|
C<< tied(%SHASH)->occupied >> to explicitly invoke the truth-value-only |
256
|
|
|
|
|
|
|
operation. However, if performance is a concern then the tied interface |
257
|
|
|
|
|
|
|
is best entirely avoided. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item scalar(keys(%SHASH)) |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item scalar(values(%SHASH)) |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
Returns the number of items that are currently in the shared hash. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
Due to a limitation of the tying system, the item count is not extracted |
266
|
|
|
|
|
|
|
atomically, but is derived by means equivalent to a loop using C. |
267
|
|
|
|
|
|
|
If the set of keys in the shared hash changes during this process, |
268
|
|
|
|
|
|
|
the count of keys visited (which is what is actually returned) does not |
269
|
|
|
|
|
|
|
necessarily match any state that the shared hash has ever been in. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=item each(%SHASH) |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Iterates over the shared hash. On each call, returns either the next key |
274
|
|
|
|
|
|
|
(in scalar context) or the next key and the value that it references |
275
|
|
|
|
|
|
|
(in list context). The iterator state, preserved between calls, is |
276
|
|
|
|
|
|
|
attached to C<%SHASH>. |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
The iteration process always visits the keys in lexicographical order. |
279
|
|
|
|
|
|
|
Unlike iteration of untied hashes, it is safe to make any changes at all |
280
|
|
|
|
|
|
|
to the shared hash content between calls to C. Subsequent calls |
281
|
|
|
|
|
|
|
see the new content, and the iteration process resumes with whatever key |
282
|
|
|
|
|
|
|
(in the new content) follows the key most recently visited (from the |
283
|
|
|
|
|
|
|
old content). |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
When using C in list context, the fetching of the next key and its |
286
|
|
|
|
|
|
|
corresponding value is not an atomic operation, due to a limitation of the |
287
|
|
|
|
|
|
|
tying system. The key and value are fetched as two separate operations |
288
|
|
|
|
|
|
|
(each one individually atomic), and it is possible for the shared hash |
289
|
|
|
|
|
|
|
content to change between them. This is noticeable if the key that was |
290
|
|
|
|
|
|
|
fetched gets deleted before the value is fetched: it will appear that |
291
|
|
|
|
|
|
|
the value is C, which is not a permitted value in a shared hash. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=item keys(%SHASH) |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=item values(%SHASH) |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=item %SHASH |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
Enumerates the shared hash's content (keys alone, values alone, or |
300
|
|
|
|
|
|
|
keys with values), and as a side effect resets the iterator state used |
301
|
|
|
|
|
|
|
by C. Always returns the content in lexicographical order of key. |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
Due to a limitation of the tying system, the content is not extracted |
304
|
|
|
|
|
|
|
atomically, and so the content returned as a whole does not necessarily |
305
|
|
|
|
|
|
|
match any state that the shared hash has ever been in. The content |
306
|
|
|
|
|
|
|
is extracted by means equivalent to a loop using C, and the |
307
|
|
|
|
|
|
|
inconsistencies that may be seen follow therefrom. |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=item %SHASH = LIST |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
Setting the entire content of the shared hash (throwing away the previous |
312
|
|
|
|
|
|
|
content) is not supported. |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=back |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head1 BUGS |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Due to details of the Perl implementation, this object-oriented interface |
319
|
|
|
|
|
|
|
to the shared hash mechanism is somewhat slower than the function |
320
|
|
|
|
|
|
|
interface, and the tied interface is much slower. The functions in |
321
|
|
|
|
|
|
|
L are the recommended interface. |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
Limitations of the tying system mean that whole-hash operations (including |
324
|
|
|
|
|
|
|
iteration and enumeration) performed on shared hashes via the tied |
325
|
|
|
|
|
|
|
interface are not as atomic as they appear. If it is necessary to see |
326
|
|
|
|
|
|
|
a consistent state of a shared hash, one must create and use a snapshot |
327
|
|
|
|
|
|
|
handle. A snapshot may be iterated over or enumerated at leisure via |
328
|
|
|
|
|
|
|
any of the interfaces. |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=head1 SEE ALSO |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
L |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
=head1 AUTHOR |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
Andrew Main (Zefram) |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
=head1 COPYRIGHT |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
Copyright (C) 2014, 2015 PhotoBox Ltd |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
Copyright (C) 2014, 2015, 2017 Andrew Main (Zefram) |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
=head1 LICENSE |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
347
|
|
|
|
|
|
|
under the same terms as Perl itself. |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
=cut |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
1; |