aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/gdbm.340
-rw-r--r--doc/gdbm.texi462
-rw-r--r--src/bucket.c10
-rw-r--r--src/gdbm.h.in91
-rw-r--r--src/gdbmclose.c3
-rw-r--r--src/gdbmconst.h2
-rw-r--r--src/gdbmdefs.h0
-rw-r--r--src/gdbmopen.c5
8 files changed, 384 insertions, 229 deletions
diff --git a/doc/gdbm.3 b/doc/gdbm.3
index 6525112..cfef634 100644
--- a/doc/gdbm.3
+++ b/doc/gdbm.3
@@ -4,66 +4,73 @@
.\" GDBM is free software; you can redistribute it and/or modify
.\" it under the terms of the GNU General Public License as published by
.\" the Free Software Foundation; either version 3, or (at your option)
.\" any later version.
.\"
.\" GDBM is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public License
.\" along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
-.TH GDBM 3 "January 27, 2020" "GDBM" "GDBM User Reference"
+.TH GDBM 3 "March 21, 2021" "GDBM" "GDBM User Reference"
.SH NAME
GDBM \- The GNU database manager. Includes \fBdbm\fR and \fBndbm\fR
compatibility.
.SH SYNOPSIS
.nf
.B #include <gdbm.h>
.sp
.BI "extern gdbm_error" " gdbm_errno";
.br
.BI "extern char *" gdbm_version ;
.br
.BI "GDBM_FILE gdbm_open (const char *" name ", int " block_size ", "
.ti +21
.BI "int " flags ", int " mode ", "
.ti +21
.BI "void (*" fatal_func ")(const char *))";
.br
.BI "int gdbm_close (GDBM_FILE " dbf ");"
.br
-.BI "int gdbm_store (GDBM_FILE " dbf ", datum " key ", datum " content ", int " flag );
+.BI "int gdbm_store (GDBM_FILE " dbf ", datum " key ", datum " content ", int " flag ");"
.br
-.BI "datum gdbm_fetch (GDBM_FILE " dbf ", datum " key );
+.BI "datum gdbm_fetch (GDBM_FILE " dbf ", datum " key ");"
.br
-.BI "int gdbm_delete (GDBM_FILE " dbf ", datum " key );
+.BI "int gdbm_delete (GDBM_FILE " dbf ", datum " key ");"
.br
.BI "datum gdbm_firstkey (GDBM_FILE " dbf ");"
.br
-.BI "datum gdbm_nextkey (GDBM_FILE " dbf ", datum " key );
+.BI "datum gdbm_nextkey (GDBM_FILE " dbf ", datum " key ");"
+.br
+.BI "int gdbm_recover (GDBM_FILE " dbf ", gdbm_recovery *" rcvr ", int" flags ");"
.br
.BI "int gdbm_reorganize (GDBM_FILE " dbf ");"
.br
.BI "int gdbm_sync (GDBM_FILE " dbf ");"
.br
-.BI "int gdbm_exists (GDBM_FILE " dbf ", datum " key );
+.BI "int gdbm_exists (GDBM_FILE " dbf ", datum " key ");"
.br
-.BI "const char *gdbm_strerror (gdbm_error " errno );
+.BI "const char *gdbm_strerror (gdbm_error " errno ");"
.br
.BI "int gdbm_setopt (GDBM_FILE " dbf ", int " option ", int " value ", int " size );
.br
.BI "int gdbm_fdesc (GDBM_FILE " dbf );
.br
+.BI "int gdbm_count (GDBM_FILE " dbf ", gdbm_count_t *" pcount ");"
+.br
+.BI "int gdbm_bucket_count (GDBM_FILE " dbf ", size_t *" pcount ");"
+.br
+.BI "int gdbm_avail_verify (GDBM_FILE " dbf ");"
.PP
.SS DBM Compatibility routines:
.PP
.B #include <dbm.h>
.sp
.BI "int dbminit (const char *" name ");"
.br
.BI "int store (datum " key ", datum " content );
.br
.BI "datum fetch (datum " key );
.br
.BI "int delete (datum " key );
@@ -152,24 +159,33 @@ writer - create new database regardless if one exists
The \fBGDBM_NOMMAP\fR added to \fIread_write\fR by bitwise or instructs
\fBgdbm_open\fR to disable the use of
.BR mmap (2).
.PP
For the last three (writers of the database) the following may be added
added to \fIread_write\fR by bitwise or:
.TP
.B GDBM_SYNC
Causes all database operations to be synchronized to the disk,
.TP
.B GDBM_NOLOCK
Prevents the library from performing any locking on the database file.
+.TP
+.B GDBM_CLOEXEC
+Set the close-on-exec flag on the database file descriptor.
+.TP
+.B GDBM_XVERIFY
+Enable additional consistency checks. With this flag, eventual
+corruptions of the database are discovered when opening it, instead of
+when a corrupted structure is read during normal operation. However,
+on large databases, it can slow down the opening process.
.PP
The option
.B GDBM_FAST
is now obsolete, since gdbm defaults to no-sync mode.
.PP
\fIMode\fR is the file mode (see \fBchmod(2)\fR and \fBopen(2)\fR) if the
file is created. \fI(*Fatal_func) ()\fR is a function for dbm to call
if it detects a fatal error. The only parameter of this function is a string.
If the value of 0 is provided, \fBgdbm\fR will use a default function.
The return value is the pointer needed by all other routines to
access that gdbm file. If the return is the \fBNULL\fR pointer, \fBgdbm_open\fR
@@ -318,27 +334,31 @@ To convert a \fBgdbm\fR error code into English text, use this routine:
.BI "const char *gdbm_strerror (gdbm_error " errno );
\fBGdbm\fR now supports the ability to set certain options on an
already open database.
.BI "int gdbm_setopt (GDBM_FILE " dbf ", int " option ", int " value ", int " size );
Where \fIdbf\fR is the return value from a previous call to \fBgdbm_open\fR,
and \fIoption\fR specifies which option to set. The valid options are
currently:
.TP
.B GDBM_CACHESIZE
-Set the size of the internal bucket cache. This option may only be set once
-on each \fIGDBM_FILE\fR descriptor, and is set automatically to 100 upon the
-first access to the database.
+Set the size of the internal bucket cache. By default, the cache size
+is selected to provide for the optimal performance. Use this option,
+if you wish to limit the memory usage at the expense of performance.
+.sp
+Use the
+.B GDBM_CACHE_AUTO
+constant to return to the default.
.TP
.B GDBM_FASTMODE
Set \fBfast mode\fR to either on or off. This allows \fBfast mode\fR to
be toggled on an already open and active database. \fIvalue\fR (see below)
should be set to either TRUE or FALSE. \fIThis option is now obsolete.\fR
.TP
.B GDBM_SYNCMODE
Turn on or off file system synchronization operations. This setting defaults
to off; \fIvalue\fR (see below) should be set to either TRUE or FALSE.
.TP
.B GDBM_CENTFREE
Set \fBcentral free block pool\fR to either on or off.
diff --git a/doc/gdbm.texi b/doc/gdbm.texi
index 9c679ca..5f42ced 100644
--- a/doc/gdbm.texi
+++ b/doc/gdbm.texi
@@ -59,43 +59,43 @@ Documentation License.''
@sp 6
@center @titlefont{GNU dbm}
@sp 2
@center A Database Manager
@sp 2
@center by Philip A. Nelson, Jason Downs and Sergey Poznyakoff
@sp 4
@center Manual by Pierre Gaumond, Philip A. Nelson, Jason Downs
@center and Sergey Poznyakoff
@sp 1
@center Edition @value{EDITION}
@sp 1
-@center for GNU @code{dbm}, Version @value{VERSION}
+@center for GNU @command{dbm}, Version @value{VERSION}
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@ifnothtml
@page
@summarycontents
@page
@end ifnothtml
@contents
@ifnottex
@node Top
@top The GNU database manager.
-GNU @code{dbm} is a library of functions implementing a hashed database
-on a disk file. This manual documents GNU @code{dbm} Version @value{VERSION}
+GNU @command{dbm} is a library of functions implementing a hashed database
+on a disk file. This manual documents GNU @command{dbm} Version @value{VERSION}
(@code{gdbm}). The software was originally written by Philip A.@:
Nelson. This document was originally written by Pierre Gaumond from
texts written by Phil.
@end ifnottex
@menu
Introduction:
* Copying:: Your rights.
* Intro:: Introduction to GNU dbm.
Functions:
@@ -106,25 +106,25 @@ Functions:
* Store:: Inserting and replacing records in the database.
* Fetch:: Searching records in the database.
* Delete:: Removing records from the database.
* Sequential:: Sequential access to records.
* Reorganization:: Database reorganization.
* Sync:: Insure all writes to disk have competed.
* Flat files:: Export and import to Flat file format.
* Errors:: Error handling.
* Recovery:: Recovery from fatal errors.
* Options:: Setting internal options.
* Locking:: File locking.
* Variables:: Useful global variables.
-
+* Additional functions::
* Error codes:: Error codes returned by @code{gdbm} calls.
* Compatibility:: Compatibility with UNIX dbm and ndbm.
Programs
* gdbmtool:: Examine and modify a GDBM database.
* gdbm_dump:: Dump the database into a flat file.
* gdbm_load:: Load the database from a flat file.
* Exit codes:: Exit codes returned by GDBM utilities.
Other topics:
@@ -134,26 +134,26 @@ Other topics:
* GNU Free Documentation License:: Document license.
* Index:: Index
@ifset WEBDOC
@ifhtml
* This Manual in Other Formats::
@end ifhtml
@end ifset
@end menu
@node Copying
@chapter Copying Conditions.
This library is @dfn{free}; this means that everyone is free to use
-it and free to redistribute it on a free basis. GNU @code{dbm} (@code{gdbm})
-is not in the public domain; it is copyrighted and there
+it and free to redistribute it on a free basis. GNU @command{dbm}
+(@code{gdbm}) is not in the public domain; it is copyrighted and there
are restrictions on its distribution, but these restrictions are
designed to permit everything that a good cooperating citizen would want
to do. What is not allowed is to try to prevent others from further
sharing any version of @code{gdbm} that they might get from
you.
Specifically, we want to make sure that you have the right to give
away copies @code{gdbm}, that you receive
source code or else can get it if you want it, that you can change these
functions or use pieces of them in new free programs, and that you know
you can do these things.
@@ -167,28 +167,28 @@ can get the source code. And you must tell them their rights.
out that there is no warranty for anything in the @code{gdbm} distribution.
If these functions are modified by someone else and passed on, we want
their recipients to know that what they have is not what we distributed,
so that any problems introduced by others will not reflect on our
reputation.
@code{Gdbm} is currently distributed under the terms of the GNU General
Public License, Version 3. (@emph{NOT} under the GNU General Library
Public License.) A copy the GNU General Public License is included with
the distribution of @code{gdbm}.
@node Intro
-@chapter Introduction to GNU @code{dbm}.
+@chapter Introduction to GNU @command{dbm}.
-GNU @code{dbm} (@code{gdbm}) is a library of database functions that use
-extensible hashing and works similar to the standard UNIX @code{dbm}
+GNU @command{dbm} (@code{gdbm}) is a library of database functions that use
+extensible hashing and works similar to the standard UNIX @command{dbm}
functions. These routines are provided to a programmer needing to
create and manipulate a hashed database. (@code{gdbm} is @emph{NOT} a
complete database package for an end user.)
@tpindex datum
The basic use of @code{gdbm} is to store key/data pairs in a data file.
Each key must be unique and each key is paired with only one data item.
The keys can not be directly accessed in sorted order. The basic unit
of data in @code{gdbm} is the structure:
@example
typedef struct
@@ -335,127 +335,157 @@ Now you can run the example program to see how it works:
$ ./example test.gdbm foo
bar
$ ./example test.gdbm baz
no such key
@end example
@node Open
@chapter Opening the database.
@cindex opening the database
@cindex database, opening or creating
@deftypefn {gdbm interface} GDBM_FILE gdbm_open (const char *@var{name}, int @var{block_size}, @
- int @var{flags}, int @var{mode}, void (*fatal_func)(const char *))
+ int @var{flags}, int @var{mode}, void (*@var{fatal_func})(const char *))
Initializes @code{gdbm} system. If the file has a size of zero bytes, a file
initialization procedure is performed, setting up the initial structure in the
file.
The arguments are:
@table @var
@item name
The name of the file (the complete name, @code{gdbm} does not append any
characters to this name).
@item block_size
It is used during initialization to determine the size of various
constructs. It is the size of a single transfer from disk to
memory. This parameter is ignored if the file has been previously
initialized. If the value is less than 512, the file system block
size is used instead. The size is adjusted so that the block can hold
exact number of directory entries, so that the effective block size
can be slightly greater than requested. However, if the
-@samp{GDBM_BSEXACT} flag is set and the size needs to be adjusted, the
-function will return with error status, setting the @samp{gdbm_errno}
-variable to @samp{GDBM_BLOCK_SIZE_ERROR}.
+@code{GDBM_BSEXACT} flag is set and the size needs to be adjusted, the
+function will return with error status, setting the @code{gdbm_errno}
+variable to @code{GDBM_BLOCK_SIZE_ERROR}.
@item flags
@kwindex GDBM_READER
@kwindex GDBM_WRITER
@kwindex GDBM_WRCREAT
@kwindex GDBM_NEWDB
-If @code{flags} is set to @samp{GDBM_READER}, the user wants to just read the
+If @code{flags} is set to @code{GDBM_READER}, the user wants to just read the
database and any call to @code{gdbm_store} or @code{gdbm_delete} will fail.
Many readers can access the database at the same time. If @code{flags} is
-set to @samp{GDBM_WRITER}, the user wants both read and write access
+set to @code{GDBM_WRITER}, the user wants both read and write access
to the database and requires exclusive access. If @code{flags} is set
-to @samp{GDBM_WRCREAT}, the user wants both read and write access to
+to @code{GDBM_WRCREAT}, the user wants both read and write access to
the database and wants it created if it does not already exist. If
-@code{flags} is set to @samp{GDBM_NEWDB}, the user want a new database
+@code{flags} is set to @code{GDBM_NEWDB}, the user want a new database
created, regardless of whether one existed, and wants read and write
access to the new database.
@kwindex GDBM_SYNC
@kwindex GDBM_NOLOCK
@kwindex GDBM_NOMMAP
-The following may also be logically or'd into the database flags:
-@samp{GDBM_SYNC}, which causes all database operations to be
-synchronized to the disk, @samp{GDBM_NOLOCK}, which prevents the library
-from performing any locking on the database file, and @samp{GDBM_NOMMAP},
-which disables the memory mapping mechanism. The option @samp{GDBM_FAST} is
-now obsolete, since @code{gdbm} defaults to no-sync mode.
+The following constants may also be logically or'd into the database
+flags:
+
+@table @code
+@kwindex GDBM_SYNC
+@item GDBM_SYNC
+Synchronize all database operations to disk immediately. This
+provides for the best database consistency at the expense of severe
+performance degradation.
+
+@kwindex GDBM_FAST
+@item GDBM_FAST
+A reverse of @code{GDBM_SYNC}. Synchronize writes only when needed.
+This is the default. The flag is provided for compatibility with
+previous versions of @command{GDBM}.
+
+@kwindex GDBM_NOLOCK
+@item GDBM_NOLOCK
+Don't lock the database file. Use this flag if you intend to do
+locking separately.
+
+@kwindex GDBM_NOMMAP
+@item GDBM_NOMMAP
+Disable memory mapping mechanism. This degrades performance.
@kwindex GDBM_BSEXACT
+@item GDBM_BSEXACT
If this flag is set and the requested @var{block_size} cannot be used
without adjustment, @code{gdbm_open} will refuse to create the
-databases. In this case it will set the @samp{gdbm_errno}
-variable to @samp{GDBM_BLOCK_SIZE_ERROR} and return @samp{NULL}.
+databases. In this case it will set the @code{gdbm_errno}
+variable to @code{GDBM_BLOCK_SIZE_ERROR} and return @code{NULL}.
@kwindex GDBM_CLOEXEC
@cindex close-on-exec
-If the host @samp{open} call
+@item GDBM_CLOEXEC
+Set the close-on-exec flag on the database file descriptor. The
+@code{libc} must support the @code{O_CLOEXEC} flag@footnote{
@ifhtml
(@uref{http://www.manpagez.com/man/2/open, open(2)})
@end ifhtml
@ifnothtml
-(@pxref{open,,,open(2),open(2) man page})
+@xref{open,,,open(2),open(2) man page}
@end ifnothtml
-supports the @samp{O_CLOEXEC} flag, the @samp{GDBM_CLOEXEC} can be
-or'd into the flags, to enable the close-on-exec flag for the
-database file descriptor.
+}
+
+@kwindex GDBM_XVERIFY
+@item GDBM_XVERIFY
+Enable additional consistency checks. With this flag, eventual
+corruptions of the database are discovered when opening it, instead of
+when a corrupted structure is read during normal operation. However,
+on large databases, it can slow down the opening process.
+
+@xref{Additional functions}.
+@end table
+
@item mode
-File mode (see
+File mode@footnote{See
@ifhtml
@uref{http://www.manpagez.com/man/2/chmod, chmod(2)},
@end ifhtml
@ifnothtml
-@ref{chmod,,change permissions of a file,chmod(2),
+@xref{chmod,,change permissions of a file,chmod(2),
chmod(2) man page},
@end ifnothtml
and
@ifhtml
@uref{http://www.manpagez.com/man/2/open, open(2)}),
@end ifhtml
@ifnothtml
-@pxref{open,,open a file,open(2), open(2) man page}),
+@ref{open,,open a file,open(2), open(2) man page}.},
@end ifnothtml
-which is used if the file is created).
+which is used if the file is created.
@item fatal_func
A function for @code{gdbm} to call if it detects a fatal error. The only
-parameter of this function is a string. If the value of @samp{NULL} is
+parameter of this function is a string. If the value of @code{NULL} is
provided, @code{gdbm} will use a default function.
@end table
The return value, is the pointer needed by all other functions to
-access that @code{gdbm} file. If the return is the @samp{NULL} pointer,
+access that @code{gdbm} file. If the return is the @code{NULL} pointer,
@code{gdbm_open} was not successful. The errors can be found in
@code{gdbm_errno} variable (@pxref{Variables, gdbm_errno}). Available
error codes are discussed in @ref{Error codes}.
In all of the following calls, the parameter @var{dbf} refers to the pointer
returned from @code{gdbm_open}.
@end deftypefn
@deftypefn {gdbm interface} GDBM_FILE gdbm_fd_open (int @var{fd},@
const char *@var{name}, int @var{block_size}, @
- int @var{flags}, int @var{mode}, void (*fatal_func)(const char *))
+ int @var{flags}, int @var{mode}, void (*@var{fatal_func})(const char *))
Alternative function for opening a GDBM database. The @var{fd}
argument is the file descriptor of the database file obtained by a
call to @code{open}(2), @code{creat}(2) or similar functions. The
descriptor is not dup'ed, and will be closed when the returned
GDBM_FILE is closed. Use @code{dup}(2) if that is not desirable.
@end deftypefn
@deftypefn {gdbm interface} int gdbm_copy_meta (GDBM_FILE @var{dst},@
GDBM_FILE @var{src})
Copy file ownership and mode from @var{src} to @var{dst}.
@end deftypefn
@@ -514,74 +544,74 @@ The parameters are:
@table @var
@item dbf
The pointer returned by @code{gdbm_open}.
@item key
The search key.
@item content
The data to be associated with the key.
@item flag
@kwindex GDBM_REPLACE
@kwindex GDBM_INSERT
Defines the action to take when the key is already in the database. The value
-@samp{GDBM_REPLACE} (defined in @file{gdbm.h}) asks that the old data
-be replaced by the new @var{content}. The value @samp{GDBM_INSERT}
+@code{GDBM_REPLACE} (defined in @file{gdbm.h}) asks that the old data
+be replaced by the new @var{content}. The value @code{GDBM_INSERT}
asks that an error be returned and no action taken if the @var{key}
already exists.
@end table
This function can return the following values:
@table @asis
@item 0
Success. The value of @var{content} is keyed by @var{key} in the
database.
@item -1
The item was not stored in the database because the caller was not an
official writer or either @var{key} or @var{content} have a
-@samp{NULL} @samp{dptr} field.
+@code{NULL} @code{dptr} field.
-Both @var{key} and @var{content} must have the @samp{dptr} field be a
-non-@samp{NULL} value. Since a @samp{NULL} @samp{dptr} field is used by
+Both @var{key} and @var{content} must have the @code{dptr} field be a
+non-@code{NULL} value. Since a @code{NULL} @code{dptr} field is used by
other functions to indicate an error, it cannot be valid data.
@item +1
The item was not stored because the argument @var{flag} was
-@samp{GDBM_INSERT} and the @var{key} was already in the database.
+@code{GDBM_INSERT} and the @var{key} was already in the database.
@end table
@end deftypefn
If you store data for a @var{key} that is already in the data base,
@code{gdbm} replaces the old data with the new data if called with
-@samp{GDBM_REPLACE}. You do not get two data items for the same
+@code{GDBM_REPLACE}. You do not get two data items for the same
@code{key} and you do not get an error from @code{gdbm_store}.
The size of datum in @code{gdbm} is restricted only by the maximum
value for an object of type @code{int} (type of the @code{dsize} member of
@code{datum}).
@node Fetch
@chapter Searching for records in the database.
@cindex fetching records
@cindex looking up records
@cindex record, fetching
@deftypefn {gdbm interface} datum gdbm_fetch (GDBM_FILE @var{dbf}, datum @var{key})
Looks up a given @var{key} and returns the information associated with it.
-The @samp{dptr} field in the structure that is returned points to a
+The @code{dptr} field in the structure that is returned points to a
memory block allocated by @code{malloc}. It is the caller's
responsibility to free it when no longer needed.
-If the @samp{dptr} is @samp{NULL}, inspect the value of the
+If the @code{dptr} is @code{NULL}, inspect the value of the
@code{gdbm_errno} variable (@pxref{Variables,gdbm_errno}). If it is
-@samp{GDBM_ITEM_NOT_FOUND}, no data was found. Any other value means an
+@code{GDBM_ITEM_NOT_FOUND}, no data was found. Any other value means an
error occurred. Use @code{gdbm_strerror} function to convert
@code{gdbm_errno} to a human-readable string.
The parameters are:
@table @var
@item dbf
The pointer returned by @code{gdbm_open}.
@item key
The search key.
@end table
@end deftypefn
@@ -597,30 +627,30 @@ if (content.dptr == NULL)
else
@{
/* do something with content.dptr */
@}
@end example
@cindex records, testing existence
You may also search for a particular key without retrieving it:
@deftypefn {gdbm interface} int gdbm_exists (GDBM_FILE @var{dbf}, datum @var{key})
Checks whether the @var{key} exists in the database @var{dbf}.
-If @var{key} is found, returns @samp{true} (@samp{1}). If it is not
-found, returns @samp{false} (@samp{0}) and sets @code{gdbm_errno} to
-@samp{GDBM_NO_ERROR} (@samp{0}).
+If @var{key} is found, returns @code{true} (@code{1}). If it is not
+found, returns @code{false} (@code{0}) and sets @code{gdbm_errno} to
+@code{GDBM_NO_ERROR} (@code{0}).
-On error, returns @samp{0} and sets @code{gdbm_errno} to a
-non-@samp{0} error code.
+On error, returns @code{0} and sets @code{gdbm_errno} to a
+non-@code{0} error code.
The parameters are:
@table @var
@item dbf
The pointer returned by @code{gdbm_open}.
@item key
The search key.
@end table
@end deftypefn
@node Delete
@@ -635,67 +665,67 @@ function.
Deletes the data associated with the given @var{key}, if it exists in
the database @var{dbf}.
The parameters are:
@table @var
@item dbf
The pointer returned by @code{gdbm_open}.
@item datum key
The search key.
@end table
-The function returns @samp{-1} if the item is not present or the
-requester is a reader. The return of @samp{0} marks a successful delete.
+The function returns @code{-1} if the item is not present or the
+requester is a reader. The return of @code{0} marks a successful delete.
@end deftypefn
@node Sequential
@chapter Sequential access to records.
@cindex sequential access
@cindex iterating over records
@cindex records, iterating over
The next two functions allow for accessing all items in the database. This
access is not @code{key} sequential, but it is guaranteed to visit every
@code{key} in the database once. The order has to do with the hash values.
@code{gdbm_firstkey} starts the visit of all keys in the database.
@code{gdbm_nextkey} finds and reads the next entry in the hash structure for
@code{dbf}.
@deftypefn {gdbm interface} datum gdbm_firstkey (GDBM_FILE @var{dbf})
Initiate sequential access to the database @var{dbf}. The returned
-value is the first key accessed in the database. If the @samp{dptr}
-field in the returned datum is @samp{NULL}, inspect the
+value is the first key accessed in the database. If the @code{dptr}
+field in the returned datum is @code{NULL}, inspect the
@code{gdbm_errno} variable (@pxref{Variables, gdbm_errno}). The value
of @code{GDBM_ITEM_NOT_FOUND} means that the database contains no
data. Other value means an error occurred.
-On success, @samp{dptr} points to a memory block obtained from
+On success, @code{dptr} points to a memory block obtained from
@code{malloc}, which holds the key value. The caller is responsible
for freeing this memory block when no longer needed.
@end deftypefn
@deftypefn {gdbm interface} datum gdbm_nextkey (GDBM_FILE @var{dbf}, datum @var{prev})
This function continues iteration over the keys in @var{dbf},
initiated by @code{gdbm_firstkey}. The parameter @var{prev} holds the
value returned from a previous call to @code{gdbm_nextkey} or
@code{gdbm_firstkey}.
-The function returns next key from the database. If the @samp{dptr}
-field in the returned datum is @samp{NULL} inspect the
+The function returns next key from the database. If the @code{dptr}
+field in the returned datum is @code{NULL} inspect the
@code{gdbm_errno} variable (@pxref{Variables, gdbm_errno}). The value
of @code{GDBM_ITEM_NOT_FOUND} means that all keys in the database
has been visited. Any other value means an error occurred.
-Otherwise, @samp{dptr} points to a memory block obtained from
+Otherwise, @code{dptr} points to a memory block obtained from
@code{malloc}, which holds the key value. The caller is responsible
for freeing this memory block when no longer needed.
@end deftypefn
@cindex iteration loop
These functions are intended to visit the database in read-only algorithms,
for instance, to validate the database or similar operations. The
usual algorithm for sequential access is:
@example
@group
key = gdbm_firstkey (dbf);
@@ -771,25 +801,25 @@ This reorganization requires creating a new file and inserting all the elements
in the old file @var{dbf} into the new file. The new file is then renamed to
the same name as the old file and @var{dbf} is updated to contain all the
correct information about the new file. If an error is detected, the return
value is negative. The value zero is returned after a successful
reorganization.
@node Sync
@chapter Database Synchronization
@cindex database synchronization
@cindex synchronization, database
@kwindex GDBM_SYNC
-Unless your database was opened with the @samp{GDBM_SYNC} flag,
+Unless your database was opened with the @code{GDBM_SYNC} flag,
@code{gdbm} does not wait for writes to be flushed to the disk before
continuing. This allows for faster writing of databases at the risk
of having a corrupted database if the application terminates in an
abnormal fashion. The following function allows the programmer to
make sure the disk version of the database has been completely updated
with all changes to the current time.
@deftypefn {gdbm interface} int gdbm_sync (GDBM_FILE @var{dbf})
Synchronizes the changes in @var{dbf} with its disk file. The
parameter is a pointer returned by @code{gdbm_open}.
This function would usually be called after a complete set of changes
@@ -856,61 +886,61 @@ creating the database from a flat file is called @dfn{importing} or
Dumps the database file to the named file in requested format.
Arguments are:
@table @var
@item dbf
A pointer to the source database, returned by a prior call to
@code{gdbm_open}.
@item filename
Name of the dump file.
@item format
-Output file format. Allowed values are: @samp{GDBM_DUMP_FMT_BINARY} to
-create a binary dump and @samp{GDBM_DUMP_FMT_ASCII} to create an ASCII
+Output file format. Allowed values are: @code{GDBM_DUMP_FMT_BINARY} to
+create a binary dump and @code{GDBM_DUMP_FMT_ASCII} to create an ASCII
dump file.
@item open_flags
-How to create the output file. If @var{flag} is @samp{GDBM_WRCREAT}
+How to create the output file. If @var{flag} is @code{GDBM_WRCREAT}
the file will be created if it does not exist. If it does exist,
the @code{gdbm_dump} will fail.
-If @var{flag} is @samp{GDBM_NEWDB}, the function will create a new
+If @var{flag} is @code{GDBM_NEWDB}, the function will create a new
output file, replacing it if it already exists.
@item mode
The permissions to use when creating the output file.
@ifhtml
See @uref{http://www.manpagez.com/man/2/open, open(2)},
@end ifhtml
@ifnothtml
See @ref{open,,open a file,open(2), open(2) man page},
@end ifnothtml
for a detailed discussion.
@end table
@end deftypefn
@anchor{gdbm_load function}
@deftypefn {gdbm interface} int gdbm_load (GDBM_FILE *@var{pdbf}, @
const char *@var{filename}, int @var{flag}, @
int @var{meta_mask}, @
unsigned long *@var{errline})
Loads data from the dump file @var{filename} into the database pointed
-to by @var{pdbf}. The latter can point to @samp{NULL}, in which case
+to by @var{pdbf}. The latter can point to @code{NULL}, in which case
the function will try to create a new database. If it succeeds, the
function will return, in the memory location pointed to by @var{pdbf},
a pointer to the newly created database. If the dump file carries no
information about the original database file name, the function will
-set @code{gdbm_errno} to @samp{GDBM_NO_DBNAME} and return
-@samp{-1}, indicating failure.
+set @code{gdbm_errno} to @code{GDBM_NO_DBNAME} and return
+@code{-1}, indicating failure.
The @var{flag} has the same meaning as the @var{flag} argument
to the @code{gdbm_store} function (@pxref{Store}).
The @var{meta_mask} argument can be used to disable restoring certain
bits of file's meta-data from the information in the input dump file.
It is a binary OR of zero or more of the following:
@table @asis
@item GDBM_META_MASK_MODE
Do not restore file mode.
@@ -934,49 +964,49 @@ Not enough memory to load data.
@item GDBM_FILE_READ_ERROR
Reading from @var{filename} failed. The @code{errno} variable can be
used to get more detail about the failure.
@item GDBM_ILLEGAL_DATA
Input contained some illegal data.
@item GDBM_ITEM_NOT_FOUND
This error can occur only when the input file is in ASCII format. It
indicates that the data part of the record about to be read lacked
length specification. Application developers are advised to treat
-this error equally as @samp{GDBM_ILLEGAL_DATA}.
+this error equally as @code{GDBM_ILLEGAL_DATA}.
@end table
Mild errors mean that the function was able to successfully load and
restore the data, but was unable to change database file metadata
afterward. The table below lists possible values for @code{gdbm_errno}
in this case. To get more detail, inspect the system @code{errno} variable.
@table @asis
@kwindex GDBM_ERR_FILE_OWNER
@item GDBM_ERR_FILE_OWNER
The function was unable to restore database file owner.
@kwindex GDBM_ERR_FILE_MODE
@item GDBM_ERR_FILE_MODE
The function was unable to restore database file mode (permission bits).
@end table
If an error occurs while loading data from an input file in ASCII
format, the number of line in which the error occurred will be stored
in the location pointed to by the @var{errline} parameter, unless it
-is @samp{NULL}.
+is @code{NULL}.
If the line information is not available or applicable, @var{errline}
-will be set to @samp{0}.
+will be set to @code{0}.
@end deftypefn
@deftypefn {gdbm interface} int gdbm_dump_to_file (GDBM_FILE @var{dbf}, @
FILE *@var{fp}, int @var{format})
This is an alternative entry point to @code{gdbm_dump} (which see).
Arguments are:
@table @var
@item dbf
A pointer to the source database, returned by a call to
@code{gdbm_open}.
@@ -1081,43 +1111,47 @@ Returns the value of the system @code{errno} variable associated with
the most recent error.
Notice, that not all GDBM errors have an associated system error
code. The following are the ones that have:
@itemize @bullet
@item GDBM_FILE_OPEN_ERROR
@item GDBM_FILE_WRITE_ERROR
@item GDBM_FILE_SEEK_ERROR
@item GDBM_FILE_READ_ERROR
@item GDBM_FILE_STAT_ERROR
@item GDBM_BACKUP_FAILED
+@item GDBM_BACKUP_FAILED
+@item GDBM_FILE_CLOSE_ERROR
+@item GDBM_FILE_SYNC_ERROR
+@item GDBM_FILE_TRUNCATE_ERROR
@end itemize
For other errors, @code{gdbm_last_syserr} will return 0.
@end deftypefn
@anchor{gdbm_check_syserr}
@deftypefn {gdbm interface} {int} gdbm_check_syserr (gdbm_errno @var{err})
-Returns @code{1}, if system errno value should be checked to get more
+Returns @code{1}, if system @code{errno} value should be checked to get more
info on the error described by GDBM code @var{err}.
@end deftypefn
To get a human-readable description of the recent error for a
particular database file, use the @code{gdbm_db_strerror} function:
@deftypefn {gdbm interface} {const char *} gdbm_db_strerror (GDBM_FILE @var{dbf})
Returns textual description of the most recent error encountered when
operating on the database @var{dbf}. The resulting string is often
more informative than what would be returned by
-@samp{gdbm_strerror(gdbm_last_errno(@var{dbf}))}. In particular, if
+@code{gdbm_strerror(gdbm_last_errno(@var{dbf}))}. In particular, if
there is a system error associated with the recent failure, it will be
described as well.
@end deftypefn
@deftypefn {gdbm interface} void gdbm_clear_error (GDBM_FILE @var{dbf})
Clears the error state for the database @var{dbf}. Normally, this
function is called upon the entry to any GDBM function.
@end deftypefn
Certain errors (such as write error when saving stored key) can leave
database file in inconsistent state. When such a critical error
occurs, the database file is marked as needing recovery. Subsequent
@@ -1282,151 +1316,161 @@ The parameters are:
@table @var
@item dbf
The pointer returned by @code{gdbm_open}.
@item option
The option to be set or retrieved.
@item value
A pointer to the value to which @var{option} will be set or where to
place the option value (depending on the option).
@item size
The length of the data pointed to by @var{value}.
@end table
-The return value will be @samp{-1} upon failure, or @samp{0} upon
+The return value will be @code{-1} upon failure, or @code{0} upon
success. The global variable @code{gdbm_errno} will be set upon failure.
@end deftypefn
The valid options are:
@table @asis
@kwindex GDBM_CACHESIZE
@kwindex GDBM_SETCACHESIZE
@item GDBM_SETCACHESIZE
@itemx GDBM_CACHESIZE
-Set the size of the internal bucket cache. This option may only be
-set once on each GDBM_FILE descriptor, and is set automatically to 100
-upon the first access to the database. The @var{value} should point
-to a @code{size_t} holding the desired cache size.
+@kwindex GDBM_CACHE_AUTO
+Set the size of the internal bucket cache. The @var{value} should
+point to a @code{size_t} holding the desired cache size, or the
+constant @code{GDBM_CACHE_AUTO}, to set the best cache size
+automatically.
+
+By default, a newly open database is configured to adapt the cache
+size to the number of index buckets in the database file. This
+provides for the best performance.
+
+Use this option if you wish to limit the memory usage at the expense
+of performance. If you chose to do so, please bear in mind that cache
+becomes effective when its size is greater then 2/3 of the number of
+index bucket counts in the database. The best performance results are
+achieved when cache size equals the number of buckets. For example:
-The @samp{GDBM_CACHESIZE} option is provided for compatibility with
-earlier versions.
+@example
+size_t bn;
+gdbm_bucket_count (dbf, &bn);
+ret = gdbm_setopt (dbf, GDBM_SETCACHESIZE, &bn, sizeof (bn));
+@end example
-For instance, to set a database to use a cache of 10, after opening it
-with @code{gdbm_open}, but prior to accessing it in any way, the following
-code could be used:
+To set the best cache size, use the constant @code{GDBM_CACHE_AUTO}:
@example
-@group
-int value = 10;
-ret = gdbm_setopt (dbf, GDBM_SETCACHESIZE, &value, sizeof (int));
-@end group
+size_t bn = GDBM_CACHE_AUTO;
+ret = gdbm_setopt (dbf, GDBM_SETCACHESIZE, &bn, sizeof (bn));
@end example
@kwindex GDBM_GETCACHESIZE
@item GDBM_GETCACHESIZE
Return the size of the internal bucket cache. The @var{value} should
point to a @code{size_t} variable, where the size will be stored.
@kwindex GDBM_GETFLAGS
@item GDBM_GETFLAGS
Return the flags describing the state of the database. The @var{value} should
point to an @code{int} variable where to store the flags. On success,