1 import struct, contextlib, math
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:])
51 if math.isnan(a) and math.isnan(b):
64 t_int = simpletype.struct(">q")
65 t_uint = simpletype.struct(">Q")
66 t_float = simpletype.struct(">d")
67 t_float.compare = floatcmp
68 t_str = simpletype((lambda ob: ob.encode("utf-8")), (lambda dat: dat.decode("utf-8")))
71 def __init__(self, db, name, datatype):
78 class ordered(index, lib.closable):
79 def __init__(self, db, name, datatype, create=True):
80 super().__init__(db, name, datatype)
81 fl = bd.DB_THREAD | bd.DB_AUTO_COMMIT
82 if create: fl |= bd.DB_CREATE
85 if a == b == "": return 0
86 return self.typ.compare(self.typ.decode(a), self.typ.decode(b))
87 db.set_flags(bd.DB_DUPSORT)
88 db.set_bt_compare(compare)
89 self.bk = db._opendb("i-" + name, bd.DB_BTREE, fl, initdb)
90 self.bk.set_get_returns_none(False)
95 class cursor(lib.closable):
96 def __init__(self, idx, cur, item, stop):
103 if self.cur is not None:
110 if self.item is None:
111 raise StopIteration()
113 rk = self.idx.typ.decode(rk)
114 rv = struct.unpack(">Q", rv)[0]
117 raise StopIteration()
125 self.item = self.cur.next()
137 except StopIteration:
140 def get(self, *, match=missing, ge=missing, gt=missing, lt=missing, le=missing, all=False):
143 cur = self.bk.cursor()
146 if match is not missing:
148 k, v = cur.set(self.typ.encode(match))
150 return self.cursor(None, None, None, None)
153 return self.cursor(self, cur, (k, v), lambda o: (self.typ.compare(o, match) != 0))
158 return self.cursor(None, None, None, None)
161 return self.cursor(self, cur, (k, v), lambda o: False)
162 elif ge is not missing or gt is not missing or lt is not missing or le is not missing:
165 if ge is not missing:
166 k, v = cur.set_range(self.typ.encode(ge))
167 elif gt is not missing:
168 k, v = cur.set_range(self.typ.encode(gt))
173 return self.cursor(None, None, None, None)
174 if lt is not missing:
175 stop = lambda o: self.typ.compare(o, lt) >= 0
176 elif le is not missing:
177 stop = lambda o: self.typ.compare(o, le) > 0
179 stop = lambda o: False
180 ret = self.cursor(self, cur, (k, v), stop)
183 while self.typ.compare(ret.peek()[0], gt) == 0:
185 except StopIteration:
190 raise NameError("invalid get() specification")
197 @txnfun(lambda self: self.db.env.env)
198 def put(self, key, id, *, tx):
199 obid = struct.pack(">Q", id)
200 if not self.db.ob.has_key(obid, txn=tx.tx):
201 raise ValueError("no such object in database: " + str(id))
203 self.bk.put(self.typ.encode(key), obid, txn=tx.tx, flags=bd.DB_NODUPDATA)
204 except bd.DBKeyExistError:
208 @txnfun(lambda self: self.db.env.env)
209 def remove(self, key, id, *, tx):
210 obid = struct.pack(">Q", id)
211 if not self.db.ob.has_key(obid, txn=tx.tx):
212 raise ValueError("no such object in database: " + str(id))
213 cur = self.bk.cursor(txn=tx.tx)
216 cur.get_both(self.typ.encode(key), obid)