@@ -10,6 +10,17 @@ class Database:
1010 DB_IN_MEMORY = ":memory:"
1111 DB_TYPE_MEMORY = 0
1212 DB_TYPE_FILE = 1
13+ DB_JRNL_WAL = False
14+
15+ # Sqlite3 journal modes
16+ DB_JOURNAL_MODE_LIST = {
17+ 0 : "DELETE" ,
18+ 1 : "TRUNCATE" ,
19+ 2 : "PERSIST" ,
20+ 3 : "MEMORY" ,
21+ 4 : "WAL" ,
22+ 5 : "OFF" ,
23+ }
1324
1425 # increase accordingly whenever the schema is updated
1526 DB_VERSION = 3
@@ -24,11 +35,16 @@ def __init__(self, dbname="db"):
2435 self ._lock = threading .RLock ()
2536 self .db = None
2637 self .db_file = Database .DB_IN_MEMORY
38+ self .db_jrnl_wal = Database .DB_JRNL_WAL
2739 self .db_name = dbname
2840
29- def initialize (self , dbtype = DB_TYPE_MEMORY , dbfile = DB_IN_MEMORY , db_name = "db" ):
41+ def initialize (self , dbtype = DB_TYPE_MEMORY , dbfile = DB_IN_MEMORY , dbjrnl_wal = DB_JRNL_WAL , db_name = "db" ):
3042 if dbtype != Database .DB_TYPE_MEMORY :
3143 self .db_file = dbfile
44+ self .db_jrnl_wal = dbjrnl_wal
45+ else :
46+ # Always disable under pure memory mode
47+ self .db_jrnl_wal = False
3248
3349 is_new_file = not os .path .isfile (self .db_file )
3450
@@ -87,19 +103,16 @@ def get_db_name(self):
87103 return self .db_name
88104
89105 def _create_tables (self ):
90- # https://www.sqlite.org/wal.html
91106 if self .db_file == Database .DB_IN_MEMORY :
92107 self .set_schema_version (self .DB_VERSION )
93- q = QSqlQuery ("PRAGMA journal_mode = OFF" , self .db )
94- q .exec_ ()
95- q = QSqlQuery ("PRAGMA synchronous = OFF" , self .db )
96- q .exec_ ()
97- q = QSqlQuery ("PRAGMA cache_size=10000" , self .db )
98- q .exec_ ()
108+ # Disable journal (default)
109+ self .set_journal_mode (5 )
110+ elif self .db_jrnl_wal is True :
111+ # Set WAL mode (file+memory)
112+ self .set_journal_mode (4 )
99113 else :
100- q = QSqlQuery ("PRAGMA synchronous = NORMAL" , self .db )
101- q .exec_ ()
102-
114+ # Set DELETE mode (file)
115+ self .set_journal_mode (0 )
103116 q = QSqlQuery ("create table if not exists connections (" \
104117 "time text, " \
105118 "node text, " \
@@ -200,6 +213,41 @@ def set_schema_version(self, version):
200213 if q .exec_ () == False :
201214 print ("Error updating updating schema version:" , q .lastError ().text ())
202215
216+ def get_journal_mode (self ):
217+ q = QSqlQuery ("PRAGMA journal_mode;" , self .db )
218+ q .exec_ ()
219+ if q .next ():
220+ return str (q .value (0 ))
221+
222+ return str ("unknown" )
223+
224+ def set_journal_mode (self , mode ):
225+ # https://www.sqlite.org/wal.html
226+ mode_str = Database .DB_JOURNAL_MODE_LIST [mode ]
227+ if self .get_journal_mode ().lower () != mode_str .lower ():
228+ print ("Setting journal_mode: " , mode_str )
229+ q = QSqlQuery ("PRAGMA journal_mode = {modestr};" .format (modestr = mode_str ), self .db )
230+ if q .exec_ () == False :
231+ print ("Error updating PRAGMA journal_mode:" , q .lastError ().text ())
232+ return False
233+ if mode == 3 or mode == 5 :
234+ print ("Setting DB memory optimizations" )
235+ q = QSqlQuery ("PRAGMA synchronous = OFF;" , self .db )
236+ if q .exec_ () == False :
237+ print ("Error updating PRAGMA synchronous:" , q .lastError ().text ())
238+ return False
239+ q = QSqlQuery ("PRAGMA cache_size=10000;" , self .db )
240+ if q .exec_ () == False :
241+ print ("Error updating PRAGMA cache_size:" , q .lastError ().text ())
242+ return False
243+ else :
244+ print ("Setting synchronous = NORMAL" )
245+ q = QSqlQuery ("PRAGMA synchronous = NORMAL;" , self .db )
246+ if q .exec_ () == False :
247+ print ("Error updating PRAGMA synchronous:" , q .lastError ().text ())
248+
249+ return True
250+
203251 def _upgrade_db_schema (self ):
204252 migrations_path = os .path .dirname (os .path .realpath (__file__ )) + "/migrations"
205253 schema_version = self .get_schema_version ()
0 commit comments