Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 1 | #!/bin/env python |
| 2 | #------------------------------------------------------------------------ |
| 3 | # Copyright (c) 1997-2001 by Total Control Software |
| 4 | # All Rights Reserved |
| 5 | #------------------------------------------------------------------------ |
| 6 | # |
| 7 | # Module Name: dbShelve.py |
| 8 | # |
| 9 | # Description: A reimplementation of the standard shelve.py that |
| 10 | # forces the use of cPickle, and DB. |
| 11 | # |
| 12 | # Creation Date: 11/3/97 3:39:04PM |
| 13 | # |
| 14 | # License: This is free software. You may use this software for any |
| 15 | # purpose including modification/redistribution, so long as |
| 16 | # this header remains intact and that you do not claim any |
| 17 | # rights of ownership or authorship of this software. This |
| 18 | # software has been tested, but no warranty is expressed or |
| 19 | # implied. |
| 20 | # |
| 21 | # 13-Dec-2000: Updated to be used with the new bsddb3 package. |
| 22 | # Added DBShelfCursor class. |
| 23 | # |
| 24 | #------------------------------------------------------------------------ |
| 25 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 26 | """Manage shelves of pickled objects using bsddb database files for the |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 27 | storage. |
| 28 | """ |
| 29 | |
| 30 | #------------------------------------------------------------------------ |
| 31 | |
| 32 | import cPickle |
Barry Warsaw | 9914227 | 2003-02-08 03:18:58 +0000 | [diff] [blame] | 33 | try: |
| 34 | from UserDict import DictMixin |
| 35 | except ImportError: |
| 36 | # DictMixin is new in Python 2.3 |
| 37 | class DictMixin: pass |
Barry Warsaw | f71de3e | 2003-01-28 17:20:44 +0000 | [diff] [blame] | 38 | try: |
| 39 | # For Python 2.3 |
| 40 | from bsddb import db |
| 41 | except ImportError: |
| 42 | # For earlier Pythons w/distutils pybsddb |
| 43 | from bsddb3 import db |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 44 | |
| 45 | #------------------------------------------------------------------------ |
| 46 | |
| 47 | |
| 48 | def open(filename, flags=db.DB_CREATE, mode=0660, filetype=db.DB_HASH, |
| 49 | dbenv=None, dbname=None): |
| 50 | """ |
| 51 | A simple factory function for compatibility with the standard |
| 52 | shleve.py module. It can be used like this, where key is a string |
| 53 | and data is a pickleable object: |
| 54 | |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 55 | from bsddb import dbshelve |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 56 | db = dbshelve.open(filename) |
| 57 | |
| 58 | db[key] = data |
| 59 | |
| 60 | db.close() |
| 61 | """ |
| 62 | if type(flags) == type(''): |
| 63 | sflag = flags |
| 64 | if sflag == 'r': |
| 65 | flags = db.DB_RDONLY |
| 66 | elif sflag == 'rw': |
| 67 | flags = 0 |
| 68 | elif sflag == 'w': |
| 69 | flags = db.DB_CREATE |
| 70 | elif sflag == 'c': |
| 71 | flags = db.DB_CREATE |
| 72 | elif sflag == 'n': |
| 73 | flags = db.DB_TRUNCATE | db.DB_CREATE |
| 74 | else: |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 75 | raise error, "flags should be one of 'r', 'w', 'c' or 'n' or use the bsddb.db.DB_* flags" |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 76 | |
| 77 | d = DBShelf(dbenv) |
| 78 | d.open(filename, dbname, filetype, flags, mode) |
| 79 | return d |
| 80 | |
| 81 | #--------------------------------------------------------------------------- |
| 82 | |
Raymond Hettinger | 30a634e | 2003-02-05 04:12:41 +0000 | [diff] [blame] | 83 | class DBShelf(DictMixin): |
Barry Warsaw | 9914227 | 2003-02-08 03:18:58 +0000 | [diff] [blame] | 84 | """A shelf to hold pickled objects, built upon a bsddb DB object. It |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 85 | automatically pickles/unpickles data objects going to/from the DB. |
| 86 | """ |
| 87 | def __init__(self, dbenv=None): |
| 88 | self.db = db.DB(dbenv) |
| 89 | self.binary = 1 |
| 90 | |
| 91 | |
| 92 | def __del__(self): |
| 93 | self.close() |
| 94 | |
| 95 | |
| 96 | def __getattr__(self, name): |
Barry Warsaw | 9914227 | 2003-02-08 03:18:58 +0000 | [diff] [blame] | 97 | """Many methods we can just pass through to the DB object. |
| 98 | (See below) |
| 99 | """ |
Martin v. Löwis | 6aa4a1f | 2002-11-19 08:09:52 +0000 | [diff] [blame] | 100 | return getattr(self.db, name) |
| 101 | |
| 102 | |
| 103 | #----------------------------------- |
| 104 | # Dictionary access methods |
| 105 | |
| 106 | def __len__(self): |
| 107 | return len(self.db) |
| 108 | |
| 109 | |
| 110 | def __getitem__(self, key): |
| 111 | data = self.db[key] |
| 112 | return cPickle.loads(data) |
| 113 | |
| 114 | |
| 115 | def __setitem__(self, key, value): |
| 116 | data = cPickle.dumps(value, self.binary) |
| 117 | self.db[key] = data |
| 118 | |
| 119 | |
| 120 | def __delitem__(self, key): |
| 121 | del self.db[key] |
| 122 | |
| 123 | |
| 124 | def keys(self, txn=None): |
| 125 | if txn != None: |
| 126 | return self.db.keys(txn) |
| 127 | else: |
| 128 | return self.db.keys() |
| 129 | |
| 130 | |
| 131 | def items(self, txn=None): |
| 132 | if txn != None: |
| 133 | items = self.db.items(txn) |
| 134 | else: |
| 135 | items = self.db.items() |
| 136 | newitems = [] |
| 137 | |
| 138 | for k, v in items: |
| 139 | newitems.append( (k, cPickle.loads(v)) ) |
| 140 | return newitems |
| 141 | |
| 142 | def values(self, txn=None): |
| 143 | if txn != None: |
| 144 | values = self.db.values(txn) |
| 145 | else: |
| 146 | values = self.db.values() |
| 147 | |
| 148 | return map(cPickle.loads, values) |
| 149 | |
| 150 | #----------------------------------- |
| 151 | # Other methods |
| 152 | |
| 153 | def append(self, value, txn=None): |
| 154 | data = cPickle.dumps(value, self.binary) |
| 155 | return self.db.append(data, txn) |
| 156 | |
| 157 | |
| 158 | def associate(self, secondaryDB, callback, flags=0): |
| 159 | def _shelf_callback(priKey, priData, realCallback=callback): |
| 160 | data = cPickle.loads(priData) |
| 161 | return realCallback(priKey, data) |
| 162 | return self.db.associate(secondaryDB, _shelf_callback, flags) |
| 163 | |
| 164 | |
| 165 | #def get(self, key, default=None, txn=None, flags=0): |
| 166 | def get(self, *args, **kw): |
| 167 | # We do it with *args and **kw so if the default value wasn't |
| 168 | # given nothing is passed to the extension module. That way |
| 169 | # an exception can be raised if set_get_returns_none is turned |
| 170 | # off. |
| 171 | data = apply(self.db.get, args, kw) |
| 172 | try: |
| 173 | return cPickle.loads(data) |
| 174 | except (TypeError, cPickle.UnpicklingError): |
| 175 | return data # we may be getting the default value, or None, |
| 176 | # so it doesn't need unpickled. |
| 177 | |
| 178 | def get_both(self, key, value, txn=None, flags=0): |
| 179 | data = cPickle.dumps(value, self.binary) |
| 180 | data = self.db.get(key, data, txn, flags) |
| 181 | return cPickle.loads(data) |
| 182 | |
| 183 | |
| 184 | def cursor(self, txn=None, flags=0): |
| 185 | c = DBShelfCursor(self.db.cursor(txn, flags)) |
| 186 | c.binary = self.binary |
| 187 | return c |
| 188 | |
| 189 | |
| 190 | def put(self, key, value, txn=None, flags=0): |
| 191 | data = cPickle.dumps(value, self.binary) |
| 192 | return self.db.put(key, data, txn, flags) |
| 193 | |
| 194 | |
| 195 | def join(self, cursorList, flags=0): |
| 196 | raise NotImplementedError |
| 197 | |
| 198 | |
| 199 | #---------------------------------------------- |
| 200 | # Methods allowed to pass-through to self.db |
| 201 | # |
| 202 | # close, delete, fd, get_byteswapped, get_type, has_key, |
| 203 | # key_range, open, remove, rename, stat, sync, |
| 204 | # upgrade, verify, and all set_* methods. |
| 205 | |
| 206 | |
| 207 | #--------------------------------------------------------------------------- |
| 208 | |
| 209 | class DBShelfCursor: |
| 210 | """ |
| 211 | """ |
| 212 | def __init__(self, cursor): |
| 213 | self.dbc = cursor |
| 214 | |
| 215 | def __del__(self): |
| 216 | self.close() |
| 217 | |
| 218 | |
| 219 | def __getattr__(self, name): |
| 220 | """Some methods we can just pass through to the cursor object. (See below)""" |
| 221 | return getattr(self.dbc, name) |
| 222 | |
| 223 | |
| 224 | #---------------------------------------------- |
| 225 | |
| 226 | def dup(self, flags=0): |
| 227 | return DBShelfCursor(self.dbc.dup(flags)) |
| 228 | |
| 229 | |
| 230 | def put(self, key, value, flags=0): |
| 231 | data = cPickle.dumps(value, self.binary) |
| 232 | return self.dbc.put(key, data, flags) |
| 233 | |
| 234 | |
| 235 | def get(self, *args): |
| 236 | count = len(args) # a method overloading hack |
| 237 | method = getattr(self, 'get_%d' % count) |
| 238 | apply(method, args) |
| 239 | |
| 240 | def get_1(self, flags): |
| 241 | rec = self.dbc.get(flags) |
| 242 | return self._extract(rec) |
| 243 | |
| 244 | def get_2(self, key, flags): |
| 245 | rec = self.dbc.get(key, flags) |
| 246 | return self._extract(rec) |
| 247 | |
| 248 | def get_3(self, key, value, flags): |
| 249 | data = cPickle.dumps(value, self.binary) |
| 250 | rec = self.dbc.get(key, flags) |
| 251 | return self._extract(rec) |
| 252 | |
| 253 | |
| 254 | def current(self, flags=0): return self.get_1(flags|db.DB_CURRENT) |
| 255 | def first(self, flags=0): return self.get_1(flags|db.DB_FIRST) |
| 256 | def last(self, flags=0): return self.get_1(flags|db.DB_LAST) |
| 257 | def next(self, flags=0): return self.get_1(flags|db.DB_NEXT) |
| 258 | def prev(self, flags=0): return self.get_1(flags|db.DB_PREV) |
| 259 | def consume(self, flags=0): return self.get_1(flags|db.DB_CONSUME) |
| 260 | def next_dup(self, flags=0): return self.get_1(flags|db.DB_NEXT_DUP) |
| 261 | def next_nodup(self, flags=0): return self.get_1(flags|db.DB_NEXT_NODUP) |
| 262 | def prev_nodup(self, flags=0): return self.get_1(flags|db.DB_PREV_NODUP) |
| 263 | |
| 264 | |
| 265 | def get_both(self, key, value, flags=0): |
| 266 | data = cPickle.dumps(value, self.binary) |
| 267 | rec = self.dbc.get_both(key, flags) |
| 268 | return self._extract(rec) |
| 269 | |
| 270 | |
| 271 | def set(self, key, flags=0): |
| 272 | rec = self.dbc.set(key, flags) |
| 273 | return self._extract(rec) |
| 274 | |
| 275 | def set_range(self, key, flags=0): |
| 276 | rec = self.dbc.set_range(key, flags) |
| 277 | return self._extract(rec) |
| 278 | |
| 279 | def set_recno(self, recno, flags=0): |
| 280 | rec = self.dbc.set_recno(recno, flags) |
| 281 | return self._extract(rec) |
| 282 | |
| 283 | set_both = get_both |
| 284 | |
| 285 | def _extract(self, rec): |
| 286 | if rec is None: |
| 287 | return None |
| 288 | else: |
| 289 | key, data = rec |
| 290 | return key, cPickle.loads(data) |
| 291 | |
| 292 | #---------------------------------------------- |
| 293 | # Methods allowed to pass-through to self.dbc |
| 294 | # |
| 295 | # close, count, delete, get_recno, join_item |
| 296 | |
| 297 | |
| 298 | #--------------------------------------------------------------------------- |
Barry Warsaw | 9a0d779 | 2002-12-30 20:53:52 +0000 | [diff] [blame] | 299 | |
| 300 | |
| 301 | |