home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
gnu
/
libg++-2.6.2.lha
/
libg++-2.6.2
/
libio
/
dbz
/
dbz.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-20
|
44KB
|
1,767 lines
/*
dbz.c V3.2
Copyright 1988 Jon Zeeff (zeeff@b-tech.ann-arbor.mi.us)
You can use this code in any manner, as long as you leave my name on it
and don't hold me responsible for any problems with it.
Hacked on by gdb@ninja.UUCP (David Butler); Sun Jun 5 00:27:08 CDT 1988
Various improvments + INCORE by moraes@ai.toronto.edu (Mark Moraes)
Major reworking by Henry Spencer as part of the C News project.
These routines replace dbm as used by the usenet news software
(it's not a full dbm replacement by any means). It's fast and
simple. It contains no AT&T code.
In general, dbz's files are 1/20 the size of dbm's. Lookup performance
is somewhat better, while file creation is spectacularly faster, especially
if the incore facility is used.
*/
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifndef __STDC__
extern int errno;
#endif
#include <dbz.h>
/*
* #ifdef index. "LIA" = "leave it alone unless you know what you're doing".
*
* FUNNYSEEKS SEEK_SET is not 0, get it from <unistd.h>
* INDEX_SIZE backward compatibility with old dbz; avoid using this
* NMEMORY number of days of memory for use in sizing new table (LIA)
* INCORE backward compatibility with old dbz; use dbzincore() instead
* DBZDEBUG enable debugging
* DEFSIZE default table size (not as critical as in old dbz)
* OLDBNEWS default case mapping as in old B News; set NOBUFFER
* BNEWS default case mapping as in current B News; set NOBUFFER
* DEFCASE default case-map algorithm selector
* NOTAGS fseek offsets are strange, do not do tagging (see below)
* NPAGBUF size of .pag buffer, in longs (LIA)
* SHISTBUF size of ASCII-file buffer, in bytes (LIA)
* MAXRUN length of run which shifts to next table (see below) (LIA)
* OVERFLOW long-int arithmetic overflow must be avoided, will trap
* NOBUFFER do not buffer hash-table i/o, B News locking is defective
*/
#ifdef FUNNYSEEKS
#include <unistd.h>
#else
#define SEEK_SET 0
#endif
#ifdef OVERFLOW
#include <limits.h>
#endif
static int dbzversion = 3; /* for validating .dir file format */
/*
* The dbz database exploits the fact that when news stores a <key,value>
* tuple, the `value' part is a seek offset into a text file, pointing to
* a copy of the `key' part. This avoids the need to store a copy of
* the key in the dbz files. However, the text file *must* exist and be
* consistent with the dbz files, or things will fail.
*
* The basic format of the database is a simple hash table containing the
* values. A value is stored by indexing into the table using a hash value
* computed from the key; collisions are resolved by linear probing (just
* search forward for an empty slot, wrapping around to the beginning of
* the table if necessary). Linear probing is a performance disaster when
* the table starts to get full, so a complication is introduced. The
* database is actually one *or more* tables, stored sequentially in the
* .pag file, and the length of linear-probe sequences is limited. The
* search (for an existing item or an empty slot) always starts in the
* first table, and whenever MAXRUN probes have been done in table N,
* probing continues in table N+1. This behaves reasonably well even in
* cases of massive overflow. There are some other small complications
* added, see comments below.
*
* The table size is fixed for any particular database, but is determined
* dynamically when a database is rebuilt. The strategy is to try to pick
* the size so the first table will be no more than 2/3 full, that being
* slightly before the point where performance starts to degrade. (It is
* desirable to be a bit conservative because the overflow strategy tends
* to produce files with holes in them, which is a nuisance.)
*/
/*
* The following is for backward compatibility.
*/
#ifdef INDEX_SIZE
#define DEFSIZE INDEX_SIZE
#endif
/*
* ANSI C says an offset into a file is a long, not an off_t, for some
* reason. This actually does simplify life a bit, but it's still nice
* to have a distinctive name for it. Beware, this is just for readability,
* don't try to change this.
*/
#define of_t long
#define SOF (sizeof(of_t))
/*
* We assume that unused areas of a binary file are zeros, and that the
* bit pattern of `(of_t)0' is all zeros. The alternative is rather
* painful file initialization. Note that okayvalue(), if OVERFLOW is
* defined, knows what value of an offset would cause overflow.
*/
#define VACANT ((of_t)0)
#define BIAS(o) ((o)+1) /* make any valid of_t non-VACANT */
#define UNBIAS(o) ((o)-1) /* reverse BIAS() effect */
/*
* In a Unix implementation, or indeed any in which an of_t is a byte
* count, there are a bunch of high bits free in an of_t. There is a
* use for them. Checking a possible hit by looking it up in the base
* file is relatively expensive, and the cost can be dramatically reduced
* by using some of those high bits to tag the value with a few more bits
* of the key's hash. This detects most false hits without the overhead of
* seek+read+strcmp. We use the top bit to indicate whether the value is
* tagged or not, and don't tag a value which is using the tag bits itself.
* We're in trouble if the of_t representation wants to use the top bit.
* The actual bitmasks and offset come from the configuration stuff,
* which permits fiddling with them as necessary, and also suppressing
* them completely (by defining the masks to 0). We build pre-shifted
* versions of the masks for efficiency.
*/
static of_t tagbits; /* pre-shifted tag mask */
static of_t taghere; /* pre-shifted tag-enable bit */
static of_t tagboth; /* tagbits|taghere */
#define HASTAG(o) ((o)&taghere)
#define TAG(o) ((o)&tagbits)
#define NOTAG(o) ((o)&~tagboth)
#define CANTAG(o) (((o)&tagboth) == 0)
#define MKTAG(v) (((v)<<conf.tagshift)&tagbits)
/*
* A new, from-scratch database, not built as a rebuild of an old one,
* needs to know table size, casemap algorithm, and tagging. Normally
* the user supplies this info, but there have to be defaults.
*/
#ifndef DEFSIZE
#define DEFSIZE 120011 /* 300007 might be better */
#endif
#ifdef OLDBNEWS
#define DEFCASE '0' /* B2.10 -- no mapping */
#define NOBUFFER /* B News locking is defective */
#endif
#ifdef BNEWS
#define DEFCASE '=' /* B2.11 -- all mapped */
#define NOBUFFER /* B News locking is defective */
#endif
#ifndef DEFCASE /* C News compatibility is the default */
#define DEFCASE 'C' /* C News -- RFC822 mapping */
#endif
#ifndef NOTAGS
#define TAGENB 0x80 /* tag enable is top bit, tag is next 7 */
#define TAGMASK 0x7f
#define TAGSHIFT 24
#else
#define TAGENB 0 /* no tags */
#define TAGMASK 0
#define TAGSHIFT 0
#endif
/*
* We read configuration info from the .dir file into this structure,
* so we can avoid wired-in assumptions for an existing database.
*
* Among the info is a record of recent peak usages, so that a new table
* size can be chosen intelligently when rebuilding. 10 is a good
* number of usages to keep, since news displays marked fluctuations
* in volume on a 7-day cycle.
*/
struct dbzconfig {
int olddbz; /* .dir file empty but .pag not? */
of_t tsize; /* table size */
# ifndef NMEMORY
# define NMEMORY 10 /* # days of use info to remember */
# endif
# define NUSEDS (1+NMEMORY)
of_t used[NUSEDS]; /* entries used today, yesterday, ... */
int valuesize; /* size of table values, == SOF */
int bytemap[SOF]; /* byte-order map */
char casemap; /* case-mapping algorithm (see cipoint()) */
char fieldsep; /* field separator in base file, if any */
of_t tagenb; /* unshifted tag-enable bit */
of_t tagmask; /* unshifted tag mask */
int tagshift; /* shift count for tagmask and tagenb */
};
static struct dbzconfig conf;
static int getconf();
static long getno();
static int putconf();
static void mybytemap();
static of_t bytemap();
/*
* For a program that makes many, many references to the database, it
* is a large performance win to keep the table in core, if it will fit.
* Note that this does hurt robustness in the event of crashes, and
* dbmclose() *must* be called to flush the in-core database to disk.
* The code is prepar