blob: 844c3b3be8df3c8d1a15d5f5b0afaba1c3fb71fd [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{gdbm}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{gdbm}
3
4\modulesynopsis{GNU's reinterpretation of dbm.}
5
Guido van Rossumb69e0951994-08-08 08:03:24 +00006
Fred Drake8ecc7051998-02-17 20:31:08 +00007% Note that if this section appears on the same page as the first
8% paragraph of the dbm module section, makeindex will produce the
9% warning:
10%
11% ## Warning (input = lib.idx, line = 1184; output = lib.ind, line = 852):
12% -- Conflicting entries: multiple encaps for the same page under same key.
13%
14% This is because the \bimodindex{gdbm} and \refbimodindex{gdbm}
15% entries in the .idx file are slightly different (the \bimodindex{}
16% version includes "|textbf" at the end to make the defining occurance
17% bold). There doesn't appear to be anything that can be done about
18% this; it's just a little annoying. The warning can be ignored, but
19% the index produced uses the non-bold version.
20
Fred Drakec2297c11997-12-04 04:45:28 +000021This module is quite similar to the \code{dbm} module, but uses \code{gdbm}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000022instead to provide some additional functionality. Please note that
Fred Drakec2297c11997-12-04 04:45:28 +000023the file formats created by \code{gdbm} and \code{dbm} are incompatible.
Fred Drake4f496cc1997-12-16 04:08:24 +000024\refbimodindex{dbm}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000025
26The \code{gdbm} module provides an interface to the GNU DBM
Fred Drakec2297c11997-12-04 04:45:28 +000027library. \code{gdbm} objects behave like mappings
Guido van Rossum3c2a0561997-07-17 16:29:42 +000028(dictionaries), except that keys and values are always strings.
Fred Drakec2297c11997-12-04 04:45:28 +000029Printing a \code{gdbm} object doesn't print the keys and values, and the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000030\code{items()} and \code{values()} methods are not supported.
31
32The module defines the following constant and functions:
33
Guido van Rossum3c2a0561997-07-17 16:29:42 +000034\begin{excdesc}{error}
Fred Drakec2297c11997-12-04 04:45:28 +000035Raised on \code{gdbm}-specific errors, such as I/O errors. \code{KeyError} is
Guido van Rossum3c2a0561997-07-17 16:29:42 +000036raised for general mapping errors like specifying an incorrect key.
37\end{excdesc}
38
Fred Drakecce10901998-03-17 06:33:25 +000039\begin{funcdesc}{open}{filename, \optional{flag, \optional{mode}}}
Fred Drakec2297c11997-12-04 04:45:28 +000040Open a \code{gdbm} database and return a \code{gdbm} object. The
41\var{filename} argument is the name of the database file.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000042
43The optional \var{flag} argument can be
44\code{'r'} (to open an existing database for reading only --- default),
45\code{'w'} (to open an existing database for reading and writing),
46\code{'c'} (which creates the database if it doesn't exist), or
47\code{'n'} (which always creates a new empty database).
48
49Appending \code{f} to the flag opens the database in fast mode;
50altered data will not automatically be written to the disk after every
51change. This results in faster writes to the database, but may result
52in an inconsistent database if the program crashes while the database
53is still open. Use the \code{sync()} method to force any unwritten
54data to be written to the disk.
55
56The optional \var{mode} argument is the \UNIX{} mode of the file, used
57only when the database has to be created. It defaults to octal
58\code{0666}.
59\end{funcdesc}
60
Fred Drakec2297c11997-12-04 04:45:28 +000061In addition to the dictionary-like methods, \code{gdbm} objects have the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000062following methods:
63
64\begin{funcdesc}{firstkey}{}
65It's possible to loop over every key in the database using this method
Fred Drakec2297c11997-12-04 04:45:28 +000066and the \code{nextkey()} method. The traversal is ordered by \code{gdbm}'s
Guido van Rossum3c2a0561997-07-17 16:29:42 +000067internal hash values, and won't be sorted by the key values. This
68method returns the starting key.
69\end{funcdesc}
70
71\begin{funcdesc}{nextkey}{key}
72Returns the key that follows \var{key} in the traversal. The
73following code prints every key in the database \code{db}, without having to
74create a list in memory that contains them all:
Fred Drake19479911998-02-13 06:58:54 +000075\begin{verbatim}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000076k=db.firstkey()
77while k!=None:
78 print k
79 k=db.nextkey(k)
Fred Drake19479911998-02-13 06:58:54 +000080\end{verbatim}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000081\end{funcdesc}
82
83\begin{funcdesc}{reorganize}{}
84If you have carried out a lot of deletions and would like to shrink
Fred Drakec2297c11997-12-04 04:45:28 +000085the space used by the \code{gdbm} file, this routine will reorganize the
86database. \code{gdbm} will not shorten the length of a database file except
Guido van Rossum3c2a0561997-07-17 16:29:42 +000087by using this reorganization; otherwise, deleted file space will be
88kept and reused as new (key,value) pairs are added.
89\end{funcdesc}
90
91\begin{funcdesc}{sync}{}
92When the database has been opened in fast mode, this method forces any
93unwritten data to be written to the disk.
94\end{funcdesc}
95