|
NAMEGenezzo::PushHash::PushHash.pm - an impure virtual class module that defines a "push hash", a hash that generates its own unique key for each value. Values are "pushed" into the hash, similar to pushing into an array.SYNOPSISuse Genezzo::PushHash::PushHash; my %tied_hash = (); my $tie_val = tie %tied_hash, 'Genezzo::PushHash::PushHash'; my $newkey = $tie_val->HPush("this is a test"); $tied_hash{$newkey} = "update this entry"; my $getcount = $tie_val->HCount(); DESCRIPTIONWhile standard perl hashes are a form of associative array, where the user supplies a key/value pair, a PushHash is more like a multiset which generates its own unique key for each element. The preferred usage is to use the HPush method, which returns the new key, but you can use the PUSH "pseudo key" to generate a new key, e.g.:$tied_hash{PUSH} = "new
value";
Note that the result of the underlying STORE only returns the pushed value, not the new key. Also, expressions like: my $pushval = tied_hash{PUSH} =
"new value";
can be problematic, since the tie may try to return a FETCH of key "PUSH", which will not work. WHYPush hashes can be used to restructure code based upon references to anonymous hashes or arrays in order to facilitate the persistent storage of data structures. Also, they are useful for implementing shared data structures or data structures with transactional update semantics, where you would want concurrent access and quick unique key generation. In addition, they can be used to create data structures larger than main memory, or handle cases where multiple keys or key traversal mechanisms get mapped to the same data. In other words, they are similar to SQL database ties or tied DB hashes, but potentially more flexible and extensible.FUNCTIONSPushHashes support all standard hash operations, with the exception that you cannot create or insert a user key -- you must push new entries and use the generated key or basic iteration to retrieve your data. It also supports two additional methods, HPush and HCount. Note that these methods are associated with the tie value (i.e. the blessed ref for the PushHash class), not the tied hash.
Why use a distinctive HPush function versus an array-like PUSH?HPush is designed to support quick appends of a single value to a push-hash and return the new key, or return an undef if the push fails. The basic perl "push" appends a LIST and returns the new number of elements in the array.
Why use a distinctive HCount function versus an array-like FETCHSIZE?The distinction is subtle. An array FETCHSIZE is a cheap operation that just returns the number of elements in the array. HCount is a potentially expensive operation that returns the number of valid data elements in the pushhash. For the example of the disk-based persistent hash, the HCount could involve reading multiple files on disk and special operations to distinguish between valid and deleted data.EXPORTRADIXPOINT - by default ".". A separator for a multipart key.AUTHORJeffrey I. Cohen, jcohen@genezzo.comSEE ALSOperl(1).Copyright (c) 2003, 2004 Jeffrey I Cohen. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Address bug reports and comments to: jcohen@genezzo.com For more information, please visit the Genezzo homepage at <http://www.genezzo.com>
Visit the GSP FreeBSD Man Page Interface. |