/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see .
*/
#include "ScanStepCheckForRemovedFiles.hpp"
#include "core/ILogger.hpp"
#include "core/Path.hpp"
#include "database/Db.hpp"
#include "database/Image.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackLyrics.hpp"
namespace lms::scanner
{
namespace
{
constexpr std::size_t batchSize = 100;
}
void ScanStepCheckForRemovedFiles::process(ScanContext& context)
{
if (_abortScan)
return;
db::Session& session{ _db.getTLSSession() };
{
auto transaction{ session.createReadTransaction() };
context.currentStepStats.totalElems = 0;
context.currentStepStats.totalElems += db::Track::getCount(session);
context.currentStepStats.totalElems += db::Image::getCount(session);
context.currentStepStats.totalElems += db::TrackLyrics::getExternalLyricsCount(session);
}
LMS_LOG(DBUPDATER, DEBUG, context.currentStepStats.totalElems << " files to be checked...");
checkForRemovedFiles(context, _settings.supportedAudioFileExtensions);
checkForRemovedFiles(context, _settings.supportedImageFileExtensions);
checkForRemovedFiles(context, _settings.supportedLyricsFileExtensions);
}
template
void ScanStepCheckForRemovedFiles::checkForRemovedFiles(ScanContext& context, std::span supportedFileExtensions)
{
using namespace db;
if (_abortScan)
return;
Session& session{ _db.getTLSSession() };
std::vector objectsToRemove;
typename Object::IdType lastCheckedId;
bool endReached{};
while (!endReached)
{
if (_abortScan)
break;
objectsToRemove.clear();
{
auto transaction{ session.createReadTransaction() };
endReached = true;
Object::find(session, lastCheckedId, batchSize, [&](const typename Object::pointer& object) {
endReached = false;
// special case for track lyrics, only check external lyrics
if constexpr (std::is_same_v