core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load

This commit is contained in:
Goffi 2015-11-25 21:41:15 +01:00
commit 95ad000f62
2 changed files with 42 additions and 2 deletions

View file

@ -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):

View file

@ -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 ##