1MCE::Shared::Ordhash(3)User Contributed Perl DocumentatioMnCE::Shared::Ordhash(3)
2
3
4

NAME

6       MCE::Shared::Ordhash - An ordered hash class featuring tombstone
7       deletion
8

VERSION

10       This document describes MCE::Shared::Ordhash version 1.873
11

DESCRIPTION

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

SYNOPSIS

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        $ret   = $oh->setnx( $key, $val );         # set only if the key exists
56        $val   = $oh->get( $key );
57        $val   = $oh->delete( $key );              # del is an alias for delete
58        $bool  = $oh->exists( $key );
59        void   = $oh->clear();
60        $len   = $oh->len();                       # scalar keys %{ $oh }
61        $len   = $oh->len( $key );                 # length $oh->{ $key }
62        @pair  = $oh->pop();
63        $len   = $oh->push( @pairs );
64        @pair  = $oh->shift();
65        $len   = $oh->unshift( @pairs );
66        %pairs = $oh->splice( $offset, $length, @pairs );
67
68        $oh2   = $oh->clone( @keys );              # @keys is optional
69        $oh3   = $oh->flush( @keys );
70        $iter  = $oh->iterator( @keys );           # ($key, $val) = $iter->()
71        @keys  = $oh->keys( @keys );
72        %pairs = $oh->pairs( @keys );
73        @vals  = $oh->values( @keys );             # vals is an alias for values
74
75        $len   = $oh->assign( $key/$val pairs );   # equivalent to ->clear, ->mset
76        $cnt   = $oh->mdel( @keys );
77        @vals  = $oh->mget( @keys );
78        $bool  = $oh->mexists( @keys );            # true if all keys exists
79        $len   = $oh->mset( $key/$val pairs );     # merge is an alias for mset
80
81        @vals  = $oh->sort();                      # by val $a <=> $b default
82        @vals  = $oh->sort( "desc" );              # by val $b <=> $a
83        @vals  = $oh->sort( "alpha" );             # by val $a cmp $b
84        @vals  = $oh->sort( "alpha desc" );        # by val $b cmp $a
85
86        @vals  = $oh->sort( "key" );               # by key $a <=> $b
87        @vals  = $oh->sort( "key desc" );          # by key $b <=> $a
88        @vals  = $oh->sort( "key alpha" );         # by key $a cmp $b
89        @vals  = $oh->sort( "key alpha desc" );    # by key $b cmp $a
90
91        # included, sugar methods without having to call set/get explicitly
92
93        $len   = $oh->append( $key, $string );     #   $val .= $string
94        $val   = $oh->decr( $key );                # --$val
95        $val   = $oh->decrby( $key, $number );     #   $val -= $number
96        $val   = $oh->getdecr( $key );             #   $val--
97        $val   = $oh->getincr( $key );             #   $val++
98        $val   = $oh->incr( $key );                # ++$val
99        $val   = $oh->incrby( $key, $number );     #   $val += $number
100        $old   = $oh->getset( $key, $new );        #   $o = $v, $v = $n, $o
101
102        # pipeline, provides atomicity for shared objects, MCE::Shared v1.09+
103
104        @vals  = $oh->pipeline(                    # ( "a_a", "b_b", "c_c" )
105           [ "set", foo => "a_a" ],
106           [ "set", bar => "b_b" ],
107           [ "set", baz => "c_c" ],
108           [ "mget", qw/ foo bar baz / ]
109        );
110
111       For normal hash behavior, the TIE interface is supported.
112
113        # non-shared or local construction for use by a single process
114
115        use MCE::Shared::Ordhash;
116
117        tie my %oh, "MCE::Shared::Ordhash", @pairs;
118        tie my %oh, "MCE::Shared::Ordhash";
119
120        # construction for sharing with other threads and processes
121        # the ordered option is needed to know to use MCE::Shared::Ordhash
122
123        use MCE::Shared;
124
125        tie my %oh, "MCE::Shared", { ordered => 1 }, @pairs;
126        tie my %oh, "MCE::Shared", ordered => 1;
127
128        # usage
129
130        my $val;
131
132        if ( !defined ( $val = $oh{some_key} ) ) {
133           $val = $oh{some_key} = "some_value";
134        }
135
136        $oh{some_key} = 0;
137
138        tied(%oh)->incrby("some_key", 20);
139        tied(%oh)->incrby(some_key => 20);
140

SYNTAX for QUERY STRING

142       Several methods take a query string for an argument. The format of the
143       string is described below. In the context of sharing, the query
144       mechanism is beneficial for the shared-manager process. It is able to
145       perform the query where the data resides versus the client-process grep
146       locally involving lots of IPC.
147
148        o Basic demonstration
149
150          @keys = $oh->keys( "query string given here" );
151          @keys = $oh->keys( "val =~ /pattern/" );
152
153        o Supported operators: =~ !~ eq ne lt le gt ge == != < <= > >=
154        o Multiple expressions delimited by :AND or :OR, mixed case allowed
155
156          "key eq 'some key' :or (val > 5 :and val < 9)"
157          "key eq some key :or (val > 5 :and val < 9)"
158          "key =~ /pattern/i :And val =~ /pattern/i"
159          "val eq foo baz :OR key !~ /pattern/i"
160
161          * key matches on keys in the hash
162          * likewise, val matches on values
163
164        o Quoting is optional inside the string
165
166          "key =~ /pattern/i :AND val eq 'foo bar'"   # val eq "foo bar"
167          "key =~ /pattern/i :AND val eq foo bar"     # val eq "foo bar"
168
169       Examples.
170
171        # search capability key/val: =~ !~ eq ne lt le gt ge == != < <= > >=
172        # key/val means to match against actual key/val respectively
173
174        @keys  = $oh->keys( "key eq 'some key' :or (val > 5 :and val < 9)" );
175        @keys  = $oh->keys( "key eq some key :or (val > 5 :and val < 9)" );
176
177        @keys  = $oh->keys( "key =~ /$pattern/i" );
178        @keys  = $oh->keys( "key !~ /$pattern/i" );
179        @keys  = $oh->keys( "val =~ /$pattern/i" );
180        @keys  = $oh->keys( "val !~ /$pattern/i" );
181
182        %pairs = $oh->pairs( "key == $number" );
183        %pairs = $oh->pairs( "key != $number :and val > 100" );
184        %pairs = $oh->pairs( "key <  $number :or key > $number" );
185        %pairs = $oh->pairs( "val <= $number" );
186        %pairs = $oh->pairs( "val >  $number" );
187        %pairs = $oh->pairs( "val >= $number" );
188
189        @vals  = $oh->vals( "key eq $string" );
190        @vals  = $oh->vals( "key ne $string with space" );
191        @vals  = $oh->vals( "key lt $string :or val =~ /$pat1|$pat2/" );
192        @vals  = $oh->vals( "val le $string :and val eq 'foo bar'" );
193        @vals  = $oh->vals( "val le $string :and val eq foo bar" );
194        @vals  = $oh->vals( "val gt $string" );
195        @vals  = $oh->vals( "val ge $string" );
196

API DOCUMENTATION

198       This module involves TIE when accessing the object via hash-like
199       behavior.  Both non-shared and shared instances are impacted if doing
200       so. Although likely fast enough for many use cases, the OO interface is
201       recommended for best performance.
202
203   MCE::Shared::Ordhash->new ( key, value [, key, value, ... ] )
204   MCE::Shared->ordhash ( key, value [, key, value, ... ] )
205       Constructs a new object, with an optional list of key-value pairs.
206
207        # non-shared or local construction for use by a single process
208
209        use MCE::Shared::Ordhash;
210
211        $oh = MCE::Shared::Ordhash->new( @pairs );
212        $oh = MCE::Shared::Ordhash->new( );
213
214        # construction for sharing with other threads and processes
215
216        use MCE::Shared;
217
218        $oh = MCE::Shared->ordhash( @pairs );
219        $oh = MCE::Shared->ordhash( );
220
221   assign ( key, value [, key, value, ... ] )
222       Clears the hash, then sets multiple key-value pairs and returns the
223       number of keys stored in the hash. This is equivalent to "clear",
224       "mset".
225
226        $len = $oh->assign( "key1" => "val1", "key2" => "val2" );  # 2
227        $len = %{$oh} = ( "key1" => "val1", "key2" => "val2" );    # 4
228
229       API available since 1.007.
230
231   clear
232       Removes all key-value pairs from the hash.
233
234        $oh->clear;
235        %{$oh} = ();
236
237   clone ( key [, key, ... ] )
238       Creates a shallow copy, a "MCE::Shared::Ordhash" object. It returns an
239       exact copy if no arguments are given. Otherwise, the object includes
240       only the given keys in the same order. Keys that do not exist in the
241       hash will have the "undef" value.
242
243        $oh2 = $oh->clone( "key1", "key2" );
244        $oh2 = $oh->clone;
245
246   delete ( key )
247       Deletes and returns the value by given key or "undef" if the key does
248       not exists in the hash.
249
250        $val = $oh->delete( "some_key" );
251        $val = delete $oh->{ "some_key" };
252
253   del
254       "del" is an alias for "delete".
255
256   exists ( key )
257       Determines if a key exists in the hash.
258
259        if ( $oh->exists( "some_key" ) ) { ... }
260        if ( exists $oh->{ "some_key" } ) { ... }
261
262   flush ( key [, key, ... ] )
263       Same as "clone". Though, clears all existing items before returning.
264
265   get ( key )
266       Gets the value of a hash key or "undef" if the key does not exists.
267
268        $val = $oh->get( "some_key" );
269        $val = $oh->{ "some_key" };
270
271   iterator ( key [, key, ... ] )
272       Returns a code reference for iterating a list of key-value pairs stored
273       in the hash when no arguments are given. Otherwise, returns a code
274       reference for iterating the given keys in the same order. Keys that do
275       not exist will have the "undef" value.
276
277       The list of keys to return is set when the closure is constructed.
278       Later keys added to the hash are not included. Subsequently, the
279       "undef" value is returned for deleted keys.
280
281        $iter = $oh->iterator;
282        $iter = $oh->iterator( "key1", "key2" );
283
284        while ( my ( $key, $val ) = $iter->() ) {
285           ...
286        }
287
288   iterator ( "query string" )
289       Returns a code reference for iterating a list of key-value pairs that
290       match the given criteria. It returns an empty list if the search found
291       nothing.  The syntax for the "query string" is described above.
292
293        $iter = $oh->iterator( "val eq some_value" );
294        $iter = $oh->iterator( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
295        $iter = $oh->iterator( "val eq sun :OR val eq moon :OR val eq foo" );
296        $iter = $oh->iterator( "key =~ /$pattern/" );
297
298        while ( my ( $key, $val ) = $iter->() ) {
299           ...
300        }
301
302   keys ( key [, key, ...] )
303       Returns hash keys in the same insertion order when no arguments are
304       given.  Otherwise, returns the given keys in the same order. Keys that
305       do not exist will have the "undef" value. In scalar context, returns
306       the size of the hash.
307
308        @keys = $oh->keys( "key1", "key2" );
309
310        @keys = $oh->keys;     # faster
311        @keys = keys %{$oh};   # involves TIE overhead
312
313        $len  = $oh->keys;     # ditto
314        $len  = keys %{$oh};
315
316   keys ( "query string" )
317       Returns only keys that match the given criteria. It returns an empty
318       list if the search found nothing. The syntax for the "query string" is
319       described above. In scalar context, returns the size of the resulting
320       list.
321
322        @keys = $oh->keys( "val eq some_value" );
323        @keys = $oh->keys( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
324        @keys = $oh->keys( "val eq sun :OR val eq moon :OR val eq foo" );
325        $len  = $oh->keys( "key =~ /$pattern/" );
326
327   len ( key )
328       Returns the size of the hash when no arguments are given. For the given
329       key, returns the length of the value stored at key or the "undef" value
330       if the key does not exists.
331
332        $size = $oh->len;
333        $len  = $oh->len( "key1" );
334        $len  = length $oh->{ "key1" };
335
336   mdel ( key [, key, ... ] )
337       Deletes one or more keys in the hash and returns the number of keys
338       deleted.  A given key which does not exist in the hash is not counted.
339
340        $cnt = $oh->mdel( "key1", "key2" );
341
342   mexists ( key [, key, ... ] )
343       Returns a true value if all given keys exists in the hash. A false
344       value is returned otherwise.
345
346        if ( $oh->mexists( "key1", "key2" ) ) { ... }
347
348   mget ( key [, key, ... ] )
349       Gets the values of all given keys. It returns "undef" for keys which do
350       not exists in the hash.
351
352        ( $val1, $val2 ) = $oh->mget( "key1", "key2" );
353
354   mset ( key, value [, key, value, ... ] )
355       Sets multiple key-value pairs in a hash and returns the number of keys
356       stored in the hash.
357
358        $len = $oh->mset( "key1" => "val1", "key2" => "val2" );
359
360   merge
361       "merge" is an alias for "mset".
362
363   pairs ( key [, key, ... ] )
364       Returns key-value pairs in the same insertion order when no arguments
365       are given.  Otherwise, returns key-value pairs for the given keys in
366       the same order. Keys that do not exist will have the "undef" value. In
367       scalar context, returns the size of the hash.
368
369        @pairs = $oh->pairs( "key1", "key2" );
370
371        @pairs = $oh->pairs;
372        $len   = $oh->pairs;
373
374   pairs ( "query string" )
375       Returns only key-value pairs that match the given criteria. It returns
376       an empty list if the search found nothing. The syntax for the "query
377       string" is described above. In scalar context, returns the size of the
378       resulting list.
379
380        @pairs = $oh->pairs( "val eq some_value" );
381        @pairs = $oh->pairs( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
382        @pairs = $oh->pairs( "val eq sun :OR val eq moon :OR val eq foo" );
383        $len   = $oh->pairs( "key =~ /$pattern/" );
384
385   pipeline ( [ func1, @args ], [ func2, @args ], ... )
386       Combines multiple commands for the object to be processed serially. For
387       shared objects, the call is made atomically due to single IPC to the
388       shared-manager process. The "pipeline" method is fully
389       "wantarray"-aware and receives a list of commands and their arguments.
390       In scalar or list context, it returns data from the last command in the
391       pipeline.
392
393        @vals = $oh->pipeline(                     # ( "a_a", "b_b", "c_c" )
394           [ "set", foo => "a_a" ],
395           [ "set", bar => "b_b" ],
396           [ "set", baz => "c_c" ],
397           [ "mget", qw/ foo bar baz / ]
398        );
399
400        $len = $oh->pipeline(                      # 3, same as $oh->len
401           [ "set", foo => "i_i" ],
402           [ "set", bar => "j_j" ],
403           [ "set", baz => "k_k" ],
404           [ "len" ]
405        );
406
407        $oh->pipeline(
408           [ "set", foo => "m_m" ],
409           [ "set", bar => "n_n" ],
410           [ "set", baz => "o_o" ]
411        );
412
413       Current API available since 1.809.
414
415   pipeline_ex ( [ func1, @args ], [ func2, @args ], ... )
416       Same as "pipeline", but returns data for every command in the pipeline.
417
418        @vals = $oh->pipeline_ex(                  # ( "a_a", "b_b", "c_c" )
419           [ "set", foo => "a_a" ],
420           [ "set", bar => "b_b" ],
421           [ "set", baz => "c_c" ]
422        );
423
424       Current API available since 1.809.
425
426   pop
427       Removes and returns the last key-value pair or value in scalar context
428       of the ordered hash. If there are no keys in the hash, returns the
429       undefined value.
430
431        ( $key, $val ) = $oh->pop;
432
433        $val = $oh->pop;
434
435   purge
436       A utility method for purging any *tombstones* in the keys array. It
437       also resets a couple counters internally. Call this method before
438       serializing to a file, which is the case in "MCE::Shared::Minidb".
439
440        $oh->purge;
441
442   push ( key, value [, key, value, ... ] )
443       Appends one or multiple key-value pairs to the tail of the ordered hash
444       and returns the new length. Any keys already existing in the hash are
445       re-inserted with the new values.
446
447        $len = $oh->push( "key1", "val1", "key2", "val2" );
448
449   set ( key, value )
450       Sets the value of the given hash key and returns its new value.
451
452        $val = $oh->set( "key", "value" );
453        $val = $oh->{ "key" } = "value";
454
455   setnx ( key, value )
456       Sets the value of a hash key, only if the key does not exist. Returns a
457       1 for new key or 0 if the key already exists and no operation was
458       performed.
459
460        $ret = $oh->setnx( "key", "value" );
461
462       Current API available since 1.872.
463
464   shift
465       Removes and returns the first key-value pair or value in scalar context
466       of the ordered hash. If there are no keys in the hash, returns the
467       undefined value.
468
469        ( $key, $val ) = $oh->shift;
470
471        $val = $oh->shift;
472
473   sort ( "BY key [ ASC | DESC ] [ ALPHA ]" )
474   sort ( "BY val [ ASC | DESC ] [ ALPHA ]" )
475       Returns sorted keys in list context, leaving the elements intact. In
476       void context, sorts the hash in-place. By default, sorting is numeric
477       and applied to values when no arguments are given.
478
479        @keys = $oh->sort( "BY val" );
480
481        $oh->sort();
482
483       If the keys or values contain string values and you want to sort them
484       lexicographically, specify the "ALPHA" modifier.
485
486        @keys = $oh->sort( "BY key ALPHA" );
487
488        $oh->sort( "BY val ALPHA" );
489
490       The default is "ASC" for sorting the hash from small to large. In order
491       to sort the hash from large to small, specify the "DESC" modifier.
492
493        @keys = $oh->sort( "BY val DESC ALPHA" );
494
495        $oh->sort( "BY key DESC ALPHA" );
496
497   splice ( offset [, length [, key, value, ... ] ] )
498       Removes the key-value pairs designated by "offset" and "length" from
499       the ordered hash, and replaces them with "key-value pairs", if any. The
500       behavior is similar to the Perl "splice" function.
501
502        @pairs = $oh->splice( 20, 2, @pairs );
503        @pairs = $oh->splice( 20, 2 );
504        @pairs = $oh->splice( 20 );
505
506   unshift ( key, value [, key, value, ... ] )
507       Prepends one or multiple key-value pairs to the head of the ordered
508       hash and returns the new length. Any keys already existing in the hash
509       are re-inserted with the new values.
510
511        $len = $oh->unshift( "key1", "val1", "key2", "val2" );
512
513   values ( key [, key, ... ] )
514       Returns hash values in the same insertion order when no arguments are
515       given.  Otherwise, returns values for the given keys in the same order.
516       Keys that do not exist will have the "undef" value. In scalar context,
517       returns the size of the hash.
518
519        @vals = $oh->values( "key1", "key2" );
520
521        @vals = $oh->values;     # faster
522        @vals = values %{$oh};   # involves TIE overhead
523
524        $len  = $oh->values;     # ditto
525        $len  = values %{$oh};
526
527   values ( "query string" )
528       Returns only values that match the given criteria. It returns an empty
529       list if the search found nothing. The syntax for the "query string" is
530       described above. In scalar context, returns the size of the resulting
531       list.
532
533        @vals = $oh->values( "val eq some_value" );
534        @vals = $oh->values( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
535        @vals = $oh->values( "val eq sun :OR val eq moon :OR val eq foo" );
536        $len  = $oh->values( "key =~ /$pattern/" );
537
538   vals
539       "vals" is an alias for "values".
540

SUGAR METHODS

542       This module is equipped with sugar methods to not have to call "set"
543       and "get" explicitly. In shared context, the benefit is atomicity and
544       reduction in inter-process communication.
545
546       The API resembles a subset of the Redis primitives
547       <http://redis.io/commands#strings> with key representing the hash key.
548
549   append ( key, string )
550       Appends a value to a key and returns its new length.
551
552        $len = $oh->append( $key, "foo" );
553
554   decr ( key )
555       Decrements the value of a key by one and returns its new value.
556
557        $num = $oh->decr( $key );
558
559   decrby ( key, number )
560       Decrements the value of a key by the given number and returns its new
561       value.
562
563        $num = $oh->decrby( $key, 2 );
564
565   getdecr ( key )
566       Decrements the value of a key by one and returns its old value.
567
568        $old = $oh->getdecr( $key );
569
570   getincr ( key )
571       Increments the value of a key by one and returns its old value.
572
573        $old = $oh->getincr( $key );
574
575   getset ( key, value )
576       Sets the value of a key and returns its old value.
577
578        $old = $oh->getset( $key, "baz" );
579
580   incr ( key )
581       Increments the value of a key by one and returns its new value.
582
583        $num = $oh->incr( $key );
584
585   incrby ( key, number )
586       Increments the value of a key by the given number and returns its new
587       value.
588
589        $num = $oh->incrby( $key, 2 );
590

CREDITS

592       Many thanks to David Golden for Hash::Ordered. This implementation is
593       inspired by Hash::Ordered v0.009.
594

MOTIVATION

596       I wanted an ordered hash implementation for use with MCE::Shared
597       without any side effects. For example, linear scans, slow deletes, or
598       excessive memory consumption. The closest module on CPAN to pass in
599       this regard is Hash::Ordered by David Golden.
600
601       MCE::Shared has only one shared-manager process which is by design.
602       Therefore, re-factored tombstone deletion with extras for lesser impact
603       to the rest of the library. This module differs in personality from
604       Hash::Ordered mainly for compatibility with the other classes included
605       with MCE::Shared.
606
607       The following simulates a usage pattern inside MCE::Hobo involving
608       random key deletion. For example, an application joining a list of
609       Hobos provided by "MCE::Hobo->list_joinable".
610
611        use MCE::Shared::Ordhash;
612        use List::Util 'shuffle';
613        use Time::HiRes 'time';
614
615        srand 0;
616
617        my $oh = MCE::Shared::Ordhash->new();
618        my $num_keys = 200000;
619        my $start = time();
620
621        $oh->set($_,$_) for 1 .. $num_keys;
622
623        for ( shuffle $oh->keys ) {
624           $oh->delete($_);
625        }
626
627        printf "duration: %7.03f secs\n", time() - $start;
628
629       Both the runtime and memory consumption are captured for the
630       demonstration.  Results are included for MCE::Shared::Hash (unordered
631       hash) for comparison.
632
633        for ( shuffle $oh->keys ) { $oh->delete($_) }
634
635        0.378 secs.  35 MB  MCE::Shared::Hash (unordered)
636        0.437 secs.  49 MB  Tie::Hash::Indexed (XS)
637        0.743 secs.  54 MB  MCE::Shared::Ordhash
638        1.028 secs.  60 MB  Hash::Ordered
639        1.752 secs. 112 MB  Tie::LLHash
640         > 42 mins.  66 MB  Tie::IxHash
641
642       Using the same demonstration above, another usage pattern inside
643       MCE::Hobo involves orderly hash-key deletion. For example, waiting for
644       and joining all Hobos provided by "MCE::Hobo->list".
645
646        for ( $oh->keys ) { $oh->delete($_) }
647
648        0.353 secs.  35 MB  MCE::Shared::Hash (unordered)
649        0.349 secs.  49 MB  Tie::Hash::Indexed (XS)
650        0.452 secs.  41 MB  MCE::Shared::Ordhash
651        0.735 secs.  54 MB  Hash::Ordered
652        1.338 secs. 112 MB  Tie::LLHash
653         > 42 mins.  66 MB  Tie::IxHash
654
655       No matter if orderly or randomly, even backwards, hash-key deletion in
656       "MCE::Shared::Ordhash" performs reasonably well. The following provides
657       the construction used for the modules mentioned.
658
659        my $oh = Hash::Ordered->new();
660           $oh->set($_,$_);   $oh->keys;  $oh->delete($_);
661
662        my $oh = Tie::Hash::Indexed->new();
663           $oh->set($_,$_);   $oh->keys;  $oh->delete($_);
664
665        my $oh = Tie::IxHash->new();
666           $oh->STORE($_,$_); $oh->Keys;  $oh->DELETE($_);
667
668        my $oh = tie my %hash, 'Tie::LLHash';
669           $oh->last($_,$_);  keys %hash; $oh->DELETE($_);
670
671       Hash::Ordered is supported for use with MCE::Shared. This includes on-
672       demand hash-like dereferencing, similarly to "hash" and "ordhash".
673
674        use feature 'say';
675
676        use MCE::Hobo;
677        use MCE::Shared;
678        use Hash::Ordered; # 0.010 or later
679
680        my $ha = MCE::Shared->hash();    # shared MCE::Shared::Hash
681        my $oh = MCE::Shared->ordhash(); # shared MCE::Shared::Ordhash
682
683        my $ho = MCE::Shared->share( Hash::Ordered->new() );
684
685        sub parallel_task {
686           my ($id) = @_;
687
688           # OO interface
689           if ($id == 1) {
690              $ha->set("$id", "foo");
691              $oh->set("$id", "foo");
692              $ho->set("$id", "foo");
693           }
694           # hash-like dereferencing
695           elsif ($id == 2) {
696              $ha->{"$id"} = "baz";
697              $oh->{"$id"} = "baz";
698              $ho->{"$id"} = "baz";
699           }
700
701           return;
702        }
703
704        MCE::Hobo->create("parallel_task", $_) for 1..2;
705        MCE::Hobo->waitall;
706
707        say $ha->{"1"};     # foo
708        say $oh->{"1"};
709        say $ho->{"1"};
710
711        say $ha->get("2");  # baz
712        say $oh->get("2");
713        say $ho->get("2");
714

SEE ALSO

716       •  Hash::Ordered
717
718       •  Tie::Hash::Indexed
719
720       •  Tie::IxHash
721
722       •  Tie::LLHash
723

INDEX

725       MCE, MCE::Hobo, MCE::Shared
726

AUTHOR

728       Mario E. Roy, <marioeroy AT gmail DOT com>
729
730
731
732perl v5.32.1                      2021-01-27           MCE::Shared::Ordhash(3)
Impressum