changeset 337:57a3051ee435

storage (pgsql): added getLastItems method, to get last items from a series of nodes and entities (needed for PEP compliance).
author Goffi <goffi@goffi.org>
date Sat, 12 Aug 2017 18:11:36 +0200
parents a468c187bbb5
children 6d059f07c2d3
files sat_pubsub/pgsql_storage.py
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/sat_pubsub/pgsql_storage.py	Sat Aug 12 18:10:14 2017 +0200
+++ b/sat_pubsub/pgsql_storage.py	Sat Aug 12 18:11:36 2017 +0200
@@ -338,6 +338,38 @@
         return self.defaultConfig[nodeType]
 
 
+    def formatLastItems(self, result):
+        last_items = []
+        for pep_jid_s, node, data, item_access_model in result:
+            pep_jid = jid.JID(pep_jid_s)
+            item = generic.stripNamespace(parseXml(data))
+            last_items.append((pep_jid, node, item, item_access_model))
+        return last_items
+
+
+    def getLastItems(self, entities, nodes, node_accesses, item_accesses, pep):
+        """get last item for several nodes and entities in a single request"""
+        if not entities or not nodes or not node_accesses or not item_accesses:
+            raise ValueError("entities, nodes and accesses must not be empty")
+        if node_accesses != ('open',) or item_accesses != ('open',):
+            raise NotImplementedError('only "open" access model is handled for now')
+        if not pep:
+            raise NotImplementedError(u"getLastItems is only implemented for PEP at the moment")
+        d = self.dbpool.runQuery("""SELECT DISTINCT ON (node_id) pep, node, data, items.access_model
+                                    FROM items
+                                    NATURAL JOIN nodes
+                                    WHERE nodes.pep IN %s
+                                    AND node IN %s
+                                    AND nodes.access_model in %s
+                                    AND items.access_model in %s
+                                    ORDER BY node_id DESC, item_id DESC""",
+                                 (tuple([e.userhost() for e in entities]),
+                                  nodes,
+                                  node_accesses,
+                                  item_accesses))
+        d.addCallback(self.formatLastItems)
+        return d
+
 
 class Node: