Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`bsddb` --- Interface to Berkeley DB library |
| 3 | ================================================= |
| 4 | |
| 5 | .. module:: bsddb |
| 6 | :synopsis: Interface to Berkeley DB database library |
Christian Heimes | 895627f | 2007-12-08 17:28:33 +0000 | [diff] [blame] | 7 | .. sectionauthor:: Skip Montanaro <skip@pobox.com> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | The :mod:`bsddb` module provides an interface to the Berkeley DB library. Users |
| 11 | can create hash, btree or record based library files using the appropriate open |
| 12 | call. Bsddb objects behave generally like dictionaries. Keys and values must be |
| 13 | strings, however, so to use other objects as keys or to store other kinds of |
| 14 | objects the user must serialize them somehow, typically using |
| 15 | :func:`marshal.dumps` or :func:`pickle.dumps`. |
| 16 | |
| 17 | The :mod:`bsddb` module requires a Berkeley DB library version from 3.3 thru |
| 18 | 4.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 | |
| 31 | A 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 |
| 33 | the above URLs. Additional features provided by the :mod:`bsddb.db` API include |
| 34 | fine tuning, transactions, logging, and multiprocess concurrent database access. |
| 35 | |
| 36 | The following is a description of the legacy :mod:`bsddb` interface compatible |
| 37 | with the old Python bsddb module. Starting in Python 2.5 this interface should |
| 38 | be safe for multithreaded access. The :mod:`bsddb.db` API is recommended for |
| 39 | threading users as it provides better control. |
| 40 | |
| 41 | The :mod:`bsddb` module defines the following functions that create objects that |
| 42 | access the appropriate type of Berkeley DB file. The first two arguments of |
| 43 | each function are the same. For ease of portability, only the first two |
| 44 | arguments 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. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | .. class:: StringValues(db) |
| 88 | |
| 89 | Wrapper class around a DB object that supports string values (rather than bytes). |
| 90 | All values are encoded as UTF-8, then passed to the underlying object. |
| 91 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
| 93 | .. seealso:: |
| 94 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 95 | Module :mod:`dbm.bsd` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | DBM-style interface to the :mod:`bsddb` |
| 97 | |
| 98 | |
| 99 | .. _bsddb-objects: |
| 100 | |
| 101 | Hash, BTree and Record Objects |
| 102 | ------------------------------ |
| 103 | |
| 104 | Once instantiated, hash, btree and record objects support the same methods as |
| 105 | dictionaries. In addition, they support the methods listed below. |
| 106 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 108 | .. describe:: key in bsddbobject |
| 109 | |
| 110 | Return ``True`` if the DB file contains the argument as a key. |
| 111 | |
| 112 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | .. method:: bsddbobject.close() |
| 114 | |
| 115 | Close the underlying file. The object can no longer be accessed. Since there |
| 116 | is no open :meth:`open` method for these objects, to open the file again a new |
| 117 | :mod:`bsddb` module open function must be called. |
| 118 | |
| 119 | |
| 120 | .. method:: bsddbobject.keys() |
| 121 | |
| 122 | Return the list of keys contained in the DB file. The order of the list is |
| 123 | unspecified and should not be relied on. In particular, the order of the list |
| 124 | returned is different for different file formats. |
| 125 | |
| 126 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | .. method:: bsddbobject.set_location(key) |
| 128 | |
| 129 | Set the cursor to the item indicated by *key* and return a tuple containing the |
| 130 | key and its value. For binary tree databases (opened using :func:`btopen`), if |
| 131 | *key* does not actually exist in the database, the cursor will point to the next |
| 132 | item in sorted order and return that key and value. For other databases, |
| 133 | :exc:`KeyError` will be raised if *key* is not found in the database. |
| 134 | |
| 135 | |
| 136 | .. method:: bsddbobject.first() |
| 137 | |
| 138 | Set the cursor to the first item in the DB file and return it. The order of |
| 139 | keys in the file is unspecified, except in the case of B-Tree databases. This |
| 140 | method raises :exc:`bsddb.error` if the database is empty. |
| 141 | |
| 142 | |
| 143 | .. method:: bsddbobject.next() |
| 144 | |
| 145 | Set the cursor to the next 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. |
| 147 | |
| 148 | |
| 149 | .. method:: bsddbobject.previous() |
| 150 | |
| 151 | Set the cursor to the previous item in the DB file and return it. The order of |
| 152 | keys in the file is unspecified, except in the case of B-Tree databases. This |
| 153 | is not supported on hashtable databases (those opened with :func:`hashopen`). |
| 154 | |
| 155 | |
| 156 | .. method:: bsddbobject.last() |
| 157 | |
| 158 | Set the cursor to the last item in the DB file and return it. The order of keys |
| 159 | in the file is unspecified. This is not supported on hashtable databases (those |
| 160 | opened with :func:`hashopen`). This method raises :exc:`bsddb.error` if the |
| 161 | database is empty. |
| 162 | |
| 163 | |
| 164 | .. method:: bsddbobject.sync() |
| 165 | |
| 166 | Synchronize the database on disk. |
| 167 | |
| 168 | Example:: |
| 169 | |
| 170 | >>> import bsddb |
| 171 | >>> db = bsddb.btopen('/tmp/spam.db', 'c') |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 172 | >>> for i in range(10): |
| 173 | ... db[str(i)] = '%d' % (i*i) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | ... |
| 175 | >>> db['3'] |
| 176 | '9' |
| 177 | >>> db.keys() |
| 178 | ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] |
| 179 | >>> db.first() |
| 180 | ('0', '0') |
| 181 | >>> db.next() |
| 182 | ('1', '1') |
| 183 | >>> db.last() |
| 184 | ('9', '81') |
| 185 | >>> db.set_location('2') |
| 186 | ('2', '4') |
| 187 | >>> db.previous() |
| 188 | ('1', '1') |
| 189 | >>> for k, v in db.iteritems(): |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 190 | ... print(k, v) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 191 | 0 0 |
| 192 | 1 1 |
| 193 | 2 4 |
| 194 | 3 9 |
| 195 | 4 16 |
| 196 | 5 25 |
| 197 | 6 36 |
| 198 | 7 49 |
| 199 | 8 64 |
| 200 | 9 81 |
| 201 | >>> '8' in db |
| 202 | True |
| 203 | >>> db.sync() |
| 204 | 0 |
| 205 | |