1 import threading, pickle, inspect, atexit, weakref
2 from . import db, index, cache
5 __all__ = ["environment", "datastore", "autostore"]
7 class environment(object):
8 def __init__(self, *, path=None, getpath=None, recover=False):
14 self.getpath = getpath
15 self.recover = recover
16 self.lk = threading.Lock()
23 self.path = self.getpath()
24 self.bk = db.environment(self.path, recover=self.recover)
25 atexit.register(self.close)
30 if self.bk is not None:
31 atexit.unregister(self.close)
35 class storedesc(object):
40 ret = t.__dict__.get("__didex_attr")
43 for st in inspect.getmro(t):
44 for nm, val in st.__dict__.items():
45 if isinstance(val, storedesc):
52 self.d = weakref.WeakKeyDictionary()
54 def __getitem__(self, key):
56 return self.d[obj][idx]
57 def __setitem__(self, key, val):
60 self.d[obj][idx] = val
62 self.d[obj] = {idx: val}
63 def __delitem__(self, key):
66 def get(self, key, default=None):
70 return self.d[obj].get(idx, default)
72 class datastore(object):
73 def __init__(self, name, *, env=None, path=".", ncache=None, codec=None):
75 self.lk = threading.Lock()
79 self.env = environment(path=path)
82 ncache = cache.cache()
84 self._encode, self._decode = codec
86 self.cache.load = self._load
87 self.icache = icache()
92 self._db = self.env().db(self.name)
95 def _decode(self, data):
97 return pickle.loads(data)
99 raise KeyError(id, "could not unpickle data")
101 def _encode(self, obj):
102 return pickle.dumps(obj)
104 @txnfun(lambda self: self.db().env.env)
105 def _load(self, id, *, tx):
106 loaded = self._decode(self.db().get(id, tx=tx))
107 if hasattr(loaded, "__didex_loaded__"):
108 loaded.__didex_loaded__(self, id)
109 for nm, attr in storedescs(loaded):
110 attr.loaded(id, loaded, tx)
113 def get(self, id, *, load=True):
114 return self.cache.get(id, load=load)
116 @txnfun(lambda self: self.db().env.env)
117 def register(self, obj, *, tx):
118 id = self.db().add(self._encode(obj), tx=tx)
119 for nm, attr in storedescs(obj):
120 attr.register(id, obj, tx)
121 self.cache.put(id, obj)
124 @txnfun(lambda self: self.db().env.env)
125 def unregister(self, id, *, vfy=None, tx):
127 if vfy is not None and obj is not vfy:
128 raise RuntimeError("object identity crisis: " + str(vfy) + " is not cached object " + obj)
129 for nm, attr in storedescs(obj):
130 attr.unregister(id, obj, tx)
131 self.db().remove(id, tx=tx)
132 self.cache.remove(id)
134 @txnfun(lambda self: self.db().env.env)
135 def update(self, id, *, vfy=None, tx):
136 obj = self.get(id, load=False)
137 if vfy is not None and obj is not vfy:
138 raise RuntimeError("object identity crisis: " + str(vfy) + " is not cached object " + obj)
139 for nm, attr, in storedescs(obj):
140 attr.update(id, obj, tx)
141 self.db().replace(id, self._encode(obj), tx=tx)
143 class autotype(type):
144 def __call__(self, *args, **kwargs):
145 new = super().__call__(*args, **kwargs)
146 new.id = self.store.register(new)
147 # XXX? ID is not saved now, but relied upon to be __didex_loaded__ later.
150 class autostore(object, metaclass=autotype):
154 def __didex_loaded__(self, store, id):
155 assert self.id is None or self.id == id
159 self.store.update(self.id, vfy=self)
162 self.store.unregister(self.id, vfy=self)