1 """(g)dbm-compatible interface to ZODB"""
5 from ZODB import FileStorage, DB, POSException
7 # prevent a second import of this module from spuriously succeeding
8 del sys.modules[__name__]
12 __all__ = ["error", "open"]
14 error = POSException.POSError # Exported for anydbm
18 def __init__(self, file, flag, mode=0o666, trans_threshold=1000):
19 create = (flag == 'n') # force recreation
20 # if flag == 'w' or 'c' and file does not exist FileStorage will set it to 1 for us
22 self.read_only = read_only = (flag == 'r')
25 self.trans_threshold = trans_threshold
26 self._transcount = 0 # transactions counter - for commiting transactions
28 storage = FileStorage.FileStorage(file, create=create, read_only = read_only)
30 self.conn = conn = db.open()
31 self.dbroot = conn.root()
37 return self.dbroot.keys()
40 return len(self.dbroot)
42 def has_key(self, key):
43 return self.dbroot.has_key(key)
45 def get(self, key, default=None):
46 if self.dbroot.has_key(key):
50 def __getitem__(self, key):
51 return self.dbroot[key]
53 def __setitem__(self, key, value):
54 self.dbroot[key] = value
57 def __delitem__(self, key):
62 if self._closed: return
63 if not self.read_only:
64 get_transaction().commit()
65 self.conn.db().close()
70 self._transcount = self._transcount + 1
71 if self._transcount == self.trans_threshold:
73 get_transaction().commit()
76 def open(file, flag, mode=0o666):
77 return ZODBhash(file, flag, mode)