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