Advanced Programming in the
UNIX Environment

by W. Richard Stevens
Addison-Wesley Professional Computing Series
0-201-56317-7 * Hardcover * 768 pages * ©1992
[Buy this book]

Chapter 16. A Database Library

Introduction * History * The Library * Implementation Overview * Centralized or Decentralized? * Concurrency * Source Code * Performance * Summary

Example Relevant Functions
16.1 Concurrent database Library
Usage example of database functions.
Create a database and write 3 records to it.
db_open, db_store, db_close,
err_sys (Stevens' lib),
err_quit (Stevens' lib),
exit
16.2 db.h
Header file defining the DB interfaces
The database is decentralized (handled
by multiple processes in parallel).
Locking is fine-grained (hash-chain based):
multiple readers and one writer
are allowed on each hash chain.
Functions appear below
16.3 db_open
Open or create a database
Same arguments as open(2)
strlen, strcpy, strcat, sprintf,
open, write, fstat,
_db_alloc, _db_free, db_rewind,
err_dump, err_sys (Stevens' lib),
writew_lock, un_lock (Stevens' lib),
16.4 _db_alloc
Allocate and initialize a DB structure
Internal (not an interface) function.
calloc, malloc,
err_dump (Stevens' lib),
16.5 _db_free
Free up a DB structure, including
all malloc'ed buffers it may point to.
Also close any open file descriptors
Internal (not an interface) function.
close, free,
err_dump (Stevens' lib)
16.6 db_close
User callable wrapper for _db_free
which does all closing/freeing
_db_free
16.7 db_fetch
fetch a specified record from DB
_db_find, _db_readdat,
un_lock (Stevens' lib),
err_dump (Stevens' lib)
16.8 _db_find
Find a specified record in the DB
Called by db_delete, db_fetch, and db_store
writew_lock (Stevens' lib),
readw_lock (Stevens' lib),
un_lock (Stevens' lib),
err_dump (Stevens' lib),
_db_hash, _db_readptr, _db_readidx,
strcmp
16.9 _db_hash.c
Calculate the hash value of a key.
May replace by more sophisticated versions
Internal (not an interface) function.
none (simple arithmetic)
16.10 _db_readptr
Return a chain pointer from the index file
The chain can be a free-list pointer,
a hash table chain ptr,
or an index record chain ptr.
Internal (not an interface) function.
lseek, read, atol,
err_dump (Stevens' lib)
16.11 _db_readidx
Reads each index record and divides
it into apropriate fields.
Internal (not an interface) function.
lseek, readv, read,
atol, atoi,
strchr,
err_dump (Stevens' lib)
16.12 _db_readdat
Read current data record into the data buffer
Return pointer to null terminated data buffer
Internal (not an interface) function.
lseek, read,
err_dump (Stevens' lib)
16.13 db_delete
Delete the specified record
_db_find, _db_dodelete,
un_lock (Stevens' lib),
err_dump (Stevens' lib),
16.14 _db_dodelete
Delete current record specified
by the db structure.
Called by db_delete, and db_store
after record has been located by _db_find
Internal (not an interface) function.
writew_lock (Stevens' lib),
_db_writedat, _db_writeidx,
_db_readptr, _db_writeptr,
un_lock (Stevens' lib),
err_dump (Stevens' lib),
16.15 _db_writedat
Write a data record
Called by _db_dodelete (to write a blank record)
and by db_store
Internal (not an interface) function.
writew_lock (Stevens' lib),
un_lock (Stevens' lib),
err_dump (Stevens' lib),
lseek, strlen, writev
16.16 _db_writeidx
Write an index record
Internal (not an interface) function.
err_quit (Stevens' lib),
err_dump (Stevens' lib),
writew_lock (Stevens' lib),
un_lock (Stevens' lib),
sprintf, strlen,
lseek, writev
16.17 _db_writeptr
Write a chain ptr field somewhere in the index file:
The free list, the hash table, or an index record.
Internal (not an interface) function.
err_quit (Stevens' lib),
err_dump (Stevens' lib),
sprintf, lseek, write
16.18 db_store
Store a record in the DB.
Check if exists (for replace data case),
find the record (or a free location in free list),
lock the chain, and update (or insert) it.
strlen,
un_lock (Stevens' lib),
err_dump (Stevens' lib),
_db_find, _db_findfree, _db_readptr,
_db_writedat, _db_writeidx,
_db_writeptr, _db_dodelete
16.19 _db_findfree
Find a free index record and an acompanying
data record of correct sizes.
Called only by db_store
Internal (not an interface) function.
writew_lock (Stevens' lib),
err_dump (Stevens' lib),
un_lock (Stevens' lib),
_db_readptr, _db_writeptr,
_db_readidx,
strlen
16.20 db_rewind
Prepare DB for sequential read.
Silently called by db_open.
Must be called before the first db_nextrec
lseek,
err_dump (Stevens' lib)
16.21 db_nextrec
Advance to next record (for sequential DB access)
readw_lock (Stevens' lib),
un_lock (Stevens' lib),
err_dump (Stevens' lib),
_db_readidx, _db_readdat,
strcpy

[Buy this book]