1111import sqlite3
1212
1313from lib .core .settings import UNICODE_ENCODING
14+ from lib .core .threads import getCurrentThreadData
1415
15- class HashDB :
16+ class HashDB ( object ) :
1617 def __init__ (self , filepath ):
17- self .connection = sqlite3 .connect (filepath )
18- self .cursor = self .connection .cursor ()
19- self .cursor .execute ("CREATE TABLE IF NOT EXISTS storage (id INTEGER PRIMARY KEY, value TEXT)" )
18+ self .filepath = filepath
19+
20+ def _get_cursor (self ):
21+ threadData = getCurrentThreadData ()
22+
23+ if threadData .hashDBCursor is None :
24+ connection = sqlite3 .connect (self .filepath , isolation_level = None )
25+ threadData .hashDBCursor = connection .cursor ()
26+ threadData .hashDBCursor .execute ("CREATE TABLE IF NOT EXISTS storage (id INTEGER PRIMARY KEY, value TEXT)" )
27+
28+ return threadData .hashDBCursor
29+
30+ cursor = property (_get_cursor )
2031
2132 def __del__ (self ):
2233 self .close ()
2334
2435 def close (self ):
2536 try :
2637 self .endTransaction ()
27- self .connection .close ()
38+ self .cursor . connection .close ()
2839 except :
2940 pass
3041
@@ -33,19 +44,6 @@ def hashKey(self, key):
3344 retVal = int (hashlib .md5 (key ).hexdigest ()[:8 ], 16 )
3445 return retVal
3546
36- def beginTransaction (self ):
37- """
38- Great speed improvement can be gained by using explicit transactions around multiple inserts.
39- Reference: http://stackoverflow.com/questions/4719836/python-and-sqlite3-adding-thousands-of-rows
40- """
41- self .cursor .execute ('BEGIN TRANSACTION' )
42-
43- def endTransaction (self ):
44- try :
45- self .cursor .execute ('END TRANSACTION' )
46- except sqlite3 .OperationalError :
47- pass
48-
4947 def retrieve (self , key ):
5048 retVal = None
5149 if key :
0 commit comments