blob: 793237b1230205c1fb8ec86baaaab92d3d552f6e [file] [log] [blame]
Fred Drake9d158811999-04-19 21:19:21 +00001\section{\module{bsddb} ---
2 Interface to Berkeley DB library}
3
4\declaremodule{extension}{bsddb}
Fred Drake9d158811999-04-19 21:19:21 +00005\modulesynopsis{Interface to Berkeley DB database library}
6\sectionauthor{Skip Montanaro}{skip@mojam.com}
7
8
Fred Drake38e5d272000-04-03 20:13:55 +00009The \module{bsddb} module provides an interface to the Berkeley DB
10library. Users can create hash, btree or record based library files
11using the appropriate open call. Bsddb objects behave generally like
12dictionaries. Keys and values must be strings, however, so to use
13other objects as keys or to store other kinds of objects the user must
Andrew M. Kuchling97473012005-12-22 20:12:54 +000014serialize them somehow, typically using \function{marshal.dumps()} or
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015\function{pickle.dumps()}.
Fred Drake9d158811999-04-19 21:19:21 +000016
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000017The \module{bsddb} module requires a Berkeley DB library version from
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000183.3 thru 4.5.
Gregory P. Smith57725132003-05-28 07:56:45 +000019
Gregory P. Smith57725132003-05-28 07:56:45 +000020\begin{seealso}
Thomas Wouters89f507f2006-12-13 04:49:30 +000021 \seeurl{http://pybsddb.sourceforge.net/}
22 {The website with documentation for the \module{bsddb.db}
23 Python Berkeley DB interface that closely mirrors the object
24 oriented interface provided in Berkeley DB 3 and 4.}
25
26 \seeurl{http://www.oracle.com/database/berkeley-db/}
27 {The Berkeley DB library.}
Gregory P. Smith57725132003-05-28 07:56:45 +000028\end{seealso}
29
Thomas Wouters0e3f5912006-08-11 14:57:12 +000030A more modern DB, DBEnv and DBSequence object interface is available in the
Thomas Wouters89f507f2006-12-13 04:49:30 +000031\module{bsddb.db} module which closely matches the Berkeley DB C API
Thomas Wouters0e3f5912006-08-11 14:57:12 +000032documented at the above URLs. Additional features provided by the
33\module{bsddb.db} API include fine tuning, transactions, logging, and
34multiprocess concurrent database access.
35
Gregory P. Smith57725132003-05-28 07:56:45 +000036The following is a description of the legacy \module{bsddb} interface
Thomas Wouters89f507f2006-12-13 04:49:30 +000037compatible with the old Python bsddb module. Starting in Python 2.5 this
Thomas Wouters0e3f5912006-08-11 14:57:12 +000038interface should be safe for multithreaded access. The \module{bsddb.db}
39API is recommended for threading users as it provides better control.
Fred Drake9d158811999-04-19 21:19:21 +000040
41The \module{bsddb} module defines the following functions that create
Fred Drake38e5d272000-04-03 20:13:55 +000042objects that access the appropriate type of Berkeley DB file. The
43first two arguments of each function are the same. For ease of
44portability, only the first two arguments should be used in most
45instances.
Fred Drake9d158811999-04-19 21:19:21 +000046
47\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000048 mode\optional{, pgsize\optional{,
Fred Drake38e5d272000-04-03 20:13:55 +000049 ffactor\optional{, nelem\optional{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000050 cachesize\optional{, lorder\optional{,
51 hflags}}}}}}}}}
Anthony Baxter83888952002-04-23 02:11:05 +000052Open the hash format file named \var{filename}. Files never intended
53to be preserved on disk may be created by passing \code{None} as the
54\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000055\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000056\character{r} (read only), \character{w} (read-write) ,
57\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000058\character{n} (read-write - truncate to zero length). The other
59arguments are rarely used and are just passed to the low-level
60\cfunction{dbopen()} function. Consult the Berkeley DB documentation
61for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000062\end{funcdesc}
63
Fred Drake9d158811999-04-19 21:19:21 +000064\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
65mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000066minkeypage\optional{, pgsize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000067
Anthony Baxter83888952002-04-23 02:11:05 +000068Open the btree format file named \var{filename}. Files never intended
69to be preserved on disk may be created by passing \code{None} as the
70\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000071\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000072\character{r} (read only), \character{w} (read-write),
73\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000074\character{n} (read-write - truncate to zero length). The other
75arguments are rarely used and are just passed to the low-level dbopen
76function. Consult the Berkeley DB documentation for their use and
77interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000078\end{funcdesc}
79
80\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000081rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000082rlen\optional{, delim\optional{, source\optional{, pad}}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000083
Anthony Baxter83888952002-04-23 02:11:05 +000084Open a DB record format file named \var{filename}. Files never intended
85to be preserved on disk may be created by passing \code{None} as the
86\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000087\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000088\character{r} (read only), \character{w} (read-write),
89\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000090\character{n} (read-write - truncate to zero length). The other
91arguments are rarely used and are just passed to the low-level dbopen
92function. Consult the Berkeley DB documentation for their use and
93interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000094\end{funcdesc}
95
96
Fred Drakeb86aa992004-06-24 06:03:59 +000097\begin{seealso}
98 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
99\end{seealso}
100
Fred Drake9d158811999-04-19 21:19:21 +0000101\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
102
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000103Once instantiated, hash, btree and record objects support
104the same methods as dictionaries. In addition, they support
Raymond Hettinger34040342003-09-16 21:45:22 +0000105the methods listed below.
106\versionchanged[Added dictionary methods]{2.3.1}
Fred Drake9d158811999-04-19 21:19:21 +0000107
Guido van Rossumd8faa362007-04-27 19:54:29 +0000108\begin{methoddesc}[bsddbobject]{close}{}
Fred Drake9d158811999-04-19 21:19:21 +0000109Close the underlying file. The object can no longer be accessed. Since
110there is no open \method{open} method for these objects, to open the file
111again a new \module{bsddb} module open function must be called.
112\end{methoddesc}
113
Guido van Rossumd8faa362007-04-27 19:54:29 +0000114\begin{methoddesc}[bsddbobject]{keys}{}
Fred Drake9d158811999-04-19 21:19:21 +0000115Return the list of keys contained in the DB file. The order of the list is
116unspecified and should not be relied on. In particular, the order of the
117list returned is different for different file formats.
118\end{methoddesc}
119
Guido van Rossumd8faa362007-04-27 19:54:29 +0000120\begin{methoddesc}[bsddbobject]{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000121Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000122\end{methoddesc}
123
Guido van Rossumd8faa362007-04-27 19:54:29 +0000124\begin{methoddesc}[bsddbobject]{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000125Set the cursor to the item indicated by \var{key} and return a tuple
126containing the key and its value. For binary tree databases (opened
127using \function{btopen()}), if \var{key} does not actually exist in
128the database, the cursor will point to the next item in sorted order
129and return that key and value. For other databases,
130\exception{KeyError} will be raised if \var{key} is not found in the
131database.
Fred Drake9d158811999-04-19 21:19:21 +0000132\end{methoddesc}
133
Guido van Rossumd8faa362007-04-27 19:54:29 +0000134\begin{methoddesc}[bsddbobject]{first}{}
Fred Drake9d158811999-04-19 21:19:21 +0000135Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000136keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drakeba100c92004-08-10 19:22:48 +0000137This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000138\end{methoddesc}
139
Guido van Rossumd8faa362007-04-27 19:54:29 +0000140\begin{methoddesc}[bsddbobject]{next}{}
Fred Drake9d158811999-04-19 21:19:21 +0000141Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000142keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000143\end{methoddesc}
144
Guido van Rossumd8faa362007-04-27 19:54:29 +0000145\begin{methoddesc}[bsddbobject]{previous}{}
Skip Montanaro61418122002-11-17 11:09:50 +0000146Set the cursor to the previous item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000147order of keys in the file is unspecified, except in the case of B-Tree
148databases. This is not supported on hashtable databases (those opened
149with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000150\end{methoddesc}
151
Guido van Rossumd8faa362007-04-27 19:54:29 +0000152\begin{methoddesc}[bsddbobject]{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000153Set the cursor to the last item in the DB file and return it. The
154order of keys in the file is unspecified. This is not supported on
155hashtable databases (those opened with \function{hashopen()}).
Fred Drakeba100c92004-08-10 19:22:48 +0000156This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000157\end{methoddesc}
158
Guido van Rossumd8faa362007-04-27 19:54:29 +0000159\begin{methoddesc}[bsddbobject]{sync}{}
Fred Drake9d158811999-04-19 21:19:21 +0000160Synchronize the database on disk.
161\end{methoddesc}
162
163Example:
164
165\begin{verbatim}
166>>> import bsddb
167>>> db = bsddb.btopen('/tmp/spam.db', 'c')
168>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
169...
170>>> db['3']
171'9'
172>>> db.keys()
173['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
174>>> db.first()
175('0', '0')
176>>> db.next()
177('1', '1')
178>>> db.last()
179('9', '81')
180>>> db.set_location('2')
181('2', '4')
182>>> db.previous()
183('1', '1')
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000184>>> for k, v in db.iteritems():
185... print k, v
1860 0
1871 1
1882 4
1893 9
1904 16
1915 25
1926 36
1937 49
1948 64
1959 81
Raymond Hettingerff294fe2003-12-07 13:00:25 +0000196>>> '8' in db
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000197True
Fred Drake9d158811999-04-19 21:19:21 +0000198>>> db.sync()
1990
200\end{verbatim}