Skip to content
Snippets Groups Projects
Commit 0b2cabdd authored by inso's avatar inso
Browse files

Working on cache problem #176

parent 1f883dc5
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ import json ...@@ -8,6 +8,7 @@ import json
import asyncio import asyncio
import random import random
class BmaAccess(QObject): class BmaAccess(QObject):
""" """
This class is used to access BMA API. This class is used to access BMA API.
...@@ -84,40 +85,14 @@ class BmaAccess(QObject): ...@@ -84,40 +85,14 @@ class BmaAccess(QObject):
:return: True if the json dicts are the same :return: True if the json dicts are the same
:rtype: bool :rtype: bool
""" """
if first is not None: def ordered(obj):
if not isinstance(first, type(second)): if isinstance(obj, dict):
return False return sorted((k, ordered(v)) for k, v in obj.items())
if isinstance(first, dict): if isinstance(obj, list):
for key in first: return sorted(ordered(x) for x in obj)
if isinstance(second, dict):
if key in second:
sec = second[key]
else:
# there are key in the first, that is not presented in the second
return False
# recursive call
return self._compare_json(first[key], sec)
else:
# second is not dict
return False
# if object is list, loop over it and check.
elif isinstance(first, list):
for (index, item) in enumerate(first):
# try to get the same index from second
sec = None
if second is not None:
try:
sec = second[index]
except (IndexError, KeyError):
# goes to difference
return False
# recursive call
return self._compare_json(first[index], sec)
# not list, not dict. check for equality
elif first != second:
return False
else: else:
return True return obj
return ordered(first) == ordered(second)
def _get_from_cache(self, request, req_args, get_args): def _get_from_cache(self, request, req_args, get_args):
""" """
...@@ -126,17 +101,22 @@ class BmaAccess(QObject): ...@@ -126,17 +101,22 @@ class BmaAccess(QObject):
:param cache_key: The key :param cache_key: The key
:return: :return:
""" """
if request == blockchain.UD:
pass
cache_key = BmaAccess._gen_cache_key(request, req_args, get_args) cache_key = BmaAccess._gen_cache_key(request, req_args, get_args)
if cache_key in self._data.keys(): if cache_key in self._data.keys():
cached_data = self._data[cache_key] cached_data = self._data[cache_key]
need_reload = False need_reload = False
if 'metadata' in cached_data: if 'metadata' in cached_data:
if str(request) not in BmaAccess.__saved_requests \ if 'block' not in cached_data['metadata']:
need_reload = False
elif str(request) not in BmaAccess.__saved_requests \
and cached_data['metadata']['block'] < self._network.latest_block: and cached_data['metadata']['block'] < self._network.latest_block:
need_reload = True need_reload = True
else: else:
need_reload = True need_reload = True
ret_data = self._data[cache_key]['value'] ret_data = cached_data['value']
else: else:
need_reload = True need_reload = True
ret_data = request.null_value ret_data = request.null_value
...@@ -181,6 +161,7 @@ class BmaAccess(QObject): ...@@ -181,6 +161,7 @@ class BmaAccess(QObject):
:return: The cached data :return: The cached data
:rtype: dict :rtype: dict
""" """
data = self._get_from_cache(request, req_args, get_args) data = self._get_from_cache(request, req_args, get_args)
need_reload = data[0] need_reload = data[0]
ret_data = data[1] ret_data = data[1]
...@@ -203,6 +184,8 @@ class BmaAccess(QObject): ...@@ -203,6 +184,8 @@ class BmaAccess(QObject):
@pyqtSlot(int, dict, dict, int) @pyqtSlot(int, dict, dict, int)
def handle_reply(self, request, req_args, get_args, tries): def handle_reply(self, request, req_args, get_args, tries):
if request == blockchain.UD:
pass
reply = self.sender() reply = self.sender()
logging.debug("Handling QtNetworkReply for {0}".format(str(request))) logging.debug("Handling QtNetworkReply for {0}".format(str(request)))
cache_key = BmaAccess._gen_cache_key(request, req_args, get_args) cache_key = BmaAccess._gen_cache_key(request, req_args, get_args)
...@@ -238,6 +221,7 @@ class BmaAccess(QObject): ...@@ -238,6 +221,7 @@ class BmaAccess(QObject):
:return: The future data :return: The future data
:rtype: dict :rtype: dict
""" """
def handle_future_reply(reply): def handle_future_reply(reply):
if reply.error() == QNetworkReply.NoError: if reply.error() == QNetworkReply.NoError:
strdata = bytes(reply.readAll()).decode('utf-8') strdata = bytes(reply.readAll()).decode('utf-8')
......
...@@ -193,6 +193,7 @@ class Identity(QObject): ...@@ -193,6 +193,7 @@ class Identity(QObject):
:return: The list of the certifiers of this community in BMA json format :return: The list of the certifiers of this community in BMA json format
""" """
certifiers = community.bma_access.get(self, qtbma.wot.CertifiersOf, {'search': self.pubkey}) certifiers = community.bma_access.get(self, qtbma.wot.CertifiersOf, {'search': self.pubkey})
if certifiers == qtbma.wot.CertifiersOf.null_value: if certifiers == qtbma.wot.CertifiersOf.null_value:
logging.debug('bma.wot.CertifiersOf request error') logging.debug('bma.wot.CertifiersOf request error')
data = community.bma_access.get(self, qtbma.wot.Lookup, {'search': self.pubkey}) data = community.bma_access.get(self, qtbma.wot.Lookup, {'search': self.pubkey})
...@@ -218,8 +219,8 @@ class Identity(QObject): ...@@ -218,8 +219,8 @@ class Identity(QObject):
certifier['cert_time']['medianTime'] = community.get_block( certifier['cert_time']['medianTime'] = community.get_block(
certifier_data['meta']['block_number'])['medianTime'] certifier_data['meta']['block_number'])['medianTime']
certifiers.append(certifier) certifiers.append(certifier)
return certifiers return certifiers
return certifiers['certifications'] return certifiers['certifications']
def unique_valid_certifiers_of(self, community): def unique_valid_certifiers_of(self, community):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment