blob: 2166d34fd113287a2b8f8f156121aee3f4f74f01 [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
Fred Drakebbac4321999-02-20 00:14:17 +000038Appending \character{f} to the flag opens the database in fast mode;
Guido van Rossum3c2a0561997-07-17 16:29:42 +000039altered data will not automatically be written to the disk after every
40change. This results in faster writes to the database, but may result
41in an inconsistent database if the program crashes while the database
Fred Drakebbac4321999-02-20 00:14:17 +000042is still open. Use the \method{sync()} method to force any unwritten
Guido van Rossum3c2a0561997-07-17 16:29:42 +000043data to be written to the disk.
44
45The optional \var{mode} argument is the \UNIX{} mode of the file, used
46only when the database has to be created. It defaults to octal
47\code{0666}.
48\end{funcdesc}
49
Fred Drakec2297c11997-12-04 04:45:28 +000050In addition to the dictionary-like methods, \code{gdbm} objects have the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000051following methods:
52
53\begin{funcdesc}{firstkey}{}
Fred Drakebbac4321999-02-20 00:14:17 +000054It's possible to loop over every key in the database using this method
55and the \method{nextkey()} method. The traversal is ordered by
56\code{gdbm}'s internal hash values, and won't be sorted by the key
57values. This method returns the starting key.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000058\end{funcdesc}
59
60\begin{funcdesc}{nextkey}{key}
61Returns the key that follows \var{key} in the traversal. The
Fred Drakebbac4321999-02-20 00:14:17 +000062following code prints every key in the database \code{db}, without
63having to create a list in memory that contains them all:
64
Fred Drake19479911998-02-13 06:58:54 +000065\begin{verbatim}
Fred Drakebbac4321999-02-20 00:14:17 +000066k = db.firstkey()
67while k != None:
Guido van Rossum3c2a0561997-07-17 16:29:42 +000068 print k
Fred Drakebbac4321999-02-20 00:14:17 +000069 k = db.nextkey(k)
Fred Drake19479911998-02-13 06:58:54 +000070\end{verbatim}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000071\end{funcdesc}
72
73\begin{funcdesc}{reorganize}{}
74If you have carried out a lot of deletions and would like to shrink
Fred Drakebbac4321999-02-20 00:14:17 +000075the space used by the \code{gdbm} file, this routine will reorganize
76the database. \code{gdbm} will not shorten the length of a database
77file except by using this reorganization; otherwise, deleted file
78space will be kept and reused as new (key, value) pairs are added.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000079\end{funcdesc}
80
81\begin{funcdesc}{sync}{}
Fred Drakebbac4321999-02-20 00:14:17 +000082When the database has been opened in fast mode, this method forces any
Guido van Rossum3c2a0561997-07-17 16:29:42 +000083unwritten data to be written to the disk.
84\end{funcdesc}
85
Fred Drake4f21d541999-04-05 22:18:12 +000086
87\begin{seealso}
88 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
89 \seemodule{whichdb}{Utility module used to determine the type of an
90 existing database.}
91\end{seealso}