|
|
@@ -20,6 +20,19 @@ import sys |
|
|
|
import os |
|
|
|
import errno |
|
|
|
|
|
|
|
# Note about performance and transactions: |
|
|
|
# |
|
|
|
# Committing a transaction in the default sync mode (PRAGMA synchronous=FULL) |
|
|
|
# takes about 125msec. sqlite3 will commit transactions at 3 times: |
|
|
|
# 1: explicit con.commit() |
|
|
|
# 2: between a series of DML commands and non-DML commands, e.g. |
|
|
|
# after a series of INSERT, SELECT, but before a CREATE TABLE or PRAGMA. |
|
|
|
# 3: at the end of an explicit transaction, e.g. "with self.con as con:" |
|
|
|
# |
|
|
|
# To speed up testing, or if this transaction speed becomes an issue, |
|
|
|
# the sync=False option to NilmDB.__init__ will set PRAGMA synchronous=OFF. |
|
|
|
|
|
|
|
|
|
|
|
# Don't touch old entries -- just add new ones. |
|
|
|
sql_schema_updates = { |
|
|
|
0: """ |
|
|
@@ -61,7 +74,7 @@ class StreamError(Exception): |
|
|
|
class NilmDB(object): |
|
|
|
verbose = 0 |
|
|
|
|
|
|
|
def __init__(self, basepath): |
|
|
|
def __init__(self, basepath, sync=True): |
|
|
|
# set up path |
|
|
|
self.basepath = basepath.rstrip('/') |
|
|
|
|
|
|
@@ -85,6 +98,12 @@ class NilmDB(object): |
|
|
|
self.con = sqlite3.connect(sqlfilename, check_same_thread = False) |
|
|
|
self.sql_schema_update() |
|
|
|
|
|
|
|
# See big comment at top about the performance implications of this |
|
|
|
if sync: |
|
|
|
self.con.execute("PRAGMA synchronous=FULL") |
|
|
|
else: |
|
|
|
self.con.execute("PRAGMA synchronous=OFF") |
|
|
|
|
|
|
|
self.opened = True |
|
|
|
|
|
|
|
def __del__(self): |
|
|
@@ -112,7 +131,6 @@ class NilmDB(object): |
|
|
|
printf("Schema updated to %d\n", version) |
|
|
|
|
|
|
|
if version != oldversion: |
|
|
|
# this takes .1 seconds, so only do it if necessary |
|
|
|
with self.con: |
|
|
|
cur.execute("PRAGMA user_version = {v:d}".format(v=version)) |
|
|
|
|
|
|
|