| App-Context documentation | Contained in the App-Context distribution. |
App::SharedDatastore - Interface for sharing data between processes
use App;
$context = App->context();
$sds = $context->service("SharedDatastore");
$sds = $context->shared_datastore();
A SharedDatastore service represents a single hash in which scalars or deep references may be stored (basically an MLDBM).
The constructor is inherited from
App::Service|App::Service/"new()".
* Signature: $sds->set($key, $value);
* Signature: $sds->set($key, $value, $options);
* Param: $key scalar
* Param: $value scalar
* Param: $options HASH (optional)
* Return: void
$sds->set($key,$value);
$options = {
info_columns => [ "col1", "col2" ],
info_values => [ "value1", "value2" ],
};
$sds->set($key, $value, $options);
* Signature: $value = $sds->get($key);
* Param: $key scalar
* Return: $value scalar
$value = $sds->get($key);
* Signature: $sds->set_ref($keyref,$valueref);
* Signature: $sds->set_ref($keyref,$valueref,$options);
* Param: $keyref anything (ref or scalar)
* Param: $valueref anything (ref or scalar)
* Param: $options HASH (optional)
* Return: void
$sds->set_ref($keyref, $valueref);
$options = {
info_columns => [ "col1", "col2" ],
info_values => [ "value1", "value2" ],
};
$sds->set_ref($keyref, $valueref, $options);
* Signature: $valueref = $sds->get_ref($keyref);
* Param: $keyref anything (ref or scalar)
* Return: $valueref anything (ref or scalar)
$valueref = $sds->get_ref($keyref);
* Signature: $blob = $sds->serialize($ref);
* Return: $ref any (ref)
* Return: $blob scalar (binary)
$blob = $sds->serialize($ref);
* Signature: $ref = $sds->deserialize($blob);
* Param: $blob scalar (binary)
* Return: $ref any (ref)
$ref = $sds->deserialize($blob);
* Signature: $hashkey = $sds->hashkey($keyref);
* Return: $keyref any (ref or scalar)
* Return: $hashkey scalar
$hashkey = $sds->hashkey($keyref);
Returns "SharedDatastore";
* Signature: $service_type = App::SharedDatastore->service_type();
* Param: void
* Return: $service_type string
$service_type = $sds->service_type();
* Author: Stephen Adkins <spadkins@gmail.com> * License: This is free software. It is licensed under the same terms as Perl itself.
App::Context|App::Context,
App::Service|App::Service
| App-Context documentation | Contained in the App-Context distribution. |
############################################################################# ## $Id: SharedDatastore.pm 10851 2008-02-28 19:50:01Z spadkins $ ############################################################################# package App::SharedDatastore; $VERSION = (q$Revision: 10851 $ =~ /(\d[\d\.]*)/)[0]; # VERSION numbers generated by svn use App; use App::Service; @ISA = ( "App::Service" ); use strict; use Storable qw(nfreeze thaw); use Digest::SHA qw(sha1_hex); $Storable::canonical = 1; # this will cause hashes to be serialized the same way every time
############################################################################# # CLASS GROUP #############################################################################
############################################################################# # CLASS #############################################################################
############################################################################# # CONSTRUCTOR METHODS #############################################################################
############################################################################# # new() #############################################################################
############################################################################# # _init() #############################################################################
sub _init { &App::sub_entry if ($App::trace); my ($self) = @_; $self->{data} = {}; if ($self->{compress}) { require Compress::Zlib; } if ($self->{base64}) { App->use("MIME::Base64"); } &App::sub_exit() if ($App::trace); } ############################################################################# # PUBLIC METHODS #############################################################################
############################################################################# # set() #############################################################################
sub set { &App::sub_entry if ($App::trace); my ($self, $key, $value, $options) = @_; $self->{data}{$key} = $value; &App::sub_exit() if ($App::trace); } ############################################################################# # get() #############################################################################
sub get { &App::sub_entry if ($App::trace); my ($self, $key) = @_; my $value = $self->{data}{$key}; &App::sub_exit($value) if ($App::trace); return($value); } ############################################################################# # set_ref() #############################################################################
sub set_ref { &App::sub_entry if ($App::trace); my ($self, $keyref, $valueref, $options) = @_; my $hashkey = $self->hashkey($keyref); my $blob = $self->serialize($valueref); $self->set($hashkey, $blob, $options); &App::sub_exit() if ($App::trace); } ############################################################################# # get_ref() #############################################################################
sub get_ref { &App::sub_entry if ($App::trace); my ($self, $keyref) = @_; my $hashkey = $self->hashkey($keyref); my $blob = $self->get($hashkey); my ($valueref); if (defined $blob) { eval { $valueref = $self->deserialize($blob); }; # we want to catch errors in derialization which may occur due to version mismatches in the Storable module # (see "man Storable" in section on "FORWARD COMPATIBILITY") if ($@) { my $context = $self->{context}; $context->log("WARNING: DataStore($self->{name})->get_ref($hashkey): $@"); } } &App::sub_exit($valueref) if ($App::trace); return($valueref); } ############################################################################# # serialize() #############################################################################
sub serialize { &App::sub_entry if ($App::trace); my ($self, $ref) = @_; my ($blob); if (defined $ref) { $blob = nfreeze($ref); if ($self->{compress}) { $blob = Compress::Zlib::memGzip($blob); } if ($self->{base64}) { $blob = MIME::Base64::encode($blob); } } else { $blob = undef; } &App::sub_exit("<frozen-ref>") if ($App::trace); return($blob); } ############################################################################# # deserialize() #############################################################################
sub deserialize { &App::sub_entry if ($App::trace); my ($self, $blob) = @_; my ($ref); if (defined $blob) { if ($self->{base64}) { $blob = MIME::Base64::decode($blob); } if ($self->{compress}) { $blob = Compress::Zlib::memGunzip($blob); } $ref = thaw($blob); } else { $ref = undef; } &App::sub_exit($ref) if ($App::trace); return($ref); } ############################################################################# # hashkey() #############################################################################
sub hashkey { &App::sub_entry if ($App::trace); my ($self, $keyref) = @_; my ($hashkey); if (ref($keyref)) { $hashkey = sha1_hex(nfreeze($keyref)); } elsif (length($keyref) == 40 && $keyref =~ /^[a-f0-9]+$/) { $hashkey = $keyref; } else { $hashkey = sha1_hex($keyref); } &App::sub_exit($hashkey) if ($App::trace); return($hashkey); } ############################################################################# # PROTECTED METHODS #############################################################################
############################################################################# # Method: service_type() #############################################################################
sub service_type () { "SharedDatastore"; }
1;