* fix: use non-transactional access in read-only mode...

This commit is contained in:
Eray Özkural
2005-12-11 12:33:15 +00:00
parent 6678f13bee
commit 8db012f1d6
2 changed files with 26 additions and 30 deletions
+15 -22
View File
@@ -59,13 +59,16 @@ class DBShelf:
def txn_proc(self, proc, txn):
# can be used to txn protect a method automatically
if not txn:
autotxn = self.dbenv.txn_begin()
try:
retval = proc(autotxn)
except db.DBError, e:
autotxn.abort()
raise e
autotxn.commit()
if self.dbenv:
autotxn = self.dbenv.txn_begin()
try:
retval = proc(autotxn)
except db.DBError, e:
autotxn.abort()
raise e
autotxn.commit()
else: # execute without transactions
proc(None)
return retval
else:
return proc(txn)
@@ -96,26 +99,16 @@ class DBShelf:
return len(self.db)
def __getitem__(self, key):
txn = self.dbenv.txn_begin()
try:
def proc(txn):
data = self.db.get(key)
except db.DBError, e:
txn.abort()
raise e
txn.commit()
return cPickle.loads(data)
return cPickle.loads(data)
return self.txn_proc(proc, txn)
def __setitem__(self, key, value):
# hyperdandik transactions
txn = self.dbenv.txn_begin()
data = cPickle.dumps(value, self.binary)
try:
def proc(txn):
self.db.put(key,data,txn)
#print 'put', value
except db.DBError, e:
txn.abort()
raise e
txn.commit()
return self.txn_proc(proc, txn)
def __delitem__(self, key):
txn = self.dbenv.txn_begin()
+11 -8
View File
@@ -34,12 +34,6 @@ class Error(pisi.Error):
pass
def init_dbenv():
ctx.dbenv = dbobj.DBEnv()
flags = (db.DB_INIT_MPOOL | # cache
db.DB_INIT_TXN | # transaction subsystem
db.DB_INIT_LOG | # logging subsystem
db.DB_RECOVER | # run normal recovery
db.DB_CREATE) # allow db to create files
if os.access(pisi.context.config.db_dir(), os.R_OK):
# try to read version
verfn = join_path(pisi.context.config.db_dir(), 'dbversion')
@@ -63,8 +57,15 @@ def init_dbenv():
else:
raise Error(_('Cannot attain read access to database environment'))
if os.access(pisi.context.config.db_dir(), os.W_OK):
pass # TODO: is it possible to have read-only txnal dbs?
ctx.dbenv.open(pisi.context.config.db_dir(), flags)
ctx.dbenv = dbobj.DBEnv()
flags = (db.DB_INIT_MPOOL | # cache
db.DB_INIT_TXN | # transaction subsystem
db.DB_INIT_LOG | # logging subsystem
db.DB_RECOVER | # run normal recovery
db.DB_CREATE) # allow db to create files
ctx.dbenv.open(pisi.context.config.db_dir(), flags)
else:
ctx.dbenv = None
#def open(filename, flags='r', mode = 0644, filetype = db.DB_BTREE):
# db = LockedDBShelf(None, mode, filetype, None, True)
@@ -82,6 +83,8 @@ class LockedDBShelf(shelve.DBShelf):
filename = join_path(pisi.context.config.db_dir(), dbname + '.bdb')
if os.access(os.path.dirname(filename), os.W_OK):
flags = 'w'
if not self.dbenv:
raise Error(_('Database writes not allowed without transactions'))
elif os.access(filename, os.R_OK):
flags = 'r'
else: