""" Sqlite Inventory """ import sqlite3 import time from threading import RLock from helper_sql import sqlQuery, SqlBulkExecute, sqlExecute from storage.storage import InventoryStorage, InventoryItem class SqliteInventory(InventoryStorage): # pylint: disable=too-many-ancestors """Inventory using SQLite""" def __init__(self): # import pdb;pdb.set_trace() super(SqliteInventory, self).__init__() # of objects (like msg payloads and pubkey payloads) # Does not include protocol headers (the first 24 bytes of each packet). self._inventory = {} # 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._objects = {} # Guarantees that two receiveDataThreads don't receive # and process the same message concurrently # (probably sent by a malicious individual) self.lock = RLock() def __contains__(self, hash_): print('__contains__(self, hash_)__contains__(self, hash_)__contains__(self, hash_) ',hash_) hash_ = str(hash_).encode() if type(hash_) == int else hash_ with self.lock: if hash_ in self._objects: return True rows = sqlQuery( 'SELECT streamnumber FROM inventory WHERE hash=?', sqlite3.Binary(hash_)) if not rows: return False self._objects[hash_] = rows[0][0] return True # def __getitem__(self, hash_): # raw = [None] # # some think broke # if hash_ == 0: # hash_ = bytes() # with self.lock: # try: # if hash_ in self._inventory: # return self._inventory[hash_] # rows = sqlQuery( # 'SELECT objecttype, streamnumber, payload, expirestime, tag FROM inventory WHERE hash=?', # sqlite3.Binary(hash_)) # if not rows: # # raise KeyError(hash_) # pass # except: # rows = [hash_] # return InventoryItem(*rows[0]) def __getitem__(self, hash_): # import pdb;pdb.set_trace() with self.lock: if hash_ in self._inventory: return self._inventory[hash_] rows = sqlQuery( 'SELECT objecttype, streamnumber, payload, expirestime, tag' ' FROM inventory WHERE hash=?', sqlite3.Binary(bytes(hash_))) if not rows: raise KeyError(hash_) return InventoryItem(*rows[0]) def __setitem__(self, hash_, value): print('----------__setitem__------------------') with self.lock: value = InventoryItem(*value) self._inventory[hash_] = value self._objects[hash_] = value.stream def __delitem__(self, hash_): print('----------__delitem__------------------') raise NotImplementedError def __iter__(self): print('----------__iter__------------------') with self.lock: hashes = self._inventory.keys()[:] hashes += (x for x, in sqlQuery('SELECT hash FROM inventory')) return hashes.__iter__() def __len__(self): print('----------__len__------------------') with self.lock: return len(self._inventory) + sqlQuery( 'SELECT count(*) FROM inventory')[0][0] def by_type_and_tag(self, objectType, tag): """Return objects filtered by object type and tag""" with self.lock: values = [value for value in self._inventory.values() if value.type == objectType and value.tag == tag] values += (InventoryItem(*value) for value in sqlQuery( 'SELECT objecttype, streamnumber, payload, expirestime, tag' ' FROM inventory WHERE objecttype=? AND tag=?', objectType, sqlite3.Binary(tag))) return values def unexpired_hashes_by_stream(self, stream): """Return unexpired inventory vectors filtered by stream""" # print ('self._inventory.items() self._inventory.items() self._inventory.items()' ,self._inventory.items()) # import pdb;pdb.set_trace() with self.lock: t = int(time.time()) hashes = [x for x, value in self._inventory.items() if value.stream == stream and value.expires > t] # print ('hasheshasheshasheshasheshasheshasheshasheshashes',hashes) hashes += (payload for payload, in sqlQuery( 'SELECT hash FROM inventory WHERE streamnumber=?' ' AND expirestime>?', stream, t)) # print ('hasheshasheshasheshasheshasheshasheshasheshashes aaaaaaaaffter',hashes) return hashes def flush(self): """Flush cache""" with self.lock: # If you use both the inventoryLock and the sqlLock, # always use the inventoryLock OUTSIDE of the sqlLock. with SqlBulkExecute() as sql: for objectHash, value in self._inventory.items(): sql.execute( 'INSERT INTO inventory VALUES (?, ?, ?, ?, ?, ?)', sqlite3.Binary(objectHash), *value) self._inventory.clear() def clean(self): """Free memory / perform garbage collection""" with self.lock: sqlExecute( 'DELETE FROM inventory WHERE expirestime