Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

746 ADatabase Library Chapter 20


#include "apue_db.h"
int db_delete(DBHANDLEdb,const char *key);
Returns: 0 if OK,−1 if recordnot found
In addition to fetching a record by specifying its key,wecan go through the entire
database, reading each record in turn. To do this, we first calldb_rewindto rewind the
database to the first recordand then calldb_nextrecin a loop to read each sequential
record.
#include "apue_db.h"
void db_rewind(DBHANDLEdb);
char *db_nextrec(DBHANDLEdb,char *key);
Returns: pointer to data if OK,NULLon end of file
Ifkeyis a non-null pointer,db_nextrecreturns the key by copying it to the memory
starting at that location.
There is no order to the records returned bydb_nextrec.All we’reguaranteed is
that we’ll read each record in the database once. If we storethree records with keys of
A, B, and C, in that order, we have no idea in which orderdb_nextrecwill return the
three records. It might return B, then A, then C, or some other (apparently random)
order.The actual order depends on the implementation of the database.
These seven functions provide the interface to the database library.Wenow
describe the actual implementation that we have chosen.

20.4 Implementation Over view


Database access libraries often use two files to storethe information: an index file and a
data file. The index file contains the actual index value (the key) and a pointer to the
corresponding data record in the data file. Numerous techniques can be used to
organize the index file so that it can be searched quickly and efficiently for any key:
hashing and B+ trees arepopular.Wehave chosen to use a fixed-size hash table with
chaining for the index file.We mentioned in the description ofdb_openthat we create
two files: one with a suffix of.idxand one with a suffix of.dat.
We storethe key and the index as null-terminated character strings; they cannot
contain arbitrary binary data. Some database systems storenumerical data in a binary
format (1, 2, or 4 bytes for an integer,for example) to save storage space. This
complicates the functions and requires morework to make the database files portable
between different computer systems. For example, if a network has two systems that
use different formats for storing binary integers, we need to account for this difference if
we want both systems to access the database. (It is not at all uncommon today to have
systems with different architectures sharing files on a network.) Storing all the records,
both keys and data, as character strings simplifies everything. It does requireadditional
disk space, but that is a small cost for portability.
Free download pdf