|
| 1 | +#---------------------------------------------------------------------- |
| 2 | +# Copyright (c) 1999-2001, Digital Creations, Fredericksburg, VA, USA |
| 3 | +# and Andrew Kuchling. All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions are |
| 7 | +# met: |
| 8 | +# |
| 9 | +# o Redistributions of source code must retain the above copyright |
| 10 | +# notice, this list of conditions, and the disclaimer that follows. |
| 11 | +# |
| 12 | +# o Redistributions in binary form must reproduce the above copyright |
| 13 | +# notice, this list of conditions, and the following disclaimer in |
| 14 | +# the documentation and/or other materials provided with the |
| 15 | +# distribution. |
| 16 | +# |
| 17 | +# o Neither the name of Digital Creations nor the names of its |
| 18 | +# contributors may be used to endorse or promote products derived |
| 19 | +# from this software without specific prior written permission. |
| 20 | +# |
| 21 | +# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS |
| 22 | +# IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 23 | +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 24 | +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL |
| 25 | +# CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 28 | +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 29 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
| 30 | +# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 31 | +# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 32 | +# DAMAGE. |
| 33 | +#---------------------------------------------------------------------- |
| 34 | + |
| 35 | + |
| 36 | +""" |
| 37 | +This package initialization module provides a compatibility interface |
| 38 | +that should enable bsddb3 to be a near drop-in replacement for the original |
| 39 | +old bsddb module. The functions and classes provided here are all |
| 40 | +wrappers around the new functionality provided in the bsddb3.db module. |
| 41 | +
|
| 42 | +People interested in the more advanced capabilites of Berkeley DB 3.x |
| 43 | +should use the bsddb3.db module directly. |
| 44 | +""" |
| 45 | + |
| 46 | +import _bsddb |
| 47 | +# bsddb3 calls it _db |
| 48 | +_db = _bsddb |
| 49 | +__version__ = _db.__version__ |
| 50 | + |
| 51 | +error = _db.DBError # So bsddb3.error will mean something... |
| 52 | + |
| 53 | +#---------------------------------------------------------------------- |
| 54 | + |
| 55 | + |
| 56 | +class _DBWithCursor: |
| 57 | + """ |
| 58 | + A simple wrapper around DB that makes it look like the bsddbobject in |
| 59 | + the old module. It uses a cursor as needed to provide DB traversal. |
| 60 | + """ |
| 61 | + def __init__(self, db): |
| 62 | + self.db = db |
| 63 | + self.dbc = None |
| 64 | + self.db.set_get_returns_none(0) |
| 65 | + |
| 66 | + def __del__(self): |
| 67 | + self.close() |
| 68 | + |
| 69 | + def _checkCursor(self): |
| 70 | + if self.dbc is None: |
| 71 | + self.dbc = self.db.cursor() |
| 72 | + |
| 73 | + def _checkOpen(self): |
| 74 | + if self.db is None: |
| 75 | + raise error, "BSDDB object has already been closed" |
| 76 | + |
| 77 | + def isOpen(self): |
| 78 | + return self.db is not None |
| 79 | + |
| 80 | + def __len__(self): |
| 81 | + self._checkOpen() |
| 82 | + return len(self.db) |
| 83 | + |
| 84 | + def __getitem__(self, key): |
| 85 | + self._checkOpen() |
| 86 | + return self.db[key] |
| 87 | + |
| 88 | + def __setitem__(self, key, value): |
| 89 | + self._checkOpen() |
| 90 | + self.db[key] = value |
| 91 | + |
| 92 | + def __delitem__(self, key): |
| 93 | + self._checkOpen() |
| 94 | + del self.db[key] |
| 95 | + |
| 96 | + def close(self): |
| 97 | + if self.dbc is not None: |
| 98 | + self.dbc.close() |
| 99 | + v = 0 |
| 100 | + if self.db is not None: |
| 101 | + v = self.db.close() |
| 102 | + self.dbc = None |
| 103 | + self.db = None |
| 104 | + return v |
| 105 | + |
| 106 | + def keys(self): |
| 107 | + self._checkOpen() |
| 108 | + return self.db.keys() |
| 109 | + |
| 110 | + def has_key(self, key): |
| 111 | + self._checkOpen() |
| 112 | + return self.db.has_key(key) |
| 113 | + |
| 114 | + def set_location(self, key): |
| 115 | + self._checkOpen() |
| 116 | + self._checkCursor() |
| 117 | + return self.dbc.set(key) |
| 118 | + |
| 119 | + def next(self): |
| 120 | + self._checkOpen() |
| 121 | + self._checkCursor() |
| 122 | + rv = self.dbc.next() |
| 123 | + return rv |
| 124 | + |
| 125 | + def previous(self): |
| 126 | + self._checkOpen() |
| 127 | + self._checkCursor() |
| 128 | + rv = self.dbc.prev() |
| 129 | + return rv |
| 130 | + |
| 131 | + def first(self): |
| 132 | + self._checkOpen() |
| 133 | + self._checkCursor() |
| 134 | + rv = self.dbc.first() |
| 135 | + return rv |
| 136 | + |
| 137 | + def last(self): |
| 138 | + self._checkOpen() |
| 139 | + self._checkCursor() |
| 140 | + rv = self.dbc.last() |
| 141 | + return rv |
| 142 | + |
| 143 | + def sync(self): |
| 144 | + self._checkOpen() |
| 145 | + return self.db.sync() |
| 146 | + |
| 147 | + |
| 148 | +#---------------------------------------------------------------------- |
| 149 | +# Compatibility object factory functions |
| 150 | + |
| 151 | +def hashopen(file, flag='c', mode=0666, pgsize=None, ffactor=None, nelem=None, |
| 152 | + cachesize=None, lorder=None, hflags=0): |
| 153 | + |
| 154 | + flags = _checkflag(flag) |
| 155 | + d = _db.DB() |
| 156 | + d.set_flags(hflags) |
| 157 | + if cachesize is not None: d.set_cachesize(0, cachesize) |
| 158 | + if pgsize is not None: d.set_pagesize(pgsize) |
| 159 | + if lorder is not None: d.set_lorder(lorder) |
| 160 | + if ffactor is not None: d.set_h_ffactor(ffactor) |
| 161 | + if nelem is not None: d.set_h_nelem(nelem) |
| 162 | + d.open(file, _db.DB_HASH, flags, mode) |
| 163 | + return _DBWithCursor(d) |
| 164 | + |
| 165 | +#---------------------------------------------------------------------- |
| 166 | + |
| 167 | +def btopen(file, flag='c', mode=0666, |
| 168 | + btflags=0, cachesize=None, maxkeypage=None, minkeypage=None, |
| 169 | + pgsize=None, lorder=None): |
| 170 | + |
| 171 | + flags = _checkflag(flag) |
| 172 | + d = _db.DB() |
| 173 | + if cachesize is not None: d.set_cachesize(0, cachesize) |
| 174 | + if pgsize is not None: d.set_pagesize(pgsize) |
| 175 | + if lorder is not None: d.set_lorder(lorder) |
| 176 | + d.set_flags(btflags) |
| 177 | + if minkeypage is not None: d.set_bt_minkey(minkeypage) |
| 178 | + if maxkeypage is not None: d.set_bt_maxkey(maxkeypage) |
| 179 | + d.open(file, _db.DB_BTREE, flags, mode) |
| 180 | + return _DBWithCursor(d) |
| 181 | + |
| 182 | +#---------------------------------------------------------------------- |
| 183 | + |
| 184 | + |
| 185 | +def rnopen(file, flag='c', mode=0666, |
| 186 | + rnflags=0, cachesize=None, pgsize=None, lorder=None, |
| 187 | + rlen=None, delim=None, source=None, pad=None): |
| 188 | + |
| 189 | + flags = _checkflag(flag) |
| 190 | + d = _db.DB() |
| 191 | + if cachesize is not None: d.set_cachesize(0, cachesize) |
| 192 | + if pgsize is not None: d.set_pagesize(pgsize) |
| 193 | + if lorder is not None: d.set_lorder(lorder) |
| 194 | + d.set_flags(rnflags) |
| 195 | + if delim is not None: d.set_re_delim(delim) |
| 196 | + if rlen is not None: d.set_re_len(rlen) |
| 197 | + if source is not None: d.set_re_source(source) |
| 198 | + if pad is not None: d.set_re_pad(pad) |
| 199 | + d.open(file, _db.DB_RECNO, flags, mode) |
| 200 | + return _DBWithCursor(d) |
| 201 | + |
| 202 | +#---------------------------------------------------------------------- |
| 203 | + |
| 204 | + |
| 205 | +def _checkflag(flag): |
| 206 | + if flag == 'r': |
| 207 | + flags = _db.DB_RDONLY |
| 208 | + elif flag == 'rw': |
| 209 | + flags = 0 |
| 210 | + elif flag == 'w': |
| 211 | + flags = _db.DB_CREATE |
| 212 | + elif flag == 'c': |
| 213 | + flags = _db.DB_CREATE |
| 214 | + elif flag == 'n': |
| 215 | + flags = _db.DB_CREATE | _db.DB_TRUNCATE |
| 216 | + else: |
| 217 | + raise error, "flags should be one of 'r', 'w', 'c' or 'n'" |
| 218 | + return flags | _db.DB_THREAD |
| 219 | + |
| 220 | +#---------------------------------------------------------------------- |
| 221 | + |
| 222 | + |
| 223 | +# This is a silly little hack that allows apps to continue to use the |
| 224 | +# DB_THREAD flag even on systems without threads without freaking out |
| 225 | +# BerkeleyDB. |
| 226 | +# |
| 227 | +# This assumes that if Python was built with thread support then |
| 228 | +# BerkeleyDB was too. |
| 229 | + |
| 230 | +try: |
| 231 | + import thread |
| 232 | + del thread |
| 233 | +except ImportError: |
| 234 | + _db.DB_THREAD = 0 |
| 235 | + |
| 236 | + |
| 237 | +#---------------------------------------------------------------------- |
0 commit comments