MAN page from Old RedHat 5.X inn-devel-1.7.2-13.i386.rpm
DBZ
Section: Misc. Reference Manual Pages (3Z)
Updated: 3 Feb 1991
Index NAME
dbminit, fetch, store, dbmclose - somewhat dbm-compatible database routines
dbzfresh, dbzagain, dbzfetch, dbzstore - database routines
dbzsync, dbzsize, dbzincore, dbzcancel, dbzdebug - database routines
SYNOPSIS
#include <dbz.h>dbminit(base)char *base;datumfetch(key)datum key;store(key, value)datum key;datum value;dbmclose()dbzfresh(base, size, fieldsep, cmap, tagmask)char *base;long size;int fieldsep;int cmap;long tagmask;dbzagain(base, oldbase)char *base;char *oldbase;datumdbzfetch(key)datum key;dbzstore(key, value)datum key;datum value;dbzsync()longdbzsize(nentries)long nentries;dbzincore(newvalue)dbzcancel()dbzdebug(newvalue)
DESCRIPTION
These functions provide an indexing system for rapid random access to atext file (the
base file).Subject to certain constraints, they are call-compatible with
dbm(3),although they also provide some extensions.(Note that they are
notfile-compatible with
dbmor any variant thereof.)
In principle,dbzstores key-value pairs, where both key and value are arbitrary sequencesof bytes, specified to the functions byvalues of typedatum,typedefed in the header file to be a structure with membersdptr(a value of typechar *pointing to the bytes)anddsize(a value of typeintindicating how long the byte sequence is).
In practice,dbzis more restricted thandbm.Adbzdatabasemust be an index into a base file,with the databasevaluesbeingfseek(3)offsets into the base file.Each suchvaluemust ``point to'' a place in the base file where the correspondingkeysequence is found.A key can be no longer thanDBZMAXKEY(a constant defined in the header file) bytes.No key can be an initial subsequence of another,which in most applications requires that keys beeither bracketed or terminated in some way (see thediscussion of thefieldsepparameter ofdbzfresh,below,for a fine point on terminators).
Dbminitopens a database,an index into the base filebase,consisting of filesbase.dirandbase.pagwhich must already exist.(If the database is new, they should be zero-length files.)Subsequent accesses go to that database untildbmcloseis called to close the database.The base file need not exist at the time of thedbminit,but it must exist before accesses are attempted.
Fetchsearches the database for the specifiedkey,returning the correspondingvalueif any.Storestores thekey-valuepair in the database.Storewill fail unless the database files are writeable.See below for a complication arising from case mapping.
Dbzfreshis a variant ofdbminitfor creating a new database with more control over details.Unlike fordbminit,the database files need not exist:they will be created if necessary,and truncated in any case.
Dbzfresh'ssizeparameter specifies the size of the first hash table within the database,in key-value pairs.Performance will be best ifsizeis a prime number andthe number of key-value pairs stored in the database does not exceedabout 2/3 ofsize.(Thedbzsizefunction, given the expected number of key-value pairs,will suggest a database size that meets these criteria.)Assuming that anfseekoffset is 4 bytes,the.pagfile will be4*sizebytes(the.dirfile is tiny and roughly constant in size)untilthe number of key-value pairs exceeds about 80% ofsize.(Nothing awful will happen if the database grows beyond 100% ofsize,but accesses will slow down somewhat and the.pagfile will grow somewhat.)
Dbzfresh'sfieldsepparameter specifies the field separator in the base file.If this is notNUL (0), and the last character of akeyargument is NUL, that NUL compares equal to either a NUL or afieldsepin the base file.This permits use of NUL to terminate key strings without requiring thatNULs appear in the base file.Thefieldsepof a database created withdbminitis the horizontal-tab character.
For use in news systems, various forms of case mapping (e.g. uppercase tolowercase) in keys are available.Thecmapparameter todbzfreshis a single character specifying which of several mapping algorithms to use.Available algorithms are:
- 0
- case-sensitive: no case mapping
- B
- same as0
- NUL
- same as0
- =
- case-insensitive: uppercase and lowercase equivalent
- b
- same as=
- C
- RFC822 message-ID rules, case-sensitive before `@' (with certain exceptions)and case-insensitive after
- ?
- whatever the local default is, normallyC
Mapping algorithm0(no mapping) is faster than the others and is overwhelmingly the correctchoice for most applications.Unless compatibility constraints interfere, it is more efficient to pre-mapthe keys, storing mapped keys in the base file, than to havedbzdo the mapping on every search.
For historical reasons,fetchandstoreexpect theirkeyarguments to be pre-mapped, but expect unmapped keys in the base file.Dbzfetchanddbzstoredo the same jobs but handle all case mapping internally,so the customer need not worry about it.
Dbzstores only the databasevaluesin its files, relying on reference to the base file to confirm a hit on a key.References to the base file can be minimized, greatly speeding up searches,if a little bit of information about the keys can be stored in thedbzfiles.This is ``free'' if there are some unused bits in anfseekoffset,so that the offset can betaggedwith some information about the key.Thetagmaskparameter ofdbzfreshallows specifying the location of unused bits.Tagmaskshould be a mask withone group ofcontiguous1bits.The bits in the mask shouldbe unused (0) inmostoffsets.The bit immediately above the mask (theflagbit) should be unused (0) inalloffsets;(dbz)storewill reject attempts to store a key-value pair in which thevaluehas the flag bit on.Apart from this restriction, tagging is invisible to the user.As a special case, atagmaskof 1 means ``no tagging'', for use with enormous base files oron systems with unusual offset representations.
Asizeof 0given todbzfreshis synonymous with the local default;the normal default is suitable for tables of 90-100,000key-value pairs.Acmapof 0 (NUL) is synonymous with the character0,signifying no case mapping(note that the character?specifies the local default mapping,normallyC).Atagmaskof 0 is synonymous with the local default tag mask,normally 0x7f000000 (specifying the top bit in a 32-bit offsetas the flag bit, and the next 7 bits as the mask,which is suitable for base files up to circa 24MB).Callingdbminit(name)with the database files empty is equivalent to callingdbzfresh(name,0,'\t','?',0).
When databases are regenerated periodically, as in news,it is simplest to pick the parameters for a new database based on the old one.This also permits some memory of past sizes of the old database, so thata new database size can be chosen to cover expected fluctuations.Dbzagainis a variant ofdbminitfor creating a new database as a new generation of an old database.The database files foroldbasemust exist.Dbzagainis equivalent to callingdbzfreshwith the same field separator, case mapping, and tag mask as the old database,and asizeequal to the result of applyingdbzsizeto the largest number of entries in theoldbasedatabase and its previous 10 generations.
When many accesses are being done by the same program,dbzis massively faster if its first hash table is in memory.If an internal flag is 1,an attempt is made to read the table in whenthe database is opened, anddbmclosewrites it out to disk again (if it was read successfully andhas been modified).Dbzincoresets the flag tonewvalue(which should be 0 or 1)and returns the previous value;this does not affect the status of a database that has already been opened.The default is 0.The attempt to read the table in may fail due to memory shortage;in this casedbzquietly falls back on its default behavior.Storesto an in-memory database are not (in general) written out to the fileuntildbmcloseordbzsync,so if robustness in the presence of crashesor concurrent accessesis crucial, in-memory databasesshould probably be avoided.
Dbzsynccauses all buffers etc. to be flushed out to the files.It is typically used as a precaution against crashes or concurrent accesseswhen adbz-usingprocess will be running for a long time.It is a somewhat expensive operation,especiallyfor an in-memory database.
Dbzcancelcancels any pending writes from buffers.This is typically useful only for in-core databases, since writes areotherwise done immediately.Its main purpose is to let a child process, in the wake of afork,do adbmclosewithout writing its parent's data to disk.
Ifdbzhas been compiled with debugging facilities available (which makes itbigger and a bit slower),dbzdebugalters the value (and returns the previous value) of an internal flagwhich (when 1; default is 0) causesverbose and cryptic debugging output on standard output.
Concurrent reading of databases is fairly safe,but there is no (inter)locking,so concurrent updating is not.
The database files include a record of the byte order of the processorcreating the database, and accesses by processors with different byteorder will work, although they will be slightly slower.Byte order is preserved bydbzagain.However,agreement on the size and internal structure of anfseekoffset is necessary, as is consensus onthe character set.
An open database occupies threestdiostreams and their corresponding file descriptors;a fourth is needed for an in-memory database.Memory consumption is negligible (except forstdiobuffers) except for in-memory databases.
SEE ALSO
dbz(1),
dbm(3)
DIAGNOSTICS
Functions returning
intvalues return 0 for success, -1 for failure.Functions returning
datumvalues return a value with
dptrset to NULL for failure.
Dbminitattempts to have
errnoset plausibly on return, but otherwise this is not guaranteed.An
errnoof
EDOMfrom
dbminitindicates that the database did not appear to be in
dbzformat.
HISTORY
The original
dbzwas written byJon Zeeff (zeeffAATTb-tech.ann-arbor.mi.us).Later contributions by David Butler and Mark Moraes.Extensive reworking,including this documentation,by Henry Spencer (henryAATTzoo.toronto.edu) aspart of the C News project.Hashing function by Peter Honeyman.
BUGS
The
dptrmembers of returned
datumvalues point to static storage which is overwritten by later calls.
Unlikedbm,dbzwill misbehave if an existing key-value pair is `overwritten' bya new(dbz)storewith the same key.The user is responsible for avoiding this by using(dbz)fetchfirst to check for duplicates;an internal optimization remembers the result of thefirst search so there is minimal overhead in this.
Waiting until afterdbminitto bring the base file into existencewill fail ifchdir(2)has been used meanwhile.
The RFC822 case mapper implements only a first approximation to thehideously-complex RFC822 case rules.
The prime finder indbzsizeis not particularly quick.
Should implement thedbmfunctionsdelete,firstkey,andnextkey.
On C implementations which trap integer overflow,dbzwill refuse to(dbz)storeanfseekoffset equal to the greatestrepresentablepositive number,as this would cause overflow in the biased representation used.
Dbzagainperhaps ought to notice when many offsetsin the old database weretoo big fortagging, and shrink the tag mask to match.
Markingdbz'sfile descriptorsclose-on-execwould be a better approach to the problemdbzcanceltries to address, but that's harder to do portably.
Index
- NAME
- SYNOPSIS
- DESCRIPTION
- SEE ALSO
- DIAGNOSTICS
- HISTORY
- BUGS
This document was created byman2html,using the manual pages.