1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
.TH DBZ 3Z "3 Feb 1991"
.BY "C News"
.SH NAME
dbminit, fetch, store, dbmclose \- somewhat dbm-compatible database routines
.br
dbzfresh, dbzagain, dbzfetch, dbzstore \- database routines
.br
dbzsync, dbzsize, dbzincore, dbzcancel, dbzdebug \- database routines
.SH SYNOPSIS
.nf
.B #include <dbz.h>
.PP
.B dbminit(base)
.B char *base;
.PP
.B datum
.B fetch(key)
.B datum key;
.PP
.B store(key, value)
.B datum key;
.B datum value;
.PP
.B dbmclose()
.PP
.B dbzfresh(base, size, fieldsep, cmap, tagmask)
.B char *base;
.B long size;
.B int fieldsep;
.B int cmap;
.B long tagmask;
.PP
.B dbzagain(base, oldbase)
.B char *base;
.B char *oldbase;
.PP
.B datum
.B dbzfetch(key)
.B datum key;
.PP
.B dbzstore(key, value)
.B datum key;
.B datum value;
.PP
.B dbzsync()
.PP
.B long
.B dbzsize(nentries)
.B long nentries;
.PP
.B dbzincore(newvalue)
.PP
.B dbzcancel()
.PP
.B dbzdebug(newvalue)
.SH DESCRIPTION
These functions provide an indexing system for rapid random access to a
text file (the
.I base
.IR file ).
Subject to certain constraints, they are call-compatible with
.IR dbm (3),
although they also provide some extensions.
(Note that they are
.I not
file-compatible with
.I dbm
or any variant thereof.)
.PP
In principle,
.I dbz
stores key-value pairs, where both key and value are arbitrary sequences
of bytes, specified to the functions by
values of type
.IR datum ,
typedefed in the header file to be a structure with members
.I dptr
(a value of type
.I char *
pointing to the bytes)
and
.I dsize
(a value of type
.I int
indicating how long the byte sequence is).
.PP
In practice,
.I dbz
is more restricted than
.IR dbm .
A
.I dbz
database
must be an index into a base file,
with the database
.IR value s
being
.IR fseek (3)
offsets into the base file.
Each such
.I value
must ``point to'' a place in the base file where the corresponding
.I key
sequence is found.
A key can be no longer than
.SM DBZMAXKEY
(a constant defined in the header file) bytes.
No key can be an initial subsequence of another,
which in most applications requires that keys be
either bracketed or terminated in some way (see the
discussion of the
.I fieldsep
parameter of
.IR dbzfresh ,
below,
for a fine point on terminators).
.PP
.I Dbminit
opens a database,
an index into the base file
.IR base ,
consisting of files
.IB base .dir
and
.IB base .pag
which must already exist.
(If the database is new, they should be zero-length files.)
Subsequent accesses go to that database until
.I dbmclose
is called to close the database.
The base file need not exist at the time of the
.IR dbminit ,
but it must exist before accesses are attempted.
.PP
.I Fetch
searches the database for the specified
.IR key ,
returning the corresponding
.IR value
if any.
.I Store
stores the
.IR key - value
pair in the database.
.I Store
will fail unless the database files are writeable.
See below for a complication arising from case mapping.
.PP
.I Dbzfresh
is a variant of
.I dbminit
for creating a new database with more control over details.
Unlike for
.IR dbminit ,
the database files need not exist:
they will be created if necessary,
and truncated in any case.
.PP
.IR Dbzfresh 's
.I size
parameter specifies the size of the first hash table within the database,
in key-value pairs.
Performance will be best if
.I size
is a prime number and
the number of key-value pairs stored in the database does not exceed
about 2/3 of
.IR size .
(The
.I dbzsize
function, given the expected number of key-value pairs,
will suggest a database size that meets these criteria.)
Assuming that an
.I fseek
offset is 4 bytes,
the
.B .pag
file will be
.RI 4* size
bytes
(the
.B .dir
file is tiny and roughly constant in size)
until
the number of key-value pairs exceeds about 80% of
.IR size .
(Nothing awful will happen if the database grows beyond 100% of
.IR size ,
but accesses will slow down somewhat and the
.B .pag
file will grow somewhat.)
.PP
.IR Dbzfresh 's
.I fieldsep
parameter specifies the field separator in the base file.
If this is not
NUL (0), and the last character of a
.I key
argument is NUL, that NUL compares equal to either a NUL or a
.I fieldsep
in the base file.
This permits use of NUL to terminate key strings without requiring that
NULs appear in the base file.
The
.I fieldsep
of a database created with
.I dbminit
is the horizontal-tab character.
.PP
For use in news systems, various forms of case mapping (e.g. uppercase to
lowercase) in keys are available.
The
.I cmap
parameter to
.I dbzfresh
is a single character specifying which of several mapping algorithms to use.
Available algorithms are:
.RS
.TP
.B 0
case-sensitive: no case mapping
.TP
.B B
same as
.B 0
.TP
.B NUL
same as
.B 0
.TP
.B =
case-insensitive: uppercase and lowercase equivalent
.TP
.B b
same as
.B =
.TP
.B C
RFC822 message-ID rules, case-sensitive before `@' (with certain exceptions)
and case-insensitive after
.TP
.B ?
whatever the local default is, normally
.B C
.RE
.PP
Mapping algorithm
.B 0
(no mapping) is faster than the others and is overwhelmingly the correct
choice for most applications.
Unless compatibility constraints interfere, it is more efficient to pre-map
the keys, storing mapped keys in the base file, than to have
.I dbz
do the mapping on every search.
.PP
For historical reasons,
.I fetch
and
.I store
expect their
.I key
arguments to be pre-mapped, but expect unmapped keys in the base file.
.I Dbzfetch
and
.I dbzstore
do the same jobs but handle all case mapping internally,
so the customer need not worry about it.
.PP
.I Dbz
stores only the database
.IR value s
in 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 the
.I dbz
files.
This is ``free'' if there are some unused bits in an
.I fseek
offset,
so that the offset can be
.I tagged
with some information about the key.
The
.I tagmask
parameter of
.I dbzfresh
allows specifying the location of unused bits.
.I Tagmask
should be a mask with
one group of
contiguous
.B 1
bits.
The bits in the mask should
be unused (0) in
.I most
offsets.
The bit immediately above the mask (the
.I flag
bit) should be unused (0) in
.I all
offsets;
.I (dbz)store
will reject attempts to store a key-value pair in which the
.I value
has the flag bit on.
Apart from this restriction, tagging is invisible to the user.
As a special case, a
.I tagmask
of 1 means ``no tagging'', for use with enormous base files or
on systems with unusual offset representations.
.PP
A
.I size
of 0
given to
.I dbzfresh
is synonymous with the local default;
the normal default is suitable for tables of 90-100,000
key-value pairs.
A
.I cmap
of 0 (NUL) is synonymous with the character
.BR 0 ,
signifying no case mapping
(note that the character
.B ?
specifies the local default mapping,
normally
.BR C ).
A
.I tagmask
of 0 is synonymous with the local default tag mask,
normally 0x7f000000 (specifying the top bit in a 32-bit offset
as the flag bit, and the next 7 bits as the mask,
which is suitable for base files up to circa 24MB).
Calling
.I dbminit(name)
with the database files empty is equivalent to calling
.IR dbzfresh(name,0,'\et','?',0) .
.PP
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 that
a new database size can be chosen to cover expected fluctuations.
.I Dbzagain
is a variant of
.I dbminit
for creating a new database as a new generation of an old database.
The database files for
.I oldbase
must exist.
.I Dbzagain
is equivalent to calling
.I dbzfresh
with the same field separator, case mapping, and tag mask as the old database,
and a
.I size
equal to the result of applying
.I dbzsize
to the largest number of entries in the
.I oldbase
database and its previous 10 generations.
.PP
When many accesses are being done by the same program,
.I dbz
is 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 when
the database is opened, and
.I dbmclose
writes it out to disk again (if it was read successfully and
has been modified).
.I Dbzincore
sets the flag to
.I newvalue
(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 case
.I dbz
quietly falls back on its default behavior.
.IR Store s
to an in-memory database are not (in general) written out to the file
until
.IR dbmclose
or
.IR dbzsync ,
so if robustness in the presence of crashes
or concurrent accesses
is crucial, in-memory databases
should probably be avoided.
.PP
.I Dbzsync
causes all buffers etc. to be flushed out to the files.
It is typically used as a precaution against crashes or concurrent accesses
when a
.IR dbz -using
process will be running for a long time.
It is a somewhat expensive operation,
especially
for an in-memory database.
.PP
.I Dbzcancel
cancels any pending writes from buffers.
This is typically useful only for in-core databases, since writes are
otherwise done immediately.
Its main purpose is to let a child process, in the wake of a
.IR fork ,
do a
.I dbmclose
without writing its parent's data to disk.
.PP
If
.I dbz
has been compiled with debugging facilities available (which makes it
bigger and a bit slower),
.I dbzdebug
alters the value (and returns the previous value) of an internal flag
which (when 1; default is 0) causes
verbose and cryptic debugging output on standard output.
.PP
Concurrent reading of databases is fairly safe,
but there is no (inter)locking,
so concurrent updating is not.
.PP
The database files include a record of the byte order of the processor
creating the database, and accesses by processors with different byte
order will work, although they will be slightly slower.
Byte order is preserved by
.IR dbzagain .
However,
agreement on the size and internal structure of an
.I fseek
offset is necessary, as is consensus on
the character set.
.PP
An open database occupies three
.I stdio
streams and their corresponding file descriptors;
a fourth is needed for an in-memory database.
Memory consumption is negligible (except for
.I stdio
buffers) except for in-memory databases.
.SH SEE ALSO
dbz(1), dbm(3)
.SH DIAGNOSTICS
Functions returning
.I int
values return 0 for success, \-1 for failure.
Functions returning
.I datum
values return a value with
.I dptr
set to NULL for failure.
.I Dbminit
attempts to have
.I errno
set plausibly on return, but otherwise this is not guaranteed.
An
.I errno
of
.B EDOM
from
.I dbminit
indicates that the database did not appear to be in
.I dbz
format.
.SH HISTORY
The original
.I dbz
was written by
Jon Zeeff (zeeff@b-tech.ann-arbor.mi.us).
Later contributions by David Butler and Mark Moraes.
Extensive reworking,
including this documentation,
by Henry Spencer (henry@zoo.toronto.edu) as
part of the C News project.
Hashing function by Peter Honeyman.
.SH BUGS
The
.I dptr
members of returned
.I datum
values point to static storage which is overwritten by later calls.
.PP
Unlike
.IR dbm ,
.I dbz
will misbehave if an existing key-value pair is `overwritten' by
a new
.I (dbz)store
with the same key.
The user is responsible for avoiding this by using
.I (dbz)fetch
first to check for duplicates;
an internal optimization remembers the result of the
first search so there is minimal overhead in this.
.PP
Waiting until after
.I dbminit
to bring the base file into existence
will fail if
.IR chdir (2)
has been used meanwhile.
.PP
The RFC822 case mapper implements only a first approximation to the
hideously-complex RFC822 case rules.
.PP
The prime finder in
.I dbzsize
is not particularly quick.
.PP
Should implement the
.I dbm
functions
.IR delete ,
.IR firstkey ,
and
.IR nextkey .
.PP
On C implementations which trap integer overflow,
.I dbz
will refuse to
.I (dbz)store
an
.I fseek
offset equal to the greatest
representable
positive number,
as this would cause overflow in the biased representation used.
.PP
.I Dbzagain
perhaps ought to notice when many offsets
in the old database were
too big for
tagging, and shrink the tag mask to match.
.PP
Marking
.IR dbz 's
file descriptors
.RI close-on- exec
would be a better approach to the problem
.I dbzcancel
tries to address, but that's harder to do portably.
|