2015-08-20 17:27:15 +02:00
|
|
|
import UserDict
|
|
|
|
import time
|
|
|
|
import constants
|
2017-10-10 19:08:22 +02:00
|
|
|
from interface import IDataStore
|
|
|
|
from zope.interface import implements
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2017-10-10 19:08:22 +02:00
|
|
|
class DictDataStore(UserDict.DictMixin):
|
2015-08-20 17:27:15 +02:00
|
|
|
""" A datastore using an in-memory Python dictionary """
|
2017-10-10 19:08:22 +02:00
|
|
|
implements(IDataStore)
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def __init__(self):
|
|
|
|
# Dictionary format:
|
|
|
|
# { <key>: (<value>, <lastPublished>, <originallyPublished> <originalPublisherID>) }
|
|
|
|
self._dict = {}
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
""" Return a list of the keys in this data store """
|
|
|
|
return self._dict.keys()
|
|
|
|
|
|
|
|
def removeExpiredPeers(self):
|
|
|
|
now = int(time.time())
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def notExpired(peer):
|
|
|
|
if (now - peer[2]) > constants.dataExpireTimeout:
|
|
|
|
return False
|
|
|
|
return True
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
for key in self._dict.keys():
|
|
|
|
unexpired_peers = filter(notExpired, self._dict[key])
|
|
|
|
self._dict[key] = unexpired_peers
|
|
|
|
|
|
|
|
def hasPeersForBlob(self, key):
|
|
|
|
if key in self._dict and len(self._dict[key]) > 0:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def addPeerToBlob(self, key, value, lastPublished, originallyPublished, originalPublisherID):
|
|
|
|
if key in self._dict:
|
|
|
|
self._dict[key].append((value, lastPublished, originallyPublished, originalPublisherID))
|
|
|
|
else:
|
|
|
|
self._dict[key] = [(value, lastPublished, originallyPublished, originalPublisherID)]
|
|
|
|
|
|
|
|
def getPeersForBlob(self, key):
|
|
|
|
if key in self._dict:
|
|
|
|
return [val[0] for val in self._dict[key]]
|