Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 20.8 Source Code 765


287 /*
288 * Read a chain ptr field from anywhere in the index file:
289 * the free list pointer, a hash table chain ptr, or an
290 * index record chain ptr.
291 */
292 static off_t
293 _db_readptr(DB *db, off_t offset)
294 {
295 char asciiptr[PTR_SZ + 1];
296 if (lseek(db->idxfd, offset, SEEK_SET) == -1)
297 err_dump("_db_readptr: lseek error to ptr field");
298 if (read(db->idxfd, asciiptr, PTR_SZ) != PTR_SZ)
299 err_dump("_db_readptr: read error of ptr field");
300 asciiptr[PTR_SZ]=0;/*null terminate */
301 return(atol(asciiptr));
302 }
303 /*
304 * Read the next index record. We start at the specified offset
305 * in the index file. We read the index record into db->idxbuf
306 * and replace the separators with null bytes. If all is OK we
307 * set db->datoff and db->datlen to the offset and length of the
308 * corresponding data record in the data file.
309 */
310 static off_t
311 _db_readidx(DB *db, off_t offset)
312 {
313 ssize_t i;
314 char *ptr1, *ptr2;
315 char asciiptr[PTR_SZ + 1], asciilen[IDXLEN_SZ + 1];
316 struct iovec iov[2];

[287 – 302] _db_readptrreads any one of three different chain pointers: (a) the pointer
at the beginning of the index file that points to the first index record on the
free list, (b) the pointers in the hash table that point to the first index record
on each hash chain, and (c) the pointers that arestored at the beginning of
each index record(whether the index record is part of a hash chain or on the
free list). We convert the pointer from ASCII to a long integer before
returning it. No locking is done by this function; that is up to the caller.
[303 – 316] The_db_readidxfunction is used to read the record at the specified offset
from the index file. On success, the function will return the offset of the next
record in the list. In this case, the function will populate several fields in the
DBstructure:idxoffcontains the offset of the current record in the index
file,ptrvalcontains the offset of the next index entry in the list,idxlen
contains the length of the current index record,idxbufcontains the actual
index record,datoffcontains the offset of the record in the data file, and
datlencontains the length of the data record.
Free download pdf