Minor refactor to make it a bit easier to parallelize tag reading.

This commit is contained in:
casey 2016-07-04 14:02:39 -07:00
parent bf1c9a0528
commit c44180db63
2 changed files with 69 additions and 59 deletions

View File

@ -281,6 +281,63 @@ void Indexer::SynchronizeInternal() {
musik::debug::info(TAG, "done!"); musik::debug::info(TAG, "done!");
} }
void Indexer::ReadMetadataFromFile(
const boost::filesystem::directory_iterator file,
const std::string& pathId)
{
musik::core::IndexerTrack track(0);
/* get cached filesize, parts, size, etc */
if (!track.NeedsToBeIndexed(file->path(), this->dbConnection)) {
return;
}
bool saveToDb = false;
/* read the tag from the plugin */
typedef MetadataReaderList::iterator Iterator;
Iterator it = this->metadataReaders.begin();
while (it != this->metadataReaders.end()) {
if ((*it)->CanRead(track.GetValue("extension").c_str())) {
if ((*it)->Read(file->path().string().c_str(), &track)) {
saveToDb = true;
break;
}
}
it++;
}
/* no tag? well... if a decoder can play it, add it to the database
with the file as the name. */
if (!saveToDb) {
std::string fullPath = file->path().string();
auto it = this->audioDecoders.begin();
while (it != this->audioDecoders.end()) {
if ((*it)->CanHandle(fullPath.c_str())) {
saveToDb = true;
track.SetValue("title", file->path().leaf().string().c_str());
break;
}
++it;
}
}
/* write it to the db, if read successfully */
if (saveToDb) {
track.SetValue("path_id", pathId.c_str());
track.Save(this->dbConnection, this->libraryPath);
this->filesSaved++;
if (this->filesSaved % 200 == 0) {
if (this->trackTransaction) {
this->trackTransaction->CommitAndRestart();
}
this->TrackRefreshed();
}
}
}
void Indexer::SyncDirectory( void Indexer::SyncDirectory(
const std::string &syncRoot, const std::string &syncRoot,
const std::string &currentPath, const std::string &currentPath,
@ -314,56 +371,7 @@ void Indexer::SyncDirectory(
} }
else { else {
++this->filesIndexed; ++this->filesIndexed;
this->ReadMetadataFromFile(file, pathIdStr);
musik::core::IndexerTrack track(0);
/* get cached filesize, parts, size, etc */
if (track.NeedsToBeIndexed(file->path(), this->dbConnection)) {
bool saveToDb = false;
/* read the tag from the plugin */
typedef MetadataReaderList::iterator Iterator;
Iterator it = this->metadataReaders.begin();
while (it != this->metadataReaders.end()) {
if ((*it)->CanRead(track.GetValue("extension").c_str())) {
if ((*it)->Read(file->path().string().c_str(), &track)) {
saveToDb = true;
break;
}
}
it++;
}
/* no tag? well... if a decoder can play it, add it to the database
with the file as the name. */
if (!saveToDb) {
std::string fullPath = file->path().string();
auto it = this->audioDecoders.begin();
while (it != this->audioDecoders.end()) {
if ((*it)->CanHandle(fullPath.c_str())) {
saveToDb = true;
track.SetValue("title", file->path().leaf().string().c_str());
break;
}
++it;
}
}
/* write it to the db, if read successfully */
if (saveToDb) {
track.SetValue("path_id", pathIdStr.c_str());
track.Save(this->dbConnection, this->libraryPath);
this->filesSaved++;
if (this->filesSaved % 200 == 0) {
if (this->trackTransaction) {
this->trackTransaction->CommitAndRestart();
}
this->TrackRefreshed(); /* no idea... something listens to this. maybe? */
}
}
}
} }
} }
} }
@ -371,10 +379,6 @@ void Indexer::SyncDirectory(
} }
} }
//////////////////////////////////////////
///\brief
///Main loop the thread is running in.
//////////////////////////////////////////
void Indexer::ThreadLoop() { void Indexer::ThreadLoop() {
boost::filesystem::path thumbPath(this->libraryPath + "thumbs/"); boost::filesystem::path thumbPath(this->libraryPath + "thumbs/");
@ -645,13 +649,13 @@ void Indexer::RunAnalyzers() {
} }
} }
if(!runningAnalyzers.empty()) { if (!runningAnalyzers.empty()) {
audio::StreamPtr stream = audio::Stream::Create(audio::Stream::NoDSP); audio::StreamPtr stream = audio::Stream::Create(audio::Stream::NoDSP);
if (stream) { if (stream) {
if (stream->OpenStream(track.URI())) { if (stream->OpenStream(track.URI())) {
/* decode the stream quickly, passing to all analyzers*/ /* decode the stream quickly, passing to all analyzers */
audio::BufferPtr buffer; audio::BufferPtr buffer;
@ -668,11 +672,12 @@ void Indexer::RunAnalyzers() {
} }
/* done with track decoding and analysis, let the plugins know */ /* done with track decoding and analysis, let the plugins know */
int successPlugins = 0; int successPlugins = 0;
PluginVector::iterator plugin = analyzers.begin(); PluginVector::iterator plugin = analyzers.begin();
for( ; plugin != analyzers.end(); ++plugin){ for ( ; plugin != analyzers.end(); ++plugin) {
if((*plugin)->End(&track)){ if ((*plugin)->End(&track)) {
successPlugins++; successPlugins++;
} }
} }
@ -680,7 +685,7 @@ void Indexer::RunAnalyzers() {
/* the analyzers can write metadata back to the DB, so if any of them /* the analyzers can write metadata back to the DB, so if any of them
completed successfully, then save the track. */ completed successfully, then save the track. */
if(successPlugins>0) { if (successPlugins>0) {
track.Save(this->dbConnection, this->libraryPath); track.Save(this->dbConnection, this->libraryPath);
} }
} }

View File

@ -44,6 +44,7 @@
#include <sigslot/sigslot.h> #include <sigslot/sigslot.h>
#include <boost/thread/thread.hpp> #include <boost/thread/thread.hpp>
#include <boost/filesystem.hpp>
#include <deque> #include <deque>
#include <vector> #include <vector>
@ -86,6 +87,10 @@ namespace musik { namespace core {
const std::string& currentPath, const std::string& currentPath,
DBID pathId); DBID pathId);
void ReadMetadataFromFile(
const boost::filesystem::directory_iterator path,
const std::string& pathId);
db::Connection dbConnection; db::Connection dbConnection;
std::string libraryPath; std::string libraryPath;