1 import struct, contextlib, math
3 from .db import bd, txnfun
5 __all__ = ["maybe", "t_int", "t_uint", "t_float", "t_str", "ordered"]
7 deadlock = bd.DBLockDeadlockError
8 notfound = bd.DBNotFoundError
10 class simpletype(object):
11 def __init__(self, encode, decode):
17 def decode(self, dat):
19 def compare(self, a, b):
29 return cls(lambda ob: struct.pack(fmt, ob),
30 lambda dat: struct.unpack(fmt, dat)[0])
33 def __init__(self, bk):
37 if ob is None: return b""
38 return b"\0" + self.bk.encode(ob)
39 def decode(self, dat):
40 if dat == b"": return None
41 return self.bk.dec(dat[1:])
42 def compare(self, a, b):
50 return self.bk.compare(a[1:], b[1:])
53 if math.isnan(a) and math.isnan(b):
66 t_int = simpletype.struct(">q")
67 t_uint = simpletype.struct(">Q")
68 t_float = simpletype.struct(">d")
69 t_float.compare = floatcmp
70 t_str = simpletype((lambda ob: ob.encode("utf-8")), (lambda dat: dat.decode("utf-8")))
73 def __init__(self, db, name, datatype):
80 class ordered(index, lib.closable):
81 def __init__(self, db, name, datatype, create=True):
82 super().__init__(db, name, datatype)
83 fl = bd.DB_THREAD | bd.DB_AUTO_COMMIT
84 if create: fl |= bd.DB_CREATE
87 if a == b == "": return 0
88 return self.typ.compare(self.typ.decode(a), self.typ.decode(b))
89 db.set_flags(bd.DB_DUPSORT)
90 db.set_bt_compare(compare)
91 self.bk = db._opendb("i-" + name, bd.DB_BTREE, fl, initdb)
92 self.bk.set_get_returns_none(False)
97 class cursor(lib.closable):
98 def __init__(self, idx, cur, item, stop):
105 if self.cur is not None:
112 if self.item is None:
113 raise StopIteration()
115 rk = self.idx.typ.decode(rk)
116 rv = struct.unpack(">Q", rv)[0]
119 raise StopIteration()
127 self.item = self.cur.next()
139 except StopIteration:
142 def get(self, *, match=missing, ge=missing, gt=missing, lt=missing, le=missing, all=False):
145 cur = self.bk.cursor()
148 if match is not missing:
150 k, v = cur.set(self.typ.encode(match))
152 return self.cursor(None, None, None, None)
155 return self.cursor(self, cur, (k, v), lambda o: (self.typ.compare(o, match) != 0))
160 return self.cursor(None, None, None, None)
163 return self.cursor(self, cur, (k, v), lambda o: False)
164 elif ge is not missing or gt is not missing or lt is not missing or le is not missing:
167 if ge is not missing:
168 k, v = cur.set_range(self.typ.encode(ge))
169 elif gt is not missing:
170 k, v = cur.set_range(self.typ.encode(gt))
175 return self.cursor(None, None, None, None)
176 if lt is not missing:
177 stop = lambda o: self.typ.compare(o, lt) >= 0
178 elif le is not missing:
179 stop = lambda o: self.typ.compare(o, le) > 0
181 stop = lambda o: False
182 ret = self.cursor(self, cur, (k, v), stop)
185 while self.typ.compare(ret.peek()[0], gt) == 0:
187 except StopIteration:
192 raise NameError("invalid get() specification")
199 @txnfun(lambda self: self.db.env.env)
200 def put(self, key, id, *, tx):
201 obid = struct.pack(">Q", id)
202 if not self.db.ob.has_key(obid, txn=tx.tx):
203 raise ValueError("no such object in database: " + str(id))
205 self.bk.put(self.typ.encode(key), obid, txn=tx.tx, flags=bd.DB_NODUPDATA)
206 except bd.DBKeyExistError:
210 @txnfun(lambda self: self.db.env.env)
211 def remove(self, key, id, *, tx):
212 obid = struct.pack(">Q", id)
213 if not self.db.ob.has_key(obid, txn=tx.tx):
214 raise ValueError("no such object in database: " + str(id))
215 cur = self.bk.cursor(txn=tx.tx)
218 cur.get_both(self.typ.encode(key), obid)