Going Off the Beaten DB Path (cont'd)

Won't Break a Sweat Building a Hash

You can go a long way simply treating the Berkeley DB file as hash frozen in space, using only Perl's built-in functions to deal with it. The advantage over a standard Perl hash, of course, is that the Berkeley DB file is stored on disk instead of being held in memory, so it can be much larger. If you tried to build a hash with 10 million rows, for example, you'd run out of memory pretty quickly, but a Berkeley DB file could handle it without breaking a sweat.

In addition to Perl's built-in functions, there's a set of Berkeley DB API methods we can use to manipulate data and get results. To use those methods, you need to get the return value from the tie function and work with it.

$obj = tie %hash, "DB_File", "myDB";

# add a key/value pair
$obj->put("key4", "value4");

# delete a key/value pair
$obj->del("key3", "value3");

# find a value from a given key, assign it to $value
$obj->get("key2", $value);

The DB_BTREE file format stores keys in alphabetical order by default, but it's possible to alter the sort order if it suits your purposes. DB_BTREE can also handle duplicate key entries, by setting the R_DUP flag:


$DB_BTREE->{'flags'} = R_DUP;


  
Next: Assigning Multiple Values to a Single Key

1 Introduction 2 Designed for Programmers, not DBAs 3 Taking Berkeley to Task
4 T-i-e Function Does the Heavy Lifting 5 Won't Break a Sweat Building a Hash 6 Assigning Multiple Values to a Single Key


Get Help - Search Tips
 

Sleepycat Software

DevX e-Learning: Relational Database Concepts

DevX's Open Source Zone

DevX's Open Source.General discussion group

 TALK BACK
Have you ever worked with an open-source database? Has it served your purpose well? How does it compare with conventional DBs? Go here to start or join the discussion.
Click here to Join


Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers