Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multisearch #469

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Search engine for Multisearch
  • Loading branch information
michalmoc committed May 19, 2024
commit bcc72e46dbb2dfcb14bd86ee16b281d993b0941d
1 change: 1 addition & 0 deletions src/libs/database/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_library(lmsdatabase SHARED
impl/AnyMedium.cpp
impl/Artist.cpp
impl/AuthToken.cpp
impl/Cluster.cpp
Expand Down
56 changes: 56 additions & 0 deletions src/libs/database/impl/AnyMedium.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "database/AnyMedium.hpp"

#include <database/Session.hpp>
#include <tuple>

#include "Utils.hpp"

using namespace lms::db;

namespace
{
AnyMediumId fromString(const std::string &type, const Wt::Dbo::dbo_default_traits::IdType id)

Check notice

Code scanning / CodeQL

Unused static function

Static function fromString is unreachable
{
if (type == "artist")
return ArtistId(id);
if (type == "release")
return ReleaseId(id);
if (type == "track")
return TrackId(id);

throw std::logic_error("unknown medium type");
}
}

RangeResults<AnyMediumId> any_medium::findIds(Session &session, const std::vector<std::string_view> &keywords, std::optional<Range> range)
{
using Columns = std::tuple<std::string, Wt::Dbo::dbo_default_traits::IdType, int>;

session.checkReadTransaction();

auto query = session.getDboSession()->query<Columns>(R"(
SELECT type, id, sum(weight) AS v
FROM keywords
)");

for (const std::string_view keyword : keywords)
{
query.orWhere("value LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'").bind("%" + utils::escapeLikeKeyword(keyword) + "%");
}

query
.groupBy("type, id")
.orderBy("v DESC");

auto columns = utils::execRangeQuery<Columns>(query, range);
auto results = std::vector<AnyMediumId>(columns.results.size());

for (const auto&[type, id, _] : columns.results)
results.emplace_back(fromString(type, id));

return {
columns.range,
results,
columns.moreResults
};
}
43 changes: 43 additions & 0 deletions src/libs/database/impl/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,49 @@ namespace lms::db
LMS_LOG(DB, INFO, "Indexes created!");
}

void Session::createViewsIfNeeded()
{
LMS_SCOPED_TRACE_OVERVIEW("Database", "ViewCreation");
LMS_LOG(DB, INFO, "Creating views...");

auto transaction{ createWriteTransaction() };
_session.execute(R"(
CREATE VIEW IF NOT EXISTS keywords (type, id, weight, value) AS
SELECT *
FROM (SELECT "artist", id, 3, name
FROM artist
UNION
SELECT "track", id, 3, name
FROM track
UNION
SELECT "track", track_id, 1, artist.name
FROM track_artist_link
JOIN artist ON (track_artist_link.artist_id = artist.id)
UNION
SELECT "release", id, 3, name
FROM "release"
UNION
SELECT "release", r.id, 1, a.name
FROM "release" r
JOIN track t ON (r.id = t.release_id)
JOIN track_artist_link tal ON (tal.track_id = t.id)
JOIN artist a ON (a.id = tal.artist_id))
)");

LMS_LOG(DB, INFO, "Views created!");
}

void Session::dropViews()
{
LMS_SCOPED_TRACE_OVERVIEW("Database", "ViewDestruction");
LMS_LOG(DB, INFO, "Dropping views...");

auto transaction{ createWriteTransaction() };
_session.execute("DROP VIEW IF EXISTS keywords");

LMS_LOG(DB, INFO, "Views dropped!");
}

void Session::vacuumIfNeeded()
{
long pageCount{};
Expand Down
21 changes: 21 additions & 0 deletions src/libs/database/include/database/AnyMedium.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <variant>
#include <vector>
#include <optional>

#include "ArtistId.hpp"
#include "ReleaseId.hpp"
#include "TrackId.hpp"
#include "Types.hpp"

namespace lms::db
{
class Session;
using AnyMediumId = std::variant<ArtistId, ReleaseId, TrackId>;

namespace any_medium
{
RangeResults<AnyMediumId> findIds(Session& session, const std::vector<std::string_view>& keywords, std::optional<Range> range);
}
}
2 changes: 2 additions & 0 deletions src/libs/database/include/database/Session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ namespace lms::db
void prepareTablesIfNeeded(); // need to run only once at startup
bool migrateSchemaIfNeeded(); // returns true if migration was performed
void createIndexesIfNeeded();
void createViewsIfNeeded();
void dropViews();
void vacuumIfNeeded();
void vacuum();
void refreshTracingLoggerStats();
Expand Down
4 changes: 4 additions & 0 deletions src/lms/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ namespace lms
bool migrationPerformed{ session.migrateSchemaIfNeeded() };
session.createIndexesIfNeeded();

if (migrationPerformed)
session.dropViews();
session.createViewsIfNeeded();

// As this may be quite long, we only do it during startup
if (migrationPerformed)
session.vacuum();
Expand Down
96 changes: 7 additions & 89 deletions src/lms/ui/explore/MultisearchCollector.cpp
Original file line number Diff line number Diff line change
@@ -1,101 +1,19 @@
#include "MultisearchCollector.hpp"

#include <algorithm>
#include <database/Artist.hpp>
#include <database/Release.hpp>

#include "database/Session.hpp"
#include "database/Track.hpp"
#include "services/scrobbling/IScrobblingService.hpp"
#include "Filters.hpp"
#include "LmsApplication.hpp"
#include <LmsApplication.hpp>
#include <database/Session.hpp>

namespace lms::ui
{
using namespace db;

// TODO: merge results and sort by relevance
// TODO: restore filter by star
RangeResults<MediumId> MultisearchCollector::get(const std::optional<Range>& requestedRange) const
// TODO: apply filters
RangeResults<AnyMediumId> MultisearchCollector::get(const std::optional<Range>& requestedRange) const
{
auto tracks = getTracks(requestedRange);
auto releases = getReleases(requestedRange);
auto artists = getArtists(requestedRange);

std::vector<MediumId> results(tracks.results.size() + artists.results.size() + releases.results.size());
for (auto e: tracks.results)
results.emplace_back(e);
for (auto e: releases.results)
results.emplace_back(e);
for (auto e: artists.results)
results.emplace_back(e);

return {
Range{},
results,
tracks.moreResults || releases.moreResults || artists.moreResults
};
}

db::RangeResults<db::TrackId> MultisearchCollector::getTracks(const std::optional<db::Range> &requestedRange) const {
const Range range{ getActualRange(requestedRange) };

RangeResults<TrackId> results;

Track::FindParameters params;
params.setClusters(getFilters().getClusters());
params.setMediaLibrary(getFilters().getMediaLibrary());
params.setKeywords(getSearchKeywords());
params.setRange(range);

{
auto transaction = LmsApp->getDbSession().createReadTransaction();
results = Track::findIds(LmsApp->getDbSession(), params);
}

if (range.offset + range.size >= getMaxCount())
results.moreResults = false;

return results;
}

db::RangeResults<db::ReleaseId> MultisearchCollector::getReleases(const std::optional<db::Range> &requestedRange) const {
const Range range{ getActualRange(requestedRange) };

RangeResults<ReleaseId> results;

Release::FindParameters params;
params.setClusters(getFilters().getClusters());
params.setMediaLibrary(getFilters().getMediaLibrary());
params.setKeywords(getSearchKeywords());
params.setRange(range);

{
auto transaction = LmsApp->getDbSession().createReadTransaction();
results = Release::findIds(LmsApp->getDbSession(), params);
}

if (range.offset + range.size >= getMaxCount())
results.moreResults = false;

return results;
}

db::RangeResults<db::ArtistId> MultisearchCollector::getArtists(const std::optional<db::Range> &requestedRange) const {
const Range range{ getActualRange(requestedRange) };

RangeResults<ArtistId> results;

Artist::FindParameters params;
params.setClusters(getFilters().getClusters());
params.setMediaLibrary(getFilters().getMediaLibrary());
params.setKeywords(getSearchKeywords());
params.setRange(range);
const Range range = getActualRange(requestedRange);

{
auto transaction = LmsApp->getDbSession().createReadTransaction();
results = Artist::findIds(LmsApp->getDbSession(), params);
}
auto transaction = LmsApp->getDbSession().createReadTransaction();
auto results = any_medium::findIds(LmsApp->getDbSession(), getSearchKeywords(), range);

if (range.offset + range.size >= getMaxCount())
results.moreResults = false;
Expand Down
11 changes: 2 additions & 9 deletions src/lms/ui/explore/MultisearchCollector.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <optional>
#include <variant>
#include <database/AnyMedium.hpp>

#include "database/TrackId.hpp"
#include "database/ArtistId.hpp"
Expand All @@ -11,19 +11,12 @@

namespace lms::ui
{
using MediumId = std::variant<db::TrackId, db::ArtistId, db::ReleaseId>;

class MultisearchCollector : public DatabaseCollectorBase
{
public:
using DatabaseCollectorBase::DatabaseCollectorBase;

[[nodiscard]] db::RangeResults<MediumId> get(const std::optional<db::Range>& requestedRange = std::nullopt) const ;

private:
[[nodiscard]] db::RangeResults<db::TrackId> getTracks(const std::optional<db::Range>& requestedRange) const ;
[[nodiscard]] db::RangeResults<db::ReleaseId> getReleases(const std::optional<db::Range>& requestedRange) const ;
[[nodiscard]] db::RangeResults<db::ArtistId> getArtists(const std::optional<db::Range>& requestedRange) const ;
[[nodiscard]] db::RangeResults<db::AnyMediumId> get(const std::optional<db::Range>& requestedRange = std::nullopt) const ;
};
} // ns UserInterface

Loading