This commit is contained in:
Miroslav Stampar 2011-09-26 13:36:08 +00:00
parent b3b4459c72
commit fd9acfd7d2
2 changed files with 17 additions and 18 deletions

View File

@ -34,6 +34,7 @@ class _ThreadData(threading.local):
global shared
self.disableStdOut = False
self.hashDBCursor = None
self.lastErrorPage = None
self.lastHTTPError = None
self.lastRedirectMsg = None

View File

@ -11,12 +11,23 @@ import hashlib
import sqlite3
from lib.core.settings import UNICODE_ENCODING
from lib.core.threads import getCurrentThreadData
class HashDB:
class HashDB(object):
def __init__(self, filepath):
self.connection = sqlite3.connect(filepath)
self.cursor = self.connection.cursor()
self.cursor.execute("CREATE TABLE IF NOT EXISTS storage (id INTEGER PRIMARY KEY, value TEXT)")
self.filepath = filepath
def _get_cursor(self):
threadData = getCurrentThreadData()
if threadData.hashDBCursor is None:
connection = sqlite3.connect(self.filepath, isolation_level=None)
threadData.hashDBCursor = connection.cursor()
threadData.hashDBCursor.execute("CREATE TABLE IF NOT EXISTS storage (id INTEGER PRIMARY KEY, value TEXT)")
return threadData.hashDBCursor
cursor = property(_get_cursor)
def __del__(self):
self.close()
@ -24,7 +35,7 @@ class HashDB:
def close(self):
try:
self.endTransaction()
self.connection.close()
self.cursor.connection.close()
except:
pass
@ -33,19 +44,6 @@ class HashDB:
retVal = int(hashlib.md5(key).hexdigest()[:8], 16)
return retVal
def beginTransaction(self):
"""
Great speed improvement can be gained by using explicit transactions around multiple inserts.
Reference: http://stackoverflow.com/questions/4719836/python-and-sqlite3-adding-thousands-of-rows
"""
self.cursor.execute('BEGIN TRANSACTION')
def endTransaction(self):
try:
self.cursor.execute('END TRANSACTION')
except sqlite3.OperationalError:
pass
def retrieve(self, key):
retVal = None
if key: