Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Please note that there are already modules that do this: DBM::Deep and MLDBM come to mind.

Here are a couple of drop-in functions to return a hash or array reference to a temporary DB_File. Not really a new idea - just had to be put together for ease.

This serves as a useful hack if we wanted our large data structure to be on-disk instead of in-memory. Remember to untie the references when done using them.

  • Use db_ref() to generate a new hash/array reference.
  • Use db_open_ref() to open an existing DB_File.
use warnings; use strict; use Data::Dumper; my $h1 = db_ref("HASH"); my $h2 = db_ref("HASH"); $h1->{hello} = { 123 => 456, 789 => [ 3, 4, 5 ] }; $h1->{crazy} = 747; $h2->{world} = [ 5, 6, 7, $h1->{hello} ]; print "key> $_\n" for keys %{ $h1 }; print Dumper($h2->{world}); untie %{ $h1 }; untie %{ $h2 }; my $a = db_ref("ARRAY", 1); # no serialization push @{ $a }, 1..4; print "item> $_\n" for @{ $a }; untie @{ $a };
sub db_ref { my $type = shift() || "HASH"; # "HASH" = DB_HASH # "BTREE" = DB_BTREE # others = DB_RECNO my $simple_db = shift(); # set true to disable serialized DB my $keep_db = shift(); # set true to not remove DB upon exit # optional 4th arg = CODE ref for filter_store_value() # optional 5th arg = CODE ref for filter_fetch_value() require File::Temp; require File::Spec; # create temporary file (undef, my $filename) = File::Temp::tempfile( File::Spec->catfile( File::Spec->tmpdir(), substr($type, 0, 1) . "_XXXXXX", ), UNLINK => !$keep_db, ); # open new DB with temporary file return db_open_ref($filename, $type, $simple_db, @_); } sub db_open_ref { my $filename = shift(); my $type = shift() || "HASH"; my $simple_db = shift(); # optional 4th arg = CODE ref for filter_store_value() # optional 5th arg = CODE ref for filter_fetch_value() return () unless defined($filename); require DB_File; require Fcntl; # determine database type my $db_type; { no warnings qw(once); $db_type = $type eq "HASH" ? $DB_File::DB_HASH : $type eq "BTREE" ? $DB_File::DB_BTREE : $DB_File::DB_RECNO; } # return tied hash/array reference to database my $db_ref; my $db = tie( ($type eq "HASH" || $type eq "BTREE" ? %{ $db_ref } : @{ $db_ref } ), "DB_File", $filename, Fcntl::O_RDWR() | Fcntl::O_CREAT(), 0600, $db_type, ) or return (); # add DB filters to serialize complex data structures unless ($simple_db) { require Storable unless @_ >= 2; $db->filter_store_value(shift() || sub { $_ = Storable::freeze +(\$_) }); $db->filter_fetch_value(shift() || sub { $_ = ${ Storable::tha +w($_) } }); } undef $db; # avoid untie() gotcha return $db_ref; }

In reply to On-disk hash/array data structures by repellent

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-03-29 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found