libbsddb.tex 6.01 KB
Newer Older
1 2 3 4 5 6 7 8 9
\section{\module{bsddb} ---
         Interface to Berkeley DB library}

\declaremodule{extension}{bsddb}
  \platform{Unix, Windows}
\modulesynopsis{Interface to Berkeley DB database library}
\sectionauthor{Skip Montanaro}{skip@mojam.com}


10 11 12 13 14 15 16
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
serialize them somehow, typically using marshal.dumps or pickle.dumps.

17
There are two incompatible versions of the underlying library.
18 19
Version 1.85 is widely available, but has some known bugs.  Version 2
is not quite as widely used, but does offer some improvements.  The
20 21 22 23 24 25 26 27
\module{bsddb} module uses the 1.85 interface.  Starting with Python
2.0, the \program{configure} script can usually determine the
version of the library which is available and build it correctly.  If
you have difficulty getting \program{configure} to do the right thing,
run it with the \longprogramopt{help} option to get information about
additional options that can help.  On Windows, you will need to define
the \code{HAVE_DB_185_H} macro if you are building Python from source
and using version 2 of the DB library.
28 29

The \module{bsddb} module defines the following functions that create
30 31 32 33
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.
34 35

\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
36 37 38 39 40 41 42 43 44 45 46 47
                           mode\optional{, bsize\optional{,
                           ffactor\optional{, nelem\optional{,
                           cachesize\optional{, hash\optional{,
                           lorder}}}}}}}}}
Open the hash format file named \var{filename}.  The optional
\var{flag} identifies the mode used to open the file.  It may be
\character{r} (read only), \character{w} (read-write),
\character{c} (read-write - create if necessary) or
\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.
48 49 50 51 52
\end{funcdesc}

\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
minkeypage\optional{, psize\optional{, lorder}}}}}}}}}
53 54 55 56 57 58 59 60 61

Open the btree format file named \var{filename}.  The optional
\var{flag} identifies the mode used to open the file.  It may be
\character{r} (read only), \character{w} (read-write),
\character{c} (read-write - create if necessary) or
\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.
62 63 64 65 66
\end{funcdesc}

\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
rnflags\optional{, cachesize\optional{, psize\optional{, lorder\optional{,
reclen\optional{, bval\optional{, bfname}}}}}}}}}}
67 68 69 70 71 72 73 74 75

Open a DB record format file named \var{filename}.  The optional
\var{flag} identifies the mode used to open the file.  It may be
\character{r} (read only), \character{w} (read-write),
\character{c} (read-write - create if necessary) or
\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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
\end{funcdesc}


\begin{seealso}
  \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
\end{seealso}


\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}

Once instantiated, hash, btree and record objects support the following
methods:

\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}
102
Return \code{1} if the DB file contains the argument as a key.
103 104 105 106 107 108 109 110
\end{methoddesc}

\begin{methoddesc}{set_location}{key}
Set the cursor to the item indicated by the key and return it.
\end{methoddesc}

\begin{methoddesc}{first}{}
Set the cursor to the first item in the DB file and return it.  The order of 
111
keys in the file is unspecified, except in the case of B-Tree databases.
112 113 114 115
\end{methoddesc}

\begin{methoddesc}{next}{}
Set the cursor to the next item in the DB file and return it.  The order of 
116
keys in the file is unspecified, except in the case of B-Tree databases.
117 118 119
\end{methoddesc}

\begin{methoddesc}{previous}{}
120
Set the cursor to the first item in the DB file and return it.  The
121 122 123
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()}).
124 125 126
\end{methoddesc}

\begin{methoddesc}{last}{}
127 128 129
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()}).
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
\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')
>>> db.sync()
0
\end{verbatim}