blob: adb130422dc48133007d160eb1d74492fa101a6e [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{gdbm} ---
2 GNU's reinterpretation of dbm.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{gdbm}
4
5\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 Drakec2297c11997-12-04 04:45:28 +000022This module is quite similar to the \code{dbm} module, but uses \code{gdbm}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000023instead to provide some additional functionality. Please note that
Fred Drakec2297c11997-12-04 04:45:28 +000024the file formats created by \code{gdbm} and \code{dbm} are incompatible.
Fred Drake4f496cc1997-12-16 04:08:24 +000025\refbimodindex{dbm}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000026
27The \code{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 Drakec2297c11997-12-04 04:45:28 +000030Printing a \code{gdbm} object doesn't print the keys and values, and the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000031\code{items()} and \code{values()} methods are not supported.
32
33The module defines the following constant and functions:
34
Guido van Rossum3c2a0561997-07-17 16:29:42 +000035\begin{excdesc}{error}
Fred Drakec2297c11997-12-04 04:45:28 +000036Raised on \code{gdbm}-specific errors, such as I/O errors. \code{KeyError} is
Guido van Rossum3c2a0561997-07-17 16:29:42 +000037raised for general mapping errors like specifying an incorrect key.
38\end{excdesc}
39
Fred Drakecce10901998-03-17 06:33:25 +000040\begin{funcdesc}{open}{filename, \optional{flag, \optional{mode}}}
Fred Drakec2297c11997-12-04 04:45:28 +000041Open a \code{gdbm} database and return a \code{gdbm} object. The
42\var{filename} argument is the name of the database file.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000043
44The optional \var{flag} argument can be
45\code{'r'} (to open an existing database for reading only --- default),
46\code{'w'} (to open an existing database for reading and writing),
47\code{'c'} (which creates the database if it doesn't exist), or
48\code{'n'} (which always creates a new empty database).
49
50Appending \code{f} to the flag opens the database in fast mode;
51altered data will not automatically be written to the disk after every
52change. This results in faster writes to the database, but may result
53in an inconsistent database if the program crashes while the database
54is still open. Use the \code{sync()} method to force any unwritten
55data to be written to the disk.
56
57The optional \var{mode} argument is the \UNIX{} mode of the file, used
58only when the database has to be created. It defaults to octal
59\code{0666}.
60\end{funcdesc}
61
Fred Drakec2297c11997-12-04 04:45:28 +000062In addition to the dictionary-like methods, \code{gdbm} objects have the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000063following methods:
64
65\begin{funcdesc}{firstkey}{}
66It's possible to loop over every key in the database using this method
Fred Drakec2297c11997-12-04 04:45:28 +000067and the \code{nextkey()} method. The traversal is ordered by \code{gdbm}'s
Guido van Rossum3c2a0561997-07-17 16:29:42 +000068internal hash values, and won't be sorted by the key values. This
69method returns the starting key.
70\end{funcdesc}
71
72\begin{funcdesc}{nextkey}{key}
73Returns the key that follows \var{key} in the traversal. The
74following code prints every key in the database \code{db}, without having to
75create a list in memory that contains them all:
Fred Drake19479911998-02-13 06:58:54 +000076\begin{verbatim}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000077k=db.firstkey()
78while k!=None:
79 print k
80 k=db.nextkey(k)
Fred Drake19479911998-02-13 06:58:54 +000081\end{verbatim}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000082\end{funcdesc}
83
84\begin{funcdesc}{reorganize}{}
85If you have carried out a lot of deletions and would like to shrink
Fred Drakec2297c11997-12-04 04:45:28 +000086the space used by the \code{gdbm} file, this routine will reorganize the
87database. \code{gdbm} will not shorten the length of a database file except
Guido van Rossum3c2a0561997-07-17 16:29:42 +000088by using this reorganization; otherwise, deleted file space will be
89kept and reused as new (key,value) pairs are added.
90\end{funcdesc}
91
92\begin{funcdesc}{sync}{}
93When the database has been opened in fast mode, this method forces any
94unwritten data to be written to the disk.
95\end{funcdesc}
96