Skip to content

Commit

Permalink
Get rid of sqlite3.Row and adjust SQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Blanchet committed Sep 20, 2022
1 parent 4b72385 commit b476b79
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 169 deletions.
2 changes: 1 addition & 1 deletion docs/changes/newsfragments/4446.improved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Improve performance of ``sqlite3`` converters and adapters used to write and read in the database.

Get rid of irrelevant unpacking from ``sqlite3.Row`` to ``list``.
Get rid of ``sqlite3.Row`` and irrelevant unpacking to ``list``.
9 changes: 6 additions & 3 deletions qcodes/dataset/data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,11 +1174,14 @@ def unsubscribe_all(self) -> None:
"""
Remove all subscribers
"""
sql = "select * from sqlite_master where type = 'trigger';"
sql = """
SELECT name FROM sqlite_master
WHERE type = 'trigger'
"""
triggers = atomic_transaction(self.conn, sql).fetchall()
with atomic(self.conn) as conn:
for trigger in triggers:
remove_trigger(conn, trigger['name'])
for (trigger,) in triggers:
remove_trigger(conn, trigger)
for sub in self.subscribers.values():
sub.schedule_stop()
sub.join()
Expand Down
9 changes: 5 additions & 4 deletions qcodes/dataset/experiment_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ def data_set(self, counter: int) -> DataSet:

def data_sets(self) -> list[DataSetProtocol]:
"""Get all the datasets of this experiment"""
runs = get_runs(self.conn, self.exp_id)
return [load_by_id(run['run_id'], conn=self.conn) for run in runs]
return [
load_by_id(run_id, conn=self.conn)
for run_id in get_runs(self.conn, self.exp_id)
]

def last_data_set(self) -> DataSetProtocol:
"""Get the last dataset of this experiment"""
Expand Down Expand Up @@ -216,8 +218,7 @@ def experiments(conn: ConnectionPlus | None = None) -> list[Experiment]:
"""
conn = conn_from_dbpath_or_conn(conn=conn, path_to_db=None)
log.info(f"loading experiments from {conn.path_to_dbfile}")
rows = get_experiments(conn)
return [load_experiment(row['exp_id'], conn) for row in rows]
return [load_experiment(exp_id, conn) for exp_id in get_experiments(conn)]


def new_experiment(
Expand Down
3 changes: 0 additions & 3 deletions qcodes/dataset/sqlite/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ def connect(name: str | Path, debug: bool = False, version: int = -1) -> Connect
f"version of QCoDeS supports up to "
f"version {latest_supported_version}")

# sqlite3 options
conn.row_factory = sqlite3.Row

# Make sure numpy ints and floats types are inserted properly
for numpy_int in numpy_ints:
sqlite3.register_adapter(numpy_int, int)
Expand Down
34 changes: 12 additions & 22 deletions qcodes/dataset/sqlite/db_upgrades/upgrade_2_to_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
atomic_transaction,
transaction,
)
from qcodes.dataset.sqlite.query_helpers import one
from qcodes.dataset.sqlite.query_helpers import get_description_map, one

log = logging.getLogger(__name__)

Expand All @@ -29,8 +29,8 @@ def _2to3_get_result_tables(conn: ConnectionPlus) -> dict[int, str]:
data = cur.fetchall()
cur.close()
results = {}
for row in data:
results[row['run_id']] = row['result_table_name']
for run_id, result_table_name in data:
results[run_id] = result_table_name
return results


Expand All @@ -47,9 +47,7 @@ def _2to3_get_layout_ids(conn: ConnectionPlus) -> DefaultDict[int, list[int]]:

results: DefaultDict[int, list[int]] = defaultdict(list)

for row in data:
run_id = row['run_id']
layout_id = row['layout_id']
for run_id, layout_id in data:
results[run_id].append(layout_id)

return results
Expand All @@ -68,9 +66,7 @@ def _2to3_get_indeps(conn: ConnectionPlus) -> DefaultDict[int, list[int]]:
cur.close()
results: DefaultDict[int, list[int]] = defaultdict(list)

for row in data:
run_id = row['run_id']
layout_id = row['layout_id']
for run_id, layout_id in data:
results[run_id].append(layout_id)

return results
Expand All @@ -89,9 +85,7 @@ def _2to3_get_deps(conn: ConnectionPlus) -> DefaultDict[int, list[int]]:
cur.close()
results: DefaultDict[int, list[int]] = defaultdict(list)

for row in data:
run_id = row['run_id']
layout_id = row['layout_id']
for run_id, layout_id in data:
results[run_id].append(layout_id)

return results
Expand All @@ -112,9 +106,7 @@ def _2to3_get_dependencies(conn: ConnectionPlus) -> DefaultDict[int, list[int]]:
if len(data) == 0:
return results

for row in data:
dep = row['dependent']
indep = row['independent']
for dep, indep in data:
results[dep].append(indep)

return results
Expand All @@ -129,11 +121,8 @@ def _2to3_get_layouts(conn: ConnectionPlus) -> dict[int, tuple[str, str, str, st
cur.execute(query)

results: dict[int, tuple[str, str, str, str]] = {}
for row in cur.fetchall():
results[row['layout_id']] = (row['parameter'],
row['label'],
row['unit'],
row['inferred_from'])
for layout_id, parameter, label, unit, inferred_from in cur.fetchall():
results[layout_id] = (parameter, label, unit, inferred_from)
return results


Expand All @@ -159,10 +148,11 @@ def _2to3_get_paramspecs(
# get the data type
sql = f'PRAGMA TABLE_INFO("{result_table_name}")'
c = transaction(conn, sql)
description = get_description_map(c)
paramtype = None
for row in c.fetchall():
if row['name'] == name:
paramtype = row['type']
if row[description["name"]] == name:
paramtype = row[description["type"]]
break
if paramtype is None:
raise TypeError(f"Could not determine type of {name} during the"
Expand Down
Loading

0 comments on commit b476b79

Please sign in to comment.