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 |
Andrew M. Kuchling | 9747301 | 2005-12-22 20:12:54 +0000 | [diff] [blame] | 15 | serialize them somehow, typically using \function{marshal.dumps()} or |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 16 | \function{pickle.dumps()}. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 17 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 18 | The \module{bsddb} module requires a Berkeley DB library version from |
| 19 | 3.3 thru 4.4. |
Gregory P. Smith | 5772513 | 2003-05-28 07:56:45 +0000 | [diff] [blame] | 20 | |
Gregory P. Smith | 5772513 | 2003-05-28 07:56:45 +0000 | [diff] [blame] | 21 | \begin{seealso} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame^] | 22 | \seeurl{http://pybsddb.sourceforge.net/} |
| 23 | {The website with documentation for the \module{bsddb.db} |
| 24 | Python Berkeley DB interface that closely mirrors the object |
| 25 | oriented interface provided in Berkeley DB 3 and 4.} |
| 26 | |
| 27 | \seeurl{http://www.oracle.com/database/berkeley-db/} |
| 28 | {The Berkeley DB library.} |
Gregory P. Smith | 5772513 | 2003-05-28 07:56:45 +0000 | [diff] [blame] | 29 | \end{seealso} |
| 30 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 31 | A more modern DB, DBEnv and DBSequence object interface is available in the |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame^] | 32 | \module{bsddb.db} module which closely matches the Berkeley DB C API |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 33 | documented at the above URLs. Additional features provided by the |
| 34 | \module{bsddb.db} API include fine tuning, transactions, logging, and |
| 35 | multiprocess concurrent database access. |
| 36 | |
Gregory P. Smith | 5772513 | 2003-05-28 07:56:45 +0000 | [diff] [blame] | 37 | The following is a description of the legacy \module{bsddb} interface |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame^] | 38 | compatible with the old Python bsddb module. Starting in Python 2.5 this |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 39 | interface should be safe for multithreaded access. The \module{bsddb.db} |
| 40 | API is recommended for threading users as it provides better control. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 41 | |
| 42 | The \module{bsddb} module defines the following functions that create |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 43 | objects that access the appropriate type of Berkeley DB file. The |
| 44 | first two arguments of each function are the same. For ease of |
| 45 | portability, only the first two arguments should be used in most |
| 46 | instances. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 47 | |
| 48 | \begin{funcdesc}{hashopen}{filename\optional{, flag\optional{, |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 49 | mode\optional{, bsize\optional{, |
| 50 | ffactor\optional{, nelem\optional{, |
| 51 | cachesize\optional{, hash\optional{, |
| 52 | lorder}}}}}}}}} |
Anthony Baxter | 8388895 | 2002-04-23 02:11:05 +0000 | [diff] [blame] | 53 | Open the hash format file named \var{filename}. Files never intended |
| 54 | to be preserved on disk may be created by passing \code{None} as the |
| 55 | \var{filename}. The optional |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 56 | \var{flag} identifies the mode used to open the file. It may be |
Fred Drake | fdccf1a | 2004-07-26 16:33:29 +0000 | [diff] [blame] | 57 | \character{r} (read only), \character{w} (read-write) , |
| 58 | \character{c} (read-write - create if necessary; the default) or |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 59 | \character{n} (read-write - truncate to zero length). The other |
| 60 | arguments are rarely used and are just passed to the low-level |
| 61 | \cfunction{dbopen()} function. Consult the Berkeley DB documentation |
| 62 | for their use and interpretation. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 63 | \end{funcdesc} |
| 64 | |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 65 | \begin{funcdesc}{btopen}{filename\optional{, flag\optional{, |
| 66 | mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{, |
Andrew M. Kuchling | 8dbe1a7 | 2005-06-08 21:51:28 +0000 | [diff] [blame] | 67 | minkeypage\optional{, pgsize\optional{, lorder}}}}}}}}} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 68 | |
Anthony Baxter | 8388895 | 2002-04-23 02:11:05 +0000 | [diff] [blame] | 69 | Open the btree format file named \var{filename}. Files never intended |
| 70 | to be preserved on disk may be created by passing \code{None} as the |
| 71 | \var{filename}. The optional |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 72 | \var{flag} identifies the mode used to open the file. It may be |
Fred Drake | fdccf1a | 2004-07-26 16:33:29 +0000 | [diff] [blame] | 73 | \character{r} (read only), \character{w} (read-write), |
| 74 | \character{c} (read-write - create if necessary; the default) or |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 75 | \character{n} (read-write - truncate to zero length). The other |
| 76 | arguments are rarely used and are just passed to the low-level dbopen |
| 77 | function. Consult the Berkeley DB documentation for their use and |
| 78 | interpretation. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 79 | \end{funcdesc} |
| 80 | |
| 81 | \begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{, |
Andrew M. Kuchling | 8dbe1a7 | 2005-06-08 21:51:28 +0000 | [diff] [blame] | 82 | rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{, |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 83 | reclen\optional{, bval\optional{, bfname}}}}}}}}}} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 84 | |
Anthony Baxter | 8388895 | 2002-04-23 02:11:05 +0000 | [diff] [blame] | 85 | Open a DB record format file named \var{filename}. Files never intended |
| 86 | to be preserved on disk may be created by passing \code{None} as the |
| 87 | \var{filename}. The optional |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 88 | \var{flag} identifies the mode used to open the file. It may be |
Fred Drake | fdccf1a | 2004-07-26 16:33:29 +0000 | [diff] [blame] | 89 | \character{r} (read only), \character{w} (read-write), |
| 90 | \character{c} (read-write - create if necessary; the default) or |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 91 | \character{n} (read-write - truncate to zero length). The other |
| 92 | arguments are rarely used and are just passed to the low-level dbopen |
| 93 | function. Consult the Berkeley DB documentation for their use and |
| 94 | interpretation. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 95 | \end{funcdesc} |
| 96 | |
| 97 | |
Skip Montanaro | 6d9f45b | 2003-05-06 20:40:17 +0000 | [diff] [blame] | 98 | \begin{notice} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 99 | Beginning in 2.3 some \UNIX{} versions of Python may have a \module{bsddb185} |
Skip Montanaro | 6d9f45b | 2003-05-06 20:40:17 +0000 | [diff] [blame] | 100 | module. This is present \emph{only} to allow backwards compatibility with |
| 101 | systems which ship with the old Berkeley DB 1.85 database library. The |
| 102 | \module{bsddb185} module should never be used directly in new code. |
| 103 | \end{notice} |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 104 | |
Fred Drake | b86aa99 | 2004-06-24 06:03:59 +0000 | [diff] [blame] | 105 | |
| 106 | \begin{seealso} |
| 107 | \seemodule{dbhash}{DBM-style interface to the \module{bsddb}} |
| 108 | \end{seealso} |
| 109 | |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 110 | \subsection{Hash, BTree and Record Objects \label{bsddb-objects}} |
| 111 | |
Raymond Hettinger | deadbf5 | 2003-09-12 06:33:37 +0000 | [diff] [blame] | 112 | Once instantiated, hash, btree and record objects support |
| 113 | the same methods as dictionaries. In addition, they support |
Raymond Hettinger | 3404034 | 2003-09-16 21:45:22 +0000 | [diff] [blame] | 114 | the methods listed below. |
| 115 | \versionchanged[Added dictionary methods]{2.3.1} |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 116 | |
| 117 | \begin{methoddesc}{close}{} |
| 118 | Close the underlying file. The object can no longer be accessed. Since |
| 119 | there is no open \method{open} method for these objects, to open the file |
| 120 | again a new \module{bsddb} module open function must be called. |
| 121 | \end{methoddesc} |
| 122 | |
| 123 | \begin{methoddesc}{keys}{} |
| 124 | Return the list of keys contained in the DB file. The order of the list is |
| 125 | unspecified and should not be relied on. In particular, the order of the |
| 126 | list returned is different for different file formats. |
| 127 | \end{methoddesc} |
| 128 | |
| 129 | \begin{methoddesc}{has_key}{key} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 130 | 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] | 131 | \end{methoddesc} |
| 132 | |
| 133 | \begin{methoddesc}{set_location}{key} |
Fred Drake | e1d4715 | 2001-01-05 06:44:19 +0000 | [diff] [blame] | 134 | Set the cursor to the item indicated by \var{key} and return a tuple |
| 135 | containing the key and its value. For binary tree databases (opened |
| 136 | using \function{btopen()}), if \var{key} does not actually exist in |
| 137 | the database, the cursor will point to the next item in sorted order |
| 138 | and return that key and value. For other databases, |
| 139 | \exception{KeyError} will be raised if \var{key} is not found in the |
| 140 | database. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 141 | \end{methoddesc} |
| 142 | |
| 143 | \begin{methoddesc}{first}{} |
| 144 | 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] | 145 | keys in the file is unspecified, except in the case of B-Tree databases. |
Fred Drake | ba100c9 | 2004-08-10 19:22:48 +0000 | [diff] [blame] | 146 | This method raises \exception{bsddb.error} if the database is empty. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 147 | \end{methoddesc} |
| 148 | |
| 149 | \begin{methoddesc}{next}{} |
| 150 | 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] | 151 | 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] | 152 | \end{methoddesc} |
| 153 | |
| 154 | \begin{methoddesc}{previous}{} |
Skip Montanaro | 6141812 | 2002-11-17 11:09:50 +0000 | [diff] [blame] | 155 | Set the cursor to the previous item in the DB file and return it. The |
Fred Drake | 29cf682 | 1999-04-23 20:32:59 +0000 | [diff] [blame] | 156 | order of keys in the file is unspecified, except in the case of B-Tree |
| 157 | databases. This is not supported on hashtable databases (those opened |
| 158 | with \function{hashopen()}). |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 159 | \end{methoddesc} |
| 160 | |
| 161 | \begin{methoddesc}{last}{} |
Fred Drake | 2ea30f4 | 1999-04-22 14:06:36 +0000 | [diff] [blame] | 162 | Set the cursor to the last item in the DB file and return it. The |
| 163 | order of keys in the file is unspecified. This is not supported on |
| 164 | hashtable databases (those opened with \function{hashopen()}). |
Fred Drake | ba100c9 | 2004-08-10 19:22:48 +0000 | [diff] [blame] | 165 | This method raises \exception{bsddb.error} if the database is empty. |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 166 | \end{methoddesc} |
| 167 | |
| 168 | \begin{methoddesc}{sync}{} |
| 169 | Synchronize the database on disk. |
| 170 | \end{methoddesc} |
| 171 | |
| 172 | Example: |
| 173 | |
| 174 | \begin{verbatim} |
| 175 | >>> import bsddb |
| 176 | >>> db = bsddb.btopen('/tmp/spam.db', 'c') |
| 177 | >>> for i in range(10): db['%d'%i] = '%d'% (i*i) |
| 178 | ... |
| 179 | >>> db['3'] |
| 180 | '9' |
| 181 | >>> db.keys() |
| 182 | ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] |
| 183 | >>> db.first() |
| 184 | ('0', '0') |
| 185 | >>> db.next() |
| 186 | ('1', '1') |
| 187 | >>> db.last() |
| 188 | ('9', '81') |
| 189 | >>> db.set_location('2') |
| 190 | ('2', '4') |
| 191 | >>> db.previous() |
| 192 | ('1', '1') |
Raymond Hettinger | deadbf5 | 2003-09-12 06:33:37 +0000 | [diff] [blame] | 193 | >>> for k, v in db.iteritems(): |
| 194 | ... print k, v |
| 195 | 0 0 |
| 196 | 1 1 |
| 197 | 2 4 |
| 198 | 3 9 |
| 199 | 4 16 |
| 200 | 5 25 |
| 201 | 6 36 |
| 202 | 7 49 |
| 203 | 8 64 |
| 204 | 9 81 |
Raymond Hettinger | ff294fe | 2003-12-07 13:00:25 +0000 | [diff] [blame] | 205 | >>> '8' in db |
Raymond Hettinger | deadbf5 | 2003-09-12 06:33:37 +0000 | [diff] [blame] | 206 | True |
Fred Drake | 9d15881 | 1999-04-19 21:19:21 +0000 | [diff] [blame] | 207 | >>> db.sync() |
| 208 | 0 |
| 209 | \end{verbatim} |