ecoc measurements

This commit is contained in:
Silas Labor Zizou
2025-04-13 16:29:12 +02:00
parent 00f1c557c0
commit 0236103b13
19 changed files with 709 additions and 359 deletions

View File

@@ -217,12 +217,31 @@ classdef DBHandler < handle
end
% Append the new row to the database table
try
sqlwrite(obj.conn, tableName, newRow);
% disp(['Successfully appended new row to the table ', tableName]);
catch e
error('Failed to append to the table %s: %s', tableName, e.message);
% Parameters for retry logic
maxRetries = 5;
basePause = 0.05; % seconds
attempt = 0;
success = false;
while ~success && attempt <= maxRetries
try
sqlwrite(obj.conn, tableName, newRow);
success = true; % Write successful
catch e
if contains(e.message, 'database is locked') || contains(e.message, 'cannot rollback transaction')
attempt = attempt + 1;
pauseTime = basePause * (1 + rand()); % Add random jitter to reduce collision chance
fprintf('Database locked. Retry %d/%d after %.3f seconds...\n', attempt, maxRetries, pauseTime);
pause(pauseTime);
else
error('Failed to append to the table %s: %s', tableName, e.message);
end
end
end
if ~success
error('Failed to append to table %s after %d retries due to database lock.', tableName, maxRetries);
end
% Retrieve the measurement_id of the newly inserted row for linking other tables
@@ -539,27 +558,38 @@ classdef DBHandler < handle
end
end
% --- Adaptive FROM Clause ---
% Use "Runs" as the main table and add LEFT JOINs for every other table in obj.tables
% (except "sqlite_sequence") that has a run_id field.
mainTable = 'Runs';
fromClause = ['FROM ', mainTable, ' '];
% Prepare join buffers
normalJoins = '';
equalizerJoin = '';
tableNamesAll = fieldnames(obj.tables);
for t = 1:numel(tableNamesAll)
tableName = tableNamesAll{t};
if strcmpi(tableName, mainTable) || strcmpi(tableName, 'sqlite_sequence')
continue;
end
if isfield(obj.tables.(tableName), 'run_id')
% most tables are directly linked to runs table
fromClause = [fromClause, 'LEFT JOIN ', tableName, ' ON ', mainTable, '.run_id = ', tableName, '.run_id '];
% Direct join to Runs
normalJoins = [normalJoins, 'LEFT JOIN ', tableName, ' ON ', mainTable, '.run_id = ', tableName, '.run_id '];
elseif isfield(obj.tables.(tableName), 'eq_id')
% equalizer is only linked to results table
fromClause = [fromClause, 'LEFT JOIN ', tableName, ' ON ', 'Results', '.eqParam_id = ', tableName, '.eq_id '];
% Equalizer depends on Results, collect this join separately
equalizerJoin = ['LEFT JOIN ', tableName, ' ON Results.eqParam_id = ', tableName, '.eq_id '];
end
end
% Combine joins: normal joins first, Equalizer last
fromClause = [fromClause, normalJoins, equalizerJoin];
% --- WHERE Clause Construction ---
baseQuery = [selectClause, ' ', fromClause, 'WHERE '];
filterClauses = [];