Mercurial > libervia-backend
changeset 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 | 9bf1262297f2 |
children | 43a127b6a4f2 |
files | src/memory/disco.py src/memory/memory.py |
diffstat | 2 files changed, 42 insertions(+), 2 deletions(-) [+] |
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):
--- 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 ##