1 import struct, contextlib
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, duplicates, create=True):
62 super().__init__(db, name, datatype)
64 fl = bd.DB_THREAD | bd.DB_AUTO_COMMIT
65 if create: fl |= bd.DB_CREATE
68 if a == b == "": return 0
69 return self.typ.compare(self.typ.decode(a), self.typ.decode(b))
70 db.set_flags(bd.DB_DUPSORT)
71 db.set_bt_compare(compare)
72 self.bk = db._opendb("i-" + name, bd.DB_BTREE, fl, initdb)
73 self.bk.set_get_returns_none(False)
78 class cursor(lib.closable):
79 def __init__(self, idx, cur, item, stop):
86 if self.cur is not None:
96 rk = self.idx.typ.decode(rk)
97 rv = struct.unpack(">Q", rv)[0]
100 raise StopIteration()
108 self.item = self.cur.next()
120 except StopIteration:
123 def get(self, *, match=missing, ge=missing, gt=missing, lt=missing, le=missing, all=False):
126 cur = self.bk.cursor()
129 if match is not missing:
131 k, v = cur.set(self.typ.encode(match))
133 return self.cursor(None, None, None, None)
136 return self.cursor(self, cur, (k, v), lambda o: (self.typ.compare(o, match) != 0))
141 return self.cursor(None, None, None, None)
144 return self.cursor(self, cur, (k, v), lambda o: False)
145 elif ge is not missing or gt is not missing or lt is not missing or le is not missing:
148 if ge is not missing:
149 k, v = cur.set_range(self.typ.encode(ge))
150 elif gt is not missing:
151 k, v = cur.set_range(self.typ.encode(gt))
156 return self.cursor(None, None, None, None)
157 if lt is not missing:
158 stop = lambda o: self.typ.compare(o, lt) >= 0
159 elif le is not missing:
160 stop = lambda o: self.typ.compare(o, le) > 0
162 stop = lambda o: False
163 ret = self.cursor(self, cur, (k, v), stop)
166 while self.typ.compare(ret.peek()[0], gt) == 0:
168 except StopIteration:
173 raise NameError("invalid get() specification")
180 def put(self, key, id):
183 with db.txn(self.db.env.env) as tx:
184 obid = struct.pack(">Q", id)
185 if not self.db.ob.has_key(obid, txn=tx.tx):
186 raise ValueError("no such object in database: " + str(id))
188 self.bk.put(self.typ.encode(key), obid, txn=tx.tx, flags=bd.DB_NODUPDATA)
189 except bd.DBKeyExistError:
196 def remove(self, key, id):
199 with db.txn(self.db.env.env) as tx:
200 obid = struct.pack(">Q", id)
201 if not self.db.ob.has_key(obid, txn=tx.tx):
202 raise ValueError("no such object in database: " + str(id))
203 cur = self.bk.cursor(txn=tx.tx)
206 cur.get_both(self.typ.encode(key), obid)