File Coverage

blib/lib/Hash/SharedMem/Handle.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


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->getd($key)) { ...
18             $value = $shash->get($key);
19             $shash->set($key, $newvalue);
20             $oldvalue = $shash->gset($key, $newvalue);
21             if($shash->cset($key, $chkvalue, $newvalue)) { ...
22              
23             $snap_shash = $shash->snapshot;
24             if($shash->is_snapshot) { ...
25              
26             $shash->idle;
27             $shash->tidy;
28              
29             $tally = $shash->tally_get;
30             $shash->tally_zero;
31             $tally = $shash->tally_gzero;
32              
33             tie %shash, "Hash::SharedMem::Handle", $shash;
34             tie %shash, "Hash::SharedMem::Handle", $filename, "rwc";
35              
36             $shash = tied(%shash);
37             if(exists($shash{$key})) { ...
38             $value = $shash{$key};
39             $shash{$key} = $newvalue;
40             $oldvalue = delete($shash{$key});
41              
42             =head1 DESCRIPTION
43              
44             An object of this class is a handle referring to a memory-mapped shared
45             hash object of the kind described in L. It can be
46             passed to the functions of that module, or the same operations can be
47             performed by calling the methods described below. Uses of the function
48             and method interfaces may be intermixed arbitrarily; they are completely
49             equivalent in function. They are not equivalent in performance, however,
50             with the method interface being somewhat slower.
51              
52             This class also supplies a tied-hash interface to shared hashes. The tied
53             interface is much slower than the function and method interfaces.
54             The behaviour of a tied hash more resembles the function and method
55             interfaces to shared hashes than it resembles the syntactically-similar
56             use of ordinary Perl hashes. Using a non-string as a key will result
57             in an exception, rather than stringification of the key. Using a
58             string containing a non-octet codepoint as a key will also result in an
59             exception, rather than merely referring to an absent hash element.
60              
61             =cut
62              
63             package Hash::SharedMem::Handle;
64              
65 8     8   638356 { use 5.006; }
  8         32  
  8         400  
66 8     8   45 use warnings;
  8         23  
  8         297  
67 8     8   48 use strict;
  8         15  
  8         301  
68              
69 8     8   2109 use Hash::SharedMem ();
  8         21  
  8         585  
70              
71             our $VERSION = "0.003";
72              
73             =head1 CLASS METHODS
74              
75             =over
76              
77             =item Hash::SharedMem::Handle->referential_handle
78              
79             Returns a truth value indicating whether each shared hash handle
80             contains a first-class reference to the shared hash to which it refers.
81             See L for discussion
82             of the significance of this.
83              
84             =back
85              
86             =head1 CONSTRUCTOR
87              
88             =over
89              
90             =item Hash::SharedMem::Handle->open(FILENAME, MODE)
91              
92             Opens and return a handle referring to a shared hash object,
93             or Cs if the shared hash can't be opened as specified.
94             See L for details.
95              
96             =back
97              
98             =head1 METHODS
99              
100             =over
101              
102             =item $shash->is_readable
103              
104             =item $shash->is_writable
105              
106             =item $shash->mode
107              
108             =item $shash->getd(KEY)
109              
110             =item $shash->get(KEY)
111              
112             =item $shash->set(KEY, NEWVALUE)
113              
114             =item $shash->gset(KEY, NEWVALUE)
115              
116             =item $shash->cset(KEY, CHKVALUE, NEWVALUE)
117              
118             =item $shash->snapshot
119              
120             =item $shash->is_snapshot
121              
122             =item $shash->idle
123              
124             =item $shash->tidy
125              
126             =item $shash->tally_get
127              
128             =item $shash->tally_zero
129              
130             =item $shash->tally_gzero
131              
132             These methods are each equivalent to the corresponding
133             "C"-prefixed function in L. See that document
134             for details.
135              
136             =back
137              
138             =head1 TIE CONSTRUCTORS
139              
140             =over
141              
142             =item tie(VARIABLE, "Hash::SharedMem::Handle", SHASH)
143              
144             I must be a hash variable, and I must be a handle
145             referring to a shared hash object. The call binds the variable to the
146             shared hash, so that the variable provides a view of the shared hash
147             that resembles an ordinary Perl hash. The shared hash handle is returned.
148              
149             =item tie(VARIABLE, "Hash::SharedMem::Handle", FILENAME, MODE)
150              
151             I must be a hash variable. The call opens a handle referring
152             to a shared hash object, as described in L,
153             and binds the variable to the shared hash, so that the variable provides a
154             view of the shared hash that resembles an ordinary Perl hash. The shared
155             hash handle is returned.
156              
157             =back
158              
159             =head1 TIED OPERATORS
160              
161             For all of these operators, the key of interest (I parameter)
162             and values can each be any octet (Latin-1) string. Strings containing
163             non-octets (Unicode characters above U+FF) and items other than strings
164             cannot be used as keys or values. If a dualvar (scalar with independent
165             string and numeric values) is supplied, only its string value will
166             be used.
167              
168             =over
169              
170             =item tied(%SHASH)
171              
172             Returns the handle via which I<%SHASH> is bound to the shared hash.
173             This is a shared hash handle that can be used by calling the methods
174             described above or by passing it to the functions of L.
175              
176             =item exists($SHASH{KEY})
177              
178             Returns a truth value indicating whether the specified key is currently
179             present in the shared hash.
180              
181             =item $SHASH{KEY}
182              
183             Returns the value currently referenced by the specified key in the shared
184             hash, or C if the key is absent.
185              
186             =item $SHASH{KEY} = NEWVALUE
187              
188             Modifies the shared hash so that the specified key henceforth references
189             the specified value. The new value must be a string.
190              
191             =item delete($SHASH{KEY})
192              
193             Modifies the shared hash so that the specified key is henceforth absent,
194             and returns the value that the key previously referenced, or C
195             if the key was already absent. This swap is performed atomically.
196              
197             =item %SHASH = LIST
198              
199             Setting the entire content of the shared hash (throwing away the previous
200             content) is not supported.
201              
202             =item each(%SHASH)
203              
204             =item keys(%SHASH)
205              
206             =item values(%SHASH)
207              
208             =item %SHASH
209              
210             Iteration over, or enumeration of, the shared hash's content is not
211             supported.
212              
213             =item scalar(%SHASH)
214              
215             Checking whether the shared hash is occupied is not supported.
216              
217             =back
218              
219             =head1 BUGS
220              
221             Due to details of the Perl implementation, this object-oriented interface
222             to the shared hash mechanism is somewhat slower than the function
223             interface, and the tied interface is much slower. The functions in
224             L are the recommended interface.
225              
226             =head1 SEE ALSO
227              
228             L
229              
230             =head1 AUTHOR
231              
232             Andrew Main (Zefram)
233              
234             =head1 COPYRIGHT
235              
236             Copyright (C) 2014 PhotoBox Ltd
237              
238             Copyright (C) 2014 Andrew Main (Zefram)
239              
240             =head1 LICENSE
241              
242             This module is free software; you can redistribute it and/or modify it
243             under the same terms as Perl itself.
244              
245             =cut
246              
247             1;