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