cinap_lenrekadd hg and python4bb9e2215 years agoHistory
208 lines · 7.7 KB · TeX
RawDownload
\section{\module{bsddb} ---         Interface to Berkeley DB library} \declaremodule{extension}{bsddb}\modulesynopsis{Interface to Berkeley DB database library}\sectionauthor{Skip Montanaro}{skip@mojam.com}  The \module{bsddb} module provides an interface to the Berkeley DBlibrary.  Users can create hash, btree or record based library filesusing the appropriate open call. Bsddb objects behave generally likedictionaries.  Keys and values must be strings, however, so to useother objects as keys or to store other kinds of objects the user mustserialize them somehow, typically using \function{marshal.dumps()} or \function{pickle.dumps()}. The \module{bsddb} module requires a Berkeley DB library version from3.3 thru 4.5. \begin{seealso}  \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.}\end{seealso} A more modern DB, DBEnv and DBSequence object interface is available in the\module{bsddb.db} module which closely matches the Berkeley DB C APIdocumented at the above URLs.  Additional features provided by the\module{bsddb.db} API include fine tuning, transactions, logging, andmultiprocess concurrent database access. The following is a description of the legacy \module{bsddb} interfacecompatible with the old Python bsddb module.  Starting in Python 2.5 thisinterface should be safe for multithreaded access.  The \module{bsddb.db}API is recommended for threading users as it provides better control. The \module{bsddb} module defines the following functions that createobjects that access the appropriate type of Berkeley DB file.  Thefirst two arguments of each function are the same.  For ease ofportability, only the first two arguments should be used in mostinstances. \begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,                           mode\optional{, pgsize\optional{,                           ffactor\optional{, nelem\optional{,                           cachesize\optional{, lorder\optional{,                           hflags}}}}}}}}}Open the hash format file named \var{filename}.  Files never intendedto be preserved on disk may be created by passing \code{None} as the \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; the default) or\character{n} (read-write - truncate to zero length).  The otherarguments are rarely used and are just passed to the low-level\cfunction{dbopen()} function.  Consult the Berkeley DB documentationfor their use and interpretation.\end{funcdesc} \begin{funcdesc}{btopen}{filename\optional{, flag\optional{,mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,minkeypage\optional{, pgsize\optional{, lorder}}}}}}}}} 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\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; the default) or\character{n} (read-write - truncate to zero length).  The otherarguments are rarely used and are just passed to the low-level dbopenfunction.  Consult the Berkeley DB documentation for their use andinterpretation.\end{funcdesc} \begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{,rlen\optional{, delim\optional{, source\optional{, pad}}}}}}}}}}} 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\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; the default) or\character{n} (read-write - truncate to zero length).  The otherarguments are rarely used and are just passed to the low-level dbopenfunction.  Consult the Berkeley DB documentation for their use andinterpretation.\end{funcdesc}  \begin{notice}Beginning in 2.3 some \UNIX{} versions of Python may have a \module{bsddb185}module.  This is present \emph{only} to allow backwards compatibility withsystems 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}  \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 supportthe same methods as dictionaries.  In addition, they supportthe methods listed below.\versionchanged[Added dictionary methods]{2.3.1} \begin{methoddesc}{close}{}Close the underlying file.  The object can no longer be accessed.  Sincethere is no open \method{open} method for these objects, to open the fileagain 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 isunspecified and should not be relied on.  In particular, the order of thelist returned is different for different file formats.\end{methoddesc} \begin{methoddesc}{has_key}{key}Return \code{1} if the DB file contains the argument as a key.\end{methoddesc} \begin{methoddesc}{set_location}{key}Set the cursor to the item indicated by \var{key} and return a tuplecontaining the key and its value.  For binary tree databases (openedusing \function{btopen()}), if \var{key} does not actually exist inthe database, the cursor will point to the next item in sorted orderand return that key and value.  For other databases,\exception{KeyError} will be raised if \var{key} is not found in thedatabase.\end{methoddesc} \begin{methoddesc}{first}{}Set the cursor to the first item in the DB file and return it.  The order of keys in the file is unspecified, except in the case of B-Tree databases.This method raises \exception{bsddb.error} if the database is empty.\end{methoddesc} \begin{methoddesc}{next}{}Set the cursor to the next item in the DB file and return it.  The order of keys in the file is unspecified, except in the case of B-Tree databases.\end{methoddesc} \begin{methoddesc}{previous}{}Set the cursor to the previous item in the DB file and return it.  Theorder of keys in the file is unspecified, except in the case of B-Treedatabases.  This is not supported on hashtable databases (those openedwith \function{hashopen()}).\end{methoddesc} \begin{methoddesc}{last}{}Set the cursor to the last item in the DB file and return it.  Theorder of keys in the file is unspecified.  This is not supported onhashtable databases (those opened with \function{hashopen()}).This method raises \exception{bsddb.error} if the database is empty.\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')>>> for k, v in db.iteritems():...     print k, v0 01 12 43 94 165 256 367 498 649 81>>> '8' in dbTrue>>> db.sync()0\end{verbatim}