Fixed some index bugs.
[didex.git] / didex / values.py
index 0e7bddc..8479d42 100644 (file)
@@ -1,7 +1,9 @@
 import threading
-from . import store, lib
+from . import store, lib, index
 from .store import storedesc
 
+__all__ = ["simple", "multi", "compound"]
+
 class cursor(lib.closable):
     def __init__(self, bk, st):
         self.bk = bk
@@ -21,16 +23,13 @@ class cursor(lib.closable):
         self.bk.skip(n)
 
 class base(storedesc):
-    def __init__(self, store, indextype, name, datatype, default):
+    def __init__(self, store, indextype, name, datatype):
         self.store = store
         self.indextype = indextype
         self.name = name
         self.typ = datatype
-        self.default = default
         self.idx = None
         self.lk = threading.Lock()
-        self.mattr = "__idx_%s_new" % name
-        self.iattr = "__idx_%s_cur" % name
 
     def index(self):
         with self.lk:
@@ -38,6 +37,16 @@ class base(storedesc):
                 self.idx = self.indextype(self.store.db(), self.name, self.typ)
             return self.idx
 
+    def get(self, **kwargs):
+        return cursor(self.index().get(**kwargs), self.store)
+
+class descbase(base):
+    def __init__(self, store, indextype, name, datatype, default):
+        super().__init__(store, indextype, name, datatype)
+        self.default = default
+        self.mattr = "__idx_%s_new" % name
+        self.iattr = "__idx_%s_cur" % name
+
     def __get__(self, obj, cls):
         if obj is None: return self
         return getattr(obj, self.mattr, self.default)
@@ -48,10 +57,7 @@ class base(storedesc):
     def __delete__(self, obj):
         delattr(obj, self.mattr)
 
-    def get(self, **kwargs):
-        return cursor(self.index().get(**kwargs), self.store)
-
-class simple(base):
+class simple(descbase):
     def __init__(self, store, indextype, name, datatype, default=None):
         super().__init__(store, indextype, name, datatype, default)
 
@@ -73,7 +79,7 @@ class simple(base):
             idx.put(val, id, tx=tx)
             tx.postcommit(lambda: setattr(obj, self.iattr, val))
 
-class multi(base):
+class multi(descbase):
     def __init__(self, store, indextype, name, datatype):
         super().__init__(store, indextype, name, datatype, ())
 
@@ -100,3 +106,27 @@ class multi(base):
             for val in vals - ivals:
                 idx.put(val, id, tx=tx)
             tx.postcommit(lambda: setattr(obj, self.iattr, vals))
+
+class compound(base):
+    def __init__(self, indextype, name, *parts):
+        super().__init__(parts[0].store, indextype, name, index.compound(*(part.typ for part in parts)))
+        self.parts = parts
+        self.iattr = "__idx_%s_cur" % name
+
+    def register(self, id, obj, tx):
+        val = tuple(part.__get__(obj, None) for part in self.parts)
+        self.index().put(val, id, tx=tx)
+        tx.postcommit(lambda: setattr(obj, self.iattr, val))
+
+    def unregister(self, id, obj, tx):
+        self.index().remove(getattr(obj, self.iattr), id, tx=tx)
+        tx.postcommit(lambda: delattr(obj, self.iattr))
+
+    def update(self, id, obj, tx):
+        val = tuple(part.__get__(obj, None) for part in self.parts)
+        ival = getattr(obj, self.iattr)
+        if val != ival:
+            idx = self.index()
+            idx.remove(ival, id, tx=tx)
+            idx.put(val, id, tx=tx)
+            tx.postcommit(lambda: setattr(obj, self.iattr, val))