From 1bf04fe0782d08411f0ee876defd6fc11dd3f884 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 25 Jul 2015 01:14:20 +0300 Subject: [PATCH] Refactor mass insert into SQLiteMassInsert context manager --- reload_db.py | 53 +++++++++++++++++++++------------------------------- xsetbg_db.py | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/reload_db.py b/reload_db.py index 8d8b0df..060b05c 100755 --- a/reload_db.py +++ b/reload_db.py @@ -10,10 +10,10 @@ __copyright__ = "Copyright (C) 2007-2015 PhiloSoft Design" __license__ = "GNU GPL" import sys -from sqlobject import sqlhub, SQLObjectNotFound +from sqlobject import SQLObjectNotFound from sqlobject.sqlbuilder import Insert from xsetbg_conf import xsetbg_conf -from xsetbg_db import recreate_db +from xsetbg_db import recreate_db, SQLiteMassInsert fs_encoding = xsetbg_conf.get("images", "fs_encoding") dump_file = open(sys.argv[1], 'rU') @@ -26,35 +26,24 @@ def convert_str(s): else: return int(float(s)) -connection = xsetbg_db._connection - -connection.query("PRAGMA synchronous=OFF") -connection.query("PRAGMA count_changes=OFF") -connection.query("PRAGMA journal_mode=MEMORY") -connection.query("PRAGMA temp_store=MEMORY") - -txn = connection.transaction() -sqlhub.processConnection = txn - -for line in dump_file: - id, timestamp, filename = line.strip().split(None, 2) - id = convert_str(id) - timestamp = convert_str(timestamp) - if fs_encoding != 'utf-8': - filename = filename.decode(fs_encoding).encode('utf-8') - try: - row = xsetbg_db.byFull_name(filename) - except SQLObjectNotFound: - values = {'last_shown': timestamp, 'full_name': filename} - if id: - values['id'] = id - query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table, values=values)) - txn.query(query) - else: - assert id is None or row.id == id - assert row.last_shown == timestamp -txn.commit() -dump_file.close() +with SQLiteMassInsert() as txn: + for line in dump_file: + id, timestamp, filename = line.strip().split(None, 2) + id = convert_str(id) + timestamp = convert_str(timestamp) + if fs_encoding != 'utf-8': + filename = filename.decode(fs_encoding).encode('utf-8') + try: + row = xsetbg_db.byFull_name(filename) + except SQLObjectNotFound: + values = {'last_shown': timestamp, 'full_name': filename} + if id: + values['id'] = id + query = txn.sqlrepr(Insert(xsetbg_db.sqlmeta.table, values=values)) + txn.query(query) + else: + assert id is None or row.id == id + assert row.last_shown == timestamp -connection.query("VACUUM %s" % xsetbg_db.sqlmeta.table) +dump_file.close() diff --git a/xsetbg_db.py b/xsetbg_db.py index e5fe5d0..dc23254 100755 --- a/xsetbg_db.py +++ b/xsetbg_db.py @@ -32,6 +32,27 @@ def recreate_db(): return xsetbg_db +class SQLiteMassInsert(object): + def __init__(self): + self.connection = sqlhub.processConnection + + def __enter__(self): + connection = self.connection + connection.query("PRAGMA synchronous=OFF") + connection.query("PRAGMA count_changes=OFF") + connection.query("PRAGMA journal_mode=MEMORY") + connection.query("PRAGMA temp_store=MEMORY") + + txn = connection.transaction() + sqlhub.processConnection = txn + return txn + + def __exit__(self, *args, **kw): + sqlhub.processConnection.commit() + connection = sqlhub.processConnection = self.connection + connection.query("VACUUM %s" % xsetbg_db.sqlmeta.table) + + db_dirs = [] try: xsetbg_db_path = xsetbg_conf.get('xsetbg', 'database') -- 2.39.2