Added more indexed types.
authorFredrik Tolf <fredrik@dolda2000.com>
Fri, 20 Mar 2015 05:14:10 +0000 (06:14 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Fri, 20 Mar 2015 05:14:10 +0000 (06:14 +0100)
didex/index.py

index a1ca061..4d42506 100644 (file)
@@ -1,4 +1,4 @@
-import struct, contextlib
+import struct, contextlib, math
 from . import db, lib
 from .db import bd, txnfun
 
 from . import db, lib
 from .db import bd, txnfun
 
@@ -47,7 +47,25 @@ class maybe(object):
         else:
             return self.bk.compare(a[1:], b[1:])
 
         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):
 
 class index(object):
     def __init__(self, db, name, datatype):