blob: 5a8d3799cf0d5298978bdd5e1c13b0eafa028e34 [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
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.. 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
85 should never be used directly in new code.
86
87
88.. seealso::
89
90 Module :mod:`dbhash`
91 DBM-style interface to the :mod:`bsddb`
92
93
94.. _bsddb-objects:
95
96Hash, BTree and Record Objects
97------------------------------
98
99Once instantiated, hash, btree and record objects support the same methods as
100dictionaries. In addition, they support the methods listed below.
101
102.. versionchanged:: 2.3.1
103 Added dictionary methods.
104
105
106.. method:: bsddbobject.close()
107
108 Close the underlying file. The object can no longer be accessed. Since there
109 is no open :meth:`open` method for these objects, to open the file again a new
110 :mod:`bsddb` module open function must be called.
111
112
113.. method:: bsddbobject.keys()
114
115 Return the list of keys contained in the DB file. The order of the list is
116 unspecified and should not be relied on. In particular, the order of the list
117 returned is different for different file formats.
118
119
120.. method:: bsddbobject.has_key(key)
121
122 Return ``1`` if the DB file contains the argument as a key.
123
124
125.. method:: bsddbobject.set_location(key)
126
127 Set the cursor to the item indicated by *key* and return a tuple containing the
128 key and its value. For binary tree databases (opened using :func:`btopen`), if
129 *key* does not actually exist in the database, the cursor will point to the next
130 item in sorted order and return that key and value. For other databases,
131 :exc:`KeyError` will be raised if *key* is not found in the database.
132
133
134.. method:: bsddbobject.first()
135
136 Set the cursor to the first item in the DB file and return it. The order of
137 keys in the file is unspecified, except in the case of B-Tree databases. This
138 method raises :exc:`bsddb.error` if the database is empty.
139
140
141.. method:: bsddbobject.next()
142
143 Set the cursor to the next item in the DB file and return it. The order of
144 keys in the file is unspecified, except in the case of B-Tree databases.
145
146
147.. method:: bsddbobject.previous()
148
149 Set the cursor to the previous item in the DB file and return it. The order of
150 keys in the file is unspecified, except in the case of B-Tree databases. This
151 is not supported on hashtable databases (those opened with :func:`hashopen`).
152
153
154.. method:: bsddbobject.last()
155
156 Set the cursor to the last item in the DB file and return it. The order of keys
157 in the file is unspecified. This is not supported on hashtable databases (those
158 opened with :func:`hashopen`). This method raises :exc:`bsddb.error` if the
159 database is empty.
160
161
162.. method:: bsddbobject.sync()
163
164 Synchronize the database on disk.
165
166Example::
167
168 >>> import bsddb
169 >>> db = bsddb.btopen('/tmp/spam.db', 'c')
170 >>> for i in range(10): db['%d'%i] = '%d'% (i*i)
171 ...
172 >>> db['3']
173 '9'
174 >>> db.keys()
175 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
176 >>> db.first()
177 ('0', '0')
178 >>> db.next()
179 ('1', '1')
180 >>> db.last()
181 ('9', '81')
182 >>> db.set_location('2')
183 ('2', '4')
184 >>> db.previous()
185 ('1', '1')
186 >>> for k, v in db.iteritems():
187 ... print k, v
188 0 0
189 1 1
190 2 4
191 3 9
192 4 16
193 5 25
194 6 36
195 7 49
196 8 64
197 9 81
198 >>> '8' in db
199 True
200 >>> db.sync()
201 0
202