Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{gdbm} --- |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 2 | GNU's reinterpretation of dbm} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 4 | \declaremodule{builtin}{gdbm} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{GNU's reinterpretation of dbm.} |
| 6 | |
Guido van Rossum | b69e095 | 1994-08-08 08:03:24 +0000 | [diff] [blame] | 7 | |
Fred Drake | 8ecc705 | 1998-02-17 20:31:08 +0000 | [diff] [blame] | 8 | % 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 Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 22 | This module is quite similar to the \module{dbm}\refbimodindex{dbm} |
| 23 | module, but uses \code{gdbm} instead to provide some additional |
| 24 | functionality. Please note that the file formats created by |
| 25 | \code{gdbm} and \code{dbm} are incompatible. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 26 | |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 27 | The \module{gdbm} module provides an interface to the GNU DBM |
Fred Drake | c2297c1 | 1997-12-04 04:45:28 +0000 | [diff] [blame] | 28 | library. \code{gdbm} objects behave like mappings |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 29 | (dictionaries), except that keys and values are always strings. |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 30 | Printing a \code{gdbm} object doesn't print the keys and values, and |
| 31 | the \method{items()} and \method{values()} methods are not supported. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 32 | |
| 33 | The module defines the following constant and functions: |
| 34 | |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 35 | \begin{excdesc}{error} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 36 | Raised on \code{gdbm}-specific errors, such as I/O errors. |
| 37 | \exception{KeyError} is raised for general mapping errors like |
| 38 | specifying an incorrect key. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 39 | \end{excdesc} |
| 40 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 41 | \begin{funcdesc}{open}{filename, \optional{flag, \optional{mode}}} |
Fred Drake | c2297c1 | 1997-12-04 04:45:28 +0000 | [diff] [blame] | 42 | Open a \code{gdbm} database and return a \code{gdbm} object. The |
| 43 | \var{filename} argument is the name of the database file. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 44 | |
| 45 | The 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 Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 51 | Appending \character{f} to the flag opens the database in fast mode; |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 52 | altered data will not automatically be written to the disk after every |
| 53 | change. This results in faster writes to the database, but may result |
| 54 | in an inconsistent database if the program crashes while the database |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 55 | is still open. Use the \method{sync()} method to force any unwritten |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 56 | data to be written to the disk. |
| 57 | |
| 58 | The optional \var{mode} argument is the \UNIX{} mode of the file, used |
| 59 | only when the database has to be created. It defaults to octal |
| 60 | \code{0666}. |
| 61 | \end{funcdesc} |
| 62 | |
Fred Drake | c2297c1 | 1997-12-04 04:45:28 +0000 | [diff] [blame] | 63 | In addition to the dictionary-like methods, \code{gdbm} objects have the |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 64 | following methods: |
| 65 | |
| 66 | \begin{funcdesc}{firstkey}{} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 67 | It's possible to loop over every key in the database using this method |
| 68 | and the \method{nextkey()} method. The traversal is ordered by |
| 69 | \code{gdbm}'s internal hash values, and won't be sorted by the key |
| 70 | values. This method returns the starting key. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 71 | \end{funcdesc} |
| 72 | |
| 73 | \begin{funcdesc}{nextkey}{key} |
| 74 | Returns the key that follows \var{key} in the traversal. The |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 75 | following code prints every key in the database \code{db}, without |
| 76 | having to create a list in memory that contains them all: |
| 77 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 78 | \begin{verbatim} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 79 | k = db.firstkey() |
| 80 | while k != None: |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 81 | print k |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 82 | k = db.nextkey(k) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 83 | \end{verbatim} |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 84 | \end{funcdesc} |
| 85 | |
| 86 | \begin{funcdesc}{reorganize}{} |
| 87 | If you have carried out a lot of deletions and would like to shrink |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 88 | the space used by the \code{gdbm} file, this routine will reorganize |
| 89 | the database. \code{gdbm} will not shorten the length of a database |
| 90 | file except by using this reorganization; otherwise, deleted file |
| 91 | space will be kept and reused as new (key, value) pairs are added. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 92 | \end{funcdesc} |
| 93 | |
| 94 | \begin{funcdesc}{sync}{} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame^] | 95 | When the database has been opened in fast mode, this method forces any |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 96 | unwritten data to be written to the disk. |
| 97 | \end{funcdesc} |
| 98 | |