blob: 55b7c7d842a5e4de7b5583b4e60c51e7b8f0f802 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`bsddb` --- Interface to Berkeley DB library
3=================================================
4
5.. module:: bsddb
6 :synopsis: Interface to Berkeley DB database library
7.. sectionauthor:: Skip Montanaro <skip@mojam.com>
8
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
184.5.
19
20
21.. seealso::
22
23 http://pybsddb.sourceforge.net/
24 The website with documentation for the :mod:`bsddb.db` Python Berkeley DB
25 interface that closely mirrors the object oriented interface provided in
26 Berkeley DB 3 and 4.
27
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
81.. class:: StringKeys(db)
82
83 Wrapper class around a DB object that supports string keys (rather than bytes).
84 All keys are encoded as UTF-8, then passed to the underlying object.
85
86 .. versionadded:: 3.0
87
88
89.. class:: StringValues(db)
90
91 Wrapper class around a DB object that supports string values (rather than bytes).
92 All values are encoded as UTF-8, then passed to the underlying object.
93
94 .. versionadded:: 3.0
95
96
97.. seealso::
98
99 Module :mod:`dbhash`
100 DBM-style interface to the :mod:`bsddb`
101
102
103.. _bsddb-objects:
104
105Hash, BTree and Record Objects
106------------------------------
107
108Once instantiated, hash, btree and record objects support the same methods as
109dictionaries. In addition, they support the methods listed below.
110
111.. versionchanged:: 2.3.1
112 Added dictionary methods.
113
114
115.. method:: bsddbobject.close()
116
117 Close the underlying file. The object can no longer be accessed. Since there
118 is no open :meth:`open` method for these objects, to open the file again a new
119 :mod:`bsddb` module open function must be called.
120
121
122.. method:: bsddbobject.keys()
123
124 Return the list of keys contained in the DB file. The order of the list is
125 unspecified and should not be relied on. In particular, the order of the list
126 returned is different for different file formats.
127
128
129.. method:: bsddbobject.has_key(key)
130
131 Return ``1`` if the DB file contains the argument as a key.
132
133
134.. method:: bsddbobject.set_location(key)
135
136 Set the cursor to the item indicated by *key* and return a tuple containing the
137 key and its value. For binary tree databases (opened using :func:`btopen`), if
138 *key* does not actually exist in the database, the cursor will point to the next
139 item in sorted order and return that key and value. For other databases,
140 :exc:`KeyError` will be raised if *key* is not found in the database.
141
142
143.. method:: bsddbobject.first()
144
145 Set the cursor to the first item in the DB file and return it. The order of
146 keys in the file is unspecified, except in the case of B-Tree databases. This
147 method raises :exc:`bsddb.error` if the database is empty.
148
149
150.. method:: bsddbobject.next()
151
152 Set the cursor to the next item in the DB file and return it. The order of
153 keys in the file is unspecified, except in the case of B-Tree databases.
154
155
156.. method:: bsddbobject.previous()
157
158 Set the cursor to the previous item in the DB file and return it. The order of
159 keys in the file is unspecified, except in the case of B-Tree databases. This
160 is not supported on hashtable databases (those opened with :func:`hashopen`).
161
162
163.. method:: bsddbobject.last()
164
165 Set the cursor to the last item in the DB file and return it. The order of keys
166 in the file is unspecified. This is not supported on hashtable databases (those
167 opened with :func:`hashopen`). This method raises :exc:`bsddb.error` if the
168 database is empty.
169
170
171.. method:: bsddbobject.sync()
172
173 Synchronize the database on disk.
174
175Example::
176
177 >>> import bsddb
178 >>> db = bsddb.btopen('/tmp/spam.db', 'c')
179 >>> for i in range(10): db['%d'%i] = '%d'% (i*i)
180 ...
181 >>> db['3']
182 '9'
183 >>> db.keys()
184 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
185 >>> db.first()
186 ('0', '0')
187 >>> db.next()
188 ('1', '1')
189 >>> db.last()
190 ('9', '81')
191 >>> db.set_location('2')
192 ('2', '4')
193 >>> db.previous()
194 ('1', '1')
195 >>> for k, v in db.iteritems():
196 ... print k, v
197 0 0
198 1 1
199 2 4
200 3 9
201 4 16
202 5 25
203 6 36
204 7 49
205 8 64
206 9 81
207 >>> '8' in db
208 True
209 >>> db.sync()
210 0
211