diff sat/memory/sqla.py @ 3744:658ddbabaf36

core (memory/sqla): new table/mapping to handle Pubsub node subscriptions: node subscriptions can now be cached, this can be useful for components which must keep track of subscibers. rel 364
author Goffi <goffi@goffi.org>
date Tue, 22 Mar 2022 17:00:42 +0100
parents 40a6374fcd44
children 10b71e3526bd
line wrap: on
line diff
--- a/sat/memory/sqla.py	Tue Mar 22 17:00:42 2022 +0100
+++ b/sat/memory/sqla.py	Tue Mar 22 17:00:42 2022 +0100
@@ -111,7 +111,7 @@
         self.initialized = defer.Deferred()
         # we keep cache for the profiles (key: profile name, value: profile id)
         # profile id to name
-        self.profiles: Dict[int, str] = {}
+        self.profiles: Dict[str, int] = {}
         # profile id to component entry point
         self.components: Dict[int, str] = {}
 
@@ -1015,6 +1015,7 @@
         service: jid.JID,
         name: str,
         with_items: bool = False,
+        with_subscriptions: bool = False,
     ) -> Optional[PubsubNode]:
         """
         """
@@ -1031,6 +1032,10 @@
                 stmt = stmt.options(
                     joinedload(PubsubNode.items)
                 )
+            if with_subscriptions:
+                stmt = stmt.options(
+                    joinedload(PubsubNode.subscriptions)
+                )
             result = await session.execute(stmt)
         return result.unique().scalar_one_or_none()
 
@@ -1043,15 +1048,17 @@
         analyser: Optional[str] = None,
         type_: Optional[str] = None,
         subtype: Optional[str] = None,
+        subscribed: bool = False,
     ) -> PubsubNode:
         node = PubsubNode(
             profile_id=self.profiles[client.profile],
             service=service,
             name=name,
-            subscribed=False,
+            subscribed=subscribed,
             analyser=analyser,
             type_=type_,
             subtype=subtype,
+            subscriptions=[],
         )
         async with self.session() as session:
             async with session.begin():
@@ -1187,6 +1194,9 @@
             "type_": types,
             "subtype": subtypes,
         }
+        if profiles is not None:
+            node_fields["profile_id"] = [self.profiles[p] for p in profiles]
+
         if any(x is not None for x in node_fields.values()):
             sub_q = select(PubsubNode.id)
             for col, values in node_fields.items():
@@ -1199,11 +1209,6 @@
                 .execution_options(synchronize_session=False)
             )
 
-        if profiles is not None:
-            stmt = stmt.where(
-                PubsubItem.profile_id.in_([self.profiles[p] for p in profiles])
-            )
-
         if created_before is not None:
             stmt = stmt.where(PubsubItem.created < created_before)