O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

DBM Databases

Problem

You need to access a DBM file.

Solution

Use my code, or SleepyCat’s code, to interface DBM from Java.

Discussion

Unix systems are commonly supplied with some form of DBM or DB[47] data file, often called a database. These are not relational databases, but are key/value pairs, rather like a java.util.Hashtable that is automatically persisted to disk whenever you called its put( ) method. This format is also used on MS-Windows by a few programs; for example, the Win32 version of Netscape keeps its history in a history.db or netscape.hst file, which is in this format. Not convinced?

daroad.darwinsys.com$ pwd
/c/program files/netscape/users/ian
daroad.darwinsys.com$ file *.hst     
netscape.hst: Berkeley DB Hash file (Version 2, Little Endian, Bucket Size 4096,
Bucket Shift 12, Directory Size 256, Segment Size 256, Segment Shift 8, Overflow 
Point 8, Last Freed 36, Max Bucket 184, High Mask 0xff, Low Mask 0x7f, Fill Factor
54, Number of Keys 733)
daroad.darwinsys.com$

The Unix file command[48] decodes file types; it’s what Unix people rely on instead of (or in addition to) filename extensions.

So the DBM format is a nice, general mapping from keys to values. But how can we use it in Java? There is no publicly defined mapping for Java, so I wrote my own. It uses a fair bit of native code, that is, C code called from Java that in turn calls the DBM library. I’ll discuss native code in Section 26.5. For now it suffices to know that we can initialize a DBM file by calling the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required