blob: 73683ca97d176490c8157f6ba81eebcaf0c3d1aa [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 Drake8ecc7051998-02-17 20:31:08 +00009% Note that if this section appears on the same page as the first
10% paragraph of the dbm module section, makeindex will produce the
11% warning:
12%
13% ## Warning (input = lib.idx, line = 1184; output = lib.ind, line = 852):
14% -- Conflicting entries: multiple encaps for the same page under same key.
15%
16% This is because the \bimodindex{gdbm} and \refbimodindex{gdbm}
17% entries in the .idx file are slightly different (the \bimodindex{}
18% version includes "|textbf" at the end to make the defining occurance
19% bold). There doesn't appear to be anything that can be done about
20% this; it's just a little annoying. The warning can be ignored, but
21% the index produced uses the non-bold version.
22
Fred Drakebbac4321999-02-20 00:14:17 +000023This module is quite similar to the \module{dbm}\refbimodindex{dbm}
24module, but uses \code{gdbm} instead to provide some additional
25functionality. Please note that the file formats created by
26\code{gdbm} and \code{dbm} are incompatible.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000027
Fred Drakebbac4321999-02-20 00:14:17 +000028The \module{gdbm} module provides an interface to the GNU DBM
Fred Drakec2297c11997-12-04 04:45:28 +000029library. \code{gdbm} objects behave like mappings
Guido van Rossum3c2a0561997-07-17 16:29:42 +000030(dictionaries), except that keys and values are always strings.
Fred Drakebbac4321999-02-20 00:14:17 +000031Printing a \code{gdbm} object doesn't print the keys and values, and
32the \method{items()} and \method{values()} methods are not supported.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000033
34The module defines the following constant and functions:
35
Guido van Rossum3c2a0561997-07-17 16:29:42 +000036\begin{excdesc}{error}
Fred Drakebbac4321999-02-20 00:14:17 +000037Raised on \code{gdbm}-specific errors, such as I/O errors.
38\exception{KeyError} is raised for general mapping errors like
39specifying an incorrect key.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000040\end{excdesc}
41
Fred Drakecce10901998-03-17 06:33:25 +000042\begin{funcdesc}{open}{filename, \optional{flag, \optional{mode}}}
Fred Drakec2297c11997-12-04 04:45:28 +000043Open a \code{gdbm} database and return a \code{gdbm} object. The
44\var{filename} argument is the name of the database file.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000045
46The optional \var{flag} argument can be
47\code{'r'} (to open an existing database for reading only --- default),
48\code{'w'} (to open an existing database for reading and writing),
49\code{'c'} (which creates the database if it doesn't exist), or
50\code{'n'} (which always creates a new empty database).
51
Fred Drakebbac4321999-02-20 00:14:17 +000052Appending \character{f} to the flag opens the database in fast mode;
Guido van Rossum3c2a0561997-07-17 16:29:42 +000053altered data will not automatically be written to the disk after every
54change. This results in faster writes to the database, but may result
55in an inconsistent database if the program crashes while the database
Fred Drakebbac4321999-02-20 00:14:17 +000056is still open. Use the \method{sync()} method to force any unwritten
Guido van Rossum3c2a0561997-07-17 16:29:42 +000057data to be written to the disk.
58
59The optional \var{mode} argument is the \UNIX{} mode of the file, used
60only when the database has to be created. It defaults to octal
61\code{0666}.
62\end{funcdesc}
63
Fred Drakec2297c11997-12-04 04:45:28 +000064In addition to the dictionary-like methods, \code{gdbm} objects have the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000065following methods:
66
67\begin{funcdesc}{firstkey}{}
Fred Drakebbac4321999-02-20 00:14:17 +000068It's possible to loop over every key in the database using this method
69and the \method{nextkey()} method. The traversal is ordered by
70\code{gdbm}'s internal hash values, and won't be sorted by the key
71values. This method returns the starting key.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000072\end{funcdesc}
73
74\begin{funcdesc}{nextkey}{key}
75Returns the key that follows \var{key} in the traversal. The
Fred Drakebbac4321999-02-20 00:14:17 +000076following code prints every key in the database \code{db}, without
77having to create a list in memory that contains them all:
78
Fred Drake19479911998-02-13 06:58:54 +000079\begin{verbatim}
Fred Drakebbac4321999-02-20 00:14:17 +000080k = db.firstkey()
81while k != None:
Guido van Rossum3c2a0561997-07-17 16:29:42 +000082 print k
Fred Drakebbac4321999-02-20 00:14:17 +000083 k = db.nextkey(k)
Fred Drake19479911998-02-13 06:58:54 +000084\end{verbatim}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000085\end{funcdesc}
86
87\begin{funcdesc}{reorganize}{}
88If you have carried out a lot of deletions and would like to shrink
Fred Drakebbac4321999-02-20 00:14:17 +000089the space used by the \code{gdbm} file, this routine will reorganize
90the database. \code{gdbm} will not shorten the length of a database
91file except by using this reorganization; otherwise, deleted file
92space will be kept and reused as new (key, value) pairs are added.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000093\end{funcdesc}
94
95\begin{funcdesc}{sync}{}
Fred Drakebbac4321999-02-20 00:14:17 +000096When the database has been opened in fast mode, this method forces any
Guido van Rossum3c2a0561997-07-17 16:29:42 +000097unwritten data to be written to the disk.
98\end{funcdesc}
99