diff src/memory/disco.py @ 1684:373ce871b0f3

core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
author Goffi <goffi@goffi.org>
date Wed, 25 Nov 2015 21:41:15 +0100
parents fd143578fe89
children cbcc223c323a
line wrap: on
line diff
--- a/src/memory/disco.py	Wed Nov 25 20:08:33 2015 +0100
+++ b/src/memory/disco.py	Wed Nov 25 21:41:15 2015 +0100
@@ -27,6 +27,8 @@
 from twisted.internet import reactor
 from twisted.python import failure
 from sat.core.constants import Const as C
+from sat.tools import xml_tools
+from sat.memory import persistent
 from wokkel import disco
 from base64 import b64encode
 from hashlib import sha1
@@ -55,13 +57,50 @@
         return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name)
 
 
+class HashManager(object):
+    """map object which manage hashes
+
+    persistent storage is update when a new hash is added
+    """
+
+    def __init__(self, persistent):
+        self.hashes = {}
+        self.persistent = persistent
+
+    def __getitem__(self, key):
+        return self.hashes[key]
+
+    def __setitem__(self, hash_, disco_info):
+        if hash_ in self.hashes:
+            log.debug(u"ignoring hash set: it is already known")
+            return
+        self.hashes[hash_] = disco_info
+        self.persistent[hash_] = disco_info.toElement().toXml()
+
+    def __contains__(self, hash_):
+        return self.hashes.__contains__(hash_)
+
+    def load(self):
+        def fillHashes(hashes):
+            for hash_, xml in hashes.iteritems():
+                element = xml_tools.ElementParser()(xml)
+                self.hashes[hash_] = disco.DiscoInfo.fromElement(element)
+            log.info(u"Disco hashes loaded")
+        d = self.persistent.load()
+        d.addCallback(fillHashes)
+        return d
+
 class Discovery(object):
     """ Manage capabilities of entities """
 
     def __init__(self, host):
         self.host = host
-        self.hashes = {} # key: capability hash, value: disco features/identities
-        # TODO: save hashes in databse, remove legacy hashes
+        # TODO: remove legacy hashes
+
+    def load(self):
+        """Load persistent hashes"""
+        self.hashes = HashManager(persistent.PersistentDict("disco"))
+        return self.hashes.load()
 
     @defer.inlineCallbacks
     def hasFeature(self, feature, jid_=None, profile=C.PROF_KEY_NONE):