ptg10805159
770 ADatabase Library Chapter 20
441 /*
442 * Read the free list pointer. Its value becomes the
443 * chain ptr field of the deleted index record. This means
444 * the deleted record becomes the head of the free list.
445 */
446 freeptr=_db_readptr(db, FREE_OFF);
447 /*
448 * Save the contents of index record chain ptr,
449 * before it’s rewritten by _db_writeidx.
450 */
451 saveptr=db->ptrval;
452 /*
453 * Rewrite the index record. This also rewrites the length
454 * of the index record, the data offset, and the data length,
455 * none of which has changed, but that’s OK.
456 */
457 _db_writeidx(db, db->idxbuf, db->idxoff, SEEK_SET, freeptr);
458 /*
459 * Write the new free list pointer.
460 */
461 _db_writeptr(db, FREE_OFF, db->idxoff);
462 /*
463 * Rewrite the chain ptr that pointed to this record being
464 * deleted. Recall that _db_find_and_lock sets db->ptroff to
465 * point to this chain ptr. We set this chain ptr to the
466 * contents of the deleted record’s chain ptr, saveptr.
467 */
468 _db_writeptr(db, db->ptroff, saveptr);
469 if (un_lock(db->idxfd, FREE_OFF, SEEK_SET, 1) < 0)
470 err_dump("_db_dodelete: un_lock error");
471 }
[441 – 461] We read the free-list pointer and then update the index record so that its next
recordpointer is set to the first recordonthe free list. (If the free list was
empty,this new chain pointer is 0.) We have already cleared the key.Then
we update the free-list pointer with the offset of the index record we are
deleting. This means that the free list is handled on a last-in, first-out basis;
that is, deleted records areadded to the front of the free list (although we
remove entries from the free list on a first-fit basis).
We don’t have a separate free list for each file. When we add a deleted index
record to the free list, the index recordstill points to the deleted data record.
Thereare better ways to do this, in exchange for added complexity.
[462 – 471] We update the previous record in the hash chain to point to the recordafter
the one we aredeleting, thus removing the deleted recordfromthe hash
chain. Finally, we unlock the free list.