From 947dfab3c174ecce6bd1ff18bdc4df7e0e4087c1 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Tue, 20 Mar 2018 23:48:28 +0100 Subject: [PATCH] Ensure that environment maintanence runs regularly. --- didex/db.py | 16 +++++++++------- didex/index.py | 4 ++-- didex/store.py | 8 ++++---- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/didex/db.py b/didex/db.py index 539fc85..068d404 100644 --- a/didex/db.py +++ b/didex/db.py @@ -64,7 +64,8 @@ def opendb(env, fnm, dnm, typ, fl, mode): class txn(object): def __init__(self, env, flags=bd.DB_TXN_WRITE_NOSYNC): - self.tx = env.txn_begin(None, flags) + self.tx = env.env.txn_begin(None, flags) + self.env = env self.done = False self.pcommit = set() @@ -78,6 +79,7 @@ class txn(object): finally: run1(list[1:]) run1(list(self.pcommit)) + self.env.maint() def abort(self): self.done = True @@ -131,14 +133,14 @@ class database(object): self.cf = self._opendb("cf", bd.DB_HASH, fl) self.ob = self._opendb("ob", bd.DB_HASH, fl) - @txnfun(lambda self: self.env.env) + @txnfun(lambda self: self.env) def _opendb(self, dnm, typ, fl, init=None, *, tx): ret = bd.DB(self.env.env) if init: init(ret) ret.open(self.fnm, dnm, typ, fl, self.mode, txn=tx.tx) return ret - @txnfun(lambda self: self.env.env) + @txnfun(lambda self: self.env) def _nextseq(self, *, tx): if self.cf.has_key(b"seq", txn=tx.tx): seq = struct.unpack(">Q", self.cf.get(b"seq", txn=tx.tx))[0] @@ -147,27 +149,27 @@ class database(object): self.cf.put(b"seq", struct.pack(">Q", seq + 1), txn=tx.tx) return seq - @txnfun(lambda self: self.env.env) + @txnfun(lambda self: self.env) def add(self, ob, *, tx): seq = self._nextseq(tx=tx) self.ob.put(struct.pack(">Q", seq), ob, txn=tx.tx, flags=bd.DB_NOOVERWRITE) return seq - @txnfun(lambda self: self.env.env) + @txnfun(lambda self: self.env) def replace(self, id, ob, *, tx): key = struct.pack(">Q", id) if not self.ob.has_key(key, txn=tx.tx): raise KeyError(id) self.ob.put(key, ob, txn=tx.tx) - @txnfun(lambda self: self.env.env) + @txnfun(lambda self: self.env) def get(self, id, *, tx): ret = self.ob.get(struct.pack(">Q", id), None) if ret is None: raise KeyError(id) return ret - @txnfun(lambda self: self.env.env) + @txnfun(lambda self: self.env) def remove(self, id, *, tx): key = struct.pack(">Q", id) if not self.ob.has_key(key, txn=tx.tx): diff --git a/didex/index.py b/didex/index.py index ff291b2..0608318 100644 --- a/didex/index.py +++ b/didex/index.py @@ -310,7 +310,7 @@ class ordered(index, lib.closable): if not done: cur.close() - @txnfun(lambda self: self.db.env.env) + @txnfun(lambda self: self.db.env) def put(self, key, id, *, tx): obid = struct.pack(">Q", id) if not self.db.ob.has_key(obid, txn=tx.tx): @@ -321,7 +321,7 @@ class ordered(index, lib.closable): return False return True - @txnfun(lambda self: self.db.env.env) + @txnfun(lambda self: self.db.env) def remove(self, key, id, *, tx): obid = struct.pack(">Q", id) if not self.db.ob.has_key(obid, txn=tx.tx): diff --git a/didex/store.py b/didex/store.py index 11fe17a..2b9d65c 100644 --- a/didex/store.py +++ b/didex/store.py @@ -101,7 +101,7 @@ class datastore(object): def _encode(self, obj): return pickle.dumps(obj) - @txnfun(lambda self: self.db().env.env) + @txnfun(lambda self: self.db().env) def _load(self, id, *, tx): loaded = self._decode(self.db().get(id, tx=tx)) if hasattr(loaded, "__didex_loaded__"): @@ -113,7 +113,7 @@ class datastore(object): def get(self, id, *, load=True): return self.cache.get(id, load=load) - @txnfun(lambda self: self.db().env.env) + @txnfun(lambda self: self.db().env) def register(self, obj, *, tx): id = self.db().add(self._encode(obj), tx=tx) for nm, attr in storedescs(obj): @@ -121,7 +121,7 @@ class datastore(object): self.cache.put(id, obj) return id - @txnfun(lambda self: self.db().env.env) + @txnfun(lambda self: self.db().env) def unregister(self, id, *, vfy=None, tx): obj = self.get(id) if vfy is not None and obj is not vfy: @@ -131,7 +131,7 @@ class datastore(object): self.db().remove(id, tx=tx) self.cache.remove(id) - @txnfun(lambda self: self.db().env.env) + @txnfun(lambda self: self.db().env) def update(self, id, *, vfy=None, tx): obj = self.get(id, load=False) if vfy is not None and obj is not vfy: -- 2.11.0