Optimized library assignation to directories
This commit is contained in:
@@ -83,6 +83,25 @@ namespace lms::db
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
std::filesystem::path getPathWithTrailingSeparator(const std::filesystem::path& path)
|
||||
{
|
||||
if (path.empty())
|
||||
return path;
|
||||
|
||||
// Convert the path to string
|
||||
std::string pathStr{ path.string() };
|
||||
|
||||
// Check if the last character is a directory separator
|
||||
if (pathStr.back() != std::filesystem::path::preferred_separator)
|
||||
{
|
||||
// If not, add the preferred separator
|
||||
pathStr += std::filesystem::path::preferred_separator;
|
||||
}
|
||||
|
||||
// Return the new path
|
||||
return std::filesystem::path{ pathStr };
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Directory::Directory(const std::filesystem::path& p)
|
||||
@@ -157,6 +176,17 @@ namespace lms::db
|
||||
return utils::execRangeQuery<DirectoryId>(query, range);
|
||||
}
|
||||
|
||||
RangeResults<DirectoryId> Directory::findMismatchedLibrary(Session& session, std::optional<Range> range, const std::filesystem::path& rootPath, MediaLibraryId expectedLibraryId)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
auto query{ session.getDboSession()->query<DirectoryId>("SELECT d.id FROM directory d") };
|
||||
query.where("d.absolute_path = ? OR d.absolute_path LIKE ?").bind(rootPath).bind(getPathWithTrailingSeparator(rootPath).string() + "%");
|
||||
query.where("d.media_library_id <> ? OR d.media_library_id IS NULL").bind(expectedLibraryId);
|
||||
|
||||
return utils::execRangeQuery<DirectoryId>(query, range);
|
||||
}
|
||||
|
||||
RangeResults<Directory::pointer> Directory::findRootDirectories(Session& session, std::optional<Range> range)
|
||||
{
|
||||
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<Directory>>("SELECT d from directory d").where("d.parent_directory_id IS NULL") };
|
||||
|
||||
@@ -102,6 +102,7 @@ namespace lms::db
|
||||
static RangeResults<Directory::pointer> find(Session& session, const FindParameters& params);
|
||||
static void find(Session& session, const FindParameters& parameters, const std::function<void(const Directory::pointer&)>& func);
|
||||
static RangeResults<DirectoryId> findOrphanIds(Session& session, std::optional<Range> range = std::nullopt);
|
||||
static RangeResults<DirectoryId> findMismatchedLibrary(Session& session, std::optional<Range> range, const std::filesystem::path& rootPath, MediaLibraryId expectedLibraryId);
|
||||
static RangeResults<pointer> findRootDirectories(Session& session, std::optional<Range> range = std::nullopt);
|
||||
|
||||
// getters
|
||||
|
||||
@@ -252,4 +252,37 @@ namespace lms::db::tests
|
||||
EXPECT_EQ(res[1]->getId(), child2.getId());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(DatabaseFixture, Directory_findMismatchedLibrary)
|
||||
{
|
||||
ScopedDirectory parent1{ session, "/root" };
|
||||
ScopedDirectory child1{ session, "/root/foo" };
|
||||
ScopedDirectory parent2{ session, "/root_1" };
|
||||
ScopedDirectory child2{ session, "/root_1/foo" };
|
||||
|
||||
ScopedMediaLibrary library{ session, "/root" };
|
||||
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
|
||||
const auto res{ Directory::findMismatchedLibrary(session, std::nullopt, library->getPath(), library->getId()).results };
|
||||
ASSERT_EQ(res.size(), 2);
|
||||
EXPECT_EQ(res[0], parent1.getId());
|
||||
EXPECT_EQ(res[1], child1.getId());
|
||||
}
|
||||
|
||||
{
|
||||
auto transaction{ session.createWriteTransaction() };
|
||||
|
||||
parent1.get().modify()->setMediaLibrary(library.get());
|
||||
child1.get().modify()->setMediaLibrary(library.get());
|
||||
}
|
||||
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
|
||||
const auto res{ Directory::findMismatchedLibrary(session, std::nullopt, library->getPath(), library->getId()).results };
|
||||
EXPECT_EQ(res.size(), 0);
|
||||
}
|
||||
}
|
||||
} // namespace lms::db::tests
|
||||
Reference in New Issue
Block a user