* fix: use non-transactional access in read-only mode...
This commit is contained in:
+15
-22
@@ -59,13 +59,16 @@ class DBShelf:
|
|||||||
def txn_proc(self, proc, txn):
|
def txn_proc(self, proc, txn):
|
||||||
# can be used to txn protect a method automatically
|
# can be used to txn protect a method automatically
|
||||||
if not txn:
|
if not txn:
|
||||||
autotxn = self.dbenv.txn_begin()
|
if self.dbenv:
|
||||||
try:
|
autotxn = self.dbenv.txn_begin()
|
||||||
retval = proc(autotxn)
|
try:
|
||||||
except db.DBError, e:
|
retval = proc(autotxn)
|
||||||
autotxn.abort()
|
except db.DBError, e:
|
||||||
raise e
|
autotxn.abort()
|
||||||
autotxn.commit()
|
raise e
|
||||||
|
autotxn.commit()
|
||||||
|
else: # execute without transactions
|
||||||
|
proc(None)
|
||||||
return retval
|
return retval
|
||||||
else:
|
else:
|
||||||
return proc(txn)
|
return proc(txn)
|
||||||
@@ -96,26 +99,16 @@ class DBShelf:
|
|||||||
return len(self.db)
|
return len(self.db)
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
txn = self.dbenv.txn_begin()
|
def proc(txn):
|
||||||
try:
|
|
||||||
data = self.db.get(key)
|
data = self.db.get(key)
|
||||||
except db.DBError, e:
|
return cPickle.loads(data)
|
||||||
txn.abort()
|
return self.txn_proc(proc, txn)
|
||||||
raise e
|
|
||||||
txn.commit()
|
|
||||||
return cPickle.loads(data)
|
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
# hyperdandik transactions
|
# hyperdandik transactions
|
||||||
txn = self.dbenv.txn_begin()
|
def proc(txn):
|
||||||
data = cPickle.dumps(value, self.binary)
|
|
||||||
try:
|
|
||||||
self.db.put(key,data,txn)
|
self.db.put(key,data,txn)
|
||||||
#print 'put', value
|
return self.txn_proc(proc, txn)
|
||||||
except db.DBError, e:
|
|
||||||
txn.abort()
|
|
||||||
raise e
|
|
||||||
txn.commit()
|
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
txn = self.dbenv.txn_begin()
|
txn = self.dbenv.txn_begin()
|
||||||
|
|||||||
+11
-8
@@ -34,12 +34,6 @@ class Error(pisi.Error):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def init_dbenv():
|
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):
|
if os.access(pisi.context.config.db_dir(), os.R_OK):
|
||||||
# try to read version
|
# try to read version
|
||||||
verfn = join_path(pisi.context.config.db_dir(), 'dbversion')
|
verfn = join_path(pisi.context.config.db_dir(), 'dbversion')
|
||||||
@@ -63,8 +57,15 @@ def init_dbenv():
|
|||||||
else:
|
else:
|
||||||
raise Error(_('Cannot attain read access to database environment'))
|
raise Error(_('Cannot attain read access to database environment'))
|
||||||
if os.access(pisi.context.config.db_dir(), os.W_OK):
|
if os.access(pisi.context.config.db_dir(), os.W_OK):
|
||||||
pass # TODO: is it possible to have read-only txnal dbs?
|
ctx.dbenv = dbobj.DBEnv()
|
||||||
ctx.dbenv.open(pisi.context.config.db_dir(), flags)
|
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):
|
#def open(filename, flags='r', mode = 0644, filetype = db.DB_BTREE):
|
||||||
# db = LockedDBShelf(None, mode, filetype, None, True)
|
# 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')
|
filename = join_path(pisi.context.config.db_dir(), dbname + '.bdb')
|
||||||
if os.access(os.path.dirname(filename), os.W_OK):
|
if os.access(os.path.dirname(filename), os.W_OK):
|
||||||
flags = 'w'
|
flags = 'w'
|
||||||
|
if not self.dbenv:
|
||||||
|
raise Error(_('Database writes not allowed without transactions'))
|
||||||
elif os.access(filename, os.R_OK):
|
elif os.access(filename, os.R_OK):
|
||||||
flags = 'r'
|
flags = 'r'
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user