1 import struct, contextlib
3 from .db import bd, txnfun
5 deadlock = bd.DBLockDeadlockError
6 notfound = bd.DBNotFoundError
8 class simpletype(object):
9 def __init__(self, encode, decode):
15 def decode(self, dat):
17 def compare(self, a, b):
27 return cls(lambda ob: struct.pack(fmt, ob),
28 lambda dat: struct.unpack(fmt, dat)[0])
31 def __init__(self, bk):
35 if ob is None: return b""
36 return b"\0" + self.bk.encode(ob)
37 def decode(self, dat):
38 if dat == b"": return None
39 return self.bk.dec(dat[1:])
40 def compare(self, a, b):
48 return self.bk.compare(a[1:], b[1:])
50 t_int = simpletype.struct(">Q")
53 def __init__(self, db, name, datatype):
60 class ordered(index, lib.closable):
61 def __init__(self, db, name, datatype, create=True):
62 super().__init__(db, name, datatype)
63 fl = bd.DB_THREAD | bd.DB_AUTO_COMMIT
64 if create: fl |= bd.DB_CREATE
67 if a == b == "": return 0
68 return self.typ.compare(self.typ.decode(a), self.typ.decode(b))
69 db.set_flags(bd.DB_DUPSORT)
70 db.set_bt_compare(compare)
71 self.bk = db._opendb("i-" + name, bd.DB_BTREE, fl, initdb)
72 self.bk.set_get_returns_none(False)
77 class cursor(lib.closable):
78 def __init__(self, idx, cur, item, stop):
85 if self.cur is not None:
95 rk = self.idx.typ.decode(rk)
96 rv = struct.unpack(">Q", rv)[0]
107 self.item = self.cur.next()
119 except StopIteration:
122 def get(self, *, match=missing, ge=missing, gt=missing, lt=missing, le=missing, all=False):
125 cur = self.bk.cursor()
128 if match is not missing:
130 k, v = cur.set(self.typ.encode(match))
132 return self.cursor(None, None, None, None)
135 return self.cursor(self, cur, (k, v), lambda o: (self.typ.compare(o, match) != 0))
140 return self.cursor(None, None, None, None)
143 return self.cursor(self, cur, (k, v), lambda o: False)
144 elif ge is not missing or gt is not missing or lt is not missing or le is not missing:
147 if ge is not missing:
148 k, v = cur.set_range(self.typ.encode(ge))
149 elif gt is not missing:
150 k, v = cur.set_range(self.typ.encode(gt))
155 return self.cursor(None, None, None, None)
156 if lt is not missing:
157 stop = lambda o: self.typ.compare(o, lt) >= 0
158 elif le is not missing:
159 stop = lambda o: self.typ.compare(o, le) > 0
161 stop = lambda o: False
162 ret = self.cursor(self, cur, (k, v), stop)
165 while self.typ.compare(ret.peek()[0], gt) == 0:
167 except StopIteration:
172 raise NameError("invalid get() specification")
179 @txnfun(lambda self: self.db.env.env)
180 def put(self, key, id, *, tx):
181 obid = struct.pack(">Q", id)
182 if not self.db.ob.has_key(obid, txn=tx.tx):
183 raise ValueError("no such object in database: " + str(id))
185 self.bk.put(self.typ.encode(key), obid, txn=tx.tx, flags=bd.DB_NODUPDATA)
186 except bd.DBKeyExistError:
190 @txnfun(lambda self: self.db.env.env)
191 def remove(self, key, id, *, tx):
192 obid = struct.pack(">Q", id)
193 if not self.db.ob.has_key(obid, txn=tx.tx):
194 raise ValueError("no such object in database: " + str(id))
195 cur = self.bk.cursor(txn=tx.tx)
198 cur.get_both(self.typ.encode(key), obid)