Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrackAnalysisScheduler: Remove dependency on TrackDAO #4480

Merged
merged 1 commit into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/analyzer/trackanalysisscheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "analyzer/trackanalysisscheduler.h"

#include "library/dao/trackdao.h"
#include "moc_trackanalysisscheduler.cpp"
#include "track/track.h"
#include "util/logger.h"
Expand Down Expand Up @@ -31,13 +30,13 @@ TrackAnalysisScheduler::NullPointer::NullPointer()

//static
TrackAnalysisScheduler::Pointer TrackAnalysisScheduler::createInstance(
const TrackDAO* pTrackDao,
std::unique_ptr<const TrackAnalysisSchedulerEnvironment> pEnvironment,
int numWorkerThreads,
const mixxx::DbConnectionPoolPtr& pDbConnectionPool,
const UserSettingsPointer& pConfig,
AnalyzerModeFlags modeFlags) {
return Pointer(new TrackAnalysisScheduler(
pTrackDao,
std::move(pEnvironment),
numWorkerThreads,
pDbConnectionPool,
pConfig,
Expand All @@ -46,17 +45,18 @@ TrackAnalysisScheduler::Pointer TrackAnalysisScheduler::createInstance(
}

TrackAnalysisScheduler::TrackAnalysisScheduler(
const TrackDAO* pTrackDao,
std::unique_ptr<const TrackAnalysisSchedulerEnvironment> pEnvironment,
int numWorkerThreads,
const mixxx::DbConnectionPoolPtr& pDbConnectionPool,
const UserSettingsPointer& pConfig,
AnalyzerModeFlags modeFlags)
: m_pTrackDao(pTrackDao),
: m_pEnvironment(std::move(pEnvironment)),
m_currentTrackProgress(kAnalyzerProgressUnknown),
m_currentTrackNumber(0),
m_dequeuedTracksCount(0),
// The first signal should always be emitted
m_lastProgressEmittedAt(Clock::now() - kProgressInhibitDuration) {
DEBUG_ASSERT(m_pEnvironment);
VERIFY_OR_DEBUG_ASSERT(numWorkerThreads > 0) {
kLogger.warning()
<< "Invalid number of worker threads:"
Expand Down Expand Up @@ -275,17 +275,12 @@ void TrackAnalysisScheduler::resume() {

bool TrackAnalysisScheduler::submitNextTrack(Worker* worker) {
DEBUG_ASSERT(worker);
const auto* const pTrackDao = m_pTrackDao.data();
VERIFY_OR_DEBUG_ASSERT(pTrackDao) {
kLogger.critical() << "TrackDAO pointer is dangling";
return false;
}
while (!m_queuedTrackIds.empty()) {
TrackId nextTrackId = m_queuedTrackIds.front();
DEBUG_ASSERT(nextTrackId.isValid());
if (nextTrackId.isValid()) {
TrackPointer nextTrack =
pTrackDao->getTrackById(nextTrackId);
m_pEnvironment->loadTrackById(nextTrackId);
if (nextTrack) {
if (m_pendingTrackIds.insert(nextTrackId).second) {
if (worker->submitNextTrack(std::move(nextTrack))) {
Expand Down
22 changes: 15 additions & 7 deletions src/analyzer/trackanalysisscheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@

#include "analyzer/analyzerthread.h"
#include "util/db/dbconnectionpool.h"
#include "util/qt.h"

// forward declaration(s)
class TrackDAO;
/// Callbacks for triggering side-effects in the outer context of
/// TrackAnalysisScheduler.
///
/// All functions will only be called from the host thread of
/// TrackAnalysisScheduler, not from worker threads.
class TrackAnalysisSchedulerEnvironment {
public:
virtual ~TrackAnalysisSchedulerEnvironment() = default;

virtual TrackPointer loadTrackById(TrackId trackId) const = 0;
};

class TrackAnalysisScheduler : public QObject {
Q_OBJECT
Expand All @@ -25,17 +33,17 @@ class TrackAnalysisScheduler : public QObject {
};

static Pointer createInstance(
const TrackDAO* pTrackDao,
std::unique_ptr<const TrackAnalysisSchedulerEnvironment> pEnvironment,
int numWorkerThreads,
const mixxx::DbConnectionPoolPtr& pDbConnectionPool,
const UserSettingsPointer& pConfig,
AnalyzerModeFlags modeFlags);

/*private*/ TrackAnalysisScheduler(
const TrackDAO* pTrackDao,
std::unique_ptr<const TrackAnalysisSchedulerEnvironment> pEnvironment,
int numWorkerThreads,
const mixxx::DbConnectionPoolPtr& pDbConnectionPool,
const UserSettingsPointer& pConfig,
const UserSettingsPointer& pUserSettings,
AnalyzerModeFlags modeFlags);
~TrackAnalysisScheduler() override;

Expand Down Expand Up @@ -139,7 +147,7 @@ class TrackAnalysisScheduler : public QObject {
m_pendingTrackIds.empty();
}

const mixxx::SafeQPointer<const TrackDAO> m_pTrackDao;
const std::unique_ptr<const TrackAnalysisSchedulerEnvironment> m_pEnvironment;

std::vector<Worker> m_workers;

Expand Down
20 changes: 19 additions & 1 deletion src/library/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,29 @@ TrackCollectionManager* Library::trackCollectionManager() const {
return m_pTrackCollectionManager;
}

namespace {
class TrackAnalysisSchedulerEnvironmentImpl : public TrackAnalysisSchedulerEnvironment {
public:
explicit TrackAnalysisSchedulerEnvironmentImpl(const Library* pLibrary)
: m_pLibrary(pLibrary) {
DEBUG_ASSERT(m_pLibrary);
}
~TrackAnalysisSchedulerEnvironmentImpl() override = default;

TrackPointer loadTrackById(TrackId trackId) const override {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marking this final might allow the compiler to optimize the virtual function call away. AFAIK

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if that would make a difference, but it could be done in another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, didn't think about it. This class is indeed supposed to be final.

return m_pLibrary->trackCollectionManager()->getTrackById(trackId);
}

private:
const Library* const m_pLibrary;
};
} // namespace

TrackAnalysisScheduler::Pointer Library::createTrackAnalysisScheduler(
int numWorkerThreads,
AnalyzerModeFlags modeFlags) const {
return TrackAnalysisScheduler::createInstance(
&m_pTrackCollectionManager->internalCollection()->getTrackDAO(),
std::make_unique<const TrackAnalysisSchedulerEnvironmentImpl>(this),
numWorkerThreads,
m_pDbConnectionPool,
m_pConfig,
Expand Down