Optimized library assignation to directories
This commit is contained in:
@@ -383,17 +383,6 @@ namespace lms::scanner
|
||||
|
||||
if (fileMatched)
|
||||
{
|
||||
// Not very efficient way to update media_library for directories
|
||||
if (path.has_parent_path())
|
||||
{
|
||||
const std::filesystem::path directory{ path.parent_path() };
|
||||
if (directory != currentDirectory)
|
||||
{
|
||||
updateDirectoryIfNeeded(currentDirectory, mediaLibrary);
|
||||
currentDirectory = directory;
|
||||
}
|
||||
}
|
||||
|
||||
context.currentStepStats.processedElems++;
|
||||
_progressCallback(context.currentStepStats);
|
||||
}
|
||||
@@ -792,34 +781,4 @@ namespace lms::scanner
|
||||
stats.updates++;
|
||||
}
|
||||
}
|
||||
|
||||
void ScanStepScanFiles::updateDirectoryIfNeeded(const std::filesystem::path& dirPath, const ScannerSettings::MediaLibraryInfo& libraryInfo)
|
||||
{
|
||||
db::Session& dbSession{ _db.getTLSSession() };
|
||||
|
||||
const bool needUpdateLibrary{ [&] {
|
||||
auto transaction{ dbSession.createReadTransaction() };
|
||||
|
||||
Directory::pointer directory{ Directory::find(dbSession, dirPath) };
|
||||
if (!directory)
|
||||
return false; // we create directories only of we find images or tracks inside
|
||||
|
||||
MediaLibrary::pointer currentLibrary{ directory->getMediaLibrary() };
|
||||
return !currentLibrary || currentLibrary->getId() != libraryInfo.id;
|
||||
}() };
|
||||
|
||||
if (needUpdateLibrary)
|
||||
{
|
||||
auto transaction{ dbSession.createWriteTransaction() };
|
||||
|
||||
Directory::pointer directory{ Directory::find(dbSession, dirPath) };
|
||||
assert(directory);
|
||||
directory.modify()->setMediaLibrary(MediaLibrary::find(dbSession, libraryInfo.id)); // may be null if settings are updated in // => next scan will correct this
|
||||
}
|
||||
|
||||
if (dirPath != libraryInfo.rootDirectory && dirPath.has_parent_path())
|
||||
{
|
||||
updateDirectoryIfNeeded(dirPath.parent_path(), libraryInfo);
|
||||
}
|
||||
}
|
||||
} // namespace lms::scanner
|
||||
|
||||
@@ -49,8 +49,6 @@ namespace lms::scanner
|
||||
void processAudioFileScanData(ScanContext& context, const std::filesystem::path& path, const metadata::Track* trackMetadata, const ScannerSettings::MediaLibraryInfo& libraryInfo);
|
||||
void processImageFileScanData(ScanContext& context, const std::filesystem::path& path, const ImageInfo* imageInfo, const ScannerSettings::MediaLibraryInfo& libraryInfo);
|
||||
|
||||
void updateDirectoryIfNeeded(const std::filesystem::path& directory, const ScannerSettings::MediaLibraryInfo& libraryInfo);
|
||||
|
||||
std::unique_ptr<metadata::IParser> _metadataParser;
|
||||
const std::vector<std::string> _extraTagsToParse;
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ScanStepUpdateLibraryFields.hpp"
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
#include "core/Path.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Directory.hpp"
|
||||
#include "database/MediaLibrary.hpp"
|
||||
#include "database/Session.hpp"
|
||||
|
||||
namespace lms::scanner
|
||||
{
|
||||
|
||||
void ScanStepUpdateLibraryFields::process(ScanContext& context)
|
||||
{
|
||||
processDirectories(context);
|
||||
}
|
||||
|
||||
void ScanStepUpdateLibraryFields::processDirectories(ScanContext& context)
|
||||
{
|
||||
for (const ScannerSettings::MediaLibraryInfo& mediaLibrary : _settings.mediaLibraries)
|
||||
{
|
||||
if (_abortScan)
|
||||
break;
|
||||
|
||||
processDirectory(context, mediaLibrary);
|
||||
}
|
||||
}
|
||||
|
||||
void ScanStepUpdateLibraryFields::processDirectory(ScanContext& context, const ScannerSettings::MediaLibraryInfo& mediaLibrary)
|
||||
{
|
||||
db::Session& session{ _db.getTLSSession() };
|
||||
|
||||
constexpr std::size_t batchSize = 100;
|
||||
|
||||
db::RangeResults<db::DirectoryId> entries;
|
||||
while (!_abortScan)
|
||||
{
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
|
||||
entries = db::Directory::findMismatchedLibrary(session, db::Range{ 0, batchSize }, mediaLibrary.rootDirectory, mediaLibrary.id);
|
||||
};
|
||||
|
||||
if (entries.results.empty())
|
||||
break;
|
||||
|
||||
{
|
||||
auto transaction{ session.createWriteTransaction() };
|
||||
|
||||
db::MediaLibrary::pointer library{ db::MediaLibrary::find(session, mediaLibrary.id) };
|
||||
if (!library) // may be legit
|
||||
break;
|
||||
|
||||
for (const db::DirectoryId directoryId : entries.results)
|
||||
{
|
||||
if (_abortScan)
|
||||
break;
|
||||
|
||||
db::Directory::pointer directory{ db::Directory::find(session, directoryId) };
|
||||
directory.modify()->setMediaLibrary(library);
|
||||
}
|
||||
}
|
||||
|
||||
context.currentStepStats.processedElems += entries.results.size();
|
||||
_progressCallback(context.currentStepStats);
|
||||
}
|
||||
}
|
||||
} // namespace lms::scanner
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "database/DirectoryId.hpp"
|
||||
|
||||
#include "ScanStepBase.hpp"
|
||||
|
||||
namespace lms::scanner
|
||||
{
|
||||
class ScanStepUpdateLibraryFields : public ScanStepBase
|
||||
{
|
||||
public:
|
||||
using ScanStepBase::ScanStepBase;
|
||||
|
||||
private:
|
||||
core::LiteralString getStepName() const override { return "Update Library fields"; }
|
||||
ScanStep getStep() const override { return ScanStep::UpdateLibraryFields; }
|
||||
void process(ScanContext& context) override;
|
||||
|
||||
void processDirectories(ScanContext& context);
|
||||
void processDirectory(ScanContext& context, const ScannerSettings::MediaLibraryInfo& mediaLibrary);
|
||||
};
|
||||
} // namespace lms::scanner
|
||||
@@ -40,6 +40,7 @@
|
||||
#include "ScanStepOptimize.hpp"
|
||||
#include "ScanStepRemoveOrphanedDbEntries.hpp"
|
||||
#include "ScanStepScanFiles.hpp"
|
||||
#include "ScanStepUpdateLibraryFields.hpp"
|
||||
|
||||
namespace lms::scanner
|
||||
{
|
||||
@@ -279,7 +280,7 @@ namespace lms::scanner
|
||||
LMS_SCOPED_TRACE_OVERVIEW("Scanner", scanStep->getStepName());
|
||||
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Starting scan step '" << scanStep->getStepName() << "'");
|
||||
scanContext.currentStepStats = ScanStepStats{ .startTime = Wt::WDateTime::currentDateTime(), .stepIndex = stepIndex++, .currentStep = scanStep->getStep() };
|
||||
scanContext.currentStepStats = ScanStepStats{ .startTime = Wt::WDateTime::currentDateTime(), .stepCount = _scanSteps.size(), .stepIndex = stepIndex++, .currentStep = scanStep->getStep() };
|
||||
|
||||
notifyInProgress(scanContext.currentStepStats);
|
||||
scanStep->process(scanContext);
|
||||
@@ -339,11 +340,12 @@ namespace lms::scanner
|
||||
_db
|
||||
};
|
||||
|
||||
// Order is important
|
||||
// Order is important, steps are sequential
|
||||
_scanSteps.clear();
|
||||
_scanSteps.push_back(std::make_unique<ScanStepDiscoverFiles>(params));
|
||||
_scanSteps.push_back(std::make_unique<ScanStepScanFiles>(params));
|
||||
_scanSteps.push_back(std::make_unique<ScanStepCheckForRemovedFiles>(params));
|
||||
_scanSteps.push_back(std::make_unique<ScanStepUpdateLibraryFields>(params));
|
||||
_scanSteps.push_back(std::make_unique<ScanStepAssociateArtistImages>(params));
|
||||
_scanSteps.push_back(std::make_unique<ScanStepRemoveOrphanedDbEntries>(params));
|
||||
_scanSteps.push_back(std::make_unique<ScanStepCompact>(params));
|
||||
|
||||
Reference in New Issue
Block a user