- caching of whether an object exists in inventory was somehow removed
since storage refactoring (or it never worked). Now existence checking
is cached in the sqlite storage backend
@ -11,14 +11,18 @@ class SqliteInventory(InventoryStorage):
def__init__(self):
super(self.__class__,self).__init__()
self._inventory={}#of objects (like msg payloads and pubkey payloads) Does not include protocol headers (the first 24 bytes of each packet).
self._streams =collections.defaultdict(set)# key = streamNumer, value = a set which holds the inventory object hashes that we are aware of. This is used whenever we receive an inv message from a peer to check to see what items are new to us. We don't delete things out of it; instead, the singleCleaner thread clears and refills it every couple hours.
self._objects ={}# cache for existing objects, used for quick lookups if we have an object. This is used for example whenever we receive an inv message from a peer to check to see what items are new to us. We don't delete things out of it; instead, the singleCleaner thread clears and refills it.
self.lock=RLock()# Guarantees that two receiveDataThreads don't receive and process the same message concurrently (probably sent by a malicious individual)
def__contains__(self,hash):
withself.lock:
ifhashinself._inventory:
ifhashinself._objects:
returnTrue
returnbool(sqlQuery('SELECT 1 FROM inventory WHERE hash=?',sqlite3.Binary(hash)))
rows=sqlQuery('SELECT streamnumber FROM inventory WHERE hash=?',sqlite3.Binary(hash))
ifnotrows:
returnFalse
self._objects[hash]=rows[0][0]
returnTrue
def__getitem__(self,hash):
withself.lock:
@ -33,7 +37,7 @@ class SqliteInventory(InventoryStorage):
withself.lock:
value=InventoryItem(*value)
self._inventory[hash]=value
self._streams[value.stream].add(hash)
self._objects[hash]=value.stream
def__delitem__(self,hash):
raiseNotImplementedError
@ -54,10 +58,6 @@ class SqliteInventory(InventoryStorage):
values+=(InventoryItem(*value)forvalueinsqlQuery('SELECT objecttype, streamnumber, payload, expirestime, tag FROM inventory WHERE objecttype=? AND tag=?',objectType,sqlite3.Binary(tag)))
returnvalues
defhashes_by_stream(self,stream):
withself.lock:
returnself._streams[stream]
defunexpired_hashes_by_stream(self,stream):
withself.lock:
t=int(time.time())
@ -75,7 +75,7 @@ class SqliteInventory(InventoryStorage):
defclean(self):
withself.lock:
sqlExecute('DELETE FROM inventory WHERE expirestime<?',int(time.time())-(60*60*3))