4
GDBM - The GNU database manager. Includes \fBdbm\fR and \fBndbm\fR
5
compatability. (Version \*(ve.)
20
.B gdbm_open (name, block_size, read_write, mode, fatal_func)
24
.B int block_size, read_write, mode;
26
.B void (*fatal_func) ();
36
.B gdbm_store (dbf, key, content, flag)
40
.B datum key, content;
46
.B gdbm_fetch (dbf, key)
54
.B gdbm_delete (dbf, key)
62
.B gdbm_firstkey (dbf)
68
.B gdbm_nextkey (dbf, key)
76
.B gdbm_reorganize (dbf)
88
.B gdbm_exists (dbf, key)
96
.B gdbm_strerror (errno)
102
.B gdbm_setopt (dbf, option, value, size)
119
.B DBM Compatability routines:
132
.B store (key, content)
134
.B datum key, content;
163
.B NDBM Compatability routines:
170
.B *dbm_open (name, flags, mode)
184
.B dbm_fetch (file, key)
192
.B dbm_store (file, key, content, flags)
196
.B datum key, content;
202
.B dbm_delete (file, key)
210
.B dbm_firstkey (file)
216
.B dbm_nextkey (file)
228
.B dbm_clearerr (file)
252
GNU dbm is a library of routines that manages data files that contain
253
key/data pairs. The access provided is that of storing,
254
retrieval, and deletion by key and a non-sorted traversal of all
255
keys. A process is allowed to use multiple data files at the
258
A process that opens a gdbm file is designated as a "reader" or a
259
"writer". Only one writer may open a gdbm file and many readers may
260
open the file. Readers and writers can not open the gdbm file at the
261
same time. The procedure for opening a gdbm file is:
265
dbf = gdbm_open ( name, block_size, read_write, mode, fatal_func )
267
\fIName\fR is the name of the file (the complete name,
268
gdbm does not append any characters to this name). \fIBlock_size\fR is
269
the size of a single transfer from disk to memory. This parameter is
270
ignored unless the file is a new file. The minimum size is 512. If
271
it is less than 512, dbm will use the stat block size for the file system.
272
\fIRead_write\fR can have one of the following values:
281
writer - if database does not exist create new one
284
writer - create new database regardless if one exists
286
For the last three (writers of the database) the following may be added
287
added to \fIread_write\fR by bitwise or:
289
which causes all database operations to be synchronized to the disk, and
291
which prevents the library from performing any locking on the database file.
294
is now obsolete, since gdbm defaults to no-sync mode.
296
\fIMode\fR is the file mode (see \fBchmod(2)\fR and \fBopen(2)\fR) if the
297
file is created. \fI(*Fatal_func) ()\fR is a function for dbm to call
298
if it detects a fatal error. The only parameter of this function is a string.
299
If the value of 0 is provided, gdbm will use a default function.
301
The return value \fIdbf\fR is the pointer needed by all other routines to
302
access that gdbm file. If the return is the NULL pointer, \fBgdbm_open\fR
303
was not successful. The errors can be found in \fIgdbm_errno\fR for gdbm
304
errors and in \fIerrno\fR for system errors. (For error codes, see
307
In all of the following calls, the parameter \fIdbf\fR refers to the pointer
308
returned from \fBgdbm_open\fR.
310
It is important that every file opened is also closed. This is needed to
311
update the reader/writer count on the file. This is done by:
316
The database is used by 3 primary routines. The first stores data in the
319
ret = gdbm_store ( dbf, key, content, flag )
321
\fIDbf\fR is the pointer returned by \fBgdbm_open\fR. \fIKey\fR is the
322
key data. \fIContent\fR is the data to be associated with the \fIkey\fR.
323
\fIFlag\fR can have one of the following values:
326
insert only, generate an error if key exists
329
replace contents if key exists.
331
If a reader calls \fBgdbm_store\fR, the return value will be -1.
332
If called with GDBM_INSERT and \fIkey\fR is in the database, the return
333
value will be 1. Otherwise, the return value is 0.
335
\fINOTICE: If you store data for a key that is already in the data base,
336
gdbm replaces the old data with the new data if called with GDBM_REPLACE.
337
You do not get two data items for the same key and you do not get an
338
error from gdbm_store.
340
NOTICE: The size in gdbm is not restricted like dbm or ndbm. Your data
341
can be as large as you want.\fR
344
To search for some data:
346
content = gdbm_fetch ( dbf, key )
348
\fIDbf\fR is the pointer returned by \fBgdbm_open\fR. \fIKey\fR is
352
If the \fIdptr\fR element of the return value is NULL, no data was
353
found. Otherwise the return value is a pointer to the found data.
354
The storage space for the \fIdptr\fR element is allocated using
355
\fBmalloc(3C)\fR. \fBGdbm\fI does not automatically free this data.
356
It is the programmer's responsibility to free this storage when it is
360
To search for some data, without retrieving it:
362
ret = gdbm_exists ( dbf, key )
364
\fIDbf\fR is the pointer returned by \fBgdbm_open\fR. \fIKey\fR is
365
the key data to search for.
367
If the \fIkey\fR is found within the database, the return value \fIret\fR
368
will be true. If nothing appropiate is found, \fIret\fR will be false.
369
This routine is useful for checking for the existance of a record,
370
without performing the memory allocation done by \fBgdbm_fetch\fR.
373
To remove some data from the database:
375
ret = gdbm_delete ( dbf, key )
377
\fIDbf\fR is the pointer returned by \fBgdbm_open\fR. \fIKey\fR is the
380
The return value is -1 if the item is not present or the requester is a reader.
381
The return value is 0 if there was a successful delete.
384
The next two routines allow for accessing all items in the database. This
385
access is not key sequential, but it is guaranteed to visit every key in
386
the database once. (The order has to do with the hash values.)
388
key = gdbm_firstkey ( dbf )
390
nextkey = gdbm_nextkey ( dbf, key )
392
\fIDbf\fR is the pointer returned by \fBgdbm_open\fR. \fIKey\fR is the
395
The return values are both of type \fBdatum\fR. If the \fIdptr\fR
396
element of the return value is NULL, there is no first key or next key.
397
Again notice that \fIdptr\fR points to data allocated by \fBmalloc(3C)\fR
398
and \fBgdbm\fR will not free it for you.
400
These functions were intended to visit the database in read-only algorithms,
401
for instance, to validate the database or similar operations.
403
File `visiting' is based on a `hash table'. \fIgdbm_delete\fR re-arranges the
404
hash table to make sure that any collisions in the table do not leave some item
405
`un-findable'. The original key order is NOT guaranteed to remain unchanged in
406
ALL instances. It is possible that some key will not be visited if a loop like
407
the following is executed:
409
key = gdbm_firstkey ( dbf );
411
nextkey = gdbm_nextkey ( dbf, key );
412
if ( some condition ) {
413
gdbm_delete ( dbf, key );
420
The following routine should be used very infrequently.
422
ret = gdbm_reorganize ( dbf )
424
If you have had a lot of deletions and would like to shrink the space
425
used by the \fBgdbm\fR file, this routine will reorganize the database.
426
\fBGdbm\fR will not shorten the length of a \fBgdbm\fR file except by
427
using this reorganization. (Deleted file space will be reused.)
430
Unless your database was opened with the GDBM_SYNC flag, gdbm does not
431
wait for writes to be flushed to the disk before continuing.
432
The following routine can be used to guarantee that the database is
433
physically written to the disk file.
437
It will not return until the disk file state is syncronized with the
438
in-memory state of the database.
441
To convert a \fBgdbm\fR error code into English text, use this routine:
443
ret = gdbm_strerror ( errno )
445
Where \fIerrno\fR is of type \fIgdbm_error\fR, usually the global
446
variable \fIgdbm_errno\fR. The appropiate phrase is returned.
449
\fBGdbm\fR now supports the ability to set certain options on an
450
already open database.
452
ret = gdbm_setopt ( dbf, option, value, size )
454
Where \fIdbf\fR is the return value from a previous call to \fBgdbm_open\fR,
455
and \fIoption\fR specifies which option to set. The valid options are
458
\fBGDBM_CACHESIZE\fR - Set the size of the internal bucket
459
cache. This option may only be set once on each \fIGDBM_FILE\fR
460
descriptor, and is set automatically to 100 upon the first
461
access to the database.
463
\fBGDBM_FASTMODE\fR - Set \fBfast mode\fR to either on or off. This
464
allows \fBfast mode\fR to be toggled on an already open and
465
active database. \fIvalue\fR (see below) should be set to either
466
TRUE or FALSE. \fIThis option is now obsolete.\fR
468
\fBGDBM_SYNCMODE\fR - Turn on or off file system synchronization operations.
469
This setting defaults to off; \fIvalue\fR (see below) should be set to either
472
\fBGDBM_CENTFREE\fR - Set \fBcentral free block pool\fR to either on or off.
473
The default is off, which is how previous versions of \fBGdbm\fR
474
handled free blocks. If set, this option causes all subsequent free
475
blocks to be placed in the \fBglobal\fR pool, allowing (in thoery)
476
more file space to be reused more quickly. \fIvalue\fR (see below) should
477
be set to either TRUE or FALSE.
478
\fINOTICE: This feature is still under study.\fR
480
\fBGDBM_COALESCEBLKS\fR - Set \fBfree block merging\fR to either on or off.
481
The default is off, which is how previous versions of \fBGdbm\fR
482
handled free blocks. If set, this option causes adjacent free blocks
483
to be merged. This can become a CPU expensive process with time, though,
484
especially if used in conjunction with \fBGDBM_CENTFREE\fR. \fIvalue\fR
485
(see below) should be set to either TRUE or FALSE.
486
\fINOTICE: This feature is still under study.\fR
488
\fIvalue\fR is the value to set \fIoption\fR to, specified as an integer
489
pointer. \fIsize\fR is the size of the data pointed to by \fIvalue\fR.
490
The return value will be -1 upon failure, or 0 upon success. The global
491
variable \fIgdbm_errno\fR will be set upon failure.
493
For instance, to set a database to use a cache of 10, after opening it
494
with \fBgdbm_open\fR, but prior to accessing it in any way, the following
499
ret = gdbm_setopt( dbf, GDBM_CACHESIZE, &value, sizeof(int));
502
If the database was opened with the \fBGDBM_NOLOCK\fR flag, the user may
503
wish to perform their own file locking on the database file in order to
504
prevent multiple writers operating on the same file simultaneously.
506
In order to support this, the \fIgdbm_fdesc\fR routine is provided.
508
ret = gdbm_fdesc ( dbf )
510
Where \fIdbf\fR is the return value from a previous call to \fBgdbm_open\fR.
511
The return value will be the file descriptor of the database.
513
The following two external variables may be useful:
515
\fIgdbm_errno\fR is the variable that contains more information about
516
gdbm errors. (gdbm.h has the definitions of the error values and
517
defines gdbm_errno as an external variable.)
519
\fIgdbm_version\fR is the string containing the version information.
522
There are a few more things of interest. First, \fBgdbm\fR files are
523
not "sparse". You can copy them with the UNIX \fBcp(1)\fR command and
524
they will not expand in the copying process. Also, there is a
525
compatibility mode for use with programs that already use UNIX
526
\fBdbm\fR. In this compatibility mode, no \fRgdbm\fR file pointer is
527
required by the programmer, and only one file may be opened at a time.
528
All users in compatibility mode are assumed to be writers. If the
529
\fBgdbm\fR file is a read only, it will fail as a writer, but will
530
also try to open it as a reader. All returned pointers in datum
531
structures point to data that \fBgdbm\fR WILL free. They should be
532
treated as static pointers (as standard UNIX \fBdbm\fR does).
536
This library is accessed by specifying \fI-lgdbm\fR as the last
537
parameter to the compile line, e.g.:
539
gcc -o prog prog.c -lgdbm
548
by Philip A. Nelson and Jason Downs.
549
Copyright (C) 1990 - 1999 Free Software Foundation, Inc.
551
GDBM is free software; you can redistribute it and/or modify
552
it under the terms of the GNU General Public License as published by
553
the Free Software Foundation; either version 1, or (at your option)
556
GDBM is distributed in the hope that it will be useful,
557
but WITHOUT ANY WARRANTY; without even the implied warranty of
558
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
559
GNU General Public License for more details.
561
You should have received a copy of the GNU General Public License
562
along with GDBM; see the file COPYING. If not, write to
563
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
565
You may contact the original author by:
567
e-mail: phil@cs.wwu.edu
569
us-mail: Philip A. Nelson
571
Computer Science Department
573
Western Washington University
577
You may contact the current maintainer by:
579
e-mail: downsj@downsj.com