ptg10805159
Section 20.8 Source Code 777
655 /*
656 * Same size data, just replace data record.
657 */
658 _db_writedat(db, data, db->datoff, SEEK_SET);
659 db->cnt_stor4++;
660 }
661 }
662 rc=0;/* OK */
663 doreturn: /* unlock hash chain locked by _db_find_and_lock */
664 if (un_lock(db->idxfd, db->chainoff, SEEK_SET, 1) < 0)
665 err_dump("db_store: un_lock error");
666 return(rc);
667 }
668 /*
669 * Try to find a free index record and accompanying data record
670 * of the correct sizes. We’re only called by db_store.
671 */
672 static int
673 _db_findfree(DB *db, int keylen, int datlen)
674 {
675 int rc;
676 off_t offset, nextoffset, saveoffset;
677 /*
678 * Lock the free list.
679 */
680 if (writew_lock(db->idxfd, FREE_OFF, SEEK_SET, 1) < 0)
681 err_dump("_db_findfree: writew_lock error");
682 /*
683 * Read the free list pointer.
684 */
685 saveoffset=FREE_OFF;
686 offset=_db_readptr(db, saveoffset);
[655 – 661] Case 4: An existing record is being replaced, and the length of the new data
recordequals the length of the existing data record. This is the easiest case;
we simply rewrite the data recordand increment the counter (cnt_stor4)
for this case.
[662 – 667] In the normal case, we set the return code to indicate success and fall
through to the common return logic. We unlock the hash chain that was
locked as a result of calling_db_find_and_lockand return to the caller.
[668 – 686] The_db_findfreefunction tries to find a free index recordand associated
data record of the specified sizes.We need to write lock the free list to avoid
interfering with any other processes using the free list. After locking the free
list, we get the pointer address at the head of the list.