Added a new preference to the indexer to NOT delete files missing from

the filesystem.
This commit is contained in:
casey 2016-06-19 14:25:03 -07:00
parent d8714bb59b
commit 4f466e6d01
2 changed files with 35 additions and 30 deletions

View File

@ -51,6 +51,10 @@
#include <boost/thread/xtime.hpp> #include <boost/thread/xtime.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#define PREFS_SYNC_ON_STARTUP "SyncOnStartup"
#define PREFS_SYNC_TIMEOUT "SyncTimeout"
#define PREFS_REMOVE_MISSING_FILES "RemoveMissingFiles"
static const std::string TAG = "Indexer"; static const std::string TAG = "Indexer";
using namespace musik::core; using namespace musik::core;
@ -80,6 +84,7 @@ Indexer::Indexer(const std::string& libraryPath, const std::string& dbFilename)
, filesSaved(0) { , filesSaved(0) {
this->dbFilename = dbFilename; this->dbFilename = dbFilename;
this->libraryPath = libraryPath; this->libraryPath = libraryPath;
this->prefs = Preferences::ForComponent("indexer");
this->thread = new boost::thread(boost::bind(&Indexer::ThreadLoop, this)); this->thread = new boost::thread(boost::bind(&Indexer::ThreadLoop, this));
} }
@ -89,7 +94,7 @@ Indexer::Indexer(const std::string& libraryPath, const std::string& dbFilename)
/// ///
///Exits and joins threads ///Exits and joins threads
////////////////////////////////////////// //////////////////////////////////////////
Indexer::~Indexer(){ Indexer::~Indexer() {
if (this->thread) { if (this->thread) {
this->Exit(); this->Exit();
this->thread->join(); this->thread->join();
@ -349,7 +354,7 @@ void Indexer::SyncDirectory(
this->filesSaved++; this->filesSaved++;
if (this->filesSaved % 100 == 0) { if (this->filesSaved % 100 == 0) {
this->TrackRefreshed(); /* no idea... something listens to this. maybe?*/ this->TrackRefreshed(); /* no idea... something listens to this. maybe? */
} }
} }
} }
@ -376,12 +381,10 @@ void Indexer::ThreadLoop() {
while (!this->Exited()) { while (!this->Exited()) {
this->restart = false; this->restart = false;
auto prefs = Preferences::ForComponent("indexer"); if(!firstTime || (firstTime && prefs->GetBool(PREFS_SYNC_ON_STARTUP, true))) { /* first time through the loop skips this */
if(!firstTime || (firstTime && prefs->GetBool("SyncOnStartup", true))) { /* first time through the loop skips this */
this->SynchronizeStart(); this->SynchronizeStart();
this->dbConnection.Open(this->dbFilename.c_str(), 0); /* ensure the db is open*/ this->dbConnection.Open(this->dbFilename.c_str(), 0); /* ensure the db is open */
this->SynchronizeInternal(); this->SynchronizeInternal();
this->RunAnalyzers(); this->RunAnalyzers();
@ -398,7 +401,7 @@ void Indexer::ThreadLoop() {
firstTime = false; firstTime = false;
int waitTime = prefs->GetInt("SyncTimeout", 3600); /* sleep before we try again... */ int waitTime = prefs->GetInt(PREFS_SYNC_TIMEOUT, 3600); /* sleep before we try again... */
if (waitTime) { if (waitTime) {
boost::xtime waitTimeout; boost::xtime waitTimeout;
@ -424,29 +427,31 @@ void Indexer::SyncDelete() {
/* remove files that are no longer on the filesystem. */ /* remove files that are no longer on the filesystem. */
db::Statement stmtRemove("DELETE FROM tracks WHERE id=?", this->dbConnection); if (prefs->GetBool(PREFS_REMOVE_MISSING_FILES, true)) {
db::Statement stmtRemove("DELETE FROM tracks WHERE id=?", this->dbConnection);
db::Statement allTracks( db::Statement allTracks(
"SELECT t.id, t.filename " "SELECT t.id, t.filename "
"FROM tracks t ", this->dbConnection); "FROM tracks t ", this->dbConnection);
while(allTracks.Step() == db::Row && !this->Exited() && !this->Restarted()) { while (allTracks.Step() == db::Row && !this->Exited() && !this->Restarted()) {
bool remove = false; bool remove = false;
std::string fn = allTracks.ColumnText(1); std::string fn = allTracks.ColumnText(1);
try { try {
boost::filesystem::path file(fn); boost::filesystem::path file(fn);
if (!boost::filesystem::exists(file)) { if (!boost::filesystem::exists(file)) {
remove = true; remove = true;
}
}
catch (...) {
} }
}
catch(...) {
}
if (remove) { if (remove) {
stmtRemove.BindInt(0, allTracks.ColumnInt(0)); stmtRemove.BindInt(0, allTracks.ColumnInt(0));
stmtRemove.Step(); stmtRemove.Step();
stmtRemove.Reset(); stmtRemove.Reset();
}
} }
} }
} }

View File

@ -39,6 +39,7 @@
#include <core/sdk/IMetadataReader.h> #include <core/sdk/IMetadataReader.h>
#include <core/sdk/IDecoderFactory.h> #include <core/sdk/IDecoderFactory.h>
#include <core/library/IIndexer.h> #include <core/library/IIndexer.h>
#include <core/support/Preferences.h>
#include <sigslot/sigslot.h> #include <sigslot/sigslot.h>
@ -102,11 +103,9 @@ namespace musik { namespace core {
int filesIndexed; int filesIndexed;
int filesSaved; int filesSaved;
struct AddRemoveContext {
class AddRemoveContext { bool add;
public: std::string path;
bool add;
std::string path;
}; };
typedef std::vector<std::shared_ptr< typedef std::vector<std::shared_ptr<
@ -119,6 +118,7 @@ namespace musik { namespace core {
MetadataReaderList metadataReaders; MetadataReaderList metadataReaders;
DecoderList audioDecoders; DecoderList audioDecoders;
std::shared_ptr<musik::core::Preferences> prefs;
}; };
typedef std::shared_ptr<Indexer> IndexerPtr; typedef std::shared_ptr<Indexer> IndexerPtr;