major improvement of HashDB speed in multi-threaded mode

This commit is contained in:
Miroslav Stampar 2011-11-22 10:09:35 +00:00
parent e94efff187
commit b117c40aa5
3 changed files with 29 additions and 0 deletions

View File

@ -65,6 +65,13 @@ def getCurrentThreadData():
return ThreadData return ThreadData
def getCurrentThreadName():
"""
Returns current's thread name
"""
return threading.current_thread().getName()
def exceptionHandledFunction(threadFunction): def exceptionHandledFunction(threadFunction):
try: try:
threadFunction() threadFunction()

View File

@ -9,14 +9,18 @@ See the file 'doc/COPYING' for copying permission
import hashlib import hashlib
import sqlite3 import sqlite3
import threading
from lib.core.data import conf from lib.core.data import conf
from lib.core.settings import UNICODE_ENCODING from lib.core.settings import UNICODE_ENCODING
from lib.core.threads import getCurrentThreadData from lib.core.threads import getCurrentThreadData
from lib.core.threads import getCurrentThreadName
class HashDB(object): class HashDB(object):
def __init__(self, filepath): def __init__(self, filepath):
self.filepath = filepath self.filepath = filepath
self._write_cache = {}
self._cache_lock = threading.Lock()
def _get_cursor(self): def _get_cursor(self):
threadData = getCurrentThreadData() threadData = getCurrentThreadData()
@ -64,6 +68,21 @@ class HashDB(object):
def write(self, key, value): def write(self, key, value):
if key: if key:
hash_ = HashDB.hashKey(key) hash_ = HashDB.hashKey(key)
self._cache_lock.acquire()
self._write_cache[hash_] = value
self._cache_lock.release()
if getCurrentThreadName() in ('0', 'MainThread'):
self.flush()
def flush(self):
self._cache_lock.acquire()
items = self._write_cache.items()
self._write_cache.clear()
self._cache_lock.release()
self.beginTransaction()
for hash_, value in items:
while True: while True:
try: try:
try: try:
@ -75,6 +94,7 @@ class HashDB(object):
raise raise
else: else:
break break
self.endTransaction()
def beginTransaction(self): def beginTransaction(self):
self.cursor.execute('BEGIN TRANSACTION') self.cursor.execute('BEGIN TRANSACTION')

View File

@ -1564,6 +1564,7 @@ class Enumeration:
try: try:
kb.dumpMode = True kb.dumpMode = True
#conf.hashDB.beginTransaction()
if not safeSQLIdentificatorNaming(conf.db) in kb.data.cachedColumns \ if not safeSQLIdentificatorNaming(conf.db) in kb.data.cachedColumns \
or safeSQLIdentificatorNaming(tbl, True) not in \ or safeSQLIdentificatorNaming(tbl, True) not in \
@ -1788,6 +1789,7 @@ class Enumeration:
finally: finally:
kb.dumpMode = False kb.dumpMode = False
#conf.hashDB.endTransaction()
def dumpAll(self): def dumpAll(self):
if conf.db is not None and conf.tbl is None: if conf.db is not None and conf.tbl is None: