X-Git-Url: http://dolda2000.com/gitweb/?p=didex.git;a=blobdiff_plain;f=didex%2Findex.py;h=a2828cedd421e6e8d4a7de8cc976296fd956058b;hp=2f3fdef21e6f1e8f61f47e79252ad5c68f3b6c6b;hb=cbf73d3a70b97f17f1f5431eee1e73dbc56a7f8e;hpb=8950191cebfc907b7ec9a643b71d6734d90f4e9b diff --git a/didex/index.py b/didex/index.py index 2f3fdef..a2828ce 100644 --- a/didex/index.py +++ b/didex/index.py @@ -1,7 +1,9 @@ -import struct, contextlib +import struct, contextlib, math from . import db, lib from .db import bd, txnfun +__all__ = ["maybe", "t_int", "t_uint", "t_float", "t_str", "ordered"] + deadlock = bd.DBLockDeadlockError notfound = bd.DBNotFoundError @@ -47,7 +49,25 @@ class maybe(object): else: return self.bk.compare(a[1:], b[1:]) -t_int = simpletype.struct(">Q") +def floatcmp(a, b): + if math.isnan(a) and math.isnan(b): + return 0 + elif math.isnan(a): + return -1 + elif math.isnan(b): + return 1 + elif a < b: + return -1 + elif a > b: + return 1 + else: + return 0 + +t_int = simpletype.struct(">q") +t_uint = simpletype.struct(">Q") +t_float = simpletype.struct(">d") +t_float.compare = floatcmp +t_str = simpletype((lambda ob: ob.encode("utf-8")), (lambda dat: dat.decode("utf-8"))) class index(object): def __init__(self, db, name, datatype): @@ -58,9 +78,8 @@ class index(object): missing = object() class ordered(index, lib.closable): - def __init__(self, db, name, datatype, duplicates, create=True): + def __init__(self, db, name, datatype, create=True): super().__init__(db, name, datatype) - self.dup = duplicates fl = bd.DB_THREAD | bd.DB_AUTO_COMMIT if create: fl |= bd.DB_CREATE def initdb(db):