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