cli: CommandUpdate: Use shared/IndexSyncer
This commit is contained in:
parent
abc126548b
commit
49e408be50
@ -4,6 +4,7 @@
|
|||||||
#include <QThreadPool>
|
#include <QThreadPool>
|
||||||
#include "commandupdate.h"
|
#include "commandupdate.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
#include "../shared/indexsyncer.h"
|
||||||
|
|
||||||
int CommandUpdate::handle(QStringList arguments)
|
int CommandUpdate::handle(QStringList arguments)
|
||||||
{
|
{
|
||||||
@ -36,91 +37,56 @@ int CommandUpdate::handle(QStringList arguments)
|
|||||||
QString threadsCount = parser.value("threads");
|
QString threadsCount = parser.value("threads");
|
||||||
QThreadPool::globalInstance()->setMaxThreadCount(threadsCount.toInt());
|
QThreadPool::globalInstance()->setMaxThreadCount(threadsCount.toInt());
|
||||||
}
|
}
|
||||||
FileSaver saver(*this->dbService);
|
|
||||||
QVector<FileData> files;
|
|
||||||
int offset = 0;
|
|
||||||
int limit = 1000;
|
|
||||||
int processedRows = dbService->getFiles(files, pattern, offset, limit);
|
|
||||||
|
|
||||||
unsigned int totalUpdatesFilesCount = 0;
|
bool hasErrors = false;
|
||||||
unsigned int totalDeletedFilesCount = 0;
|
IndexSyncer *syncer = new IndexSyncer(*this->dbService);
|
||||||
|
syncer->setKeepGoing(keepGoing);
|
||||||
|
syncer->setVerbose(verbose);
|
||||||
|
syncer->setPattern(pattern);
|
||||||
|
syncer->setDryRun(dryRun);
|
||||||
|
syncer->setRemoveDeletedFromIndex(deleteMissing);
|
||||||
|
|
||||||
while(processedRows > 0)
|
if(dryRun)
|
||||||
{
|
{
|
||||||
QVector<QString> filePathsToUpdate;
|
connect(syncer, &IndexSyncer::removedDryRun, this,
|
||||||
for(FileData &fileData : files)
|
[](QString path) { Logger::info() << "Would delete" << path << Qt::endl; });
|
||||||
{
|
connect(syncer, &IndexSyncer::updatedDryRun, this,
|
||||||
QFileInfo fileInfo(fileData.absPath);
|
[](QString path) { Logger::info() << "Would update" << path << Qt::endl; });
|
||||||
if(fileInfo.exists())
|
|
||||||
{
|
|
||||||
if(fileInfo.isFile())
|
|
||||||
{
|
|
||||||
if(fileInfo.lastModified().toSecsSinceEpoch() != fileData.mtime)
|
|
||||||
{
|
|
||||||
if(!dryRun)
|
|
||||||
{
|
|
||||||
filePathsToUpdate.append(fileData.absPath);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Logger::info() << "Would update" << fileData.absPath << Qt::endl;
|
connect(syncer, &IndexSyncer::removed, this,
|
||||||
|
[](QString path) { Logger::info() << "Removed " << path << Qt::endl; });
|
||||||
|
/* TODO: updated not printed, handled be verbose in FileSaver, but this can be improved */
|
||||||
}
|
}
|
||||||
}
|
connect(syncer, &IndexSyncer::finished, this,
|
||||||
}
|
[&](unsigned int totalUpdated, unsigned int totalRemoved, unsigned int totalErrors)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(deleteMissing)
|
|
||||||
{
|
{
|
||||||
|
Logger::info() << "Syncing finished" << Qt::endl;
|
||||||
|
|
||||||
if(!dryRun)
|
if(!dryRun)
|
||||||
{
|
{
|
||||||
if(!this->dbService->deleteFile(fileData.absPath))
|
Logger::info() << "Total updated:" << totalUpdated << Qt::endl;
|
||||||
{
|
Logger::info() << "Total removed from index: " << totalRemoved << Qt::endl;
|
||||||
Logger::error()
|
Logger::info() << "Total deleted:" << totalErrors << Qt::endl;
|
||||||
<< "Error: Failed to delete" << fileData.absPath << "from the index" << Qt::endl;
|
|
||||||
if(!keepGoing)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(verbose)
|
|
||||||
{
|
|
||||||
Logger::info() << "Deleted from index:" << fileData.absPath << Qt::endl;
|
|
||||||
}
|
|
||||||
++totalDeletedFilesCount;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
Logger::info() << "Would delete from index" << fileData.absPath << Qt::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int updatedFilesCount = saver.updateFiles(filePathsToUpdate, keepGoing, verbose);
|
int retval = 0;
|
||||||
int shouldHaveUpdatedCount = filePathsToUpdate.size();
|
if(hasErrors && !keepGoing)
|
||||||
if(updatedFilesCount != shouldHaveUpdatedCount)
|
|
||||||
{
|
{
|
||||||
if(!keepGoing)
|
retval = 1;
|
||||||
|
}
|
||||||
|
emit finishedCmd(retval);
|
||||||
|
});
|
||||||
|
connect(syncer, &IndexSyncer::error, this,
|
||||||
|
[&](QString error)
|
||||||
{
|
{
|
||||||
Logger::error() << "Failed to update all files selected for updating in this batch. Updated"
|
Logger::error() << error << Qt::endl;
|
||||||
<< updatedFilesCount << "out of" << shouldHaveUpdatedCount << "selected for updating"
|
hasErrors = true;
|
||||||
<< Qt::endl;
|
});
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
offset += limit;
|
|
||||||
files.clear();
|
|
||||||
processedRows = this->dbService->getFiles(files, pattern, offset, limit);
|
|
||||||
|
|
||||||
totalUpdatesFilesCount += static_cast<unsigned int>(updatedFilesCount);
|
|
||||||
}
|
|
||||||
if(!dryRun)
|
|
||||||
{
|
|
||||||
Logger::info() << "Total (updated): " << totalUpdatesFilesCount << Qt::endl;
|
|
||||||
Logger::info() << "Total (deleted from index): " << totalDeletedFilesCount << Qt::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
this->autoFinish = false;
|
||||||
|
syncer->sync();
|
||||||
|
/* Actual return code is handled by finishedCmd signal */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user