MLDBM::Easy - Provides NON-piecemeal access to MLDBM files


MLDBM-Easy documentation Contained in the MLDBM-Easy distribution.

Index


Code Index:

NAME

Top

MLDBM::Easy - Provides NON-piecemeal access to MLDBM files

SYNOPSIS

Top

  use MLDBM::Easy;  # as a drop-in for MLDBM

DESCRIPTION

Top

This module allows you to work with multi-dimensional databases, just like MLDBM, but it does work behind the scenes to allow you to treat the multi-dimensional database like a normal data structure. Basically, you don't need to use the piecemeal access that MLDBM required:

  # old and busted
  my $record = $db{some_key};
  $record->[2] += 100;
  $db{some_key} = $record;

  # new hotness
  $db{some_key}[2] += 100;

Of course, with this convenience comes a loss of speed. Deal with it.

SEE ALSO

Top

Check MLDBM for all other documentation.

AUTHOR

Top

Jeff japhy Pinyan, <japhy@pobox.com>

COPYRIGHT AND LICENSE

Top


MLDBM-Easy documentation Contained in the MLDBM-Easy distribution.

package MLDBM::Easy;

use warnings;

@ISA = 'MLDBM';
$VERSION = '0.01';

my %cache;

sub import {
  my $pkg = shift;
  require MLDBM;
  MLDBM->import(@_);
}


sub FETCH {
  my ($self, $key) = @_;
  my $ret = $self->{SR}->deserialize($self->{DB}->FETCH($key));
  return $cache{$self,$key} if $cache{$self,$key};

  if (ref($ret) eq "HASH") {
    tie my(%h), 'MLDBM::Easy::Hash', $self, $key, $ret;
    return $cache{$self,$key} = \%h;
  }

  if (ref($ret) eq "ARRAY") {
    tie my(@a), 'MLDBM::Easy::Array', $self, $key, $ret;
    return $cache{$self,$key} = \@a;
  }

  if (ref($ret) eq "SCALAR") {
    tie my($s), 'MLDBM::Easy::Scalar', $self, $key, $ret;
    return $cache{$self,$key} = \$s;
  }
}


sub STORE {
  my ($self, $key, $value) = @_;
  $value = $self->{SR}->serialize($cache{$self,$key});
  $self->{DB}->STORE($key, $value);
}


package MLDBM::Easy::Hash;
use Tie::Hash;
@ISA = 'Tie::StdHash';

sub TIEHASH {
  my ($class, $mldbm, $key, $href) = @_;
  my $self = bless $href, $class;
  $cache{$self} = [$mldbm, $key];
  return $self;
}

sub STORE {
  my ($self, $key, $value) = @_;
  $self->{$key} = $value;
  $cache{$self}->[0]->STORE($cache{$self}->[1], $self);
}


package MLDBM::Easy::Array;
use Tie::Array;
@ISA = 'Tie::StdArray';

sub TIEARRAY {
  my ($class, $mldbm, $key, $href) = @_;
  my $self = bless $href, $class;
  $cache{$self} = [$mldbm, $key];
  return $self;
}

sub STORE {
  my ($self, $idx, $value) = @_;
  $self->[$idx] = $value;
  $cache{$self}->[0]->STORE($cache{$self}->[1], $self);
}


package MLDBM::Easy::Scalar;
use Tie::Scalar;
@ISA = 'Tie::StdScalar';

sub TIESCALAR {
  my ($class, $mldbm, $key, $sref) = @_;
  my $self = bless $sref, $class;
  $cache{$self} = [$mldbm, $key];
  return $self;
}

sub STORE {
  my ($self, $value) = @_;
  $$self = $value;
  $cache{$self}->[0]->STORE($cache{$self}->[1], $self);
}


1;

__END__