# HG changeset patch # User Goffi # Date 1448484075 -3600 # Node ID 373ce871b0f3b39e5157e08684e07d12e7fcd1b4 # Parent 9bf1262297f2ee4dca79325df8c3730013d7ee58 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load diff -r 9bf1262297f2 -r 373ce871b0f3 src/memory/disco.py --- 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): diff -r 9bf1262297f2 -r 373ce871b0f3 src/memory/memory.py --- a/src/memory/memory.py Wed Nov 25 20:08:33 2015 +0100 +++ b/src/memory/memory.py Wed Nov 25 21:41:15 2015 +0100 @@ -246,6 +246,7 @@ d = self.storage.initialized.addCallback(lambda ignore: self.load()) self.memory_data = PersistentDict("memory") d.addCallback(lambda ignore: self.memory_data.load()) + d.addCallback(lambda ignore: self.disco.load()) d.chainDeferred(self.initialized) ## Configuration ##