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