1999-04-19 18:19:21 -03:00
|
|
|
\section{\module{bsddb} ---
|
|
|
|
Interface to Berkeley DB library}
|
|
|
|
|
|
|
|
\declaremodule{extension}{bsddb}
|
|
|
|
\modulesynopsis{Interface to Berkeley DB database library}
|
|
|
|
\sectionauthor{Skip Montanaro}{skip@mojam.com}
|
|
|
|
|
|
|
|
|
2000-04-03 17:13:55 -03:00
|
|
|
The \module{bsddb} module provides an interface to the Berkeley DB
|
|
|
|
library. Users can create hash, btree or record based library files
|
|
|
|
using the appropriate open call. Bsddb objects behave generally like
|
|
|
|
dictionaries. Keys and values must be strings, however, so to use
|
|
|
|
other objects as keys or to store other kinds of objects the user must
|
2005-12-22 16:12:54 -04:00
|
|
|
serialize them somehow, typically using \function{marshal.dumps()} or
|
2006-06-15 06:23:52 -03:00
|
|
|
\function{pickle.dumps()}.
|
2000-04-03 17:13:55 -03:00
|
|
|
|
2006-04-13 16:19:01 -03:00
|
|
|
The \module{bsddb} module requires a Berkeley DB library version from
|
2007-01-04 21:59:42 -04:00
|
|
|
3.3 thru 4.5.
|
2003-05-28 04:56:45 -03:00
|
|
|
|
|
|
|
\begin{seealso}
|
2006-10-05 21:03:45 -03:00
|
|
|
\seeurl{http://pybsddb.sourceforge.net/}
|
|
|
|
{The website with documentation for the \module{bsddb.db}
|
|
|
|
Python Berkeley DB interface that closely mirrors the object
|
|
|
|
oriented interface provided in Berkeley DB 3 and 4.}
|
|
|
|
|
|
|
|
\seeurl{http://www.oracle.com/database/berkeley-db/}
|
|
|
|
{The Berkeley DB library.}
|
2003-05-28 04:56:45 -03:00
|
|
|
\end{seealso}
|
|
|
|
|
2006-06-15 06:23:52 -03:00
|
|
|
A more modern DB, DBEnv and DBSequence object interface is available in the
|
2006-10-05 21:03:45 -03:00
|
|
|
\module{bsddb.db} module which closely matches the Berkeley DB C API
|
2006-06-15 06:23:52 -03:00
|
|
|
documented at the above URLs. Additional features provided by the
|
|
|
|
\module{bsddb.db} API include fine tuning, transactions, logging, and
|
|
|
|
multiprocess concurrent database access.
|
|
|
|
|
2003-05-28 04:56:45 -03:00
|
|
|
The following is a description of the legacy \module{bsddb} interface
|
2006-10-06 07:41:01 -03:00
|
|
|
compatible with the old Python bsddb module. Starting in Python 2.5 this
|
2006-06-15 06:23:52 -03:00
|
|
|
interface should be safe for multithreaded access. The \module{bsddb.db}
|
|
|
|
API is recommended for threading users as it provides better control.
|
1999-04-19 18:19:21 -03:00
|
|
|
|
|
|
|
The \module{bsddb} module defines the following functions that create
|
2000-04-03 17:13:55 -03:00
|
|
|
objects that access the appropriate type of Berkeley DB file. The
|
|
|
|
first two arguments of each function are the same. For ease of
|
|
|
|
portability, only the first two arguments should be used in most
|
|
|
|
instances.
|
1999-04-19 18:19:21 -03:00
|
|
|
|
|
|
|
\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
|
2007-03-14 04:17:40 -03:00
|
|
|
mode\optional{, pgsize\optional{,
|
2000-04-03 17:13:55 -03:00
|
|
|
ffactor\optional{, nelem\optional{,
|
2007-03-14 04:17:40 -03:00
|
|
|
cachesize\optional{, lorder\optional{,
|
|
|
|
hflags}}}}}}}}}
|
2002-04-22 23:11:05 -03:00
|
|
|
Open the hash format file named \var{filename}. Files never intended
|
|
|
|
to be preserved on disk may be created by passing \code{None} as the
|
|
|
|
\var{filename}. The optional
|
2000-04-03 17:13:55 -03:00
|
|
|
\var{flag} identifies the mode used to open the file. It may be
|
2004-07-26 13:33:29 -03:00
|
|
|
\character{r} (read only), \character{w} (read-write) ,
|
|
|
|
\character{c} (read-write - create if necessary; the default) or
|
2000-04-03 17:13:55 -03:00
|
|
|
\character{n} (read-write - truncate to zero length). The other
|
|
|
|
arguments are rarely used and are just passed to the low-level
|
|
|
|
\cfunction{dbopen()} function. Consult the Berkeley DB documentation
|
|
|
|
for their use and interpretation.
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
|
|
|
|
mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
|
2005-06-08 18:51:28 -03:00
|
|
|
minkeypage\optional{, pgsize\optional{, lorder}}}}}}}}}
|
2000-04-03 17:13:55 -03:00
|
|
|
|
2002-04-22 23:11:05 -03:00
|
|
|
Open the btree format file named \var{filename}. Files never intended
|
|
|
|
to be preserved on disk may be created by passing \code{None} as the
|
|
|
|
\var{filename}. The optional
|
2000-04-03 17:13:55 -03:00
|
|
|
\var{flag} identifies the mode used to open the file. It may be
|
2004-07-26 13:33:29 -03:00
|
|
|
\character{r} (read only), \character{w} (read-write),
|
|
|
|
\character{c} (read-write - create if necessary; the default) or
|
2000-04-03 17:13:55 -03:00
|
|
|
\character{n} (read-write - truncate to zero length). The other
|
|
|
|
arguments are rarely used and are just passed to the low-level dbopen
|
|
|
|
function. Consult the Berkeley DB documentation for their use and
|
|
|
|
interpretation.
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
|
2005-06-08 18:51:28 -03:00
|
|
|
rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{,
|
2007-03-14 04:17:40 -03:00
|
|
|
rlen\optional{, delim\optional{, source\optional{, pad}}}}}}}}}}}
|
2000-04-03 17:13:55 -03:00
|
|
|
|
2002-04-22 23:11:05 -03:00
|
|
|
Open a DB record format file named \var{filename}. Files never intended
|
|
|
|
to be preserved on disk may be created by passing \code{None} as the
|
|
|
|
\var{filename}. The optional
|
2000-04-03 17:13:55 -03:00
|
|
|
\var{flag} identifies the mode used to open the file. It may be
|
2004-07-26 13:33:29 -03:00
|
|
|
\character{r} (read only), \character{w} (read-write),
|
|
|
|
\character{c} (read-write - create if necessary; the default) or
|
2000-04-03 17:13:55 -03:00
|
|
|
\character{n} (read-write - truncate to zero length). The other
|
|
|
|
arguments are rarely used and are just passed to the low-level dbopen
|
|
|
|
function. Consult the Berkeley DB documentation for their use and
|
|
|
|
interpretation.
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
|
2003-05-06 17:40:17 -03:00
|
|
|
\begin{notice}
|
2006-07-30 00:03:43 -03:00
|
|
|
Beginning in 2.3 some \UNIX{} versions of Python may have a \module{bsddb185}
|
2003-05-06 17:40:17 -03:00
|
|
|
module. This is present \emph{only} to allow backwards compatibility with
|
|
|
|
systems which ship with the old Berkeley DB 1.85 database library. The
|
|
|
|
\module{bsddb185} module should never be used directly in new code.
|
|
|
|
\end{notice}
|
1999-04-19 18:19:21 -03:00
|
|
|
|
2004-06-24 03:03:59 -03:00
|
|
|
|
|
|
|
\begin{seealso}
|
|
|
|
\seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
|
|
|
|
\end{seealso}
|
|
|
|
|
1999-04-19 18:19:21 -03:00
|
|
|
\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
|
|
|
|
|
2003-09-12 03:33:37 -03:00
|
|
|
Once instantiated, hash, btree and record objects support
|
|
|
|
the same methods as dictionaries. In addition, they support
|
2003-09-16 18:45:22 -03:00
|
|
|
the methods listed below.
|
|
|
|
\versionchanged[Added dictionary methods]{2.3.1}
|
1999-04-19 18:19:21 -03:00
|
|
|
|
|
|
|
\begin{methoddesc}{close}{}
|
|
|
|
Close the underlying file. The object can no longer be accessed. Since
|
|
|
|
there is no open \method{open} method for these objects, to open the file
|
|
|
|
again a new \module{bsddb} module open function must be called.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{keys}{}
|
|
|
|
Return the list of keys contained in the DB file. The order of the list is
|
|
|
|
unspecified and should not be relied on. In particular, the order of the
|
|
|
|
list returned is different for different file formats.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{has_key}{key}
|
2000-04-03 17:13:55 -03:00
|
|
|
Return \code{1} if the DB file contains the argument as a key.
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{set_location}{key}
|
2001-01-05 02:44:19 -04:00
|
|
|
Set the cursor to the item indicated by \var{key} and return a tuple
|
|
|
|
containing the key and its value. For binary tree databases (opened
|
|
|
|
using \function{btopen()}), if \var{key} does not actually exist in
|
|
|
|
the database, the cursor will point to the next item in sorted order
|
|
|
|
and return that key and value. For other databases,
|
|
|
|
\exception{KeyError} will be raised if \var{key} is not found in the
|
|
|
|
database.
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{first}{}
|
|
|
|
Set the cursor to the first item in the DB file and return it. The order of
|
1999-04-23 17:32:59 -03:00
|
|
|
keys in the file is unspecified, except in the case of B-Tree databases.
|
2004-08-10 16:22:48 -03:00
|
|
|
This method raises \exception{bsddb.error} if the database is empty.
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{next}{}
|
|
|
|
Set the cursor to the next item in the DB file and return it. The order of
|
1999-04-23 17:32:59 -03:00
|
|
|
keys in the file is unspecified, except in the case of B-Tree databases.
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{previous}{}
|
2002-11-17 07:09:50 -04:00
|
|
|
Set the cursor to the previous item in the DB file and return it. The
|
1999-04-23 17:32:59 -03:00
|
|
|
order of keys in the file is unspecified, except in the case of B-Tree
|
|
|
|
databases. This is not supported on hashtable databases (those opened
|
|
|
|
with \function{hashopen()}).
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{last}{}
|
1999-04-22 11:06:36 -03:00
|
|
|
Set the cursor to the last item in the DB file and return it. The
|
|
|
|
order of keys in the file is unspecified. This is not supported on
|
|
|
|
hashtable databases (those opened with \function{hashopen()}).
|
2004-08-10 16:22:48 -03:00
|
|
|
This method raises \exception{bsddb.error} if the database is empty.
|
1999-04-19 18:19:21 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{sync}{}
|
|
|
|
Synchronize the database on disk.
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
>>> import bsddb
|
|
|
|
>>> db = bsddb.btopen('/tmp/spam.db', 'c')
|
|
|
|
>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
|
|
|
|
...
|
|
|
|
>>> db['3']
|
|
|
|
'9'
|
|
|
|
>>> db.keys()
|
|
|
|
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
|
|
|
|
>>> db.first()
|
|
|
|
('0', '0')
|
|
|
|
>>> db.next()
|
|
|
|
('1', '1')
|
|
|
|
>>> db.last()
|
|
|
|
('9', '81')
|
|
|
|
>>> db.set_location('2')
|
|
|
|
('2', '4')
|
|
|
|
>>> db.previous()
|
|
|
|
('1', '1')
|
2003-09-12 03:33:37 -03:00
|
|
|
>>> for k, v in db.iteritems():
|
|
|
|
... print k, v
|
|
|
|
0 0
|
|
|
|
1 1
|
|
|
|
2 4
|
|
|
|
3 9
|
|
|
|
4 16
|
|
|
|
5 25
|
|
|
|
6 36
|
|
|
|
7 49
|
|
|
|
8 64
|
|
|
|
9 81
|
2003-12-07 09:00:25 -04:00
|
|
|
>>> '8' in db
|
2003-09-12 03:33:37 -03:00
|
|
|
True
|
1999-04-19 18:19:21 -03:00
|
|
|
>>> db.sync()
|
|
|
|
0
|
|
|
|
\end{verbatim}
|