Reduce PRS1Loader dependency on Machine data structure.

This commit is contained in:
sawinglogz 2021-09-03 12:45:00 -04:00
parent 4bfe062ae9
commit 18e97bb025
5 changed files with 42 additions and 14 deletions

View File

@ -12,6 +12,11 @@
#include "SleepLib/importcontext.h" #include "SleepLib/importcontext.h"
ImportContext::ImportContext()
: m_machine(nullptr)
{
}
ImportContext::~ImportContext() ImportContext::~ImportContext()
{ {
FlushUnexpectedMessages(); FlushUnexpectedMessages();
@ -38,6 +43,24 @@ void ImportContext::FlushUnexpectedMessages()
m_unexpectedMessages.clear(); m_unexpectedMessages.clear();
} }
QString ImportContext::GetBackupPath()
{
Q_ASSERT(m_machine);
return m_machine->getBackupPath();
}
bool ImportContext::SessionExists(SessionID sid)
{
Q_ASSERT(m_machine);
return m_machine->SessionExists(sid);
}
Session* ImportContext::CreateSession(SessionID sid)
{
Q_ASSERT(m_machine);
return new Session(m_machine, sid);
}
ProfileImportContext::ProfileImportContext(Profile* profile) ProfileImportContext::ProfileImportContext(Profile* profile)
: m_profile(profile) : m_profile(profile)

View File

@ -16,7 +16,7 @@ class ImportContext : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
ImportContext() {} ImportContext();
virtual ~ImportContext(); virtual ~ImportContext();
// Loaders will call this directly. It manages the machine's stored set of previously seen messages // Loaders will call this directly. It manages the machine's stored set of previously seen messages
@ -33,6 +33,12 @@ public:
// TODO: Isolate the Machine object from the loader rather than returning it. // TODO: Isolate the Machine object from the loader rather than returning it.
virtual Machine* CreateMachineFromInfo(const MachineInfo & info) = 0; virtual Machine* CreateMachineFromInfo(const MachineInfo & info) = 0;
// TODO: Eventually backup (and rebuild) should be handled invisibly to loaders.
virtual QString GetBackupPath();
virtual bool SessionExists(SessionID sid);
virtual Session* CreateSession(SessionID sid);
void FlushUnexpectedMessages(); void FlushUnexpectedMessages();
protected: protected:

View File

@ -775,7 +775,7 @@ int PRS1Loader::OpenMachine(const QString & path)
emit updateMessage(QObject::tr("Backing Up Files...")); emit updateMessage(QObject::tr("Backing Up Files..."));
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QString backupPath = m->getBackupPath() + path.section("/", -2); QString backupPath = context()->GetBackupPath() + path.section("/", -2);
if (QDir::cleanPath(path).compare(QDir::cleanPath(backupPath)) != 0) { if (QDir::cleanPath(path).compare(QDir::cleanPath(backupPath)) != 0) {
copyPath(path, backupPath); copyPath(path, backupPath);
@ -785,7 +785,7 @@ int PRS1Loader::OpenMachine(const QString & path)
QCoreApplication::processEvents(); QCoreApplication::processEvents();
// Walk through the files and create an import task for each logical session. // Walk through the files and create an import task for each logical session.
ScanFiles(paths, sessionid_base, m); ScanFiles(paths, sessionid_base);
int tasks = countTasks(); int tasks = countTasks();
@ -907,7 +907,7 @@ static QString chunkComparison(const PRS1DataChunk* a, const PRS1DataChunk* b)
} }
void PRS1Loader::ScanFiles(const QStringList & paths, int sessionid_base, Machine * m) void PRS1Loader::ScanFiles(const QStringList & paths, int sessionid_base)
{ {
Q_ASSERT(m_ctx); Q_ASSERT(m_ctx);
SessionID sid; SessionID sid;
@ -980,7 +980,7 @@ void PRS1Loader::ScanFiles(const QStringList & paths, int sessionid_base, Machin
// chunks, which might not correspond to the filename. But before we can // chunks, which might not correspond to the filename. But before we can
// fix this we need to come up with a reasonably fast way to filter previously // fix this we need to come up with a reasonably fast way to filter previously
// imported files without re-reading all of them. // imported files without re-reading all of them.
if (m->SessionExists(sid)) { if (context()->SessionExists(sid)) {
// Skip already imported session // Skip already imported session
qDebug() << path << "session already exists, skipping" << sid; qDebug() << path << "session already exists, skipping" << sid;
continue; continue;
@ -1005,7 +1005,7 @@ void PRS1Loader::ScanFiles(const QStringList & paths, int sessionid_base, Machin
// Should probably check if session already imported has this data missing.. // Should probably check if session already imported has this data missing..
// Create the group if we see it first.. // Create the group if we see it first..
task = new PRS1Import(this, sid, m, sessionid_base); task = new PRS1Import(this, sid, sessionid_base);
sesstasks[sid] = task; sesstasks[sid] = task;
queTask(task); queTask(task);
} }
@ -1055,7 +1055,7 @@ void PRS1Loader::ScanFiles(const QStringList & paths, int sessionid_base, Machin
// the first available filename isn't the first session contained in the file. // the first available filename isn't the first session contained in the file.
//qDebug() << fi.canonicalFilePath() << "first session is" << chunk_sid << "instead of" << sid; //qDebug() << fi.canonicalFilePath() << "first session is" << chunk_sid << "instead of" << sid;
} }
if (m->SessionExists(chunk_sid)) { if (context()->SessionExists(chunk_sid)) {
qDebug() << path << "session already imported, skipping" << sid << chunk_sid; qDebug() << path << "session already imported, skipping" << sid << chunk_sid;
delete chunk; delete chunk;
continue; continue;
@ -1074,7 +1074,7 @@ void PRS1Loader::ScanFiles(const QStringList & paths, int sessionid_base, Machin
if (it != sesstasks.end()) { if (it != sesstasks.end()) {
task = it.value(); task = it.value();
} else { } else {
task = new PRS1Import(this, chunk_sid, m, sessionid_base); task = new PRS1Import(this, chunk_sid, sessionid_base);
sesstasks[chunk_sid] = task; sesstasks[chunk_sid] = task;
// save a loop an que this now // save a loop an que this now
queTask(task); queTask(task);
@ -2505,7 +2505,7 @@ bool PRS1Import::ParseSession(void)
{ {
bool ok = false; bool ok = false;
bool save = false; bool save = false;
session = new Session(mach, sessionid); session = loader->context()->CreateSession(sessionid);
do { do {
if (compliance != nullptr) { if (compliance != nullptr) {
@ -2639,7 +2639,7 @@ void PRS1Import::SaveSessionToDatabase(void)
// Save is not threadsafe // Save is not threadsafe
loader->saveMutex.lock(); loader->saveMutex.lock();
session->Store(mach->getDataPath()); session->Store(session->machine()->getDataPath());
loader->saveMutex.unlock(); loader->saveMutex.unlock();
// Unload them from memory // Unload them from memory

View File

@ -67,7 +67,7 @@ class PRS1Loader;
class PRS1Import:public ImportTask class PRS1Import:public ImportTask
{ {
public: public:
PRS1Import(PRS1Loader * l, SessionID s, Machine * m, int base): loader(l), sessionid(s), mach(m), m_sessionid_base(base) { PRS1Import(PRS1Loader * l, SessionID s, int base): loader(l), sessionid(s), m_sessionid_base(base) {
summary = nullptr; summary = nullptr;
compliance = nullptr; compliance = nullptr;
session = nullptr; session = nullptr;
@ -117,7 +117,6 @@ protected:
Session * session; Session * session;
PRS1Loader * loader; PRS1Loader * loader;
SessionID sessionid; SessionID sessionid;
Machine * mach;
QHash<ChannelID,EventList*> m_importChannels; // map channel ID to the session's current EventList* QHash<ChannelID,EventList*> m_importChannels; // map channel ID to the session's current EventList*
int summary_duration; int summary_duration;
@ -250,7 +249,7 @@ class PRS1Loader : public CPAPLoader
Machine* CreateMachineFromProperties(QString propertyfile); Machine* CreateMachineFromProperties(QString propertyfile);
//! \brief Scans the given directories for session data and create an import task for each logical session. //! \brief Scans the given directories for session data and create an import task for each logical session.
void ScanFiles(const QStringList & paths, int sessionid_base, Machine * m); void ScanFiles(const QStringList & paths, int sessionid_base);
// //! \brief Parses "properties.txt" file containing machine information // //! \brief Parses "properties.txt" file containing machine information
// bool ParseProperties(Machine *m, QString filename); // bool ParseProperties(Machine *m, QString filename);

View File

@ -103,7 +103,7 @@ void parseAndEmitSessionYaml(const QString & path)
return; return;
} }
s_loader->ScanFiles(paths, sessionid_base, m); s_loader->ScanFiles(paths, sessionid_base);
// Each session now has a PRS1Import object in m_MLtasklist // Each session now has a PRS1Import object in m_MLtasklist
QList<ImportTask*>::iterator i; QList<ImportTask*>::iterator i;