Added SqlBulkExecute class so we can update inventory without a million commits

This commit is contained in:
Grant T. Olson 2013-08-31 10:40:11 -04:00
parent b83781cefb
commit 8d8e43b1fc
3 changed files with 48 additions and 18 deletions

View File

@ -30,10 +30,12 @@ class singleCleaner(threading.Thread):
while True: while True:
shared.UISignalQueue.put(( shared.UISignalQueue.put((
'updateStatusBar', 'Doing housekeeping (Flushing inventory in memory to disk...)')) 'updateStatusBar', 'Doing housekeeping (Flushing inventory in memory to disk...)'))
with SqlBulkExecute() as sql:
for hash, storedValue in shared.inventory.items(): for hash, storedValue in shared.inventory.items():
objectType, streamNumber, payload, receivedTime = storedValue objectType, streamNumber, payload, receivedTime = storedValue
if int(time.time()) - 3600 > receivedTime: if int(time.time()) - 3600 > receivedTime:
sqlExecute( sql.execute(
'''INSERT INTO inventory VALUES (?,?,?,?,?,?)''', '''INSERT INTO inventory VALUES (?,?,?,?,?,?)''',
hash, hash,
objectType, objectType,

View File

@ -36,3 +36,31 @@ def sqlStoredProcedure(procName):
sqlLock.acquire() sqlLock.acquire()
sqlSubmitQueue.put(procName) sqlSubmitQueue.put(procName)
sqlLock.release() sqlLock.release()
class SqlBulkExecute:
def __enter__(self):
sqlLock.acquire()
return self
def __exit__(self, type, value, traceback):
sqlSubmitQueue.put('commit')
sqlLock.release()
def execute(self, sqlStatement, *args):
sqlSubmitQueue.put(sqlStatement)
if args == ():
sqlSubmitQueue.put('')
else:
sqlSubmitQueue.put(args)
sqlReturnQueue.get()
def query(self, sqlStatement, *args):
sqlSubmitQueue.put(sqlStatement)
if args == ():
sqlSubmitQueue.put('')
else:
sqlSubmitQueue.put(args)
return sqlReturnQueue.get()

View File

@ -305,10 +305,10 @@ def broadcastToSendDataQueues(data):
def flushInventory(): def flushInventory():
#Note that the singleCleanerThread clears out the inventory dictionary from time to time, although it only clears things that have been in the dictionary for a long time. This clears the inventory dictionary Now. #Note that the singleCleanerThread clears out the inventory dictionary from time to time, although it only clears things that have been in the dictionary for a long time. This clears the inventory dictionary Now.
with SqlBulkExecute() as sql:
for hash, storedValue in inventory.items(): for hash, storedValue in inventory.items():
objectType, streamNumber, payload, receivedTime = storedValue objectType, streamNumber, payload, receivedTime = storedValue
t = () sql.execute('''INSERT INTO inventory VALUES (?,?,?,?,?,?)''',
sqlExecute('''INSERT INTO inventory VALUES (?,?,?,?,?,?)''',
hash,objectType,streamNumber,payload,receivedTime,'') hash,objectType,streamNumber,payload,receivedTime,'')
del inventory[hash] del inventory[hash]