Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 20.8 Source Code 769


412 /*
413 * Delete the current record specified by the DB structure.
414 * This function is called by db_delete and db_store, after
415 * the record has been located by _db_find_and_lock.
416 */
417 static void
418 _db_dodelete(DB *db)
419 {
420 int i;
421 char *ptr;
422 off_t freeptr, saveptr;
423 /*
424 * Set data buffer and key to all blanks.
425 */
426 for (ptr = db->datbuf, i = 0; i < db->datlen - 1; i++)
427 *ptr++=SPACE;
428 *ptr=0;/*null terminate for _db_writedat */
429 ptr=db->idxbuf;
430 while (*ptr)
431 *ptr++=SPACE;

432 /*
433 * We have to lock the free list.
434 */
435 if (writew_lock(db->idxfd, FREE_OFF, SEEK_SET, 1) < 0)
436 err_dump("_db_dodelete: writew_lock error");

437 /*
438 * Write the data record with all blanks.
439 */
440 _db_writedat(db, db->datbuf, db->datoff, SEEK_SET);

[412 – 431] The_db_dodeletefunction does all the work necessary to delete a record
from the database. (This function is also called bydb_store.) Most of the
function just updates two linked lists: the free list and the hash chain for this
key.When a record is deleted, we set its key and data record to blanks. This
fact is used bydb_nextrec,which we’ll examine later in this section.
[432 – 440] We callwritew_lockto write lock the free list. This step prevents two
processes that aredeleting records at the same time, on two different hash
chains, from interfering with each other.Since we’ll add the deleted record
to the free list, which changes the free-list pointer,only one process at a time
can be doing this.
We write the all-blank data record by calling_db_writedat.Note that
there is no need for_db_writedatto lock the data file in this case. Since
db_deletehas write locked the hash chain for this record, we know that no
other process is reading or writing this particular data record.
Free download pdf