1MCE::Shared::Ordhash(3)User Contributed Perl DocumentatioMnCE::Shared::Ordhash(3)
2
3
4
6 MCE::Shared::Ordhash - An ordered hash class featuring tombstone
7 deletion
8
10 This document describes MCE::Shared::Ordhash version 1.862
11
13 An ordered-hash helper class for use as a standalone or managed by
14 MCE::Shared.
15
16 This module implements an ordered hash featuring tombstone deletion,
17 inspired by Hash::Ordered. An ordered hash is very much like a normal
18 hash but with key insertion order preserved.
19
20 It provides "splice", sorting, plus extra capabilities for use with
21 MCE::Shared::Minidb. Tombstone deletion is further optimized to not
22 impact "store", "push", "unshift", and "merge". Tombstones are purged
23 in-place for lesser memory consumption. In addition, "pop" and "shift"
24 run optimally when an index is present. The optimization also applies
25 to forward and reverse deletes. The end result is achieving a new level
26 of performance, for a pure-Perl ordered hash implementation.
27
29 # non-shared or local construction for use by a single process
30
31 use MCE::Shared::Ordhash;
32
33 my $oh = MCE::Shared::Ordhash->new( @pairs );
34
35 # construction for sharing with other threads and processes
36
37 use MCE::Shared;
38
39 my $oh = MCE::Shared->ordhash( @pairs );
40
41 # hash-like dereferencing
42
43 my $val = $oh->{$key};
44 $oh->{$key} = $val;
45
46 %{$oh} = ();
47
48 # OO interface
49
50 if ( !defined ( $val = $oh->get("some_key") ) ) {
51 $val = $oh->set( some_key => "some_value" );
52 }
53
54 $val = $oh->set( $key, $val );
55 $val = $oh->get( $key );
56 $val = $oh->delete( $key ); # del is an alias for delete
57 $bool = $oh->exists( $key );
58 void = $oh->clear();
59 $len = $oh->len(); # scalar keys %{ $oh }
60 $len = $oh->len( $key ); # length $oh->{ $key }
61 @pair = $oh->pop();
62 $len = $oh->push( @pairs );
63 @pair = $oh->shift();
64 $len = $oh->unshift( @pairs );
65 %pairs = $oh->splice( $offset, $length, @pairs );
66
67 $oh2 = $oh->clone( @keys ); # @keys is optional
68 $oh3 = $oh->flush( @keys );
69 $iter = $oh->iterator( @keys ); # ($key, $val) = $iter->()
70 @keys = $oh->keys( @keys );
71 %pairs = $oh->pairs( @keys );
72 @vals = $oh->values( @keys ); # vals is an alias for values
73
74 $len = $oh->assign( $key/$val pairs ); # equivalent to ->clear, ->mset
75 $cnt = $oh->mdel( @keys );
76 @vals = $oh->mget( @keys );
77 $bool = $oh->mexists( @keys ); # true if all keys exists
78 $len = $oh->mset( $key/$val pairs ); # merge is an alias for mset
79
80 @vals = $oh->sort(); # by val $a <=> $b default
81 @vals = $oh->sort( "desc" ); # by val $b <=> $a
82 @vals = $oh->sort( "alpha" ); # by val $a cmp $b
83 @vals = $oh->sort( "alpha desc" ); # by val $b cmp $a
84
85 @vals = $oh->sort( "key" ); # by key $a <=> $b
86 @vals = $oh->sort( "key desc" ); # by key $b <=> $a
87 @vals = $oh->sort( "key alpha" ); # by key $a cmp $b
88 @vals = $oh->sort( "key alpha desc" ); # by key $b cmp $a
89
90 # included, sugar methods without having to call set/get explicitly
91
92 $len = $oh->append( $key, $string ); # $val .= $string
93 $val = $oh->decr( $key ); # --$val
94 $val = $oh->decrby( $key, $number ); # $val -= $number
95 $val = $oh->getdecr( $key ); # $val--
96 $val = $oh->getincr( $key ); # $val++
97 $val = $oh->incr( $key ); # ++$val
98 $val = $oh->incrby( $key, $number ); # $val += $number
99 $old = $oh->getset( $key, $new ); # $o = $v, $v = $n, $o
100
101 # pipeline, provides atomicity for shared objects, MCE::Shared v1.09+
102
103 @vals = $oh->pipeline( # ( "a_a", "b_b", "c_c" )
104 [ "set", foo => "a_a" ],
105 [ "set", bar => "b_b" ],
106 [ "set", baz => "c_c" ],
107 [ "mget", qw/ foo bar baz / ]
108 );
109
110 For normal hash behavior, the TIE interface is supported.
111
112 # non-shared or local construction for use by a single process
113
114 use MCE::Shared::Ordhash;
115
116 tie my %oh, "MCE::Shared::Ordhash", @pairs;
117 tie my %oh, "MCE::Shared::Ordhash";
118
119 # construction for sharing with other threads and processes
120 # the ordered option is needed to know to use MCE::Shared::Ordhash
121
122 use MCE::Shared;
123
124 tie my %oh, "MCE::Shared", { ordered => 1 }, @pairs;
125 tie my %oh, "MCE::Shared", ordered => 1;
126
127 # usage
128
129 my $val;
130
131 if ( !defined ( $val = $oh{some_key} ) ) {
132 $val = $oh{some_key} = "some_value";
133 }
134
135 $oh{some_key} = 0;
136
137 tied(%oh)->incrby("some_key", 20);
138 tied(%oh)->incrby(some_key => 20);
139
141 Several methods take a query string for an argument. The format of the
142 string is described below. In the context of sharing, the query
143 mechanism is beneficial for the shared-manager process. It is able to
144 perform the query where the data resides versus the client-process grep
145 locally involving lots of IPC.
146
147 o Basic demonstration
148
149 @keys = $oh->keys( "query string given here" );
150 @keys = $oh->keys( "val =~ /pattern/" );
151
152 o Supported operators: =~ !~ eq ne lt le gt ge == != < <= > >=
153 o Multiple expressions delimited by :AND or :OR, mixed case allowed
154
155 "key eq 'some key' :or (val > 5 :and val < 9)"
156 "key eq some key :or (val > 5 :and val < 9)"
157 "key =~ /pattern/i :And val =~ /pattern/i"
158 "val eq foo baz :OR key !~ /pattern/i"
159
160 * key matches on keys in the hash
161 * likewise, val matches on values
162
163 o Quoting is optional inside the string
164
165 "key =~ /pattern/i :AND val eq 'foo bar'" # val eq "foo bar"
166 "key =~ /pattern/i :AND val eq foo bar" # val eq "foo bar"
167
168 Examples.
169
170 # search capability key/val: =~ !~ eq ne lt le gt ge == != < <= > >=
171 # key/val means to match against actual key/val respectively
172
173 @keys = $oh->keys( "key eq 'some key' :or (val > 5 :and val < 9)" );
174 @keys = $oh->keys( "key eq some key :or (val > 5 :and val < 9)" );
175
176 @keys = $oh->keys( "key =~ /$pattern/i" );
177 @keys = $oh->keys( "key !~ /$pattern/i" );
178 @keys = $oh->keys( "val =~ /$pattern/i" );
179 @keys = $oh->keys( "val !~ /$pattern/i" );
180
181 %pairs = $oh->pairs( "key == $number" );
182 %pairs = $oh->pairs( "key != $number :and val > 100" );
183 %pairs = $oh->pairs( "key < $number :or key > $number" );
184 %pairs = $oh->pairs( "val <= $number" );
185 %pairs = $oh->pairs( "val > $number" );
186 %pairs = $oh->pairs( "val >= $number" );
187
188 @vals = $oh->vals( "key eq $string" );
189 @vals = $oh->vals( "key ne $string with space" );
190 @vals = $oh->vals( "key lt $string :or val =~ /$pat1|$pat2/" );
191 @vals = $oh->vals( "val le $string :and val eq 'foo bar'" );
192 @vals = $oh->vals( "val le $string :and val eq foo bar" );
193 @vals = $oh->vals( "val gt $string" );
194 @vals = $oh->vals( "val ge $string" );
195
197 This module involves TIE when accessing the object via hash-like
198 behavior. Both non-shared and shared instances are impacted if doing
199 so. Although likely fast enough for many use cases, the OO interface is
200 recommended for best performance.
201
202 MCE::Shared::Ordhash->new ( key, value [, key, value, ... ] )
203 MCE::Shared->ordhash ( key, value [, key, value, ... ] )
204 Constructs a new object, with an optional list of key-value pairs.
205
206 # non-shared or local construction for use by a single process
207
208 use MCE::Shared::Ordhash;
209
210 $oh = MCE::Shared::Ordhash->new( @pairs );
211 $oh = MCE::Shared::Ordhash->new( );
212
213 # construction for sharing with other threads and processes
214
215 use MCE::Shared;
216
217 $oh = MCE::Shared->ordhash( @pairs );
218 $oh = MCE::Shared->ordhash( );
219
220 assign ( key, value [, key, value, ... ] )
221 Clears the hash, then sets multiple key-value pairs and returns the
222 number of keys stored in the hash. This is equivalent to "clear",
223 "mset".
224
225 $len = $oh->assign( "key1" => "val1", "key2" => "val2" ); # 2
226 $len = %{$oh} = ( "key1" => "val1", "key2" => "val2" ); # 4
227
228 API available since 1.007.
229
230 clear
231 Removes all key-value pairs from the hash.
232
233 $oh->clear;
234 %{$oh} = ();
235
236 clone ( key [, key, ... ] )
237 Creates a shallow copy, a "MCE::Shared::Ordhash" object. It returns an
238 exact copy if no arguments are given. Otherwise, the object includes
239 only the given keys in the same order. Keys that do not exist in the
240 hash will have the "undef" value.
241
242 $oh2 = $oh->clone( "key1", "key2" );
243 $oh2 = $oh->clone;
244
245 delete ( key )
246 Deletes and returns the value by given key or "undef" if the key does
247 not exists in the hash.
248
249 $val = $oh->delete( "some_key" );
250 $val = delete $oh->{ "some_key" };
251
252 del
253 "del" is an alias for "delete".
254
255 exists ( key )
256 Determines if a key exists in the hash.
257
258 if ( $oh->exists( "some_key" ) ) { ... }
259 if ( exists $oh->{ "some_key" } ) { ... }
260
261 flush ( key [, key, ... ] )
262 Same as "clone". Though, clears all existing items before returning.
263
264 get ( key )
265 Gets the value of a hash key or "undef" if the key does not exists.
266
267 $val = $oh->get( "some_key" );
268 $val = $oh->{ "some_key" };
269
270 iterator ( key [, key, ... ] )
271 Returns a code reference for iterating a list of key-value pairs stored
272 in the hash when no arguments are given. Otherwise, returns a code
273 reference for iterating the given keys in the same order. Keys that do
274 not exist will have the "undef" value.
275
276 The list of keys to return is set when the closure is constructed.
277 Later keys added to the hash are not included. Subsequently, the
278 "undef" value is returned for deleted keys.
279
280 $iter = $oh->iterator;
281 $iter = $oh->iterator( "key1", "key2" );
282
283 while ( my ( $key, $val ) = $iter->() ) {
284 ...
285 }
286
287 iterator ( "query string" )
288 Returns a code reference for iterating a list of key-value pairs that
289 match the given criteria. It returns an empty list if the search found
290 nothing. The syntax for the "query string" is described above.
291
292 $iter = $oh->iterator( "val eq some_value" );
293 $iter = $oh->iterator( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
294 $iter = $oh->iterator( "val eq sun :OR val eq moon :OR val eq foo" );
295 $iter = $oh->iterator( "key =~ /$pattern/" );
296
297 while ( my ( $key, $val ) = $iter->() ) {
298 ...
299 }
300
301 keys ( key [, key, ...] )
302 Returns hash keys in the same insertion order when no arguments are
303 given. Otherwise, returns the given keys in the same order. Keys that
304 do not exist will have the "undef" value. In scalar context, returns
305 the size of the hash.
306
307 @keys = $oh->keys( "key1", "key2" );
308
309 @keys = $oh->keys; # faster
310 @keys = keys %{$oh}; # involves TIE overhead
311
312 $len = $oh->keys; # ditto
313 $len = keys %{$oh};
314
315 keys ( "query string" )
316 Returns only keys that match the given criteria. It returns an empty
317 list if the search found nothing. The syntax for the "query string" is
318 described above. In scalar context, returns the size of the resulting
319 list.
320
321 @keys = $oh->keys( "val eq some_value" );
322 @keys = $oh->keys( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
323 @keys = $oh->keys( "val eq sun :OR val eq moon :OR val eq foo" );
324 $len = $oh->keys( "key =~ /$pattern/" );
325
326 len ( key )
327 Returns the size of the hash when no arguments are given. For the given
328 key, returns the length of the value stored at key or the "undef" value
329 if the key does not exists.
330
331 $size = $oh->len;
332 $len = $oh->len( "key1" );
333 $len = length $oh->{ "key1" };
334
335 mdel ( key [, key, ... ] )
336 Deletes one or more keys in the hash and returns the number of keys
337 deleted. A given key which does not exist in the hash is not counted.
338
339 $cnt = $oh->mdel( "key1", "key2" );
340
341 mexists ( key [, key, ... ] )
342 Returns a true value if all given keys exists in the hash. A false
343 value is returned otherwise.
344
345 if ( $oh->mexists( "key1", "key2" ) ) { ... }
346
347 mget ( key [, key, ... ] )
348 Gets the values of all given keys. It returns "undef" for keys which do
349 not exists in the hash.
350
351 ( $val1, $val2 ) = $oh->mget( "key1", "key2" );
352
353 mset ( key, value [, key, value, ... ] )
354 Sets multiple key-value pairs in a hash and returns the number of keys
355 stored in the hash.
356
357 $len = $oh->mset( "key1" => "val1", "key2" => "val2" );
358
359 merge
360 "merge" is an alias for "mset".
361
362 pairs ( key [, key, ... ] )
363 Returns key-value pairs in the same insertion order when no arguments
364 are given. Otherwise, returns key-value pairs for the given keys in
365 the same order. Keys that do not exist will have the "undef" value. In
366 scalar context, returns the size of the hash.
367
368 @pairs = $oh->pairs( "key1", "key2" );
369
370 @pairs = $oh->pairs;
371 $len = $oh->pairs;
372
373 pairs ( "query string" )
374 Returns only key-value pairs that match the given criteria. It returns
375 an empty list if the search found nothing. The syntax for the "query
376 string" is described above. In scalar context, returns the size of the
377 resulting list.
378
379 @pairs = $oh->pairs( "val eq some_value" );
380 @pairs = $oh->pairs( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
381 @pairs = $oh->pairs( "val eq sun :OR val eq moon :OR val eq foo" );
382 $len = $oh->pairs( "key =~ /$pattern/" );
383
384 pipeline ( [ func1, @args ], [ func2, @args ], ... )
385 Combines multiple commands for the object to be processed serially. For
386 shared objects, the call is made atomically due to single IPC to the
387 shared-manager process. The "pipeline" method is fully
388 "wantarray"-aware and receives a list of commands and their arguments.
389 In scalar or list context, it returns data from the last command in the
390 pipeline.
391
392 @vals = $oh->pipeline( # ( "a_a", "b_b", "c_c" )
393 [ "set", foo => "a_a" ],
394 [ "set", bar => "b_b" ],
395 [ "set", baz => "c_c" ],
396 [ "mget", qw/ foo bar baz / ]
397 );
398
399 $len = $oh->pipeline( # 3, same as $oh->len
400 [ "set", foo => "i_i" ],
401 [ "set", bar => "j_j" ],
402 [ "set", baz => "k_k" ],
403 [ "len" ]
404 );
405
406 $oh->pipeline(
407 [ "set", foo => "m_m" ],
408 [ "set", bar => "n_n" ],
409 [ "set", baz => "o_o" ]
410 );
411
412 Current API available since 1.809.
413
414 pipeline_ex ( [ func1, @args ], [ func2, @args ], ... )
415 Same as "pipeline", but returns data for every command in the pipeline.
416
417 @vals = $oh->pipeline_ex( # ( "a_a", "b_b", "c_c" )
418 [ "set", foo => "a_a" ],
419 [ "set", bar => "b_b" ],
420 [ "set", baz => "c_c" ]
421 );
422
423 Current API available since 1.809.
424
425 pop
426 Removes and returns the last key-value pair or value in scalar context
427 of the ordered hash. If there are no keys in the hash, returns the
428 undefined value.
429
430 ( $key, $val ) = $oh->pop;
431
432 $val = $oh->pop;
433
434 purge
435 A utility method for purging any *tombstones* in the keys array. It
436 also resets a couple counters internally. Call this method before
437 serializing to a file, which is the case in "MCE::Shared::Minidb".
438
439 $oh->purge;
440
441 push ( key, value [, key, value, ... ] )
442 Appends one or multiple key-value pairs to the tail of the ordered hash
443 and returns the new length. Any keys already existing in the hash are
444 re-inserted with the new values.
445
446 $len = $oh->push( "key1", "val1", "key2", "val2" );
447
448 set ( key, value )
449 Sets the value of the given hash key and returns its new value.
450
451 $val = $oh->set( "key", "value" );
452 $val = $oh->{ "key" } = "value";
453
454 shift
455 Removes and returns the first key-value pair or value in scalar context
456 of the ordered hash. If there are no keys in the hash, returns the
457 undefined value.
458
459 ( $key, $val ) = $oh->shift;
460
461 $val = $oh->shift;
462
463 sort ( "BY key [ ASC | DESC ] [ ALPHA ]" )
464 sort ( "BY val [ ASC | DESC ] [ ALPHA ]" )
465 Returns sorted keys in list context, leaving the elements intact. In
466 void context, sorts the hash in-place. By default, sorting is numeric
467 and applied to values when no arguments are given.
468
469 @keys = $oh->sort( "BY val" );
470
471 $oh->sort();
472
473 If the keys or values contain string values and you want to sort them
474 lexicographically, specify the "ALPHA" modifier.
475
476 @keys = $oh->sort( "BY key ALPHA" );
477
478 $oh->sort( "BY val ALPHA" );
479
480 The default is "ASC" for sorting the hash from small to large. In order
481 to sort the hash from large to small, specify the "DESC" modifier.
482
483 @keys = $oh->sort( "BY val DESC ALPHA" );
484
485 $oh->sort( "BY key DESC ALPHA" );
486
487 splice ( offset [, length [, key, value, ... ] ] )
488 Removes the key-value pairs designated by "offset" and "length" from
489 the ordered hash, and replaces them with "key-value pairs", if any. The
490 behavior is similar to the Perl "splice" function.
491
492 @pairs = $oh->splice( 20, 2, @pairs );
493 @pairs = $oh->splice( 20, 2 );
494 @pairs = $oh->splice( 20 );
495
496 unshift ( key, value [, key, value, ... ] )
497 Prepends one or multiple key-value pairs to the head of the ordered
498 hash and returns the new length. Any keys already existing in the hash
499 are re-inserted with the new values.
500
501 $len = $oh->unshift( "key1", "val1", "key2", "val2" );
502
503 values ( key [, key, ... ] )
504 Returns hash values in the same insertion order when no arguments are
505 given. Otherwise, returns values for the given keys in the same order.
506 Keys that do not exist will have the "undef" value. In scalar context,
507 returns the size of the hash.
508
509 @vals = $oh->values( "key1", "key2" );
510
511 @vals = $oh->values; # faster
512 @vals = values %{$oh}; # involves TIE overhead
513
514 $len = $oh->values; # ditto
515 $len = values %{$oh};
516
517 values ( "query string" )
518 Returns only values that match the given criteria. It returns an empty
519 list if the search found nothing. The syntax for the "query string" is
520 described above. In scalar context, returns the size of the resulting
521 list.
522
523 @vals = $oh->values( "val eq some_value" );
524 @vals = $oh->values( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
525 @vals = $oh->values( "val eq sun :OR val eq moon :OR val eq foo" );
526 $len = $oh->values( "key =~ /$pattern/" );
527
528 vals
529 "vals" is an alias for "values".
530
532 This module is equipped with sugar methods to not have to call "set"
533 and "get" explicitly. In shared context, the benefit is atomicity and
534 reduction in inter-process communication.
535
536 The API resembles a subset of the Redis primitives
537 <http://redis.io/commands#strings> with key representing the hash key.
538
539 append ( key, string )
540 Appends a value to a key and returns its new length.
541
542 $len = $oh->append( $key, "foo" );
543
544 decr ( key )
545 Decrements the value of a key by one and returns its new value.
546
547 $num = $oh->decr( $key );
548
549 decrby ( key, number )
550 Decrements the value of a key by the given number and returns its new
551 value.
552
553 $num = $oh->decrby( $key, 2 );
554
555 getdecr ( key )
556 Decrements the value of a key by one and returns its old value.
557
558 $old = $oh->getdecr( $key );
559
560 getincr ( key )
561 Increments the value of a key by one and returns its old value.
562
563 $old = $oh->getincr( $key );
564
565 getset ( key, value )
566 Sets the value of a key and returns its old value.
567
568 $old = $oh->getset( $key, "baz" );
569
570 incr ( key )
571 Increments the value of a key by one and returns its new value.
572
573 $num = $oh->incr( $key );
574
575 incrby ( key, number )
576 Increments the value of a key by the given number and returns its new
577 value.
578
579 $num = $oh->incrby( $key, 2 );
580
582 Many thanks to David Golden for Hash::Ordered. This implementation is
583 inspired by Hash::Ordered v0.009.
584
586 I wanted an ordered hash implementation for use with MCE::Shared
587 without any side effects. For example, linear scans, slow deletes, or
588 excessive memory consumption. The closest module on CPAN to pass in
589 this regard is Hash::Ordered by David Golden.
590
591 MCE::Shared has only one shared-manager process which is by design.
592 Therefore, re-factored tombstone deletion with extras for lesser impact
593 to the rest of the library. This module differs in personality from
594 Hash::Ordered mainly for compatibility with other classes included with
595 MCE::Shared.
596
597 The following simulates a usage pattern inside MCE::Hobo involving
598 random key deletion. For example, an application joining a list of
599 Hobos provided by "MCE::Hobo->list_joinable".
600
601 use MCE::Shared::Ordhash;
602 use List::Util 'shuffle';
603 use Time::HiRes 'time';
604
605 srand 0;
606
607 my $oh = MCE::Shared::Ordhash->new();
608 my $num_keys = 200000;
609 my $start = time();
610
611 $oh->set($_,$_) for 1 .. $num_keys;
612
613 for ( shuffle $oh->keys ) {
614 $oh->delete($_);
615 }
616
617 printf "duration: %7.03f secs\n", time() - $start;
618
619 Both the runtime and memory consumption are captured for the
620 demonstration. Results are included for MCE::Shared::Hash (unordered
621 hash) for comparison.
622
623 for ( shuffle $oh->keys ) { $oh->delete($_) }
624
625 0.378 secs. 35 MB MCE::Shared::Hash (unordered)
626 0.437 secs. 49 MB Tie::Hash::Indexed (XS)
627 0.743 secs. 54 MB MCE::Shared::Ordhash
628 1.028 secs. 60 MB Hash::Ordered
629 1.752 secs. 112 MB Tie::LLHash
630 > 42 mins. 66 MB Tie::IxHash
631
632 Using the same demonstration above, another usage pattern inside
633 MCE::Hobo involves orderly hash-key deletion. For example, waiting for
634 and joining all Hobos provided by "MCE::Hobo->list".
635
636 for ( $oh->keys ) { $oh->delete($_) }
637
638 0.353 secs. 35 MB MCE::Shared::Hash (unordered)
639 0.349 secs. 49 MB Tie::Hash::Indexed (XS)
640 0.452 secs. 41 MB MCE::Shared::Ordhash
641 0.735 secs. 54 MB Hash::Ordered
642 1.338 secs. 112 MB Tie::LLHash
643 > 42 mins. 66 MB Tie::IxHash
644
645 No matter if orderly or randomly, even backwards, hash-key deletion in
646 "MCE::Shared::Ordhash" performs reasonably well. The following provides
647 the construction used for the modules mentioned.
648
649 my $oh = Hash::Ordered->new();
650 $oh->set($_,$_); $oh->keys; $oh->delete($_);
651
652 my $oh = Tie::Hash::Indexed->new();
653 $oh->set($_,$_); $oh->keys; $oh->delete($_);
654
655 my $oh = Tie::IxHash->new();
656 $oh->STORE($_,$_); $oh->Keys; $oh->DELETE($_);
657
658 my $oh = tie my %hash, 'Tie::LLHash';
659 $oh->last($_,$_); keys %hash; $oh->DELETE($_);
660
661 Hash::Ordered is supported for use with MCE::Shared. This includes on-
662 demand hash-like dereferencing, similarly to "hash" and "ordhash".
663
664 use feature 'say';
665
666 use MCE::Hobo;
667 use MCE::Shared;
668 use Hash::Ordered; # 0.010 or later
669
670 my $ha = MCE::Shared->hash(); # shared MCE::Shared::Hash
671 my $oh = MCE::Shared->ordhash(); # shared MCE::Shared::Ordhash
672
673 my $ho = MCE::Shared->share( Hash::Ordered->new() );
674
675 sub parallel_task {
676 my ($id) = @_;
677
678 # OO interface
679 if ($id == 1) {
680 $ha->set("$id", "foo");
681 $oh->set("$id", "foo");
682 $ho->set("$id", "foo");
683 }
684 # hash-like dereferencing
685 elsif ($id == 2) {
686 $ha->{"$id"} = "baz";
687 $oh->{"$id"} = "baz";
688 $ho->{"$id"} = "baz";
689 }
690
691 return;
692 }
693
694 MCE::Hobo->create("parallel_task", $_) for 1..2;
695 MCE::Hobo->waitall;
696
697 say $ha->{"1"}; # foo
698 say $oh->{"1"};
699 say $ho->{"1"};
700
701 say $ha->get("2"); # baz
702 say $oh->get("2");
703 say $ho->get("2");
704
706 · Hash::Ordered
707
708 · Tie::Hash::Indexed
709
710 · Tie::IxHash
711
712 · Tie::LLHash
713
715 MCE, MCE::Hobo, MCE::Shared
716
718 Mario E. Roy, <marioeroy AT gmail DOT com>
719
720
721
722perl v5.30.0 2019-09-19 MCE::Shared::Ordhash(3)