import struct, contextlib
from . import db, lib
-from .db import bd
+from .db import bd, txnfun
deadlock = bd.DBLockDeadlockError
notfound = bd.DBNotFoundError
missing = object()
class ordered(index, lib.closable):
- def __init__(self, db, name, datatype, duplicates, create=True):
+ def __init__(self, db, name, datatype, create=True):
super().__init__(db, name, datatype)
- self.dup = duplicates
fl = bd.DB_THREAD | bd.DB_AUTO_COMMIT
if create: fl |= bd.DB_CREATE
def initdb(db):
except deadlock:
continue
- def put(self, key, id):
- while True:
- try:
- with db.txn(self.db.env.env) as tx:
- obid = struct.pack(">Q", id)
- if not self.db.ob.has_key(obid, txn=tx.tx):
- raise ValueError("no such object in database: " + str(id))
- try:
- self.bk.put(self.typ.encode(key), obid, txn=tx.tx, flags=bd.DB_NODUPDATA)
- except bd.DBKeyExistError:
- return False
- tx.commit()
- return True
- except deadlock:
- continue
-
- def remove(self, key, id):
- while True:
+ @txnfun(lambda self: self.db.env.env)
+ def put(self, key, id, *, tx):
+ obid = struct.pack(">Q", id)
+ if not self.db.ob.has_key(obid, txn=tx.tx):
+ raise ValueError("no such object in database: " + str(id))
+ try:
+ self.bk.put(self.typ.encode(key), obid, txn=tx.tx, flags=bd.DB_NODUPDATA)
+ except bd.DBKeyExistError:
+ return False
+ return True
+
+ @txnfun(lambda self: self.db.env.env)
+ def remove(self, key, id, *, tx):
+ obid = struct.pack(">Q", id)
+ if not self.db.ob.has_key(obid, txn=tx.tx):
+ raise ValueError("no such object in database: " + str(id))
+ cur = self.bk.cursor(txn=tx.tx)
+ try:
try:
- with db.txn(self.db.env.env) as tx:
- obid = struct.pack(">Q", id)
- if not self.db.ob.has_key(obid, txn=tx.tx):
- raise ValueError("no such object in database: " + str(id))
- cur = self.bk.cursor(txn=tx.tx)
- try:
- try:
- cur.get_both(self.typ.encode(key), obid)
- except notfound:
- return False
- cur.delete()
- finally:
- cur.close()
- tx.commit()
- return True
- except deadlock:
- continue
+ cur.get_both(self.typ.encode(key), obid)
+ except notfound:
+ return False
+ cur.delete()
+ finally:
+ cur.close()
+ return True