blob: 7e26b78f336ed9af995880681b0f0287a9704a02 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{gdbm} ---
Fred Drakebbac4321999-02-20 00:14:17 +00002 GNU's reinterpretation of dbm}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakebbac4321999-02-20 00:14:17 +00004\declaremodule{builtin}{gdbm}
Fred Drakea54a8871999-03-02 17:03:42 +00005 \platform{Unix}
Fred Drakeb91e9341998-07-23 17:59:49 +00006\modulesynopsis{GNU's reinterpretation of dbm.}
7
Guido van Rossumb69e0951994-08-08 08:03:24 +00008
Fred Drake4f21d541999-04-05 22:18:12 +00009This module is quite similar to the \refmodule{dbm}\refbimodindex{dbm}
Fred Drakebbac4321999-02-20 00:14:17 +000010module, but uses \code{gdbm} instead to provide some additional
11functionality. Please note that the file formats created by
12\code{gdbm} and \code{dbm} are incompatible.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000013
Fred Drakebbac4321999-02-20 00:14:17 +000014The \module{gdbm} module provides an interface to the GNU DBM
Fred Drakec2297c11997-12-04 04:45:28 +000015library. \code{gdbm} objects behave like mappings
Guido van Rossum3c2a0561997-07-17 16:29:42 +000016(dictionaries), except that keys and values are always strings.
Fred Drakebbac4321999-02-20 00:14:17 +000017Printing a \code{gdbm} object doesn't print the keys and values, and
18the \method{items()} and \method{values()} methods are not supported.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000019
20The module defines the following constant and functions:
21
Guido van Rossum3c2a0561997-07-17 16:29:42 +000022\begin{excdesc}{error}
Fred Drakebbac4321999-02-20 00:14:17 +000023Raised on \code{gdbm}-specific errors, such as I/O errors.
24\exception{KeyError} is raised for general mapping errors like
25specifying an incorrect key.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000026\end{excdesc}
27
Fred Drakecce10901998-03-17 06:33:25 +000028\begin{funcdesc}{open}{filename, \optional{flag, \optional{mode}}}
Fred Drakec2297c11997-12-04 04:45:28 +000029Open a \code{gdbm} database and return a \code{gdbm} object. The
30\var{filename} argument is the name of the database file.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000031
32The optional \var{flag} argument can be
33\code{'r'} (to open an existing database for reading only --- default),
34\code{'w'} (to open an existing database for reading and writing),
35\code{'c'} (which creates the database if it doesn't exist), or
36\code{'n'} (which always creates a new empty database).
37
Neil Schemenauere9e860f2000-12-17 07:14:13 +000038The following additional characters may be appended to the flag to
39control how the database is opened:
40
41\begin{itemize}
42\item \code{'f'} --- Open the database in fast mode. Writes to the database
43 will not be syncronized.
44\item \code{'s'} --- Synchronized mode. This will cause changes to the database
45 will be immediately written to the file.
46\item \code{'u'} --- Do not lock database.
47\end{itemize}
48
49Not all flags are valid for all versions of \code{gdbm}. The
50module constant \code{open_flags} is a string of supported flag
51characters. The exception \exception{error} is raised if an invalid
52flag is specified.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000053
54The optional \var{mode} argument is the \UNIX{} mode of the file, used
55only when the database has to be created. It defaults to octal
56\code{0666}.
57\end{funcdesc}
58
Fred Drakec2297c11997-12-04 04:45:28 +000059In addition to the dictionary-like methods, \code{gdbm} objects have the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000060following methods:
61
62\begin{funcdesc}{firstkey}{}
Fred Drakebbac4321999-02-20 00:14:17 +000063It's possible to loop over every key in the database using this method
64and the \method{nextkey()} method. The traversal is ordered by
65\code{gdbm}'s internal hash values, and won't be sorted by the key
66values. This method returns the starting key.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000067\end{funcdesc}
68
69\begin{funcdesc}{nextkey}{key}
70Returns the key that follows \var{key} in the traversal. The
Fred Drakebbac4321999-02-20 00:14:17 +000071following code prints every key in the database \code{db}, without
72having to create a list in memory that contains them all:
73
Fred Drake19479911998-02-13 06:58:54 +000074\begin{verbatim}
Fred Drakebbac4321999-02-20 00:14:17 +000075k = db.firstkey()
76while k != None:
Guido van Rossum3c2a0561997-07-17 16:29:42 +000077 print k
Fred Drakebbac4321999-02-20 00:14:17 +000078 k = db.nextkey(k)
Fred Drake19479911998-02-13 06:58:54 +000079\end{verbatim}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000080\end{funcdesc}
81
82\begin{funcdesc}{reorganize}{}
83If you have carried out a lot of deletions and would like to shrink
Fred Drakebbac4321999-02-20 00:14:17 +000084the space used by the \code{gdbm} file, this routine will reorganize
85the database. \code{gdbm} will not shorten the length of a database
86file except by using this reorganization; otherwise, deleted file
87space will be kept and reused as new (key, value) pairs are added.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000088\end{funcdesc}
89
90\begin{funcdesc}{sync}{}
Fred Drakebbac4321999-02-20 00:14:17 +000091When the database has been opened in fast mode, this method forces any
Guido van Rossum3c2a0561997-07-17 16:29:42 +000092unwritten data to be written to the disk.
93\end{funcdesc}
94
Fred Drake4f21d541999-04-05 22:18:12 +000095
96\begin{seealso}
97 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
98 \seemodule{whichdb}{Utility module used to determine the type of an
99 existing database.}
100\end{seealso}