2018-07-03 06:51:25 +02:00
|
|
|
from collections import UserDict
|
|
|
|
from . import constants
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2018-07-03 06:51:25 +02:00
|
|
|
class DictDataStore(UserDict):
|
2015-08-20 17:27:15 +02:00
|
|
|
""" A datastore using an in-memory Python dictionary """
|
2018-07-03 06:51:25 +02:00
|
|
|
#implements(IDataStore)
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2018-05-23 22:53:35 +02:00
|
|
|
def __init__(self, getTime=None):
|
2015-08-20 17:27:15 +02:00
|
|
|
# Dictionary format:
|
2018-05-29 17:04:13 +02:00
|
|
|
# { <key>: (<contact>, <value>, <lastPublished>, <originallyPublished> <originalPublisherID>) }
|
2015-08-20 17:27:15 +02:00
|
|
|
self._dict = {}
|
2018-05-23 22:53:35 +02:00
|
|
|
if not getTime:
|
|
|
|
from twisted.internet import reactor
|
|
|
|
getTime = reactor.seconds
|
|
|
|
self._getTime = getTime
|
2018-06-29 18:01:46 +02:00
|
|
|
self.completed_blobs = set()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
""" Return a list of the keys in this data store """
|
|
|
|
return self._dict.keys()
|
|
|
|
|
2018-05-24 21:52:37 +02:00
|
|
|
def filter_bad_and_expired_peers(self, key):
|
|
|
|
"""
|
|
|
|
Returns only non-expired and unknown/good peers
|
|
|
|
"""
|
|
|
|
return filter(
|
|
|
|
lambda peer:
|
|
|
|
self._getTime() - peer[3] < constants.dataExpireTimeout and peer[0].contact_is_good is not False,
|
|
|
|
self._dict[key]
|
|
|
|
)
|
|
|
|
|
|
|
|
def filter_expired_peers(self, key):
|
|
|
|
"""
|
|
|
|
Returns only non-expired peers
|
|
|
|
"""
|
2018-05-29 17:04:13 +02:00
|
|
|
return filter(lambda peer: self._getTime() - peer[3] < constants.dataExpireTimeout, self._dict[key])
|
2018-05-24 21:52:37 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def removeExpiredPeers(self):
|
|
|
|
for key in self._dict.keys():
|
2018-05-24 21:52:37 +02:00
|
|
|
unexpired_peers = self.filter_expired_peers(key)
|
2018-05-24 16:23:22 +02:00
|
|
|
if not unexpired_peers:
|
|
|
|
del self._dict[key]
|
|
|
|
else:
|
|
|
|
self._dict[key] = unexpired_peers
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def hasPeersForBlob(self, key):
|
2018-05-29 17:04:13 +02:00
|
|
|
return True if key in self._dict and len(self.filter_bad_and_expired_peers(key)) else False
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2018-05-24 21:52:37 +02:00
|
|
|
def addPeerToBlob(self, contact, key, compact_address, lastPublished, originallyPublished, originalPublisherID):
|
2015-08-20 17:27:15 +02:00
|
|
|
if key in self._dict:
|
2018-05-24 21:52:37 +02:00
|
|
|
if compact_address not in map(lambda store_tuple: store_tuple[1], self._dict[key]):
|
2018-05-29 22:50:23 +02:00
|
|
|
self._dict[key].append(
|
|
|
|
(contact, compact_address, lastPublished, originallyPublished, originalPublisherID)
|
|
|
|
)
|
2015-08-20 17:27:15 +02:00
|
|
|
else:
|
2018-05-24 21:52:37 +02:00
|
|
|
self._dict[key] = [(contact, compact_address, lastPublished, originallyPublished, originalPublisherID)]
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def getPeersForBlob(self, key):
|
2018-05-24 21:52:37 +02:00
|
|
|
return [] if key not in self._dict else [val[1] for val in self.filter_bad_and_expired_peers(key)]
|
2017-10-10 19:27:44 +02:00
|
|
|
|
2018-05-24 21:52:37 +02:00
|
|
|
def getStoringContacts(self):
|
|
|
|
contacts = set()
|
|
|
|
for key in self._dict:
|
|
|
|
for values in self._dict[key]:
|
|
|
|
contacts.add(values[0])
|
|
|
|
return list(contacts)
|