From 95ad000f6211cf804db9ce6f542923c6d10e5752 Mon Sep 17 00:00:00 2001 From: Goffi Date: Wed, 25 Nov 2015 21:41:15 +0100 Subject: [PATCH] core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load --- src/memory/disco.py | 43 +++++++++++++++++++++++++++++++++++++++++-- src/memory/memory.py | 1 + 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/memory/disco.py b/src/memory/disco.py index 2d2e89ff..4fb18304 100755 --- a/src/memory/disco.py +++ b/src/memory/disco.py @@ -27,6 +27,8 @@ from twisted.internet import defer 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 @@ class ByteIdentity(object): 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 --git a/src/memory/memory.py b/src/memory/memory.py index c23614ea..96789ef0 100644 --- a/src/memory/memory.py +++ b/src/memory/memory.py @@ -246,6 +246,7 @@ class Memory(object): 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 ##