ptg10805159
Section 20.8 Source Code 775
597 /*
598 * _db_find_and_lock locked the hash chain for us; read
599 * the chain ptr to the first index record on hash chain.
600 */
601 ptrval=_db_readptr(db, db->chainoff);
602 if (_db_findfree(db, keylen, datlen) < 0) {
603 /*
604 * Can’t find an empty record big enough. Append the
605 * new record to the ends of the index and data files.
606 */
607 _db_writedat(db, data, 0, SEEK_END);
608 _db_writeidx(db, key, 0, SEEK_END, ptrval);
609 /*
610 * db->idxoff was set by _db_writeidx. The new
611 * record goes to the front of the hash chain.
612 */
613 _db_writeptr(db, db->chainoff, db->idxoff);
614 db->cnt_stor1++;
615 } else {
616 /*
617 * Reuse an empty record. _db_findfree removed it from
618 * the free list and set both db->datoff and db->idxoff.
619 * Reused record goes to the front of the hash chain.
620 */
621 _db_writedat(db, data, db->datoff, SEEK_SET);
622 _db_writeidx(db, key, db->idxoff, SEEK_SET, ptrval);
623 _db_writeptr(db, db->chainoff, db->idxoff);
624 db->cnt_stor2++;
625 }
[597 – 601] After we call_db_find_and_lock,the code divides into four cases. In the
first two, no recordwas found, so we areadding a new record. Weread the
offset of the first entry on the hash list.
[602 – 614] Case 1: we call_db_findfreeto search the free list for a deleted record
with the same size key and same size data. If no such record is found, we
have to append the new record to the ends of the index and data files. We
call_db_writedatto write the data part,_db_writeidxto write the
index part, and_db_writeptrto place the new record on the front of the
hash chain. We increment a count (cnt_stor1) of the number of times we
executed this case to allow us to characterize the behavior of the database.
[615 – 625] Case 2:_db_findfreefound an empty recordwith the correct sizes and
removed it from the free list (we’ll see the implementation of
_db_findfreeshortly). Wewrite the data and index portions of the new
recordand add the record to the front of the hash chain as we did in case 1.
Thecnt_stor2field counts how many times we’ve executed this case.