blob: ed45619847d556b598398cb8ee5f86a0aefc43ef [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`bsddb` --- Interface to Berkeley DB library
3=================================================
4
5.. module:: bsddb
6 :synopsis: Interface to Berkeley DB database library
Skip Montanaro54662462007-12-08 15:26:16 +00007.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl8ec7f652007-08-15 14:28:01 +00008
9
10The :mod:`bsddb` module provides an interface to the Berkeley DB library. Users
11can create hash, btree or record based library files using the appropriate open
12call. Bsddb objects behave generally like dictionaries. Keys and values must be
13strings, however, so to use other objects as keys or to store other kinds of
14objects the user must serialize them somehow, typically using
15:func:`marshal.dumps` or :func:`pickle.dumps`.
16
17The :mod:`bsddb` module requires a Berkeley DB library version from 3.3 thru
Gregory P. Smithd1e57152008-05-24 21:21:23 +0000184.7.
Georg Brandl8ec7f652007-08-15 14:28:01 +000019
20
21.. seealso::
22
Gregory P. Smithd1e57152008-05-24 21:21:23 +000023 http://www.jcea.es/programacion/pybsddb.htm
Georg Brandl8ec7f652007-08-15 14:28:01 +000024 The website with documentation for the :mod:`bsddb.db` Python Berkeley DB
25 interface that closely mirrors the object oriented interface provided in
Gregory P. Smithd1e57152008-05-24 21:21:23 +000026 Berkeley DB 4.x itself.
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
28 http://www.oracle.com/database/berkeley-db/
29 The Berkeley DB library.
30
31A more modern DB, DBEnv and DBSequence object interface is available in the
32:mod:`bsddb.db` module which closely matches the Berkeley DB C API documented at
33the above URLs. Additional features provided by the :mod:`bsddb.db` API include
34fine tuning, transactions, logging, and multiprocess concurrent database access.
35
36The following is a description of the legacy :mod:`bsddb` interface compatible
37with the old Python bsddb module. Starting in Python 2.5 this interface should
38be safe for multithreaded access. The :mod:`bsddb.db` API is recommended for
39threading users as it provides better control.
40
41The :mod:`bsddb` module defines the following functions that create objects that
42access the appropriate type of Berkeley DB file. The first two arguments of
43each function are the same. For ease of portability, only the first two
44arguments should be used in most instances.
45
46
47.. function:: hashopen(filename[, flag[, mode[, pgsize[, ffactor[, nelem[, cachesize[, lorder[, hflags]]]]]]]])
48
49 Open the hash format file named *filename*. Files never intended to be
50 preserved on disk may be created by passing ``None`` as the *filename*. The
51 optional *flag* identifies the mode used to open the file. It may be ``'r'``
52 (read only), ``'w'`` (read-write) , ``'c'`` (read-write - create if necessary;
53 the default) or ``'n'`` (read-write - truncate to zero length). The other
54 arguments are rarely used and are just passed to the low-level :cfunc:`dbopen`
55 function. Consult the Berkeley DB documentation for their use and
56 interpretation.
57
58
59.. function:: btopen(filename[, flag[, mode[, btflags[, cachesize[, maxkeypage[, minkeypage[, pgsize[, lorder]]]]]]]])
60
61 Open the btree format file named *filename*. Files never intended to be
62 preserved on disk may be created by passing ``None`` as the *filename*. The
63 optional *flag* identifies the mode used to open the file. It may be ``'r'``
64 (read only), ``'w'`` (read-write), ``'c'`` (read-write - create if necessary;
65 the default) or ``'n'`` (read-write - truncate to zero length). The other
66 arguments are rarely used and are just passed to the low-level dbopen function.
67 Consult the Berkeley DB documentation for their use and interpretation.
68
69
70.. function:: rnopen(filename[, flag[, mode[, rnflags[, cachesize[, pgsize[, lorder[, rlen[, delim[, source[, pad]]]]]]]]]])
71
72 Open a DB record format file named *filename*. Files never intended to be
73 preserved on disk may be created by passing ``None`` as the *filename*. The
74 optional *flag* identifies the mode used to open the file. It may be ``'r'``
75 (read only), ``'w'`` (read-write), ``'c'`` (read-write - create if necessary;
76 the default) or ``'n'`` (read-write - truncate to zero length). The other
77 arguments are rarely used and are just passed to the low-level dbopen function.
78 Consult the Berkeley DB documentation for their use and interpretation.
79
80.. note::
81
82 Beginning in 2.3 some Unix versions of Python may have a :mod:`bsddb185` module.
83 This is present *only* to allow backwards compatibility with systems which ship
84 with the old Berkeley DB 1.85 database library. The :mod:`bsddb185` module
Brett Cannon768d44f2008-05-10 02:47:54 +000085 should never be used directly in new code. The module has been removed in
Skip Montanaro45f58592008-05-10 14:48:49 +000086 Python 3.0. If you find you still need it look in PyPI.
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
88
89.. seealso::
90
91 Module :mod:`dbhash`
92 DBM-style interface to the :mod:`bsddb`
93
94
95.. _bsddb-objects:
96
97Hash, BTree and Record Objects
98------------------------------
99
100Once instantiated, hash, btree and record objects support the same methods as
101dictionaries. In addition, they support the methods listed below.
102
103.. versionchanged:: 2.3.1
104 Added dictionary methods.
105
106
107.. method:: bsddbobject.close()
108
109 Close the underlying file. The object can no longer be accessed. Since there
110 is no open :meth:`open` method for these objects, to open the file again a new
111 :mod:`bsddb` module open function must be called.
112
113
114.. method:: bsddbobject.keys()
115
116 Return the list of keys contained in the DB file. The order of the list is
117 unspecified and should not be relied on. In particular, the order of the list
118 returned is different for different file formats.
119
120
121.. method:: bsddbobject.has_key(key)
122
123 Return ``1`` if the DB file contains the argument as a key.
124
125
126.. method:: bsddbobject.set_location(key)
127
128 Set the cursor to the item indicated by *key* and return a tuple containing the
129 key and its value. For binary tree databases (opened using :func:`btopen`), if
130 *key* does not actually exist in the database, the cursor will point to the next
131 item in sorted order and return that key and value. For other databases,
132 :exc:`KeyError` will be raised if *key* is not found in the database.
133
134
135.. method:: bsddbobject.first()
136
137 Set the cursor to the first item in the DB file and return it. The order of
138 keys in the file is unspecified, except in the case of B-Tree databases. This
139 method raises :exc:`bsddb.error` if the database is empty.
140
141
142.. method:: bsddbobject.next()
143
144 Set the cursor to the next item in the DB file and return it. The order of
145 keys in the file is unspecified, except in the case of B-Tree databases.
146
147
148.. method:: bsddbobject.previous()
149
150 Set the cursor to the previous item in the DB file and return it. The order of
151 keys in the file is unspecified, except in the case of B-Tree databases. This
152 is not supported on hashtable databases (those opened with :func:`hashopen`).
153
154
155.. method:: bsddbobject.last()
156
157 Set the cursor to the last item in the DB file and return it. The order of keys
158 in the file is unspecified. This is not supported on hashtable databases (those
159 opened with :func:`hashopen`). This method raises :exc:`bsddb.error` if the
160 database is empty.
161
162
163.. method:: bsddbobject.sync()
164
165 Synchronize the database on disk.
166
167Example::
168
169 >>> import bsddb
170 >>> db = bsddb.btopen('/tmp/spam.db', 'c')
171 >>> for i in range(10): db['%d'%i] = '%d'% (i*i)
172 ...
173 >>> db['3']
174 '9'
175 >>> db.keys()
176 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
177 >>> db.first()
178 ('0', '0')
179 >>> db.next()
180 ('1', '1')
181 >>> db.last()
182 ('9', '81')
183 >>> db.set_location('2')
184 ('2', '4')
185 >>> db.previous()
186 ('1', '1')
187 >>> for k, v in db.iteritems():
188 ... print k, v
189 0 0
190 1 1
191 2 4
192 3 9
193 4 16
194 5 25
195 6 36
196 7 49
197 8 64
198 9 81
199 >>> '8' in db
200 True
201 >>> db.sync()
202 0
203