Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 1 | \section{\module{bsddb} --- |
| 2 | Interface to Berkeley DB library} |
| 3 | |
| 4 | \declaremodule{extension}{bsddb} |
| 5 | \platform{Unix, Windows} |
| 6 | \modulesynopsis{Interface to Berkeley DB database library} |
| 7 | \sectionauthor{Skip Montanaro}{skip@mojam.com} |
| 8 | |
| 9 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 10 | The \module{bsddb} module provides an interface to the Berkeley DB |
| 11 | library. Users can create hash, btree or record based library files |
| 12 | using the appropriate open call. Bsddb objects behave generally like |
| 13 | dictionaries. Keys and values must be strings, however, so to use |
| 14 | other objects as keys or to store other kinds of objects the user must |
| 15 | serialize them somehow, typically using marshal.dumps or pickle.dumps. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 16 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 17 | The \module{bsddb} module is only available on \UNIX{} systems, so it |
| 18 | is not built by default in the standard Python distribution. Also, |
| 19 | there are two incompatible versions of the underlying library. |
| 20 | Version 1.85 is widely available, but has some known bugs. Version 2 |
| 21 | is not quite as widely used, but does offer some improvements. The |
| 22 | \module{bsddb} module uses the 1.85 interface. Users wishing to use |
| 23 | version 2 of the Berkeley DB library will have to modify the source |
| 24 | for the module to include \file{db_185.h} instead of |
| 25 | \file{db.h} (\file{db_185.h} contains the version 1.85 compatibility |
| 26 | interface). |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 27 | |
| 28 | The \module{bsddb} module defines the following functions that create |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 29 | objects that access the appropriate type of Berkeley DB file. The |
| 30 | first two arguments of each function are the same. For ease of |
| 31 | portability, only the first two arguments should be used in most |
| 32 | instances. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 33 | |
| 34 | \begin{funcdesc}{hashopen}{filename\optional{, flag\optional{, |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 35 | mode\optional{, bsize\optional{, |
| 36 | ffactor\optional{, nelem\optional{, |
| 37 | cachesize\optional{, hash\optional{, |
| 38 | lorder}}}}}}}}} |
| 39 | Open the hash format file named \var{filename}. The optional |
| 40 | \var{flag} identifies the mode used to open the file. It may be |
| 41 | \character{r} (read only), \character{w} (read-write), |
| 42 | \character{c} (read-write - create if necessary) or |
| 43 | \character{n} (read-write - truncate to zero length). The other |
| 44 | arguments are rarely used and are just passed to the low-level |
| 45 | \cfunction{dbopen()} function. Consult the Berkeley DB documentation |
| 46 | for their use and interpretation. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 47 | \end{funcdesc} |
| 48 | |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 49 | \begin{funcdesc}{btopen}{filename\optional{, flag\optional{, |
| 50 | mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{, |
| 51 | minkeypage\optional{, psize\optional{, lorder}}}}}}}}} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 52 | |
| 53 | Open the btree format file named \var{filename}. The optional |
| 54 | \var{flag} identifies the mode used to open the file. It may be |
| 55 | \character{r} (read only), \character{w} (read-write), |
| 56 | \character{c} (read-write - create if necessary) or |
| 57 | \character{n} (read-write - truncate to zero length). The other |
| 58 | arguments are rarely used and are just passed to the low-level dbopen |
| 59 | function. Consult the Berkeley DB documentation for their use and |
| 60 | interpretation. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 61 | \end{funcdesc} |
| 62 | |
| 63 | \begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{, |
| 64 | rnflags\optional{, cachesize\optional{, psize\optional{, lorder\optional{, |
| 65 | reclen\optional{, bval\optional{, bfname}}}}}}}}}} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 66 | |
| 67 | Open a DB record format file named \var{filename}. The optional |
| 68 | \var{flag} identifies the mode used to open the file. It may be |
| 69 | \character{r} (read only), \character{w} (read-write), |
| 70 | \character{c} (read-write - create if necessary) or |
| 71 | \character{n} (read-write - truncate to zero length). The other |
| 72 | arguments are rarely used and are just passed to the low-level dbopen |
| 73 | function. Consult the Berkeley DB documentation for their use and |
| 74 | interpretation. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 75 | \end{funcdesc} |
| 76 | |
| 77 | |
| 78 | \begin{seealso} |
| 79 | \seemodule{dbhash}{DBM-style interface to the \module{bsddb}} |
| 80 | \end{seealso} |
| 81 | |
| 82 | |
| 83 | \subsection{Hash, BTree and Record Objects \label{bsddb-objects}} |
| 84 | |
| 85 | Once instantiated, hash, btree and record objects support the following |
| 86 | methods: |
| 87 | |
| 88 | \begin{methoddesc}{close}{} |
| 89 | Close the underlying file. The object can no longer be accessed. Since |
| 90 | there is no open \method{open} method for these objects, to open the file |
| 91 | again a new \module{bsddb} module open function must be called. |
| 92 | \end{methoddesc} |
| 93 | |
| 94 | \begin{methoddesc}{keys}{} |
| 95 | Return the list of keys contained in the DB file. The order of the list is |
| 96 | unspecified and should not be relied on. In particular, the order of the |
| 97 | list returned is different for different file formats. |
| 98 | \end{methoddesc} |
| 99 | |
| 100 | \begin{methoddesc}{has_key}{key} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 101 | Return \code{1} if the DB file contains the argument as a key. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 102 | \end{methoddesc} |
| 103 | |
| 104 | \begin{methoddesc}{set_location}{key} |
| 105 | Set the cursor to the item indicated by the key and return it. |
| 106 | \end{methoddesc} |
| 107 | |
| 108 | \begin{methoddesc}{first}{} |
| 109 | Set the cursor to the first item in the DB file and return it. The order of |
Fred Drake | 29cf682 | 1999-04-23 20:32:59 +0000 | [diff] [blame] | 110 | keys in the file is unspecified, except in the case of B-Tree databases. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 111 | \end{methoddesc} |
| 112 | |
| 113 | \begin{methoddesc}{next}{} |
| 114 | Set the cursor to the next item in the DB file and return it. The order of |
Fred Drake | 29cf682 | 1999-04-23 20:32:59 +0000 | [diff] [blame] | 115 | keys in the file is unspecified, except in the case of B-Tree databases. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 116 | \end{methoddesc} |
| 117 | |
| 118 | \begin{methoddesc}{previous}{} |
Fred Drake | 2ea30f4 | 1999-04-22 14:06:36 +0000 | [diff] [blame] | 119 | Set the cursor to the first item in the DB file and return it. The |
Fred Drake | 29cf682 | 1999-04-23 20:32:59 +0000 | [diff] [blame] | 120 | order of keys in the file is unspecified, except in the case of B-Tree |
| 121 | databases. This is not supported on hashtable databases (those opened |
| 122 | with \function{hashopen()}). |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 123 | \end{methoddesc} |
| 124 | |
| 125 | \begin{methoddesc}{last}{} |
Fred Drake | 2ea30f4 | 1999-04-22 14:06:36 +0000 | [diff] [blame] | 126 | Set the cursor to the last item in the DB file and return it. The |
| 127 | order of keys in the file is unspecified. This is not supported on |
| 128 | hashtable databases (those opened with \function{hashopen()}). |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 129 | \end{methoddesc} |
| 130 | |
| 131 | \begin{methoddesc}{sync}{} |
| 132 | Synchronize the database on disk. |
| 133 | \end{methoddesc} |
| 134 | |
| 135 | Example: |
| 136 | |
| 137 | \begin{verbatim} |
| 138 | >>> import bsddb |
| 139 | >>> db = bsddb.btopen('/tmp/spam.db', 'c') |
| 140 | >>> for i in range(10): db['%d'%i] = '%d'% (i*i) |
| 141 | ... |
| 142 | >>> db['3'] |
| 143 | '9' |
| 144 | >>> db.keys() |
| 145 | ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] |
| 146 | >>> db.first() |
| 147 | ('0', '0') |
| 148 | >>> db.next() |
| 149 | ('1', '1') |
| 150 | >>> db.last() |
| 151 | ('9', '81') |
| 152 | >>> db.set_location('2') |
| 153 | ('2', '4') |
| 154 | >>> db.previous() |
| 155 | ('1', '1') |
| 156 | >>> db.sync() |
| 157 | 0 |
| 158 | \end{verbatim} |