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