File Coverage

blib/lib/Hash/SharedMem.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Hash::SharedMem - efficient shared mutable hash
4              
5             =head1 SYNOPSIS
6              
7             use Hash::SharedMem qw(shash_referential_handle);
8              
9             if(shash_referential_handle) { ...
10              
11             use Hash::SharedMem qw(is_shash check_shash);
12              
13             if(is_shash($arg)) { ...
14             check_shash($arg);
15              
16             use Hash::SharedMem qw(shash_open);
17              
18             $shash = shash_open($filename, "rwc");
19              
20             use Hash::SharedMem qw(
21             shash_is_readable shash_is_writable shash_mode);
22              
23             if(shash_is_readable($shash)) { ...
24             if(shash_is_writable($shash)) { ...
25             $mode = shash_mode($shash);
26              
27             use Hash::SharedMem qw(
28             shash_exists shash_length shash_get
29             shash_set shash_gset shash_cset);
30              
31             if(shash_exists($shash, $key)) { ...
32             $length = shash_length($shash, $key);
33             $value = shash_get($shash, $key);
34             shash_set($shash, $key, $newvalue);
35             $oldvalue = shash_gset($shash, $key, $newvalue);
36             if(shash_cset($shash, $key, $chkvalue, $newvalue)) { ...
37              
38             use Hash::SharedMem qw(
39             shash_occupied shash_count shash_size
40             shash_key_min shash_key_max
41             shash_key_ge shash_key_gt shash_key_le shash_key_lt
42             shash_keys_array shash_keys_hash
43             shash_group_get_hash);
44              
45             if(shash_occupied($shash)) { ...
46             $count = shash_count($shash);
47             $size = shash_size($shash);
48             $key = shash_key_min($shash);
49             $key = shash_key_max($shash);
50             $key = shash_key_ge($shash, $key);
51             $key = shash_key_gt($shash, $key);
52             $key = shash_key_le($shash, $key);
53             $key = shash_key_lt($shash, $key);
54             $keys = shash_keys_array($shash);
55             $keys = shash_keys_hash($shash);
56             $group = shash_group_get_hash($shash);
57              
58             use Hash::SharedMem qw(shash_snapshot shash_is_snapshot);
59              
60             $snap_shash = shash_snapshot($shash);
61             if(shash_is_snapshot($shash)) { ...
62              
63             use Hash::SharedMem qw(shash_idle shash_tidy);
64              
65             shash_idle($shash);
66             shash_tidy($shash);
67              
68             use Hash::SharedMem qw(
69             shash_tally_get shash_tally_zero shash_tally_gzero);
70              
71             $tally = shash_tally_get($shash);
72             shash_tally_zero($shash);
73             $tally = shash_tally_gzero($shash);
74              
75             =head1 DESCRIPTION
76              
77             This module provides a facility for efficiently sharing mutable data
78             between processes on one host. Data is organised as a key/value store,
79             resembling a Perl hash. The keys and values are restricted to octet
80             (Latin-1) strings. Structured objects may be stored by serialising them
81             using a mechanism such as L.
82              
83             The data is represented in files that are mapped into each process's
84             memory space, which for interprocess communication amounts to the
85             processes sharing memory. Processes are never blocked waiting for each
86             other. The use of files means that there is some persistence, with the
87             data continuing to exist when there are no processes with it mapped.
88              
89             The data structure is optimised for the case where all the data fits
90             into RAM. This happens either via buffering of a disk-based filesystem,
91             or as the normal operation of a memory-backed filesystem, in either case
92             as long as there isn't much swapping activity. If RAM isn't large enough,
93             such that the data has to reside mainly on disk and parts of it have to
94             be frequently reread from disk, speed will seriously suffer. The data
95             structure exhibits poor locality of reference, and is not designed to
96             play nicely with filesystem block sizes.
97              
98             =head2 Consistency and synchronisation
99              
100             A shared hash is held in regular files, grouped in a directory. At all
101             times, the OS-visible state of the files provides a consistent view of the
102             hash's content, from which read and write operations can proceed. It is
103             no problem for a process using the shared hash to crash; other processes,
104             running concurrently or later, will be unimpeded in using the shared hash.
105              
106             It is mainly intended that the shared hash should be held on a
107             memory-backed filesystem, and will therefore only last as long as the
108             machine stays up. However, it can use any filesystem that supports
109             L, including conventional disk filesystems such as ext2.
110             In this case, as long as the OS shuts down cleanly (synching all file
111             writes to the underlying disk), a consistent state of the shared hash
112             will persist between boots, and usage of the shared hash can continue
113             after the OS boots again. Note that only the OS is required to shut
114             down cleanly; it still doesn't matter if user processes crash.
115              
116             Because the OS is likely to reorder file writes when writing them to disk,
117             the instantaneous state of the shared hash's files on disk is generally
118             I guaranteed to be consistent. So if the OS crashes, upon reboot
119             the files are liable to be in an inconsistent state (corrupted).
120              
121             Maintaining consistency across an OS crash is a feature likely to be
122             added to this module in the future. Durability of writes, for which
123             that is a prerequisite, is another likely future addition.
124              
125             =head2 File permissions
126              
127             To read normally from a shared hash requires read and search (execute)
128             permission on the shared hash directory and read permission on all the
129             regular files in the directory. To write normally requires read, write,
130             and search permissions on the directory and read and write permissions
131             on all the regular files. For security purposes, some information about
132             shared hash content can be gleaned by anyone who has read or search
133             permission on the directory, and content can be modified by anyone who
134             has search permission on the directory and write permission on either
135             the directory or any of the regular files.
136              
137             The file permission bits on a shared hash are determined by the
138             circumstances in which it was created, specifically by the umask in
139             effect at the point of creation. As shared hash creation is unavoidably
140             non-atomic, competing creation attempts can cause trouble, and the
141             resulting permissions are only guaranteed if all competing attempts at
142             creation use the same umask. After the shared hash is fully created,
143             subsequently opening it with the create flag set doesn't affect
144             permissions.
145              
146             The directory gets permissions C modified by the creation
147             umask, and the regular files in it get permissions C modified
148             by the creation umask. All the regular files that contain any part of
149             the shared hash content will get the same permission bits. This includes
150             files created long after initial creation of the shared hash, which are
151             created as part of shared hash write operations; the umask in effect at
152             the point of those operations is insignificant.
153              
154             File ownership and group assignment are not so controlled. An attempt
155             is made to give all files the same group assignment and ownership,
156             determined by the creation of the shared hash, but the ability to do so
157             is limited by OS semantics. Typically, users other than the superuser
158             cannot affect ownership, and can only assign files to groups of which
159             they are members. Also, as with permission bits, competing creation
160             attempts can make ownerships and group assignments inconsistent, even
161             if they are generally controllable.
162              
163             Where they can't be freely set, each regular file gets whatever ownership
164             and group assignment arise naturally from the circumstances in which it
165             was created. If multiple users write to a single shared hash, it is to be
166             expected that the constituent files will end up having different owners.
167             It is up to the user to ensure that the varying ownerships combined
168             with the consistent permission bits yield compatible permissions for all
169             intended users of the shared hash. Group-based permissions should work
170             provided that all writers are members of the relevant group.
171              
172             =head2 Filesystem referential integrity
173              
174             If the system calls L et al are supported by the kernel
175             and the C library, then an open shared hash handle contains an
176             OS-supported first-class reference to the shared hash to which it refers.
177             (Specifically, it has a file descriptor referring to the shared hash
178             directory.) In this situation, the reference remains valid regardless of
179             filename changes. The shared hash can be renamed or moved arbitrarily,
180             within the filesystem, or the process can change its current directory
181             or root directory, and the handle remains valid.
182              
183             If these modern system calls are not available, then an open shared
184             hash handle cannot contain a first-class reference to the shared hash
185             directory. Instead it must repeatedly refer to the directory by name.
186             The name supplied to L is resolved to an absolute pathname,
187             so the handle will continue to work if the process changes its current
188             directory. But any renaming of the shared hash, or the process changing
189             its root directory, will cause the handle to fail at the next operation
190             that requires the use of filenames. (This is unlikely to be the very
191             next operation after the pathname becomes invalid.) An attempt is made to
192             ensure that the stored pathname is still correct each time it is used, but
193             there is unavoidably a race condition, whereby some very unluckily timed
194             renaming could cause an operation to be applied to the wrong directory.
195              
196             The means by which shared hash handles reference their directories is
197             indicated by the constant L.
198              
199             When a shared hash is being opened, if it already exists then the name
200             passed to L is resolved just once to determine to what shared
201             hash it refers. If the modern system calls are supported, this yields
202             perfectly clean name resolution semantics. However, if a shared hash does
203             not already exist, its creation cannot currently be so perfectly clean.
204             The name passed to L must be resolved at least twice, once
205             to create the shared hash directory and once to acquire a reference to it
206             (of whichever type). There is unavoidably a race condition here.
207              
208             =head2 File operations
209              
210             Because a shared hash is encapsulated in a directory, rather than being a
211             single non-directory file, the ability to perform file operations on it is
212             limited. Although it can be renamed or moved, under POSIX semantics such
213             a rename can't atomically replace any file other than an empty directory.
214             In particular, it can't atomically replace another shared hash. It also
215             can't be hard-linked to have multiple names. (However, a major use
216             case for L, non-overwriting renaming, can be achieved through
217             L due to the latter's limitations for directories.) Finally,
218             it can't be unlinked. (Linking and unlinking directories are possible for
219             root on some systems, but cause undesirable filesystem irregularities.)
220              
221             A shared hash can be disposed of by applying C to its directory.
222             This is not equivalent to L (C) on a regular file,
223             because it not only removes the object's name but also disrupts its
224             internal structure. If a process has an open handle referring to the
225             shared hash at the time of C, the use of the shared hash through
226             that handle is likely to fail, although probably not immediately. If a
227             process is writing to the shared hash at the time of C, there is a
228             race condition that could prevent the removal from completing. C
229             should therefore only be applied after all processes have finished using
230             the shared hash.
231              
232             A shared hash can be copied by means of C (not mere C), C,
233             or similar means. It is safe to do this while processes have open handles
234             referring to the shared hash, and while processes are reading from it.
235             However, as with most forms of database file, if a process is writing to
236             the shared hash then the file copier is liable to pick up an inconsistent
237             (corrupted) view of the shared hash. Copying should therefore only
238             be attempted at a time when no write operations are being performed.
239             It is acceptable for processes to have the shared hash open in write
240             mode, provided that they do not actually perform any write operation
241             while the copy is being made.
242              
243             A file-level copying operation applied to a shared hash is likely to
244             result in a copy that occupies much more filesystem space than the
245             original. This occurs because most of the time a large part of the
246             main data file is a filesystem hole, not occupying any actual storage.
247             Some copying mechanisms (such as GNU C) can recognise this and avoid
248             allocating unnecessary storage for the copy, but others (such as GNU
249             C) will blindly fill space with a lot of zeroes. If the copy is
250             subsequently used in shared hash write operations, ultimately it will
251             recover from this inefficient block allocation.
252              
253             =head2 Forking
254              
255             If a process is duplicated by L while it holds a shared hash
256             handle, the handle is duplicated with the rest of the process, so
257             both resulting processes have handles referring to the same underlying
258             shared hash. Provided that the duplication did not happen during a shared
259             hash operation, both processes' handles will subsequently work normally,
260             and can be used independently.
261              
262             Things are more difficult if a L happens while a shared hash
263             operation is in progress. This should not normally be possible to
264             achieve from Perl code: arbitrary Perl code should not run during the
265             critical part of an operation. If a shared hash operator is given a
266             tied variable as a parameter, the magic method call for access to that
267             parameter occurs before the critical part, so a L
268             in that method is safe. If a signal is received during a shared hash
269             operation, any signal handler installed in L<%SIG|perlvar/%SIG> will
270             be deferred until the operation is complete, so a L
271             in such a signal handler is also safe. A problematic L should
272             only be achievable by XS code.
273              
274             If a L does happen during the critical part of a shared hash
275             operation, the two resulting handles are liable to interfere if the
276             operation is resumed in both processes. In this case, it is safe for at
277             most one process (which may be either of them) to resume the operation.
278             The other process must neither resume the operation in progress nor make
279             any further use of the handle. It is safe for the non-resuming process
280             to chain a new program with L, to terminate with L<_exit(2)>,
281             or generally to make use of the C library before doing either of those.
282             Attempting to run Perl code would be unwise.
283              
284             On platforms lacking a native L, the Perl function
285             L actually creates a Perl L. In that
286             case the behaviour should be similar to that seen with a real L,
287             as described in the next section.
288              
289             =head2 Threads
290              
291             This module can be used in multiple Perl L simultaneously.
292             The module may be loaded by multiple threads separately, or from
293             Perl 5.8.9 onwards may be loaded by a thread that spawns new threads.
294             (Prior to Perl 5.8.9, limitations of the threading system mean that
295             module data can't be correctly cloned upon thread spawning. Any but
296             the most trivial cases of thread spawning with this module loaded will
297             crash the interpreter. The rest of this section only applies to Perls
298             that fully support cloning.)
299              
300             If a thread is spawned while the parent thread has an open shared hash
301             handle, the handle is duplicated, so that both resulting threads have
302             handles referring to the same underlying shared hash. Provided that the
303             duplication did not happen during a shared hash operation, both threads'
304             handles will subsequently work normally, and can be used independently.
305              
306             =head2 Tainting
307              
308             If L is enabled, taintedness is relevant
309             to some operations on shared hashes. Shared hash handles mostly behave
310             like regular file handles for tainting purposes. Where the following
311             description says that a result is "not tainted", that means it does
312             not get the taint flag set merely by virtue of the operation performed;
313             it may still be marked as tainted if other tainted data is part of the
314             same expression, due to Perl's conservative taint tracking.
315              
316             The classification functions are happy to operate on tainted arguments.
317             Their results are not tainted.
318              
319             When opening a shared hash, if the shared hash filename or the mode
320             string is tainted then it is not permitted to open for writing or with
321             the possibility of creating. It is permitted to open non-creatingly for
322             reading regardless of taint status. Of course, any kind of opening is
323             permitted in an untainted expression.
324              
325             A shared hash handle per se is never tainted.
326              
327             The results of the mode checking functions are not tainted.
328              
329             The content of a shared hash is always treated as tainted. It is
330             permitted to write tainted data to a shared hash. The data operations
331             all accept tainted arguments. When reading from a shared hash, the keys
332             existing in the hash and the values referenced by them are always tainted,
333             but an absent item is treated as clean. So where a data operation returns
334             a key or value from the shared hash, the result will be tainted if it is
335             a string, but C representing an absent item will not be tainted.
336             The count of keys existing in the hash, size of the hash, and the length
337             of an existing value are also tainted, being derived from tainted content.
338             However, the truth values returned by some operations are not tainted,
339             even if they are derived entirely from tainted data.
340              
341             =cut
342              
343             package Hash::SharedMem;
344              
345 30     30   1838307 { use 5.006; }
  30         103  
346 30     30   155 use warnings;
  30         58  
  30         759  
347 30     30   140 use strict;
  30         60  
  30         1007  
348              
349             our $VERSION = "0.005";
350              
351 30     30   600 use parent "Exporter";
  30         522  
  30         178  
352             our @EXPORT_OK = qw(
353             shash_referential_handle
354             is_shash check_shash
355             shash_open
356             shash_is_readable shash_is_writable shash_mode
357             shash_exists shash_getd shash_length shash_get
358             shash_set shash_gset shash_cset
359             shash_occupied shash_count shash_size
360             shash_key_min shash_key_max
361             shash_key_ge shash_key_gt shash_key_le shash_key_lt
362             shash_keys_array shash_keys_hash
363             shash_group_get_hash
364             shash_snapshot shash_is_snapshot
365             shash_idle shash_tidy
366             shash_tally_get shash_tally_zero shash_tally_gzero
367             );
368              
369             eval { local $SIG{__DIE__};
370             require Devel::CallChecker;
371             Devel::CallChecker->VERSION(0.003);
372             };
373              
374             require XSLoader;
375             XSLoader::load(__PACKAGE__, $VERSION);
376              
377             sub _deparse_shash_unop {
378 32     32   55 my($self, $op, $name) = @_;
379 32         36 my @k;
380 32         156 for(my $k = $op->first; !$k->isa("B::NULL"); $k = $k->sibling) {
381 48         205 push @k, $k;
382             }
383             return __PACKAGE__."::".$name.
384 32         74 "(".join(", ", map { $self->deparse($_, 6) } @k).")";
  48         3639  
385             }
386              
387             if("$]" >= 5.013007) {
388             foreach my $name (@EXPORT_OK) {
389             next if $name eq "shash_referential_handle";
390 30     30   7154 no strict "refs";
  30         61  
  30         4584  
391             *{"B::Deparse::pp_$name"} =
392 32     32   19859 sub { _deparse_shash_unop($_[0], $_[1], $name) };
393             }
394             }
395              
396             =head1 CONSTANTS
397              
398             =over
399              
400             =item shash_referential_handle
401              
402             Truth value indicating whether each shared hash handle contains
403             a first-class reference to the shared hash to which it refers.
404             See L above for discussion of the
405             significance of this.
406              
407             =back
408              
409             =head1 FUNCTIONS
410              
411             The I parameter that most of these functions take must be a handle
412             referring to a shared hash object.
413              
414             =head2 Classification
415              
416             =over
417              
418             =item is_shash(ARG)
419              
420             Returns a truth value indicating whether I is a handle referring
421             to a shared hash object.
422              
423             =item check_shash(ARG)
424              
425             Checks whether I is a handle referring to a shared hash object.
426             Returns normally if it is, or Cs if it is not.
427              
428             =back
429              
430             =head2 Opening
431              
432             =over
433              
434             =item shash_open(FILENAME, MODE)
435              
436             Opens and returns a handle referring to a shared hash object, or Cs
437             if the shared hash can't be opened as specified. I must
438             refer to the directory that encapsulates the shared hash.
439              
440             If the filename string contains non-ASCII characters, then the filename
441             actually used consists of the octets of the internal encoding of the
442             string, which does not necessarily match the ostensible characters of the
443             string. This gives inconsistent behaviour for the same character sequence
444             represented in the two different ways that Perl uses internally. This is
445             consistent with the treatment of filenames in Perl's built-in operators
446             such as L; see L
447             Not Happen">. This may change in future versions of Perl (beyond 5.26).
448              
449             I is a string controlling how the shared hash will be used.
450             It can contain these characters:
451              
452             =over
453              
454             =item B
455              
456             The shared hash is to be readable. If this is not specified then
457             read operations through the handle will be denied. Beware that at the
458             system-call level the files are necessarily opened readably. Thus read
459             permission on the files is required even if one will only be writing.
460              
461             =item B
462              
463             The shared hash is to be writable. If this is not specified then write
464             operations through the handle will be denied. This flag also determines
465             in what mode the files are opened at the system-call level, so write
466             permission on the files operates as expected.
467              
468             =item B
469              
470             The shared hash will be created if it does not already exist. The
471             permission bits on the shared hash will be controlled by the creating
472             process's umask. If this flag is not specified then the shared hash
473             must already exist.
474              
475             =item B
476              
477             The shared hash must not already exist. If this is not specified and the
478             shared hash already exists then it will be opened normally. This flag
479             is meant to be used with B; it means that a successful open implies
480             that this process, rather than any other, is the one that created the
481             shared hash.
482              
483             =back
484              
485             When a shared hash is created, some of its constituent files will
486             be opened in read/write mode even if read-only mode was requested.
487             Shared hash creation is not an atomic process, so if a creation attempt
488             is interrupted it may leave a half-created shared hash behind. This does
489             not prevent a subsequent creation attempt on the same shared hash from
490             succeeding: creation will continue from whatever stage it had reached.
491             Likewise, multiple simultaneous creation attempts may each do part of the
492             job. This can result in ownerships and permissions being inconsistent;
493             see L above.
494              
495             Regardless of the combination of efforts leading to the creation of a
496             shared hash, completion of the process is atomic. Non-creating open
497             attempts will either report that there is no shared hash or open the
498             created shared hash. Exactly one creation attempt will be judged to have
499             created the shared hash, and this is detectable through the B flag.
500              
501             =back
502              
503             =head2 Mode checking
504              
505             =over
506              
507             =item shash_is_readable(SHASH)
508              
509             Returns a truth value indicating whether the shared hash can be read
510             from through this handle.
511              
512             =item shash_is_writable(SHASH)
513              
514             Returns a truth value indicating whether the shared hash can be written
515             to through this handle.
516              
517             =item shash_mode(SHASH)
518              
519             Returns a string in which characters indicate the mode of this handle.
520             It matches the form of the I parameter to L, but
521             mode flags that are only relevant during the opening process (B
522             and B) are not included. The returned string can therefore contain
523             these characters:
524              
525             =over
526              
527             =item B
528              
529             The shared hash can be read from through this handle.
530              
531             =item B
532              
533             The shared hash can be written to through this handle.
534              
535             =back
536              
537             =back
538              
539             =head2 Single-key data operations
540              
541             For all of these functions, the key of interest (I parameter)
542             can be any octet (Latin-1) string, and values (I parameters and
543             some return values) can be any octet string or C. The C
544             value represents the absence of a key from the hash; there is no
545             present-but-undefined state. Strings containing non-octets (Unicode
546             characters above U+FF) and items other than strings cannot be used as
547             keys or values. If a dualvar (scalar with independent string and numeric
548             values) is supplied, only its string value will be used.
549              
550             =over
551              
552             =item shash_exists(SHASH, KEY)
553              
554             Returns a truth value indicating whether the specified key currently
555             references a defined value in the shared hash.
556              
557             =item shash_getd(SHASH, KEY)
558              
559             Deprecated alias for L.
560              
561             =item shash_length(SHASH, KEY)
562              
563             Returns the length (in octets) of the value currently referenced by the
564             specified key in the shared hash, or C if the key is absent.
565              
566             =item shash_get(SHASH, KEY)
567              
568             Returns the value currently referenced by the specified key in the
569             shared hash.
570              
571             =item shash_set(SHASH, KEY, NEWVALUE)
572              
573             Modifies the shared hash so that the specified key henceforth references
574             the specified value.
575              
576             =item shash_gset(SHASH, KEY, NEWVALUE)
577              
578             Modifies the shared hash so that the specified key henceforth references
579             the value I, and returns the value that the key previously
580             referenced. This swap is performed atomically.
581              
582             =item shash_cset(SHASH, KEY, CHKVALUE, NEWVALUE)
583              
584             Examines the value currently referenced by the specified key in the
585             shared hash. If it is identical to I, the function modifies
586             the shared hash so that the specified key henceforth references the value
587             I, and returns true. If the current value is not identical
588             to I, the function leaves it unmodified and returns false.
589             This conditional modification is performed atomically.
590              
591             This function can be used as a core on which to build arbitrarily
592             complex kinds of atomic operation (on a single key). For example,
593             an atomic increment can be implemented as
594              
595             do {
596             $ov = shash_get($shash, $key);
597             $nv = $ov + 1;
598             } until shash_cset($shash, $key, $ov, $nv);
599              
600             =back
601              
602             =head2 Whole-hash data operations
603              
604             =over
605              
606             =item shash_occupied(SHASH)
607              
608             Returns a truth value indicating whether there are currently any items
609             in the shared hash.
610              
611             =item shash_count(SHASH)
612              
613             Returns the number of items that are currently in the shared hash.
614              
615             =item shash_size(SHASH)
616              
617             Returns the approximate size (in octets) of the entire content of the
618             shared hash. The size of a hash is not a well-defined quantity, so the
619             return value of this function should be interpreted with care. It aims
620             specifically to indicate how much space is required in a shared hash
621             data file to represent this content. It is affected by details of the
622             file format (which may differ between shared hashes on one system) and
623             by accidents of how the content is laid out in a particular shared hash.
624             Calling this function twice on identical content will not necessarily
625             produce identical results. The details of the size estimation may also
626             change in the future.
627              
628             Although this function computes size specifically with respect to the
629             file format used by this module, this function does not directly indicate
630             the amount of space occupied by a shared hash. There is some non-content
631             overhead, and, more importantly, the process by which content is modified
632             requires space to store multiple versions of the content. It is normal
633             for the amount of space actually occupied to fluctuate over the cycle
634             of data file rewriting. If L is being used appropriately,
635             the space occupied can be expected to vary up to a little over five
636             times the size of the nominal content, and if L is not used
637             then the normal maximum will be more than ten times the content size.
638             Occasional spikes above these levels can be expected in any case, and
639             fixed overheads make these multipliers inapplicable if the content is
640             very small.
641              
642             =item shash_key_min(SHASH)
643              
644             Returns the lexicographically least of the keys that are currently in
645             the shared hash, or C if there are none.
646              
647             =item shash_key_max(SHASH)
648              
649             Returns the lexicographically greatest of the keys that are currently
650             in the shared hash, or C if there are none.
651              
652             =item shash_key_ge(SHASH, KEY)
653              
654             Returns the least of the keys currently in the shared hash that are
655             lexicographically no less than the specified key, or C if there
656             are none.
657              
658             =item shash_key_gt(SHASH, KEY)
659              
660             Returns the least of the keys currently in the shared hash that are
661             lexicographically greater than the specified key, or C if there
662             are none.
663              
664             =item shash_key_le(SHASH, KEY)
665              
666             Returns the greatest of the keys currently in the shared hash that
667             are lexicographically no greater than the specified key, or C
668             if there are none.
669              
670             =item shash_key_lt(SHASH, KEY)
671              
672             Returns the greatest of the keys currently in the shared hash that are
673             lexicographically less than the specified key, or C if there
674             are none.
675              
676             =item shash_keys_array(SHASH)
677              
678             Returns a reference to a plain array containing all the keys currently
679             in the shared hash in lexicographical order. The array and the key
680             scalars in it are unwritable.
681              
682             =item shash_keys_hash(SHASH)
683              
684             Returns a reference to a plain hash in which the keys are all the keys
685             currently in the shared hash and all the values are C. The value
686             scalars are unwritable. Writability of the hash is not guaranteed:
687             currently in practice it is writable, but this may change in the future.
688              
689             =item shash_group_get_hash(SHASH)
690              
691             Returns a reference to a plain hash representing the entire current
692             content of the shared hash. The value scalars are unwritable.
693             Writability of the hash is not guaranteed: currently in practice it is
694             writable, but this may change in the future.
695              
696             =back
697              
698             =head2 Snapshots
699              
700             =over
701              
702             =item shash_snapshot(SHASH)
703              
704             Returns a shared hash handle that encapsulates the current content of the
705             shared hash. The entire state of the shared hash is captured atomically,
706             and the returned handle can be used to perform arbitrarily many read
707             operations on that state: it will never reflect later modifications to
708             the shared hash. The snapshot handle cannot be used for writing.
709              
710             =item shash_is_snapshot(SHASH)
711              
712             Returns a truth value indicating whether this handle refers to a snapshot
713             (as opposed to a live shared hash).
714              
715             =back
716              
717             =head2 Maintenance
718              
719             =over
720              
721             =item shash_idle(SHASH)
722              
723             Puts the shared hash handle into a state where it occupies less
724             resources, at the expense of making the next operation through the
725             handle more expensive. This doesn't change the visible behaviour of the
726             handle, and doesn't affect the state of the shared hash itself at all.
727             The invisible operations performed by this function may vary between
728             versions of this module.
729              
730             This function should be called when the handle is going to be unused for
731             a lengthy period. For example, if a long-running daemon uses a shared
732             hash in brief bursts once an hour, it should idle its handle at the end
733             of each burst of activity.
734              
735             Currently the effect of this operation is to discard the handle's
736             memory mapping of the shared hash data file. The next operation has to
737             reestablish the mapping. The benefit of discarding the mapping is that
738             periodically the data file has to be replaced with a new one, but the
739             old data file continues to exist as long as some process has it mapped.
740             A process that is actively using the shared hash will quickly notice that
741             the data file has been replaced and will unmap the old one. A process
742             with a handle that it's not using, however, could keep the old data
743             file in existence, occupying storage, long after it has no further use.
744             A handle that has been put into the idle state won't perpetuate the
745             existence of an obsolete data file.
746              
747             =item shash_tidy(SHASH)
748              
749             Rearranges the storage of the shared hash if it seems useful to do
750             so, to avoid tidying work having to be performed by other processes.
751             This doesn't change the visible content of the shared hash, but the
752             handle must be open for writing, and this counts as a write operation for
753             purposes concerned with the state of the underlying files. The invisible
754             operations performed by this function may vary between versions of
755             this module.
756              
757             This function should be called in circumstances where it is acceptable
758             to incur some delay for this maintenance work to complete. For example,
759             it could be called periodically by a cron job. Essentially, calling this
760             function signals that this is a convenient time at which (and process
761             in which) to perform maintenance.
762              
763             If this maintenance work is not carried out by means of this function,
764             then ultimately it will be performed anyway, but less predictably and
765             possibly less conveniently. Eventually it will become necessary to
766             perform maintenance in order to continue using the shared hash, at which
767             point the next process that attempts to write to it will carry out the
768             work and incur the cost. The shared hash will still work properly in
769             that case, but the unlucky writer will experience a disproportionately
770             large delay in the completion of its write operation. This could well
771             be a problem if the shared hash is large.
772              
773             =back
774              
775             =head2 Event counters
776              
777             =over
778              
779             =item shash_tally_get(SHASH)
780              
781             Returns a reference to a hash of counts of events that have occurred with
782             this shared hash handle. These counts may be of interest for profiling
783             and debugging purposes, but should not be relied upon for semantic
784             purposes. The event types may vary between versions of this module.
785             Few of the event types make sense in terms of the API supplied by this
786             module: most of them are internal implementation details.
787              
788             Events are counted separately for each handle. The events counted are
789             associated specifically with the handle, rather than with the shared hash
790             as a whole. Generally, an idle handle will accumulate no events, even
791             if the shared hash to which it refers is active. The event counters
792             start at zero when a handle is opened, and can be reset to zero by
793             L or L.
794              
795             In the returned hash, each key identifies a type of event, and the
796             corresponding value is (unless wrapped) the number of times events of that
797             type have occurred on the handle since the counters were last zeroed.
798             Currently the event counters are held in fixed-size variables and can
799             wrap, so if event counts might get as high as 2^64 then they can't be
800             relied upon to be accurate. Wrapping will not occur at less than 2^64;
801             in other respects, wrapping behaviour may change in the future.
802              
803             The event types that are currently counted are:
804              
805             =over
806              
807             =item B
808              
809             Parse an octet string representation in a shared hash data file.
810              
811             =item B
812              
813             Write an octet string representation into a shared hash data file.
814              
815             =item B
816              
817             Parse a B-tree node representation in a shared hash data file.
818              
819             =item B
820              
821             Write a B-tree node representation into a shared hash data file.
822              
823             =item B
824              
825             Compare two strings as shared hash keys.
826              
827             =item B
828              
829             Attempt to replace the root pointer in a shared hash data file. This may
830             be done to change the content of the shared hash, or as part of the
831             process of switching to a new data file.
832              
833             =item B
834              
835             Succeed in replacing the root pointer in a shared hash data file.
836             An attempt will fail if another process changed the root pointer during
837             the operation that required this process to change the root pointer.
838              
839             =item B
840              
841             Attempt to replace the data file in a shared hash. This is necessary
842             from time to time as data files fill up.
843              
844             =item B
845              
846             Succeed in replacing the data file in a shared hash. An attempt will
847             fail if another process replaced the data file while this process was
848             initialising its new one.
849              
850             =item B
851              
852             Perform a high-level data operation that purely reads from the shared
853             hash: L, L, L,
854             L, L, L,
855             L, L,
856             L, L, L, L,
857             L, L,
858             or L.
859              
860             =item B
861              
862             Perform a high-level data operation that writes, or at least may write,
863             to the shared hash: L, L, or L.
864              
865             =back
866              
867             The value scalars in the returned hash are unwritable. Writability of
868             the hash is not guaranteed: currently in practice it is writable, but
869             this may change in the future.
870              
871             =item shash_tally_zero(SHASH)
872              
873             Zero the event counters that can be read by L.
874              
875             =item shash_tally_gzero(SHASH)
876              
877             Zero the event counters that can be read by L, and
878             return the values the event counters previously had, in the same form
879             as L. This swap is performed atomically.
880              
881             =back
882              
883             =head1 BUGS
884              
885             As explained for L, creation of a shared hash is not atomic.
886             This is an unavoidable consequence of the need for the shared hash to
887             consist of multiple files in a directory. Multi-party creation can result
888             in the files having different permission bits; to avoid this, all creators
889             should use the same umask. Multiple users writing to a shared hash can
890             result in the files having different ownerships, so the permission bits
891             must be chosen to work appropriately with the chimeric ownership.
892              
893             When calls to the functions supplied by this module are compiled down to
894             custom ops (which is attempted for performance reasons), the ability to
895             deparse the resulting code with L is limited. Prior to Perl
896             5.13.7, deparsing will generate very incorrect code. From Perl 5.13.7
897             onwards, deparsing should normally work, but will break if another module
898             defines a separate type of custom op that happens to have the same short
899             name (though these ops do not clash in other respects).
900              
901             =head1 SEE ALSO
902              
903             L,
904             L
905              
906             =head1 AUTHOR
907              
908             Andrew Main (Zefram)
909              
910             =head1 COPYRIGHT
911              
912             Copyright (C) 2014, 2015 PhotoBox Ltd
913              
914             Copyright (C) 2014, 2015, 2017 Andrew Main (Zefram)
915              
916             =head1 LICENSE
917              
918             This module is free software; you can redistribute it and/or modify it
919             under the same terms as Perl itself.
920              
921             =cut
922              
923             1;