ptg10805159
Section 20.8 Source Code 779
720 /*
721 * Rewind the index file for db_nextrec.
722 * Automatically called by db_open.
723 * Must be called before first db_nextrec.
724 */
725 void
726 db_rewind(DBHANDLE h)
727 {
728 DB *db = h;
729 off_t offset;
730 offset=(db->nhash + 1) * PTR_SZ; /* +1 for free list ptr */
731 /*
732 * We’re just setting the file offset for this process
733 * to the start of the index records; no need to lock.
734 * +1 below for newline at end of hash table.
735 */
736 if ((db->idxoff = lseek(db->idxfd, offset+1, SEEK_SET)) == -1)
737 err_dump("db_rewind: lseek error");
738 }
739 /*
740 * Return the next sequential record.
741 * We just step our way through the index file, ignoring deleted
742 * records. db_rewind must be called before this function is
743 * called the first time.
744 */
745 char *
746 db_nextrec(DBHANDLE h, char *key)
747 {
748 DB *db = h;
749 char c;
750 char *ptr;
[720 – 738] Thedb_rewindfunction is used to reset the database to ‘‘the beginning;’’
we set the file offset for the index file to point to the first record in the index
file (immediately following the hash table). (Recall the structure of the index
file from Figure20.2.)
[739 – 750] Thedb_nextrecfunction returns the next record in the database. The
return value is a pointer to the data buffer.Ifthe caller provides a non-null
value for thekeyparameter,the corresponding key is copied to this address.
The caller is responsible for allocating a buffer big enough to storethe key.
Abuffer whose size isIDXLEN_MAXbytes is large enough to hold any key.
Records arereturned sequentially, in the order that they happen to be stored
in the database file. Thus, the records arenot sorted by key value. Also,
because we do not follow the hash chains, we can come across records that
have been deleted, but we will not return these to the caller.