Mailing List Archive

storing mdim structures in DBM file
Tom, here's a more efficient version (without the hacks) of the
the stuff I sent you in the morning.

- Sarathy.
gsar@engin.umich.edu
[cc-ed to p5p in case anyone can use this]
--------------------------------8<-------------------------------------
#
# MLDBM - storing complex structures in DBM files
#
# refs are stringified and stored. non-refs are stored
# in the native format
#
# needs SDBM and the Data::Dumper package (available at CPAN)
#
# gsar@umich.edu
#

package MLDBM;

require SDBM_File;
require TieHash;

use Data::Dumper;

@ISA = qw(TieHash);

# this has to be something unique since we try to store
# stuff natively if it is not a ref
$key = 'CrYpTiCkEy';

sub TIEHASH {
my $c = shift;
return bless { db => SDBM_File->TIEHASH(@_) }, $c;
}

sub FETCH {
my($s, $k) = @_;
my $ret = $s->{db}->FETCH($k);
if ($ret =~ /^\$$key/o) {
eval "undef \$$key;" . $ret;
if ($@) {
warn "MLDBM error: $@\twhile evaluating:\n $ret";
$ret = undef;
}
else {
$ret = $$key;
}
}
return $ret;
}

sub STORE {
my($s, $k, $v) = @_;
if (ref $v) {
local $Data::Dumper::Indent = 0;
local $Data::Dumper::Purity = 1;
$v = Data::Dumper->Dump([$v], [$key]);
}
$s->{db}->STORE($k, $v);
}

sub DELETE {
my $s = shift;
$s->{db}->DELETE(@_);
}

sub FIRSTKEY {
my $s = shift;
$s->{db}->FIRSTKEY(@_);
}

sub NEXTKEY {
my $s = shift;
$s->{db}->NEXTKEY(@_);
}

1;
__END__
# try this
use Fcntl; # to get 'em constants
use MLDBM;
use Data::Dumper;
tie %o, MLDBM, 'testmldbm', O_CREAT|O_RDWR, 0640 or die $!;

$c = [\'c'];
$b = {};
$a = [1, $b, $c];
$b->{a} = $a;
$b->{b} = $a->[1];
$b->{c} = $a->[2];
@o{qw(a b c)} = ($a, $b, $c);
print Data::Dumper->Dump([@o{qw(a b c)}], [qw(a b c)]);
Re: storing mdim structures in DBM file [ In reply to ]
On Tue, 21 Nov 1995 11:34:12 +0100, teachers wrote:
>don't you think that unless you use DB_File or GDBM_File
>that the size would become too big?
>

Sure, I don't have either DB_File or GDBM_File installed properly,
that's all.

To get the maximum possible efficiency, Data::Dumper will probably
have to be written (esp. the internal recursive routine) in XS.
Perhaps one of these days..

- Sarathy.
gsar@engin.umich.edu
Re: storing mdim structures in DBM file [ In reply to ]
don't you think that unless you use DB_File or GDBM_File
that the size would become too big?

--tom
Re: storing mdim structures in DBM file [ In reply to ]
>On Tue, 21 Nov 1995 11:34:12 +0100, teachers wrote:
>>don't you think that unless you use DB_File or GDBM_File
>>that the size would become too big?
>>

>Sure, I don't have either DB_File or GDBM_File installed properly,
>that's all.

>To get the maximum possible efficiency, Data::Dumper will probably
>have to be written (esp. the internal recursive routine) in XS.
>Perhaps one of these days..

hm... like Storable?

--tom
Re: storing mdim structures in DBM file [ In reply to ]
> From: Gurusamy Sarathy <gsar@engin.umich.edu>
> Date: Mon, 20 Nov 1995 18:04:13 -0500
>
> # MLDBM - storing complex structures in DBM files
> # refs are stringified and stored. non-refs are stored
> # in the native format

> sub STORE {
> my($s, $k, $v) = @_;
> if (ref $v) {
> local $Data::Dumper::Indent = 0;
> local $Data::Dumper::Purity = 1;
> $v = Data::Dumper->Dump([$v], [$key]);
> }
> $s->{db}->STORE($k, $v);
> }

Where did $key come from?

You need a dose of 'strict'ness!

:-)

Tim.

p.s. I'm reading old mail in random access mode now!
Re: storing mdim structures in DBM file [ In reply to ]
On Wed, 06 Dec 1995 23:08:42 GMT, Tim Bunce wrote:
>
>> From: Gurusamy Sarathy <gsar@engin.umich.edu>
>> Date: Mon, 20 Nov 1995 18:04:13 -0500
>>
>> # MLDBM - storing complex structures in DBM files
>> # refs are stringified and stored. non-refs are stored
>> # in the native format
>
>> sub STORE {
>> my($s, $k, $v) = @_;
>> if (ref $v) {
>> local $Data::Dumper::Indent = 0;
>> local $Data::Dumper::Purity = 1;
>> $v = Data::Dumper->Dump([$v], [$key]);
>
>Where did $key come from?
>

It is a package global (declared at the top of file).

>You need a dose of 'strict'ness!
>
>:-)
>

Sure do. :-)

>Tim.
>
>p.s. I'm reading old mail in random access mode now!
>

I saw the overnight downpour of 54 inches of mailrain,
and thought to myself, Tim is back! Thanks for your
incessant contribution.

- Sarathy.
gsar@engin.umich.edu