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