Indexer sources can now be interrupted, and failed sources can have their

transactions rolled back.
This commit is contained in:
Casey Langen 2017-04-09 12:10:40 -07:00
parent 1dd69725f5
commit ccb1fc8ef9
6 changed files with 51 additions and 10 deletions

View File

@ -123,7 +123,7 @@ void CddaIndexerSource::OnAfterScan() {
/* nothing to do... */
}
void CddaIndexerSource::Scan(musik::core::sdk::IIndexerWriter* indexer) {
ScanResult CddaIndexerSource::Scan(IIndexerWriter* indexer) {
for (auto disc : this->discs) {
char driveLetter = disc->GetDriveLetter();
std::string cddbId = disc->GetCddbId();
@ -152,7 +152,11 @@ void CddaIndexerSource::Scan(musik::core::sdk::IIndexerWriter* indexer) {
}
}
discIds.clear();
return ScanCommit;
}
void CddaIndexerSource::Interrupt() {
}
void CddaIndexerSource::ScanTrack(

View File

@ -52,13 +52,15 @@ class CddaIndexerSource :
virtual void OnAfterScan();
virtual int SourceId();
virtual void Scan(musik::core::sdk::IIndexerWriter* indexer);
virtual musik::core::sdk::ScanResult Scan(musik::core::sdk::IIndexerWriter* indexer);
virtual void ScanTrack(
musik::core::sdk::IIndexerWriter* indexer,
musik::core::sdk::IRetainedTrackWriter* track,
const char* externalId);
virtual void Interrupt();
/* CddaDataModel::EventListener */
virtual void OnAudioDiscInsertedOrRemoved();

View File

@ -110,6 +110,10 @@ Indexer::~Indexer() {
{
boost::mutex::scoped_lock lock(this->stateMutex);
this->exit = true;
if (this->currentSource) {
this->currentSource->Interrupt();
}
}
this->waitCondition.notify_all();
@ -201,20 +205,40 @@ void Indexer::Synchronize(const SyncContext& context, boost::asio::io_service* i
if (type == SyncType::All || (type == SyncType::Sources && sourceId == 0)) {
for (auto it : this->sources) {
this->SyncSource(it.get());
this->trackTransaction->CommitAndRestart();
if (this->Exited()) {
break;
}
this->currentSource = it;
if (this->SyncSource(it.get()) == ScanRollback) {
this->trackTransaction->Cancel();
}
this->trackTransaction->CommitAndRestart();
break;
}
this->currentSource.reset();
}
/* otherwise, we may have just been asked to index a single one... */
else if (type == SyncType::Sources && sourceId != 0) {
for (auto it : this->sources) {
if (this->Exited()) {
break;
}
if (it->SourceId() == sourceId) {
this->SyncSource(it.get());
this->currentSource = it;
if (this->SyncSource(it.get()) == ScanRollback) {
this->trackTransaction->Cancel();
}
this->trackTransaction->CommitAndRestart();
}
}
this->currentSource.reset();
}
/* process local files */
@ -428,9 +452,9 @@ void Indexer::SyncDirectory(
#undef WAIT_FOR_ACTIVE
}
void Indexer::SyncSource(IIndexerSource* source) {
ScanResult Indexer::SyncSource(IIndexerSource* source) {
if (source->SourceId() == 0) {
return;
return ScanRollback;
}
source->OnBeforeScan();
@ -452,9 +476,11 @@ void Indexer::SyncSource(IIndexerSource* source) {
/* now tell it to do a wide-open scan. it can use this opportunity to
remove old tracks, or add new ones. */
source->Scan(this);
auto result = source->Scan(this);
source->OnAfterScan();
return result;
}
void Indexer::ThreadLoop() {

View File

@ -120,7 +120,7 @@ namespace musik { namespace core {
void FinalizeSync(const SyncContext& context);
void SyncDelete();
void SyncCleanup();
void SyncSource(musik::core::sdk::IIndexerSource* source);
musik::core::sdk::ScanResult SyncSource(musik::core::sdk::IIndexerSource* source);
void ProcessAddRemoveQueue();
void SyncOptimize();
void RunAnalyzers();
@ -154,6 +154,7 @@ namespace musik { namespace core {
std::shared_ptr<musik::core::Preferences> prefs;
std::shared_ptr<musik::core::db::ScopedTransaction> trackTransaction;
std::vector<std::string> paths;
std::shared_ptr<musik::core::sdk::IIndexerSource> currentSource;
boost::interprocess::interprocess_semaphore readSemaphore;
};

View File

@ -34,6 +34,7 @@
#pragma once
#include "constants.h"
#include "IIndexerWriter.h"
#include "ITrackWriter.h"
@ -47,13 +48,15 @@ namespace musik { namespace core { namespace sdk {
virtual void OnAfterScan() = 0;
virtual void Scan(IIndexerWriter* indexer) = 0;
virtual ScanResult Scan(IIndexerWriter* indexer) = 0;
virtual void ScanTrack(
IIndexerWriter* indexer,
IRetainedTrackWriter* track,
const char* externalId) = 0;
virtual void Interrupt() = 0;
virtual int SourceId() = 0;
};

View File

@ -87,6 +87,11 @@ namespace musik {
Prebuffer = 0x01
};
enum ScanResult {
ScanCommit,
ScanRollback
};
namespace category {
static const char* Album = "album";
static const char* Artist = "artist";