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